ModificationFile implements AutoCloseable { public void abort() throws IOException { synchronized (this) { if (!modifications.isEmpty()) { writer.abort(); modifications.remove(modifications.size() - 1); } } } ModificationFile(String filePath); void close(); void abort(); void write(Modification mod); Collection<Modification> getModifications(); String getFilePath(); void setFilePath(String filePath); void remove(); boolean exists(); ModificationFile createHardlink(); static final String FILE_SUFFIX; }
@Test public void testAbort() { String tempFileName = TestConstant.BASE_OUTPUT_PATH.concat("mod.temp"); Modification[] modifications = new Modification[]{ new Deletion(new PartialPath(new String[]{"d1", "s1"}), 1, 1), new Deletion(new PartialPath(new String[]{"d1", "s2"}), 2, 2), new Deletion(new PartialPath(new String[]{"d1", "s3"}), 3, 3, 4), new Deletion(new PartialPath(new String[]{"d1", "s4"}), 4, 4, 5), }; try (ModificationFile mFile = new ModificationFile(tempFileName)) { for (int i = 0; i < 2; i++) { mFile.write(modifications[i]); } List<Modification> modificationList = (List<Modification>) mFile.getModifications(); for (int i = 0; i < 2; i++) { assertEquals(modifications[i], modificationList.get(i)); } for (int i = 2; i < 4; i++) { mFile.write(modifications[i]); } modificationList = (List<Modification>) mFile.getModifications(); mFile.abort(); for (int i = 0; i < 3; i++) { assertEquals(modifications[i], modificationList.get(i)); } } catch (IOException e) { fail(e.getMessage()); } finally { new File(tempFileName).delete(); } }