package org.yi.happy.archive;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

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

import org.junit.Test;
import org.yi.happy.archive.test_data.TestData;

/**
 * Tests for {@link StoreFileGetMain}.
 */
public class StoreFileGetMainTest {
    /**
     * an expected good run.
     * 
     * @throws IOException
     */
    @Test
    public void test1() throws IOException {
        /*
         * NOTE this is not strictly speaking a unit test since there are two
         * layers of objects in use to exercise the functionality.
         */

        final FragmentSaveMemory target = new FragmentSaveMemory();
        final MapClearBlockSource store = new MapClearBlockSource();
        final NeedCapture needHandler = new NeedCapture();

        WaitHandler waitHandler = new WaitHandler() {
            @Override
            public void doWait(boolean progress) throws IOException {
                state.doWait(progress);
            }

            private WaitHandler state = new WaitHandler() {
                @Override
                public void doWait(boolean progress) throws IOException {
                    assertFalse(progress);

                    /*
                     * check the request list
                     */
                    assertEquals(Arrays.asList(TestData.KEY_CONTENT_MAP
                            .getLocatorKey()), needHandler.getKeys());

                    /*
                     * add the map block to the store
                     */
                    store.put(TestData.KEY_CONTENT_MAP);

                    state = state2;
                }
            };

            private final WaitHandler state2 = new WaitHandler() {
                @Override
                public void doWait(boolean progress) throws IOException {
                    assertTrue(progress);

                    /*
                     * check the request list
                     */
                    assertEquals(Arrays.asList(
                            TestData.KEY_CONTENT_1.getLocatorKey(),
                            TestData.KEY_CONTENT_2.getLocatorKey()),
                            needHandler.getKeys());

                    /*
                     * add the second part
                     */
                    store.put(TestData.KEY_CONTENT_2);

                    state = state3;
                }
            };

            private final WaitHandler state3 = new WaitHandler() {
                @Override
                public void doWait(boolean progress) throws IOException {
                    assertTrue(progress);

                    /*
                     * check the request list
                     */
                    assertEquals(Arrays.asList(TestData.KEY_CONTENT_1
                            .getLocatorKey()), needHandler.getKeys());

                    /*
                     * add the first part
                     */
                    store.put(TestData.KEY_CONTENT_1);

                    state = state4;
                }
            };

            private final WaitHandler state4 = new WaitHandler() {
                @Override
                public void doWait(boolean progress) {
                    fail();
                }
            };

        };

        List<String> args = Arrays.asList(TestData.KEY_CONTENT_MAP.getFullKey()
                .toString(), "out");
        new StoreFileGetMain(store, target, waitHandler, needHandler, args)
                .run();

        assertArrayEquals(ByteString.toUtf8("0123456789"), target.get("out"));
    }
}

@@ -272,16 +272,44 @@
 eption;%0A
+import java.util.ArrayList;%0A
 import j
@@ -368,24 +368,68 @@
 junit.Test;%0A
+import org.yi.happy.archive.key.LocatorKey;%0A
 import org.y
@@ -556,307 +556,434 @@
     
-/**%0A     * an expected good run.%0A     * %0A     * @throws IOException%0A     */%0A    @Test%0A    public void 
+private static final Bytes D = new Bytes(%220123456789%22);%0A    private static final String N = %22out%22;%0A    private static final TestData MAP = TestData.KEY_CONTENT_MAP;%0A    private static final TestData C1 = TestData.KEY_CONTENT_1;%0A    priva
 te
+ 
 st
-1() throws IOException %7B%0A    
+atic final TestData C2 = TestData.KEY_CONTENT_2;%0A%0A
     /*
+*
 %0A     
-    * NOTE this is not strictly speaking a unit test since there are two
+* an expected good run.%0A     * %0A     * @throws IOException
 %0A     
+*/%0A
     
-* layers of objects in use to exercise the functionality.%0A         */%0A
+@Test%0A    public void test1() throws IOException %7B
 %0A   
@@ -1711,91 +1711,19 @@
 als(
-Arrays.asList(TestData.KEY_CONTENT_MAP%0A                            .getLocatorKey()
+keyList(MAP
 ), n
@@ -1877,37 +1877,16 @@
 ore.put(
-TestData.KEY_CONTENT_
 MAP);%0A%0A 
@@ -2294,187 +2294,24 @@
 als(
-Arrays.asList(%0A                            TestData.KEY_CONTENT_1.getLocatorKey(),%0A                            TestData.KEY_CONTENT_2.getLocatorKey()),%0A                           
+keyList(C1, C2),
  nee
@@ -2452,37 +2452,17 @@
 ore.put(
-TestData.KEY_CONTENT_
+C
 2);%0A%0A   
@@ -2868,89 +2868,18 @@
 als(
-Arrays.asList(TestData.KEY_CONTENT_1%0A                            .getLocatorKey()
+keyList(C1
 ), n
@@ -3021,37 +3021,17 @@
 ore.put(
-TestData.KEY_CONTENT_
+C
 1);%0A%0A   
@@ -3351,76 +3351,29 @@
 s = 
-Arrays.asList(TestData.KEY_CONTENT_MAP.getFullKey()%0A                
+list(MAP.getFullKey()
 .toS
@@ -3381,21 +3381,17 @@
 ring(), 
-%22out%22
+N
 );%0A     
@@ -3516,65 +3516,369 @@
 als(
-ByteString.toUtf8(%220123456789%22), target.get(%22out%22));%0A    %7D
+D.toByteArray(), target.get(N));%0A    %7D%0A%0A    private %3CT%3E List%3CT%3E list(T... items) %7B%0A        return Arrays.asList(items);%0A    %7D%0A%0A    private List%3CLocatorKey%3E keyList(TestData... items) %7B%0A        List%3CLocatorKey%3E out = new ArrayList%3CLocatorKey%3E();%0A        for (TestData item : items) %7B%0A            out.add(item.getLocatorKey());%0A        %7D%0A        return out;%0A    %7D%0A
 %0A%7D%0A

simplify StoreFileGetMainTest