From: Stefan Bund Date: Tue, 21 Sep 2010 14:57:26 +0000 (+0200) Subject: add JUnit X-Git-Url: http://g0dil.de/git?p=jpim.git;a=commitdiff_plain;h=aa75934c56fbe55732fd790eaa1c3b3676d80f41 add JUnit --- diff --git a/.classpath b/.classpath index eabe221..bb8b28c 100644 --- a/.classpath +++ b/.classpath @@ -2,6 +2,7 @@ + @@ -26,5 +27,7 @@ + + diff --git a/.gitignore b/.gitignore index 4ce8045..c91a3a5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /bin/ /config.xml +*.class diff --git a/.gitmodules b/.gitmodules index 54d4eb9..1f2afa9 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "libs/spring"] path = libs/spring url = git://g0dil.de/java-spring.git +[submodule "libs/junit"] + path = libs/junit + url = git://g0dil.de/java-junit.git diff --git a/build.xml b/build.xml index a6a3342..99ba17a 100644 --- a/build.xml +++ b/build.xml @@ -5,19 +5,24 @@ - + + - + - + + + + + - + @@ -27,7 +32,24 @@ - + + + + + + + + + + + + + + + + + + diff --git a/libs/junit b/libs/junit new file mode 160000 index 0000000..f728fcc --- /dev/null +++ b/libs/junit @@ -0,0 +1 @@ +Subproject commit f728fccc23a159c0cd9037c5cfd779b726880afd diff --git a/src/de/j32/pimstuff/data/Addressbook.java b/src/de/j32/pimstuff/data/Addressbook.java index 2f84683..9e31ea1 100644 --- a/src/de/j32/pimstuff/data/Addressbook.java +++ b/src/de/j32/pimstuff/data/Addressbook.java @@ -3,7 +3,7 @@ package de.j32.pimstuff.data; import java.util.Iterator; import java.util.LinkedList; -public class Addressbook implements EntryConsumer, EntryProducer +public class Addressbook implements EntryConsumer, EntryProducer, Iterable { LinkedList data_ = new LinkedList(); @@ -12,11 +12,6 @@ public class Addressbook implements EntryConsumer, EntryProducer data_.add(entry); } - public Iterator entries() - { - return data_.iterator(); - } - public void consume(Entry entry) { add(entry); @@ -27,4 +22,10 @@ public class Addressbook implements EntryConsumer, EntryProducer for (Entry entry : data_) consumer.consume(entry); } + + @Override + public Iterator iterator() + { + return data_.iterator(); + } } diff --git a/src/de/j32/util/Util.java b/src/de/j32/util/Util.java index bbf5295..cd4efb8 100644 --- a/src/de/j32/util/Util.java +++ b/src/de/j32/util/Util.java @@ -40,4 +40,11 @@ public class Util if (it.hasNext()) return it.next(); return null; } + + public static E first(Iterator it) + { + if (it.hasNext()) return it.next(); + return null; + } + } diff --git a/tests/de/j32/pimstuff/data/AddressbookTest.java b/tests/de/j32/pimstuff/data/AddressbookTest.java new file mode 100644 index 0000000..8dd68d8 --- /dev/null +++ b/tests/de/j32/pimstuff/data/AddressbookTest.java @@ -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); + } + } + +}