Search in sources :

Example 1 with WriterStatement

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();
}
Also used : WriterStatement(org.apache.zookeeper.common.AtomicFileWritingIdiom.WriterStatement) IOException(java.io.IOException) File(java.io.File) Writer(java.io.Writer) Test(org.junit.Test)

Example 2 with WriterStatement

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();
}
Also used : WriterStatement(org.apache.zookeeper.common.AtomicFileWritingIdiom.WriterStatement) IOException(java.io.IOException) File(java.io.File) Writer(java.io.Writer) Test(org.junit.Test)

Example 3 with WriterStatement

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());
}
Also used : WriterStatement(org.apache.zookeeper.common.AtomicFileWritingIdiom.WriterStatement) IOException(java.io.IOException) File(java.io.File) Writer(java.io.Writer) Test(org.junit.Test)

Example 4 with WriterStatement

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();
}
Also used : WriterStatement(org.apache.zookeeper.common.AtomicFileWritingIdiom.WriterStatement) IOException(java.io.IOException) File(java.io.File) Writer(java.io.Writer) Test(org.junit.Test)

Example 5 with WriterStatement

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();
}
Also used : WriterStatement(org.apache.zookeeper.common.AtomicFileWritingIdiom.WriterStatement) IOException(java.io.IOException) File(java.io.File) Writer(java.io.Writer) Test(org.junit.Test)

Aggregations

File (java.io.File)8 IOException (java.io.IOException)8 Writer (java.io.Writer)8 WriterStatement (org.apache.zookeeper.common.AtomicFileWritingIdiom.WriterStatement)8 Test (org.junit.Test)6 AtomicFileWritingIdiom (org.apache.zookeeper.common.AtomicFileWritingIdiom)2 FileInputStream (java.io.FileInputStream)1 StringWriter (java.io.StringWriter)1 Entry (java.util.Map.Entry)1 Properties (java.util.Properties)1 VerifyingFileFactory (org.apache.zookeeper.server.util.VerifyingFileFactory)1