add JUnit
Stefan Bund [Tue, 21 Sep 2010 14:57:26 +0000 (16:57 +0200)]
.classpath
.gitignore
.gitmodules
build.xml
libs/junit [new submodule]
src/de/j32/pimstuff/data/Addressbook.java
src/de/j32/util/Util.java
tests/de/j32/pimstuff/data/AddressbookTest.java [new file with mode: 0644]

index eabe221..bb8b28c 100644 (file)
@@ -2,6 +2,7 @@
 <classpath>
        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
        <classpathentry kind="src" path="src"/>
+       <classpathentry kind="src" path="tests"/>
        <classpathentry kind="lib" path="libs/commons-logging/commons-logging-1.1.1.jar"/>
        <classpathentry kind="lib" path="libs/commons-logging/commons-logging-adapters-1.1.1.jar"/>
        <classpathentry kind="lib" path="libs/commons-logging/commons-logging-api-1.1.1.jar"/>
@@ -26,5 +27,7 @@
        <classpathentry kind="lib" path="libs/spring/dist/org.springframework.web.portlet-3.0.4.RELEASE.jar"/>
        <classpathentry kind="lib" path="libs/spring/dist/org.springframework.web.servlet-3.0.4.RELEASE.jar"/>
        <classpathentry kind="lib" path="libs/spring/dist/org.springframework.web.struts-3.0.4.RELEASE.jar"/>
+       <classpathentry kind="lib" path="libs/junit/junit-4.8.2.jar"/>
+       <classpathentry kind="lib" path="libs/junit/junit-dep-4.8.2.jar"/>
        <classpathentry kind="output" path="bin"/>
 </classpath>
index 4ce8045..c91a3a5 100644 (file)
@@ -1,2 +1,3 @@
 /bin/
 /config.xml
+*.class
index 54d4eb9..1f2afa9 100644 (file)
@@ -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
index a6a3342..99ba17a 100644 (file)
--- a/build.xml
+++ b/build.xml
@@ -5,19 +5,24 @@
        <property name="build" location="bin" />
        <property name="main" value="de.j32.pimstuff.Main" />
        <property name="lib" value="libs" />
-       
+       <property name="test" value="tests" />
+
        <target name="makedirs">
                <mkdir dir="${build}" />
        </target>
 
-       <path id="classpath.lib">
+       <path id="classpath">
                <fileset dir="${lib}">
                        <include name="**/*.jar" />
                </fileset>
        </path>
-       
+
+       <fileset id="tests" dir="${test}">
+               <include name="**/*.java" />
+       </fileset>
+
        <target name="build" depends="makedirs" description="Compile project to ${build} directory">
-               <javac srcdir="${src}" destdir="${build}" />
+               <javac srcdir="${src}" destdir="${build}" classpathref="classpath" />
        </target>
 
        <target name="clean" description="Clean up ${build} directory">
        </target>
 
        <target name="run" depends="build" description="Start main class">
-               <java fork="true" classpath="${build}" classpathref="classpath.lib" classname="${main}" />
+               <java fork="true" classpath="${build}" classpathref="classpath" classname="${main}" />
+       </target>
+
+       <target name="build-tests" depends="build" description="Compile tests">
+               <javac srcdir="${test}" destdir="${build}" classpath="${build}" classpathref="classpath" />
+       </target>
+
+       <target name="test" depends="build-tests" description="Run JUnit tests">
+               <junit fork="true" printsummary="withOutAndErr" haltonerror="true">
+                       <classpath>
+                               <path refid="classpath" />
+                               <pathelement path="${build}" />
+                               <pathelement path="${test}" />
+                       </classpath>
+                       <batchtest>
+                               <fileset refid="tests" />
+                       </batchtest>
+               </junit>
        </target>
 
 </project>
diff --git a/libs/junit b/libs/junit
new file mode 160000 (submodule)
index 0000000..f728fcc
--- /dev/null
@@ -0,0 +1 @@
+Subproject commit f728fccc23a159c0cd9037c5cfd779b726880afd
index 2f84683..9e31ea1 100644 (file)
@@ -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<Entry>
 {
     LinkedList<Entry> data_ = new LinkedList<Entry>();
 
@@ -12,11 +12,6 @@ public class Addressbook implements EntryConsumer, EntryProducer
         data_.add(entry);
     }
 
-    public Iterator<Entry> 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<Entry> iterator()
+    {
+        return data_.iterator();
+    }
 }
index bbf5295..cd4efb8 100644 (file)
@@ -40,4 +40,11 @@ public class Util
         if (it.hasNext()) return it.next();
         return null;
     }
+
+    public static <E> E first(Iterator<E> 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 (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);
+        }
+    }
+
+}