package org.pidster.tomcat.embed;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import java.util.Properties;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;


public class TomcatSimpleTest {

    private TomcatRuntime runtime;

    @Before
    public void setupTomcat() throws Exception {

        Properties properties = new Properties();
        properties.put("catalina.base", "src/test/resources");
        properties.put("catalina.home", "src/test/resources");

        File catalinaBase = new File("src/test/resources");

        Tomcat tomcat = new TomcatFactory().create()
            .newStandardServer(catalinaBase)
                .createApplication("/test0", "test0")
                .withDefaultConfig()
                .addServletContextListener(DummyListener.class)
                .addServletFilter(DummyFilter.class, "/*")
                .addServlet(DummyServlet.class, "/foo")
            .build();

        this.runtime = tomcat.start(5000L);
    }

    @Test
    public void test0IndexHtml() throws Exception {

        URL url = new URL("http://127.0.0.1:8090/test0/index.html");

        HttpURLConnection connection = connect(url);
        connection.connect();

        try (InputStream is = connection.getInputStream()) {

            int responseCode = connection.getResponseCode();
            Assert.assertEquals(200, responseCode);
        }
        catch (IOException e) {
            e.printStackTrace();
            Assert.fail(e.getMessage());
        }
    }

    @Test
    public void test0IndexFile() throws Exception {

        URL url = new URL("http://127.0.0.1:8090/test0/");

        HttpURLConnection connection = connect(url);
        connection.connect();

        try (InputStream is = connection.getInputStream()) {

            int responseCode = connection.getResponseCode();
            Assert.assertEquals(200, responseCode);
        }
        catch (IOException e) {
            e.printStackTrace();
            Assert.fail(e.getMessage());
        }
    }

    @Test
    public void test0Foo() throws Exception {

        URL url = new URL("http://127.0.0.1:8090/test0/foo");

        HttpURLConnection connection = connect(url);
        connection.connect();

        try (InputStream is = connection.getInputStream()) {

            int responseCode = connection.getResponseCode();
            Assert.assertEquals(200, responseCode);
        }
        catch (IOException e) {
            e.printStackTrace();
            Assert.fail(e.getMessage());
        }
    }

    @Test
    public void test1IndexFile() throws Exception {

        URL url = new URL("http://127.0.0.1:8090/test1/");

        HttpURLConnection connection = connect(url);
        connection.connect();

        try (InputStream is = connection.getInputStream()) {

            int responseCode = connection.getResponseCode();
            Assert.assertEquals(200, responseCode);
        }
        catch (IOException e) {
            e.printStackTrace();
            Assert.fail(e.getMessage());
        }
    }

    @After
    public void teardown() {
        if (runtime == null) {
            return;
        }

        runtime.stop(5000L);
    }

    private HttpURLConnection connect(URL url) throws IOException {
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setRequestMethod("GET");
        connection.addRequestProperty("Host", "localhost");
        connection.addRequestProperty("Accept", "*");

        return connection;
    }

}

@@ -425,185 +425,8 @@
  %7B%0A%0A
-        Properties properties = new Properties();%0A        properties.put(%22catalina.base%22, %22src/test/resources%22);%0A        properties.put(%22catalina.home%22, %22src/test/resources%22);%0A%0A
     
@@ -2480,523 +2480,8 @@
  %7D%0A%0A
-    @Test%0A    public void test1IndexFile() throws Exception %7B%0A%0A        URL url = new URL(%22http://127.0.0.1:8090/test1/%22);%0A%0A        HttpURLConnection connection = connect(url);%0A        connection.connect();%0A%0A        try (InputStream is = connection.getInputStream()) %7B%0A%0A            int responseCode = connection.getResponseCode();%0A            Assert.assertEquals(200, responseCode);%0A        %7D%0A        catch (IOException e) %7B%0A            e.printStackTrace();%0A            Assert.fail(e.getMessage());%0A        %7D%0A    %7D%0A%0A
     

remove unused from test