diff --git a/src/test/java/com/upserve/uppend/UppendMainTest.java b/src/test/java/com/upserve/uppend/UppendMainTest.java new file mode 100644 index 00000000..0f3fe471 --- /dev/null +++ b/src/test/java/com/upserve/uppend/UppendMainTest.java @@ -0,0 +1,31 @@ +package com.upserve.uppend; + +import org.junit.*; + +import java.io.*; + +import static org.junit.Assert.*; + +public class UppendMainTest { + private final ByteArrayOutputStream outStream = new ByteArrayOutputStream(); + private final ByteArrayOutputStream errStream = new ByteArrayOutputStream(); + + @Before + public void setUpStreams() { + System.setOut(new PrintStream(outStream)); + System.setErr(new PrintStream(errStream)); + } + + @After + public void cleanUpStreams() { + System.setOut(null); + System.setErr(null); + } + + @Test + public void testMain() throws Exception { + Uppend.main("--help"); + String errStr = errStream.toString(); + assertTrue("didn't find expected 'Usage: uppend' in main stderr output: " + errStr, errStr.contains("Usage: uppend")); + } +} diff --git a/src/test/java/com/upserve/uppend/UppendTest.java b/src/test/java/com/upserve/uppend/UppendTest.java new file mode 100644 index 00000000..f0eeb5f3 --- /dev/null +++ b/src/test/java/com/upserve/uppend/UppendTest.java @@ -0,0 +1,52 @@ +package com.upserve.uppend; + +import com.upserve.uppend.util.SafeDeleting; +import lombok.extern.slf4j.Slf4j; +import org.junit.Test; + +import java.nio.file.*; + +import static org.junit.Assert.*; + +@Slf4j +public class UppendTest { + @Test + public void testVersion() { + // NOTE: source of this value is src/test/resources/com/upserve/uppend/main.properties + assertEquals("test-version", Uppend.VERSION); + } + + @Test + public void testStore() throws Exception { + final String pathStr = "build/tmp/test/uppend-test-store/"; + final Path path = Paths.get(pathStr); + SafeDeleting.removeTempPath(path); + assertFalse(Files.exists(path)); + FileAppendOnlyStore store = Uppend.store(pathStr).build(); + store.append("partition", "foo", "bar".getBytes()); + assertTrue(Files.exists(path)); + store.flush(); + FileAppendOnlyStore store2 = Uppend.store(path).build(); + assertArrayEquals(new String[] { "bar" }, store2.read("partition", "foo").map(String::new).toArray()); + store.close(); + store2.close(); + SafeDeleting.removeTempPath(path); + } + + @Test + public void testCounterStore() throws Exception { + final String pathStr = "build/tmp/test/uppend-test-counter-store/"; + final Path path = Paths.get(pathStr); + SafeDeleting.removeTempPath(path); + assertFalse(Files.exists(path)); + FileCounterStore store = Uppend.counterStore(pathStr).build(); + store.increment("partition", "foo", 5); + assertTrue(Files.exists(path)); + store.flush(); + FileCounterStore store2 = Uppend.counterStore(path).build(); + assertEquals(5, store2.get("partition", "foo")); + store.close(); + store2.close(); + SafeDeleting.removeTempPath(path); + } +} diff --git a/src/test/java/com/upserve/uppend/lookup/LookupDataTest.java b/src/test/java/com/upserve/uppend/lookup/LookupDataTest.java new file mode 100644 index 00000000..0540071f --- /dev/null +++ b/src/test/java/com/upserve/uppend/lookup/LookupDataTest.java @@ -0,0 +1,56 @@ +package com.upserve.uppend.lookup; + +import com.upserve.uppend.util.SafeDeleting; +import org.junit.*; + +import java.nio.file.*; + +import static org.junit.Assert.*; + +public class LookupDataTest { + private Path lookupDir = Paths.get("build/test/tmp/lookup-data"); + + @Before + public void initialize() throws Exception { + SafeDeleting.removeTempPath(lookupDir); + } + + @Test + public void testCtor() throws Exception { + new LookupData(5, lookupDir.resolve("data"), lookupDir.resolve("meta")); + } + + @Test + public void testGetAndPut() throws Exception { + LookupData data = new LookupData(5, lookupDir.resolve("data"), lookupDir.resolve("meta")); + final LookupKey key = new LookupKey("mykey"); // len = 5 + assertEquals(Long.MIN_VALUE, data.get(key)); + data.put(key, 80); + assertEquals(80, data.get(key)); + } + + @Test + public void testPutThrowsWithBadKeySize() throws Exception { + LookupData data = new LookupData(5, lookupDir.resolve("data"), lookupDir.resolve("meta")); + final LookupKey key = new LookupKey("myke"); + Exception expected = null; + try { + data.put(key, 80); + } catch (Exception e) { + expected = e; + } + assertNotNull(expected); + assertTrue(expected.getMessage().contains("unexpected key length")); + } + + @Test + public void testFlushAndClose() throws Exception { + LookupData data = new LookupData(5, lookupDir.resolve("data"), lookupDir.resolve("meta")); + final LookupKey key = new LookupKey("mykey"); // len = 5 + data.put(key, 80); + data.flush(); + data.close(); + data = new LookupData(5, lookupDir.resolve("data"), lookupDir.resolve("meta")); + assertEquals(80, data.get(key)); + } +} diff --git a/src/test/resources/com/upserve/uppend/main.properties b/src/test/resources/com/upserve/uppend/main.properties new file mode 100644 index 00000000..e7a0bde7 --- /dev/null +++ b/src/test/resources/com/upserve/uppend/main.properties @@ -0,0 +1 @@ +uppend.version=test-version