Skip to content

Commit

Permalink
trying to make the save() method atomic
Browse files Browse the repository at this point in the history
  • Loading branch information
Luigi R. Viggiano committed Sep 18, 2013
1 parent eafb7ee commit e8ce13c
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/test/java/org/aeonbits/owner/UtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,16 @@
public class UtilTest {
@Test(expected = UnsupportedOperationException.class)
public void testConstructor() {
new Util(){};
new Util() {
};
}

@Test
public void testReverse() {
Integer[] i = { 1, 2, 3, 4, 5};
Integer[] i = {1, 2, 3, 4, 5};
Integer[] result = Util.reverse(i);
assertTrue(Arrays.equals(new Integer[]{1, 2, 3, 4, 5}, i));
assertTrue(Arrays.equals(new Integer[]{5, 4, 3, 2, 1}, result));
assertTrue(Arrays.equals(new Integer[] {1, 2, 3, 4, 5}, i));
assertTrue(Arrays.equals(new Integer[] {5, 4, 3, 2, 1}, result));
}

@Test
Expand All @@ -58,14 +59,27 @@ public void testIgnore() {
public void testUnreachable() {
try {
unreachable();
} catch(AssertionError err) {
} catch (AssertionError err) {
assertEquals("this code should never be reached", err.getMessage());
}
}

public static void save(File target, Properties p) throws IOException {
target.getParentFile().mkdirs();
p.store(new FileWriter(target), "saved for test");
File parent = target.getParentFile();
parent.mkdirs();
if (isWindows()) {
p.store(new FileWriter(target), "saved for test");
} else {
File tempFile = File.createTempFile(target.getName(), "temp", parent);
p.store(new FileWriter(tempFile), "saved for test");
if (!tempFile.renameTo(target))
throw new IOException(String.format("Failed to overwrite %s to %s", tempFile.toString(),
target.toString()));
}
}

private static boolean isWindows() {
return System.getProperty("os.name").toLowerCase().indexOf("win") >= 0;
}

public static void delete(File target) throws IOException {
Expand Down

0 comments on commit e8ce13c

Please sign in to comment.