package org.cli;

import org.commcare.util.cli.ApplicationHost;
import org.commcare.util.cli.CliCommand;
import org.commcare.util.engine.CommCareConfigEngine;
import org.javarosa.core.util.externalizable.LivePrototypeFactory;
import org.javarosa.core.util.externalizable.PrototypeFactory;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.io.Reader;
import java.io.StringReader;

/**
 * Created by wpride on 12/14/2016.
 */

public class CliTests {

    @Test
    public void testApplicationHost() throws Exception {
        ClassLoader classLoader = getClass().getClassLoader();
        File appFile = new File(classLoader.getResource("basic_app/basic.ccz").getFile());
        String resourcePath = appFile.getAbsolutePath();
        PrototypeFactory prototypeFactory = new LivePrototypeFactory();
        CommCareConfigEngine engine = CliCommand.configureApp(resourcePath, prototypeFactory);

        PrintStream outStream = new PrintStream(new ByteArrayOutputStream());
        BufferedReader bufferedReader = new TestReader(new String[] {"1"}, outStream);


        ApplicationHost host = new ApplicationHost(engine, prototypeFactory, bufferedReader, outStream);
        File restoreFile = new File(classLoader.getResource("case_create_basic.xml").getFile());
        String restorePath = restoreFile.getAbsolutePath();
        host.setRestoreToLocalFile(restorePath);
        host.run();

    }

    class TestReader extends BufferedReader {

        private String[] args;
        private int index;
        private PrintStream outStream;

        public TestReader(String[] args, PrintStream outStream) {
            super(new StringReader("derp"));
            this.args = args;
            this.outStream = outStream;
        }

        @Override
        public String readLine() throws IOException {
            if (index >= args.length) {
                System.out.println("Overflow!");
            }
            String ret = args[index];
            System.out.println("Reading line! " + ret);
            index++;
            return ret;
        }
    }

}

@@ -274,32 +274,57 @@
 ototypeFactory;%0A
+import org.junit.Assert;%0A
 import org.junit
@@ -528,16 +528,110 @@
 gReader;
+%0Aimport java.nio.charset.StandardCharsets;%0A%0Aimport static junit.framework.TestCase.assertTrue;
 %0A%0A/**%0A *
@@ -1148,16 +1148,82 @@
         
+ByteArrayOutputStream baos = new ByteArrayOutputStream();%0A        
 PrintStr
@@ -1258,35 +1258,12 @@
 eam(
-new ByteArrayOutputStream()
+baos
 );%0A 
@@ -1337,20 +1337,26 @@
 %7B%221%22
-%7D, outStream
+, %220%22, %22%5Cn%22%7D, baos
 );%0A%0A
@@ -1679,20 +1679,173 @@
     
-host.run();%0A
+boolean passed = false;%0A        try %7B%0A            host.run();%0A        %7D catch (RuntimeException e) %7B%0A            passed = true;%0A        %7D%0A        assertTrue(passed);
 %0A   
@@ -1969,20 +1969,30 @@
 private 
-Prin
+ByteArrayOutpu
 tStream 
@@ -2044,20 +2044,30 @@
 %5D args, 
-Prin
+ByteArrayOutpu
 tStream 
@@ -2120,11 +2120,12 @@
 r(%22d
-erp
+ummy
 %22));
@@ -2270,32 +2270,121 @@
 s IOException %7B%0A
+            String output = new String(outStream.toByteArray(), StandardCharsets.UTF_8);%0A
             if (
@@ -2393,71 +2393,565 @@
 dex 
-%3E= args.length) %7B%0A                System.out.println(%22Overflow!
+== 0) %7B%0A                Assert.assertTrue(output.contains(%22Basic Tests%22));%0A                Assert.assertTrue(output.contains(%220)Basic Form Tests%22));%0A            %7D else if (index == 1) %7B%0A                Assert.assertTrue(output.contains(%220)Constraints%22));%0A            %7D else if (index == 2) %7B%0A                Assert.assertTrue(output.contains(%22Press Return to proceed%22));%0A            %7D else if (index == 3) %7B%0A                Assert.assertTrue(output.contains(%22This form tests different logic constraints.%22));%0A                throw new IOException(%22Good crash
 %22);%0A
@@ -3079,16 +3079,47 @@
 ndex++;%0A
+            outStream.reset();%0A
         

Fix test