package org.yi.happy.archive;

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

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;

import org.junit.Test;
import org.yi.happy.archive.commandLine.Env;
import org.yi.happy.archive.commandLine.EnvBuilder;
import org.yi.happy.archive.file_system.FakeFileSystem;
import org.yi.happy.archive.file_system.FileSystem;
import org.yi.happy.archive.test_data.TestData;

/**
 * Tests for {@link FileStoreTagGetMain}.
 */
public class FileStoreTagGetMainTest {
    /**
     * restore two files, with all the data already available.
     * 
     * @throws IOException
     */
    @Test
    public void test1() throws IOException {
        ByteArrayInputStream in = new ByteArrayInputStream(TestData.TAG_FILES
                .getBytes());
        FileSystem fs = new FakeFileSystem();
        FileBlockStore store = new FileBlockStore(fs, "store");
        store.put(TestData.KEY_CONTENT.getEncodedBlock());
        store.put(TestData.KEY_CONTENT_40.getEncodedBlock());

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

        Env env = new EnvBuilder().withStore("store").withNeed("request")
                .create();
        new FileStoreTagGetMain(fs, waitHandler, in, null).run(env);

        assertArrayEquals(TestData.FILE_CONTENT.getBytes(), fs
                .load("hello.txt"));
        assertArrayEquals(TestData.FILE_CONTENT_40.getBytes(), fs
                .load("test.dat"));
    }

    /**
     * restore two files, with all the data available after it is asked for.
     * 
     * @throws IOException
     */
    @Test
    public void test2() throws IOException {
        ByteArrayInputStream in = new ByteArrayInputStream(TestData.TAG_FILES
                .getBytes());
        final FileSystem fs = new FakeFileSystem();
        final FileBlockStore store = new FileBlockStore(fs, "store");

        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);

                    String want = TestData.KEY_CONTENT.getLocatorKey() + "\n"
                            + TestData.KEY_CONTENT_40.getLocatorKey() + "\n";
                    assertArrayEquals(ByteString.toUtf8(want), fs
                            .load("request"));

                    store.put(TestData.KEY_CONTENT.getEncodedBlock());
                    store.put(TestData.KEY_CONTENT_40.getEncodedBlock());

                    state = state1;
                }
            };

            private WaitHandler state1 = new WaitHandler() {
                @Override
                public void doWait(boolean progress) throws IOException {
                    fail();
                }
            };
        };

        Env env = new EnvBuilder().withStore("store").withNeed("request")
                .create();
        new FileStoreTagGetMain(fs, waitHandler, in, null).run(env);

        assertArrayEquals(TestData.FILE_CONTENT.getBytes(), fs
                .load("hello.txt"));
        assertArrayEquals(TestData.FILE_CONTENT_40.getBytes(), fs
                .load("test.dat"));
    }

    /**
     * trigger printing of the command usage.
     * 
     * @throws IOException
     */
    @Test
    public void test3() throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Env env = new EnvBuilder().create();

        new FileStoreTagGetMain(null, null, null, new PrintStream(out, true))
                .run(env);

        assertTrue(out.size() > 0);
    }
}

@@ -122,51 +122,8 @@
 se;%0A
-import static org.junit.Assert.assertTrue;%0A
 impo
@@ -212,85 +212,19 @@
 .io.
-ByteArrayOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.PrintStream
+IOException
 ;%0A%0Ai
@@ -3594,419 +3594,7 @@
    %7D
-%0A%0A    /**%0A     * trigger printing of the command usage.%0A     * %0A     * @throws IOException%0A     */%0A    @Test%0A    public void test3() throws IOException %7B%0A        ByteArrayOutputStream out = new ByteArrayOutputStream();%0A        Env env = new EnvBuilder().create();%0A%0A        new FileStoreTagGetMain(null, null, null, new PrintStream(out, true))%0A                .run(env);%0A%0A        assertTrue(out.size() %3E 0);%0A    %7D
 %0A%7D%0A

Drop test that checks if a sub-command prints out usage when not enough arguments are given.