Added test suite for empty elements in result set

Change-Id: I3787285f613dd2f01a6b859d0e4344cd104275da
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/ElementSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/ElementSpans.java
index ba056d1..44ac329 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/ElementSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/ElementSpans.java
@@ -141,7 +141,10 @@
             bb.put(payload.get(0));
 
             this.payloadTypeIdentifier = bb.get(0);
-            this.matchEndPosition = bb.getInt(9);
+
+            if (payloadTypeIdentifier == PayloadTypeIdentifier.ELEMENT.value) {
+				this.matchEndPosition = bb.getInt(9);
+			};
 
             if (payloadTypeIdentifier == PayloadTypeIdentifier.ELEMENT.value
                     && length > 15) {
@@ -161,7 +164,7 @@
             return;
         }
 
-        this.matchEndPosition = this.matchStartPosition;
+		this.matchEndPosition = this.matchStartPosition;
         this.setSpanId((short) -1);
         this.hasSpanId = false;
         this.matchPayload = null;
diff --git a/src/main/java/de/ids_mannheim/korap/response/Match.java b/src/main/java/de/ids_mannheim/korap/response/Match.java
index 4bf7109..0c89010 100644
--- a/src/main/java/de/ids_mannheim/korap/response/Match.java
+++ b/src/main/java/de/ids_mannheim/korap/response/Match.java
@@ -57,7 +57,7 @@
     private final static Logger log = LoggerFactory.getLogger(Match.class);
 
     // This advices the java compiler to ignore all loggings
-    public static final boolean DEBUG = false;
+    public static final boolean DEBUG = true;
 
     // Mapper for JSON serialization
     ObjectMapper mapper = new ObjectMapper();
@@ -989,6 +989,10 @@
         // First element of sorted array
         HighlightCombinatorElement elem = this.snippetArray.getFirst();
 
+		// Untested
+		if (elem == null)
+			return null;
+		
         // Create context
         sb.append("<span class=\"context-left\">");
         if (this.startMore)
@@ -1017,14 +1021,19 @@
             // decrement end
             end--;
         };
-        if (this.endMore)
+
+		if (this.endMore)
             rightContext.append("<span class=\"more\"></span>");
+
         rightContext.append("</span>");
 
         // Iterate through all remaining elements
         sb.append("<span class=\"match\">");
         for (short i = start; i <= end; i++) {
-            sb.append(this.snippetArray.get(i).toHTML(this, level, levelCache));
+			elem = this.snippetArray.get(i);
+			// UNTESTED
+			if (elem != null)
+				sb.append(elem.toHTML(this, level, levelCache));
         };
         sb.append("</span>");
         sb.append(rightContext);
diff --git a/src/test/java/de/ids_mannheim/korap/highlight/TestHighlight.java b/src/test/java/de/ids_mannheim/korap/highlight/TestHighlight.java
index 5507a89..a749606 100644
--- a/src/test/java/de/ids_mannheim/korap/highlight/TestHighlight.java
+++ b/src/test/java/de/ids_mannheim/korap/highlight/TestHighlight.java
@@ -403,4 +403,50 @@
                         + "<span class=\"context-right\"></span>",
                 km.getSnippetHTML());
     };
+
+	
+    @Test
+    public void highlightEmptySpan () throws IOException, QueryException {
+
+        KrillIndex ki = new KrillIndex();
+
+		// <>:s$<b>65<i>38<b>0
+        // <a>x<a>y<a>zhij</a>hij</a>hij</a>hij</a>
+        FieldDocument fd = new FieldDocument();
+        fd.addTV("base", "x  y  z  h  i  j  h  i  j  h  i  j  ",
+                "[(0-3)s:x|<>:a$<b>64<i>0<i>3<i>12<b>0]"
+                        + "[(3-6)s:y|<>:a$<b>64<i>3<i>6<i>9<b>0]"
+                        + "[(6-9)s:z|<>:a$<b>64<i>6<i>9<i>6|<>:a$<b>65<i>6]"
+                        + "[(9-12)s:h<b>0]" + "[(12-15)s:i]" + "[(15-18)s:j]"
+                        + "[(18-21)s:h]" + "[(21-24)s:i]" + "[(24-27)s:j]"
+                        + "[(27-30)s:h]" + "[(30-33)s:i]" + "[(33-36)s:j]");
+        ki.addDoc(fd);
+
+        // Commit!
+        ki.commit();
+        QueryBuilder kq = new QueryBuilder("base");
+        SpanQuery q = (SpanQuery) kq.tag("a").toQuery();
+
+		Krill qs = new Krill(q);
+        qs.getMeta().getContext().left.setToken(true).setLength((short) 5);
+        qs.getMeta().getContext().right.setToken(true).setLength((short) 5);
+
+        Result kr = ki.search(qs);
+        assertEquals((long) 4, kr.getTotalResults());
+
+		Match km = kr.getMatch(2);
+		assertEquals(
+			"<span class=\"context-left\">"+
+			"</span>"+
+			"<span class=\"match\">"+
+			"<mark>x  y  z  </mark>"+
+			"</span><span class=\"context-right\">h  i  j  h  i  j  h  i  j  </span>",
+			km.getSnippetHTML());
+
+		km = kr.getMatch(3);
+		assertEquals(
+			"",
+			km.getSnippetHTML());
+
+	};
 };