import junit.framework.TestCase;
import org.junit.rules.ExpectedException;
import org.junit.Test;
import org.junit.Rule;

import traildb.*;

import java.util.UUID;
import java.io.FileNotFoundException;

public class TestTrailDB extends TestCase {

	protected void setUp() throws FileNotFoundException {
		System.out.println(System.getProperty("java.library.path"));
		TrailDBConstructor cons = new TrailDBConstructor("test", new String[] {"user", "action"});

		UUID cookie1 = UUID.randomUUID();
		UUID cookie2 = UUID.randomUUID();

		cons.add(cookie1, 1, new String[] {"bob", "run"});
		cons.add(cookie2, 2, new String[] {"fred", "walk"});
		cons.add(cookie1, 4, new String[] {"jerry", "speak"});
		cons.add(cookie1, 5, new String[] {"ted", "fly"});

		cons.finalize();
		cons.close();
	}

	public void testBasic() throws FileNotFoundException {
		TrailDB tdb = new TrailDB("test.tdb");
		TrailDBCursor cur = tdb.cursorNew();
		long numCookies = tdb.numTrails();
		TrailDBEvent event;
		for (int i=0; i < numCookies; i++) {
			cur.getTrail(i);
			while ((event = cur.next()) != null) {
				switch ((int) event.timestamp) {
					case 1:
						assertEquals(event.getItem(0), "bob");
						assertEquals(event.getItem(1), "run");
						break;
					case 2:
						assertEquals(event.getItem(0), "fred");
						assertEquals(event.getItem(1), "walk");
						break;
					case 4:
						assertEquals(event.getItem(0), "jerry");
						assertEquals(event.getItem(1), "speak");
						break;
					case 5:
						assertEquals(event.getItem(0), "ted");
						assertEquals(event.getItem(1), "fly");
						break;
					default:
						fail("Unrecognized timestamp " + event.timestamp);
				}
			}
		}
	}

	public void testPeek() throws FileNotFoundException {
		TrailDB tdb = new TrailDB("test.tdb");
		TrailDBCursor cur = tdb.cursorNew();
		long numCookies = tdb.numTrails();
		TrailDBEvent event;
		int foundEvents = 0;
		for (int i=0; i < numCookies; i++) {
			cur.getTrail(i);
			while ((event = cur.next()) != null) {
				foundEvents++;
				cur.peek();
				switch ((int) event.timestamp) {
					case 1:
						assertEquals(event.getItem(0), "bob");
						assertEquals(event.getItem(1), "run");
						break;
					case 2:
						assertEquals(event.getItem(0), "fred");
						assertEquals(event.getItem(1), "walk");
						break;
					case 4:
						assertEquals(event.getItem(0), "jerry");
						assertEquals(event.getItem(1), "speak");
						break;
					case 5:
						assertEquals(event.getItem(0), "ted");
						assertEquals(event.getItem(1), "fly");
						break;
					default:
						fail("Unrecognized timestamp " + event.timestamp);
				}
				event = cur.peek();
			}
		}
		assertEquals(foundEvents, 4);
	}


	public void testExceptionFileNotFound() {
		try {
			TrailDB tdb = new TrailDB("wat.tdb");
			fail("Expected TrailDB to fail but it succeeded");
		} catch (FileNotFoundException e) {
			return;
		}
	}
}

@@ -300,71 +300,8 @@
 n %7B%0A
-%09%09System.out.println(System.getProperty(%22java.library.path%22));%0A
 %09%09Tr
@@ -676,24 +676,78 @@
 d%22, %22fly%22%7D);
+%0A%09%09cons.add(cookie2, 6, new String%5B%5D %7B%22doug%22, %22dab%22%7D);
 %0A%0A%09%09cons.fin
@@ -1862,16 +1862,49 @@
  event;%0A
+%09%09TrailDBEvent peekEvent = null;%0A
 %09%09int fo
@@ -2023,16 +2023,184 @@
 ) %7B%0A%09%09%09%09
+cur.peek();%0A%09%09%09%09if (foundEvents != 0) %7B%0A%09%09%09%09%09assertEquals(event.getItem(0), peekEvent.getItem(0));%0A%09%09%09%09%09assertEquals(event.getItem(1), peekEvent.getItem(1));%0A%09%09%09%09%7D%0A%09%09%09%09
 foundEve
@@ -2206,31 +2206,16 @@
 ents++;%0A
-%09%09%09%09cur.peek();
 %0A%09%09%09%09swi
@@ -2706,32 +2706,149 @@
 );%0A%09%09%09%09%09%09break;%0A
+%09%09%09%09%09case 6:%0A%09%09%09%09%09%09assertEquals(event.getItem(0), %22doug%22);%0A%09%09%09%09%09%09assertEquals(event.getItem(1), %22dab%22);%0A%09%09%09%09%09%09break;%0A
 %09%09%09%09%09default:%0A%09%09
@@ -2912,17 +2912,21 @@
 %09%09%7D%0A%09%09%09%09
-e
+peekE
 vent = c
@@ -2973,17 +2973,17 @@
 Events, 
-4
+5
 );%0A%09%7D%0A%0A%0A

Improve peek test