PropertyTools { public Properties loadProperties(@NonNls final String filePath) throws IOException { Validate.notEmpty(filePath, "File path can not be empty"); final Properties properties = new Properties(); InputStream inputStream = null; try { inputStream = new FileInputStream(filePath); properties.load(inputStream); return properties; } finally { ioTools.close(inputStream); } }  Properties loadProperties(@NonNls final String filePath); void saveProperties(@NonNls final String filePath, final Properties properties,
                               final String comment);  }
@Test public void loadPropertiesShouldThrowExceptionIfFilePathIsNull() throws IOException { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("File path can not be empty"); propertyTools.loadProperties(null); }
@Test public void loadPropertiesShouldThrowExceptionIfFilePathIsEmpty() throws IOException { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("File path can not be empty"); propertyTools.loadProperties(" "); }
@Test public void loadPropertiesShouldThrowExceptionIfFileNotFound() throws IOException { expectedException.expect(FileNotFoundException.class); expectedException.expectMessageContaining("unknown.properties ("); propertyTools.loadProperties("unknown.properties"); }
@Test @Ignore("Looks in the Android sdk folder for some reason") public void loadPropertiesShouldSuccessfullyLoadAllPropertiesInFileFromFullFileSystemPath() throws IOException { final File filePath = getPathTo("test-messages.properties"); final Properties properties = propertyTools.loadProperties(filePath.getAbsolutePath()); assertEquals(3, properties.size()); assertEquals("This is the first string", properties.getProperty("test.string1")); assertEquals("This is the second string", properties.getProperty("test.string2")); assertEquals("Say hello to {0} from {1}!", properties.getProperty("test.hello")); }
@Test @Ignore("Looks in the Android sdk folder for some reason") public void loadPropertiesShouldCloseInputStreamWhenDoneLoading() throws IOException { final File filePath = getPathTo("test-messages.properties"); propertyTools.loadProperties(filePath.getAbsolutePath()); verify(ioTools).close(any(InputStream.class)); }
@Test public void loadPropertiesShouldCloseInputStreamEvenOnException() { try { propertyTools.loadProperties("nothing"); fail("Should fail to load properties"); } catch (final IOException e) { verify(ioTools).close(nullable(InputStream.class)); } }