FileSystemUtil { public static String readFileAsText(String filename) { try (InputStream is = openStream(filename)) { String result = CharStreams.toString(new InputStreamReader(is, Charset.defaultCharset())); if (StringUtils.isEmpty(result)) { throw new IllegalArgumentException("File '" + filename + "' is empty."); } return result; } catch (IOException e) { throw new IllegalStateException("Error reading file '" + filename + "': " + e.getMessage(), e); } } private  FileSystemUtil(); static boolean checkFileOrResourceExists(String filename); static void assertFileOrResourceExists(String filename); static String readFileAsText(String filename); static String readFileAsText(String fileName, String alternateDir); @SuppressWarnings("squid:S1166") // wants log or rethrow exception. It's logged well enough here. static void writeStringToFile(String content, String filename); @SuppressWarnings("squid:S1166") // wants log or rethrow exception. It's logged well enough here. static void appendStringToFile(String content, String filename); @SuppressWarnings("squid:S1166") // wants log or rethrow exception. It's logged well enough here. static byte[] readBytesFromFile(String filename); static void deleteDirectoryQuietly(String rootDirName); static final String CLASSPATH_PREFIX; }
@Test public void readFileAsTextFromResource() { String result = FileSystemUtil.readFileAsText("classpath:/stg/Test.stg"); assertThat(result, containsString("group TEST")); }
@Test(expected = IllegalArgumentException.class) public void readFileAsTextFromResourceNotFound() { FileSystemUtil.readFileAsText("classpath:/not/existing/resource.txt"); }
@Test public void readFileAsTextFromFile() { String result = FileSystemUtil.readFileAsText("src/test/resources/stg/Test.stg"); assertThat(result, containsString("group TEST")); }
@Test public void readFileAsTextFromResourceAtAlternateLocation() { String result = FileSystemUtil.readFileAsText("Test.stg", "classpath:/stg"); assertThat(result, containsString("group TEST")); }
@Test public void readFileAsTextFromFileAtAlternateLocation() { String result = FileSystemUtil.readFileAsText("Test.stg", "src/test/resources/stg"); assertThat(result, containsString("group TEST")); }
@Test public void readFileAsTextFromFileAtAlternateLocationButFindAtFirst() { String result = FileSystemUtil.readFileAsText("src/test/resources/stg/Test.stg", "src/test/resources/stg"); assertThat(result, containsString("group TEST")); }
@Test(expected = IllegalArgumentException.class) public void readFileAsTextNotFound() { FileSystemUtil.readFileAsText("/not/existing/file.txt"); }
@Test(expected = IllegalArgumentException.class) public void readFileAsTextNotFoundFromAlternateLocation() { FileSystemUtil.readFileAsText("/not/existing/file.txt", "/also/not/here"); }
@Test(expected = IllegalArgumentException.class) public void readFileAsTextNotFoundWithEmptAlternateLocation() { FileSystemUtil.readFileAsText("/not/existing/file.txt", ""); }
@Test(expected = IllegalArgumentException.class) public void readFileAsTextEmpty() { FileSystemUtil.readFileAsText("classpath:/empty_file.txt", ""); }