net.i2cat.netconf.test.ClassLoaderResourceTest.java Source code

Java tutorial

Introduction

Here is the source code for net.i2cat.netconf.test.ClassLoaderResourceTest.java

Source

/**
 * This file is part of Netconf4j.
 *
 * Netconf4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Netconf4j is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Netconf4j. If not, see <http://www.gnu.org/licenses/>.
 */
package net.i2cat.netconf.test;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import junit.framework.Assert;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;

public class ClassLoaderResourceTest {
    private Log log = LogFactory.getLog(ClassLoaderResourceTest.class);

    @Test
    public void readMockFiles() {
        String file = "/test/example.txt";
        try {
            String information = readStringFromFile(file);
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
            Assert.fail();
        }

        // log.info("info: " + '\n' + information + '\n');
    }

    /**
     * Simple parser. It was used for proves with xml files
     * 
     * @param stream
     * @return
     */
    private String readStringFromFile(String pathFile) throws Exception {
        String answer = null;
        InputStream inputFile = getClass().getResourceAsStream(pathFile);
        InputStreamReader streamReader = new InputStreamReader(inputFile);
        StringBuffer fileData = new StringBuffer(1000);
        BufferedReader reader = new BufferedReader(streamReader);
        char[] buf = new char[1024];
        int numRead = 0;
        while ((numRead = reader.read(buf)) != -1) {
            String readData = String.valueOf(buf, 0, numRead);
            fileData.append(readData);
            buf = new char[1024];
        }
        reader.close();
        answer = fileData.toString();

        return answer;
    }

}