Fix and test list iterations

Change-Id: Ie5b5bd482610936c925f68e4882585b85efc1b53
diff --git a/plugin/src/main/java/de/ids_mannheim/korap/plkexport/MatchAggregator.java b/plugin/src/main/java/de/ids_mannheim/korap/plkexport/MatchAggregator.java
index 19e4709..9720d6d 100644
--- a/plugin/src/main/java/de/ids_mannheim/korap/plkexport/MatchAggregator.java
+++ b/plugin/src/main/java/de/ids_mannheim/korap/plkexport/MatchAggregator.java
@@ -34,6 +34,11 @@
      */
     public MatchAggregator (String resp) throws IOException {
 
+        matches = new LinkedList();
+
+        if (resp == null)
+            return;
+        
         JsonParser parser = mapper.getFactory().createParser(resp);
         JsonNode actualObj = mapper.readTree(parser);
 
@@ -52,12 +57,12 @@
         
         // Iterate over the results of the current file
         Iterator<JsonNode> mNode = mNodes.elements();
-        matches = new LinkedList();
         while (mNode.hasNext()) {
             this.matches.add(mNode.next());
         };
     };
 
+
     /**
      * Append more matches to the result set.
      */
@@ -87,16 +92,17 @@
         };
     };
 
+
     /**
      * Return an iterator for all matches
      */
-    public Iterator<JsonNode> iterator() { 
+    public MatchIterator iterator() { 
         return new MatchIterator(); 
     };
 
 
     // Private iterator class
-    private class MatchIterator implements Iterator<JsonNode> {
+    public class MatchIterator implements Iterator<JsonNode> {
         private int listIndex, fileIndex;
 
         // Constructor
@@ -109,7 +115,7 @@
 
         @Override
         public boolean hasNext () {
-            if (listIndex >= 0 || fileIndex >= 0) {
+            if (this.listIndex >= 0 || this.fileIndex >= 0) {
                 return true;
             };
             return false;
@@ -117,10 +123,12 @@
 
         @Override
         public JsonNode next () {
-            if (this.listIndex > 0) {
+            if (this.listIndex >= 0) {
                 int i = this.listIndex;
-                if (i >= matches.size()) {
+                if (i >= matches.size() - 1) {
                     this.listIndex = -1;
+                } else {
+                    this.listIndex++;
                 };
                 return matches.get(i);
             };
diff --git a/plugin/src/test/java/de/ids_mannheim/korap/plkexport/MatchAggregatorTest.java b/plugin/src/test/java/de/ids_mannheim/korap/plkexport/MatchAggregatorTest.java
index a819414..2859597 100644
--- a/plugin/src/test/java/de/ids_mannheim/korap/plkexport/MatchAggregatorTest.java
+++ b/plugin/src/test/java/de/ids_mannheim/korap/plkexport/MatchAggregatorTest.java
@@ -5,6 +5,7 @@
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.fail;
 import org.junit.Test;
 
@@ -18,6 +19,16 @@
         assertNull(m.meta);
         assertNull(m.query);
         assertNull(m.collection);
+
+        MatchAggregator.MatchIterator mi = m.iterator();
+
+        assertFalse(mi.hasNext());
+        assertNull(mi.next());
+
+        m = new MatchAggregator(null);
+        assertNull(m.meta);
+        assertNull(m.query);
+        assertNull(m.collection);
     };
 
     @Test
@@ -29,4 +40,22 @@
         assertNull(m.query);
         assertNull(m.collection);
     };
+  
+    @Test
+    public void testMatchesInit () throws IOException {
+        MatchAggregator m = new MatchAggregator(
+            "{\"matches\":[\"first\",\"second\"]}"
+            );
+        assertNull(m.meta);
+        assertNull(m.query);
+        assertNull(m.collection);
+
+        MatchAggregator.MatchIterator mi = m.iterator();
+
+        assertTrue(mi.hasNext());
+        assertEquals(mi.next().toString(),"\"first\"");
+        assertTrue(mi.hasNext());
+        assertEquals(mi.next().toString(),"\"second\"");
+        assertFalse(mi.hasNext());
+    };
 };