add JUnit
[jpim.git] / tests / de / j32 / pimstuff / data / AddressbookTest.java
diff --git a/tests/de/j32/pimstuff/data/AddressbookTest.java b/tests/de/j32/pimstuff/data/AddressbookTest.java
new file mode 100644 (file)
index 0000000..8dd68d8
--- /dev/null
@@ -0,0 +1,115 @@
+package de.j32.pimstuff.data;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import de.j32.util.Util;
+
+public class AddressbookTest
+{
+    Addressbook ab;
+
+    @Before
+    public void setUp() throws Exception
+    {
+        ab = new Addressbook();
+    }
+
+    @After
+    public void tearDown() throws Exception
+    {}
+
+    @Test
+    public final void testAdd()
+    {
+        Entry in = new Entry();
+        in.name("John Doe");
+        in.id(123);
+        in.attribute("phone", "work", "+12 345 67890");
+
+        ab.add(in);
+
+        Entry out = Util.first(ab);
+        assertEquals(in.name(), out.name());
+        assertEquals(in.id(), out.id());
+        Attribute inattr = Util.first(in.attributes());
+        Attribute outattr = Util.first(out.attributes());
+        assertEquals(inattr.type, outattr.type);
+        assertEquals(inattr.rel, outattr.rel);
+        assertEquals(inattr.value, outattr.value);
+        assertEquals(inattr.index, outattr.index);
+    }
+
+    @Test
+    public final void testConsume()
+    {
+        Entry in = new Entry();
+        in.name("John Doe");
+        in.id(124);
+        in.attribute("phone", "work", "+12 345 67890");
+
+        ab.consume(in);
+
+        Entry out = Util.first(ab);
+        assertEquals(in.name(), out.name());
+        assertEquals(in.id(), out.id());
+        Attribute inattr = Util.first(in.attributes());
+        Attribute outattr = Util.first(out.attributes());
+        assertEquals(inattr.type, outattr.type);
+        assertEquals(inattr.rel, outattr.rel);
+        assertEquals(inattr.value, outattr.value);
+        assertEquals(inattr.index, outattr.index);
+    }
+
+    @Test
+    public final void testSendTo()
+    {
+        Entry in = new Entry();
+        in.name("John Doe");
+        in.id(125);
+        in.attribute("phone", "work", "+12 345 67890");
+        ab.add(in);
+
+        final Entry[] out = { new Entry() };
+        ab.sendTo(new EntryConsumer() {
+            public void consume(Entry entry)
+            {
+                out[0] = entry;
+            }
+        });
+
+        assertEquals(in.name(), out[0].name());
+        assertEquals(in.id(), out[0].id());
+        Attribute inattr = Util.first(in.attributes());
+        Attribute outattr = Util.first(out[0].attributes());
+        assertEquals(inattr.type, outattr.type);
+        assertEquals(inattr.rel, outattr.rel);
+        assertEquals(inattr.value, outattr.value);
+        assertEquals(inattr.index, outattr.index);
+    }
+
+    @Test
+    public final void testIterator()
+    {
+        Entry in = new Entry();
+        in.name("John Doe");
+        in.id(126);
+        in.attribute("phone", "work", "+12 345 67890");
+        ab.add(in);
+
+        for (Entry out : ab) {
+            assertEquals(in.name(), out.name());
+            assertEquals(in.id(), out.id());
+            Attribute inattr = Util.first(in.attributes());
+            Attribute outattr = Util.first(out.attributes());
+            assertEquals(inattr.type, outattr.type);
+            assertEquals(inattr.rel, outattr.rel);
+            assertEquals(inattr.value, outattr.value);
+            assertEquals(inattr.index, outattr.index);
+        }
+    }
+
+}