use of org.apache.zookeeper.common.AtomicFileWritingIdiom.WriterStatement in project zookeeper by apache.
the class AtomicFileWritingIdiomTest method testWriterSuccessNE.
@Test
public void testWriterSuccessNE() throws IOException {
File target = new File(tmpdir, "target.txt");
final File tmp = new File(tmpdir, "target.txt.tmp");
target.delete();
assertFalse("file should not exist", target.exists());
new AtomicFileWritingIdiom(target, new WriterStatement() {
@Override
public void write(Writer os) throws IOException {
os.write("after");
assertTrue("implementation of AtomicFileOutputStream has changed, update the test", tmp.exists());
}
});
assertFalse("tmp file should have been deleted", tmp.exists());
// content changed
assertEquals("after", getContent(target));
target.delete();
}
use of org.apache.zookeeper.common.AtomicFileWritingIdiom.WriterStatement in project zookeeper by apache.
the class AtomicFileWritingIdiomTest method testWriterFailure.
@Test
public void testWriterFailure() throws IOException {
File target = new File(tmpdir, "target.txt");
final File tmp = new File(tmpdir, "target.txt.tmp");
createFile(target, "before");
assertEquals("before", getContent(target));
boolean exception = false;
try {
new AtomicFileWritingIdiom(target, new WriterStatement() {
@Override
public void write(Writer os) throws IOException {
os.write("after");
os.flush();
assertTrue("implementation of AtomicFileOutputStream has changed, update the test", tmp.exists());
throw new RuntimeException();
}
});
} catch (RuntimeException ex) {
exception = true;
}
assertFalse("tmp file should have been deleted", tmp.exists());
assertTrue("should have raised an exception", exception);
// content preserved
assertEquals("before", getContent(target));
target.delete();
}
use of org.apache.zookeeper.common.AtomicFileWritingIdiom.WriterStatement in project zookeeper by apache.
the class AtomicFileWritingIdiomTest method testWriterFailureNE.
@Test
public void testWriterFailureNE() throws IOException {
File target = new File(tmpdir, "target.txt");
final File tmp = new File(tmpdir, "target.txt.tmp");
target.delete();
assertFalse("file should not exist", target.exists());
boolean exception = false;
try {
new AtomicFileWritingIdiom(target, new WriterStatement() {
@Override
public void write(Writer os) throws IOException {
os.write("after");
os.flush();
assertTrue("implementation of AtomicFileOutputStream has changed, update the test", tmp.exists());
throw new RuntimeException();
}
});
} catch (RuntimeException ex) {
exception = true;
}
assertFalse("tmp file should have been deleted", tmp.exists());
assertTrue("should have raised an exception", exception);
// file should not exist
assertFalse("file should not exist", target.exists());
}
use of org.apache.zookeeper.common.AtomicFileWritingIdiom.WriterStatement in project zookeeper by apache.
the class AtomicFileWritingIdiomTest method testWriterFailureError.
@Test
public void testWriterFailureError() throws IOException {
File target = new File(tmpdir, "target.txt");
final File tmp = new File(tmpdir, "target.txt.tmp");
createFile(target, "before");
assertEquals("before", getContent(target));
boolean exception = false;
try {
new AtomicFileWritingIdiom(target, new WriterStatement() {
@Override
public void write(Writer os) throws IOException {
os.write("after");
os.flush();
assertTrue("implementation of AtomicFileOutputStream has changed, update the test", tmp.exists());
throw new Error();
}
});
} catch (Error ex) {
exception = true;
}
assertFalse("tmp file should have been deleted", tmp.exists());
assertTrue("should have raised an exception", exception);
// content preserved
assertEquals("before", getContent(target));
target.delete();
}
use of org.apache.zookeeper.common.AtomicFileWritingIdiom.WriterStatement in project zookeeper by apache.
the class AtomicFileWritingIdiomTest method testWriterSuccess.
@Test
public void testWriterSuccess() throws IOException {
File target = new File(tmpdir, "target.txt");
final File tmp = new File(tmpdir, "target.txt.tmp");
createFile(target, "before");
assertEquals("before", getContent(target));
new AtomicFileWritingIdiom(target, new WriterStatement() {
@Override
public void write(Writer os) throws IOException {
os.write("after");
assertTrue("implementation of AtomicFileOutputStream has changed, update the test", tmp.exists());
}
});
assertFalse("tmp file should have been deleted", tmp.exists());
// content changed
assertEquals("after", getContent(target));
target.delete();
}
Aggregations