/** * Gets the contents of a classpath resource as a String using the * specified character encoding. * * <p> * It is expected the given <code>name</code> to be absolute. The * behavior is not well-defined otherwise. * </p> * * @param name name of the desired resource * @param encoding the encoding to use, null means platform default * @return the requested String * @throws IOException if an I/O error occurs * * @since 2.6 */ public static String resourceToString(final String name, final Charset encoding) throws IOException { return resourceToString(name, encoding, null); }
@Test public void testResourceToString_NullCharset_WithClassLoader() throws Exception { IOUtils.resourceToString("test-file-utf8.bin", null, ClassLoader.getSystemClassLoader()); }
@Test(expected = IOException.class) public void testResourceToString_NonExistingResource_WithClassLoader() throws Exception { IOUtils.resourceToString("non-existing-file.bin", StandardCharsets.UTF_8, ClassLoader.getSystemClassLoader()); }
@Test(expected = IOException.class) public void testResourceToString_NonExistingResource() throws Exception { IOUtils.resourceToString("/non-existing-file.bin", StandardCharsets.UTF_8); }
@Test public void testResourceToString_NullCharset() throws Exception { IOUtils.resourceToString("/test-file-utf8.bin", null); }
@Test public void testResourceToString_ExistingResourceAtRootPackage_WithClassLoader() throws Exception { final long fileSize = new File(getClass().getResource("/test-file-simple-utf8.bin").getFile()).length(); final String content = IOUtils.resourceToString( "test-file-simple-utf8.bin", StandardCharsets.UTF_8, ClassLoader.getSystemClassLoader() ); assertNotNull(content); assertEquals(fileSize, content.getBytes().length); }
@Test public void testResourceToString_ExistingResourceAtRootPackage() throws Exception { final long fileSize = new File(getClass().getResource("/test-file-simple-utf8.bin").getFile()).length(); final String content = IOUtils.resourceToString("/test-file-simple-utf8.bin", StandardCharsets.UTF_8); assertNotNull(content); assertEquals(fileSize, content.getBytes().length); }
@Test public void testResourceToString_ExistingResourceAtSubPackage_WithClassLoader() throws Exception { final long fileSize = new File(getClass().getResource("/org/apache/commons/io/FileUtilsTestDataCR.dat").getFile()).length(); final String content = IOUtils.resourceToString( "org/apache/commons/io/FileUtilsTestDataCR.dat", StandardCharsets.UTF_8, ClassLoader.getSystemClassLoader() ); assertNotNull(content); assertEquals(fileSize, content.getBytes().length); }
@Test public void testResourceToString_ExistingResourceAtSubPackage() throws Exception { final long fileSize = new File(getClass().getResource("/org/apache/commons/io/FileUtilsTestDataCR.dat").getFile()).length(); final String content = IOUtils.resourceToString("/org/apache/commons/io/FileUtilsTestDataCR.dat", StandardCharsets.UTF_8); assertNotNull(content); assertEquals(fileSize, content.getBytes().length); }
@Test public void testResourceToString_NullResource_WithClassLoader() throws Exception { boolean exceptionOccurred = false; try { IOUtils.resourceToString(null, StandardCharsets.UTF_8, ClassLoader.getSystemClassLoader()); fail(); } catch (final NullPointerException npe) { exceptionOccurred = true; assertNotNull(npe); } assertTrue(exceptionOccurred); }
@Test public void testResourceToString_NullResource() throws Exception { boolean exceptionOccurred = false; try { IOUtils.resourceToString(null, StandardCharsets.UTF_8); fail(); } catch (final NullPointerException npe) { exceptionOccurred = true; assertNotNull(npe); } assertTrue(exceptionOccurred); }
/** * Gets the contents of a classpath resource as a String using the * specified character encoding. * * <p> * It is expected the given <code>name</code> to be absolute. The * behavior is not well-defined otherwise. * </p> * * @param name name of the desired resource * @param encoding the encoding to use, null means platform default * @return the requested String * @throws IOException if an I/O error occurs * * @since 2.6 */ public static String resourceToString(final String name, final Charset encoding) throws IOException { return resourceToString(name, encoding, null); }
@NonNull private JsonObject jsonObject() { try { final String input = IOUtils.resourceToString(file, StandardCharsets.UTF_8, TestDataReader.class.getClassLoader()); final ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory()); final Object object = objectMapper.readValue(input, Object.class); final ObjectMapper jsonWriter = new ObjectMapper(); final String json = jsonWriter.writeValueAsString(object); return new Gson().fromJson(json, JsonObject.class); } catch (Throwable t) { throw new RuntimeException(t); } }