Skip to content

Commit

Permalink
add coverage for Uppend and LookupData
Browse files Browse the repository at this point in the history
  • Loading branch information
bfulton committed Nov 7, 2017
1 parent 6715bbe commit 62aeac8
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/test/java/com/upserve/uppend/UppendMainTest.java
Original file line number Diff line number Diff line change
@@ -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"));
}
}
52 changes: 52 additions & 0 deletions src/test/java/com/upserve/uppend/UppendTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
56 changes: 56 additions & 0 deletions src/test/java/com/upserve/uppend/lookup/LookupDataTest.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
1 change: 1 addition & 0 deletions src/test/resources/com/upserve/uppend/main.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uppend.version=test-version

0 comments on commit 62aeac8

Please sign in to comment.