Example usage for org.apache.commons.io FileUtils readFileToString

List of usage examples for org.apache.commons.io FileUtils readFileToString

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils readFileToString.

Prototype

public static String readFileToString(File file, String encoding) throws IOException 

Source Link

Document

Reads the contents of a file into a String.

Usage

From source file:RegisterTest.java

/**
 * Test of processRequest method, of class Register.
 * @throws java.lang.Exception/*from  w  w  w  . ja v  a2  s  . c o m*/
 */
@Test
public void testProcessRequest() throws Exception {
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);

    when(request.getParameter("uid")).thenReturn("me");
    when(request.getParameter("pass")).thenReturn("secret");
    PrintWriter writer = new PrintWriter("testregister.txt");
    when(response.getWriter()).thenReturn(writer);
    Register instance = new Register();
    instance.doPost(request, response);

    verify(request, atLeast(1)).getParameter("name"); // only if you want to verify username was called...
    writer.flush(); // it may not have been flushed yet...
    assertTrue(FileUtils.readFileToString(new File("testregister.txt"), "UTF-8")
            .contains("You are sucessfully registered"));
}

From source file:com.yata.core.FileManager.java

public static String getFileContent(String fileName, String encoding) throws IOException {

    File dataFile = new File(fileName);

    if (!dataFile.exists()) {

        System.out.println("contentEquals@" + className + " : File :-> " + fileName + " : doesn't Exist...");
        throw new IOException("contentEquals@" + className + " : File :-> " + fileName + " : doesn't Exist...");
    }//from  www.  j  av  a2  s  .co  m

    String fileContentString;
    try {
        fileContentString = FileUtils.readFileToString(dataFile, encoding);
    } catch (IOException e) {
        e.printStackTrace();
        throw e;
    }

    return fileContentString;
}

From source file:net.sf.taverna.raven.helloworld.TestHelloWorld.java

@Test
public void mainWithFilename() throws IOException {
    File tmpFile = File.createTempFile(getClass().getCanonicalName(), "test");
    tmpFile.deleteOnExit();//from   w  ww.  j av a 2 s  .c  om
    assertTrue(tmpFile.isFile());
    String fileContent = FileUtils.readFileToString(tmpFile, "utf8");
    assertEquals("File was not empty", "", fileContent);

    HelloWorld.main(new String[] { tmpFile.getAbsolutePath() });
    fileContent = FileUtils.readFileToString(tmpFile, "utf8");
    assertEquals("File did not contain expected output", HelloWorld.TEST_DATA, fileContent);
}

From source file:com.github.stagirs.docextractor.wiki.WikiDocProcessorTest.java

@Test
public void test3() throws IOException {
    WikiDocProcessor processor = new WikiDocProcessor();
    Document doc = processor.processDocument("", FileUtils.readFileToString(
            new File("src/test/resources/?? ?"), "utf-8"));
    assertEquals(doc.getPoints().size(), 120);
}

From source file:TakeAttendanceTest.java

/**
 * Test of processRequest method, of class TakeAttendance.
 *//*from  w ww .j  a v  a 2 s  .c om*/
@Test
public void testProcessRequest() throws Exception {

    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);

    when(request.getParameter("asname")).thenReturn("1301010");
    when(request.getParameter("day")).thenReturn("Anubhav");
    when(request.getParameter("uids")).thenReturn("Attendee");

    PrintWriter writer = new PrintWriter("testtakeattendance.txt");
    when(response.getWriter()).thenReturn(writer);
    TakeAttendance instance = new TakeAttendance();
    instance.doPost(request, response);

    verify(request, atLeast(1)).getParameter("asname");
    writer.flush(); // it may not have been flushed yet...
    assertTrue(FileUtils.readFileToString(new File("testtakeattendance.txt"), "UTF-8")
            .contains("Attendance Added"));

}

From source file:com.intuit.karate.cucumber.CucumberRunnerTest.java

private boolean contains(String reportPath, String textToFind) {
    try {/*from  www.j a  v a  2  s.  c o m*/
        String contents = FileUtils.readFileToString(new File(reportPath), "utf-8");
        return contents.contains(textToFind);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:net.sf.taverna.raven.helloworld.HelloWorld.java

public void run(PrintStream out) throws IOException {
    File tmpFile = File.createTempFile("helloworld", "test");
    tmpFile.deleteOnExit();/*from  w w  w.  ja va  2s .  c  o  m*/
    FileUtils.writeStringToFile(tmpFile, TEST_DATA, "utf8");
    String read = FileUtils.readFileToString(tmpFile, "utf8");
    out.print(read);
}

From source file:de.micromata.tpsb.doc.sources.FileSystemSourceFileRepository.java

@Override
public Collection<JavaSourceFileHolder> getSources() {
    List<JavaSourceFileHolder> fileHolderList = new ArrayList<JavaSourceFileHolder>();

    for (String loc : getLocations()) {
        Collection<File> foundJavaFiles = FileUtils.listFiles(new File(loc), new String[] { "java" }, true);
        for (File javaFile : foundJavaFiles) {
            try {
                String javaFileContent = FileUtils.readFileToString(javaFile, CharEncoding.UTF_8);
                String javaFilename = javaFile.getName();
                JavaSourceFileHolder fileHolder = new JavaSourceFileHolder(javaFilename, javaFileContent);
                fileHolder.setSource(Source.FileSystem);
                fileHolder.setOrigin(loc);
                fileHolderList.add(fileHolder);
            } catch (IOException e) {
                e.printStackTrace();/*w ww . j  a v a 2s .  c o m*/
            }
        }
    }
    return fileHolderList;
}

From source file:javancss.JavancssTest.java

public void testCummulating() throws IOException {
    // Nr. 35//from w w w  .  j  ava2  s  .c o m
    String sTogether;
    String sTest11 = "";
    String sTest12 = "";
    try {
        sTest11 = FileUtils.readFileToString(getTestFile(11), "ISO-8859-1");
        sTest12 = FileUtils.readFileToString(getTestFile(12), "ISO-8859-1");
    } catch (IOException e) {
        fail();
    }
    sTogether = sTest11 + sTest12;
    Javancss javancss = new Javancss(new StringReader(sTogether));
    List<FunctionMetric> vFunctions = javancss.getFunctionMetrics();
    String sFirstFunction = vFunctions.get(0).name;
    assertFalse(!sFirstFunction.equals("ccl.util.Test11.atoi(String)"));
    String sSomeFunction = vFunctions.get(32).name;
    assertFalse("Function: " + sSomeFunction, !sSomeFunction.equals("Test12.readFile(URL)"));
    List<PackageMetric> vPackages = javancss.getPackageMetrics();
    assertFalse(vPackages.size() != 2);
    int ncss38 = javancss.getNcss();

    String[] asArg = new String[3];
    asArg[0] = getTestFile(11).getAbsolutePath();
    asArg[1] = asArg[0];
    asArg[2] = getTestFile(12).getAbsolutePath();
    javancss = measureWithArgs(asArg);
    vPackages = javancss.getPackageMetrics();
    assertFalse(vPackages.size() != 2);
    assertFalse(ncss38 == javancss.getNcss());
}

From source file:au.org.ala.delta.confor.ToNatHTest.java

private void testFile(String fileName, String outputPath) throws IOException {
    File expectedFile = new File(FilenameUtils.concat(_samplePath, "expected_results/" + fileName));
    String expected = FileUtils.readFileToString(expectedFile, "cp1252");

    System.out.println(expected);

    File actualFile = new File(FilenameUtils.concat(_samplePath, outputPath + fileName));
    String actual = FileUtils.readFileToString(actualFile, "cp1252");

    System.out.print(actual);//from   w w w. jav  a  2 s. c o  m

    boolean dosEol = expected.contains("\r\n");
    String expectedLineSeparator = "\n";
    if (dosEol) {
        expectedLineSeparator = "\r\n";
    }

    if (!System.getProperty("line.separator").equals(expectedLineSeparator)) {
        expected = expected.replaceAll(expectedLineSeparator, System.getProperty("line.separator"));
        //actual = actual.replaceAll(expectedLineSeparator, System.getProperty("line.separator"));
    }
    // The heading contains the date so will be different.

    for (int i = 0; i < expected.length(); i++) {
        if (expected.charAt(i) != actual.charAt(i)) {
            System.out.println("Difference @ char: " + i + " Expected: " + expected.charAt(i)
                    + (int) expected.charAt(i) + ", Actual: " + actual.charAt(i) + (int) actual.charAt(i));
            break;
        }
    }
    //assertEquals(expected, actual);
}