Skip to content

Commit

Permalink
Close PrintStream properly in TestConfigurationLoader tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dnskr authored and tdcmeehan committed Apr 24, 2024
1 parent 961ff31 commit 1016fbd
Showing 1 changed file with 16 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,47 +68,39 @@ public void testLoadsFromFile()
throws IOException
{
final File file = File.createTempFile("config", ".properties", tempDir);
PrintStream out = new PrintStream(new FileOutputStream(file));
try {
try (PrintStream out = new PrintStream(new FileOutputStream(file))) {
out.print("test: foo");
}
catch (Exception e) {
out.close();
}

System.setProperty("config", file.getAbsolutePath());
System.setProperty("config", file.getAbsolutePath());

Map<String, String> properties = loadProperties();
Map<String, String> properties = loadProperties();

assertEquals(properties.get("test"), "foo");
assertEquals(properties.get("config"), file.getAbsolutePath());
assertEquals(properties.get("test"), "foo");
assertEquals(properties.get("config"), file.getAbsolutePath());

System.getProperties().remove("config");
System.getProperties().remove("config");
}
}

@Test
public void testSystemOverridesFile()
throws IOException
{
final File file = File.createTempFile("config", ".properties", tempDir);
PrintStream out = new PrintStream(new FileOutputStream(file));
try {
try (PrintStream out = new PrintStream(new FileOutputStream(file))) {
out.println("key1: original");
out.println("key2: original");
}
catch (Exception e) {
out.close();
}

System.setProperty("config", file.getAbsolutePath());
System.setProperty("key1", "overridden");
System.setProperty("config", file.getAbsolutePath());
System.setProperty("key1", "overridden");

Map<String, String> properties = loadProperties();
Map<String, String> properties = loadProperties();

assertEquals(properties.get("config"), file.getAbsolutePath());
assertEquals(properties.get("key1"), "overridden");
assertEquals(properties.get("key2"), "original");
assertEquals(properties.get("config"), file.getAbsolutePath());
assertEquals(properties.get("key1"), "overridden");
assertEquals(properties.get("key2"), "original");

System.getProperties().remove("config");
System.getProperties().remove("config");
}
}
}

0 comments on commit 1016fbd

Please sign in to comment.