package org.geirove.exmeso;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import org.codehaus.jackson.annotate.JsonProperty;
import org.geirove.exmeso.ExternalMergeSort.JacksonSort;
import org.geirove.exmeso.ExternalMergeSort.MergeIterator;
import org.junit.Test;

public class ExternalMergeSortTest {

    public static class StringPojo {
        private String value;
        public StringPojo(@JsonProperty("value") String value) {
            this.value = value;
        }
        public String getValue() {
            return value;
        }
        public String toString() {
            return "{\"value\":" + value + "\"}";
        }
    }

    @Test
    public void test() throws IOException {
        Comparator<StringPojo> comparator = new Comparator<StringPojo>() {
            @Override
            public int compare(StringPojo o1, StringPojo o2) {
                return o1.getValue().compareTo(o2.getValue());
            }
        };
        JacksonSort<StringPojo> writer = new JacksonSort<StringPojo>(comparator, StringPojo.class);
        ExternalMergeSort<StringPojo> sort = new ExternalMergeSort<StringPojo>(writer, new File("/tmp"), 2);

        List<StringPojo> chunk = Arrays.<StringPojo>asList(new StringPojo("B"), new StringPojo("D"), new StringPojo("E"), new StringPojo("C"), new StringPojo("A"));

        MergeIterator<StringPojo> iter = sort.mergeSort(chunk.iterator());
        try {
            while (iter.hasNext()) {
                StringPojo o = iter.next();
                System.out.println("O: " + o);
            }
        } finally {
            iter.close();
        }
    }

}

@@ -22,16 +22,99 @@
 xmeso;%0A%0A
+import static org.junit.Assert.fail;%0Aimport static org.junit.Assert.assertEquals;%0A%0A
 import j
@@ -204,24 +204,51 @@
 Comparator;%0A
+import java.util.Iterator;%0A
 import java.
@@ -832,17 +832,637 @@
   %7D%0A    
-%7D
+    public boolean equals(Object o) %7B%0A            if (o instanceof StringPojo) %7B%0A                StringPojo other = (StringPojo)o;%0A                return other.value.equals(value);%0A            %7D%0A            return false;%0A        %7D%0A        public int hashCode() %7B%0A            return value.hashCode();%0A        %7D%0A    %7D%0A%0A    private static final StringPojo A = new StringPojo(%22A%22);%0A    private static final StringPojo B = new StringPojo(%22B%22);%0A    private static final StringPojo C = new StringPojo(%22C%22);%0A    private static final StringPojo D = new StringPojo(%22D%22);%0A    private static final StringPojo E = new StringPojo(%22E%22);
 %0A%0A    @T
@@ -1789,20 +1789,21 @@
 ngPojo%3E 
-writ
+handl
 er = new
@@ -1858,16 +1858,87 @@
 class);%0A
+        String tmpdir = System.getProperty(%22java.io.tmpdir%22);%0A        %0A
         
@@ -2012,12 +2012,13 @@
 jo%3E(
-writ
+handl
 er, 
@@ -2030,14 +2030,14 @@
 ile(
-%22/
 tmp
-%22
+dir
 ), 2
@@ -2065,21 +2065,24 @@
 ngPojo%3E 
-chunk
+expected
  = Array
@@ -2106,114 +2106,335 @@
 ist(
-new StringPojo(%22B%22), new StringPojo(%22D%22), new StringPojo(%22E%22), new StringPojo(%22C%22), new StringPojo(%22A%22));%0A
+A, B, C, D, E);%0A        List%3CStringPojo%3E input = Arrays.%3CStringPojo%3EasList(B, D, E, C, A);%0A%0A        MergeIterator%3CStringPojo%3E result = sort.mergeSort(input.iterator());%0A        assertResult(result, expected);%0A    %7D%0A%0A    private void assertResult(MergeIterator%3CStringPojo%3E result, List%3CStringPojo%3E expected) throws IOException %7B
 %0A   
@@ -2430,37 +2430,32 @@
 ption %7B%0A        
-Merge
 Iterator%3CStringP
@@ -2470,28 +2470,16 @@
 r = 
-sort.mergeSort(chunk
+expected
 .ite
@@ -2477,33 +2477,32 @@
 ected.iterator()
-)
 ;%0A        try %7B%0A
@@ -2520,20 +2520,22 @@
  while (
-iter
+result
 .hasNext
@@ -2571,16 +2571,18 @@
 ojo 
-o = iter
+r = result
 .nex
@@ -2629,17 +2629,295 @@
 %22O: %22 + 
-o
+r);%0A                if (!iter.hasNext()) %7B%0A                    fail(%22More than expected%22);%0A                %7D%0A                StringPojo e = iter.next();%0A                assertEquals(r, e);%0A            %7D%0A            if (iter.hasNext()) %7B%0A                fail(%22Fewer than expected%22
 );%0A     
@@ -2957,20 +2957,22 @@
         
-iter
+result
 .close()

Test now actually a test.