PropertyTools { public void saveProperties(@NonNls final String filePath, final Properties properties, final String comment) throws IOException { Validate.notEmpty(filePath, "File path can not be empty"); Validate.notNull(properties, "Properties can not be null"); FileWriter fileWriter = null; try { fileWriter = new FileWriter(filePath); properties.store(fileWriter, comment); } finally { ioTools.flush(fileWriter); ioTools.close(fileWriter); } }  Properties loadProperties(@NonNls final String filePath); void saveProperties(@NonNls final String filePath, final Properties properties,
                               final String comment);  }
@Test public void savePropertiesShouldThrowExceptionIfFilePathIsNull() throws IOException { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("File path can not be empty"); propertyTools.saveProperties(null, new Properties(), null); }
@Test public void savePropertiesShouldThrowExceptionIfFilePathIsEmpty() throws IOException { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("File path can not be empty"); propertyTools.saveProperties(" ", new Properties(), null); }
@Test public void savePropertiesShouldThrowExceptionIfPropertiesIsNull() throws IOException { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Properties can not be null"); propertyTools.saveProperties("file", null, null); }
@Test @Ignore("Looks in the Android sdk folder for some reason") public void savePropertiesShouldThrowExceptionIfFileCouldNotBeSaved() throws IOException { expectedException.expect(FileNotFoundException.class); expectedException.expectMessageContaining("test-classes ("); final File filePath = getPathTo(""); propertyTools.saveProperties(filePath.getAbsolutePath(), new Properties(), null); }
@Test public void savePropertiesShouldFlushAndCloseFileWriterWhenDone() throws IOException { final File filePath = getTempPathTo("temp4.properties"); assertFalse(filePath.exists()); final Properties properties = new Properties(); properties.put("key", "value"); propertyTools.saveProperties(filePath.getAbsolutePath(), properties, null); assertTrue(filePath.exists()); verify(ioTools).flush(any(FileWriter.class)); verify(ioTools).close(any(FileWriter.class)); }
@Test public void savePropertiesShouldFlushAndCloseWriterEvenOnException() { final File filePath = getPathTo(""); try { propertyTools.saveProperties(filePath.getAbsolutePath(), new Properties(), null); fail("Should fail to save properties"); } catch (final IOException e) { verify(ioTools).flush(nullable(FileWriter.class)); verify(ioTools).close(nullable(FileWriter.class)); } }