package spark.embeddedserver;

import java.io.File;

import org.eclipse.jetty.server.NCSARequestLog;
import org.eclipse.jetty.server.Server;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import spark.embeddedserver.jetty.EmbeddedJettyFactory;
import spark.embeddedserver.jetty.JettyServerFactory;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static spark.Spark.get;
import static spark.Spark.stop;

public class EmbeddedServersTest {
    @Rule
    public TemporaryFolder temporaryFolder = new TemporaryFolder();


    @Test
    public void testAddAndCreate_whenCreate_createsCustomServer() throws Exception {
        // Create custom Server
        Server server = new Server();
        File requestLogDir = temporaryFolder.newFolder();
        File requestLogFile = new File(requestLogDir, "request.log");
        server.setRequestLog(new NCSARequestLog(requestLogFile.getAbsolutePath()));
        JettyServerFactory serverFactory = mock(JettyServerFactory.class);
        when(serverFactory.create(0, 0, 0)).thenReturn(server);

        String id = "custom";

        // Register custom server
        EmbeddedServers.add(id, new EmbeddedJettyFactory(serverFactory));
        EmbeddedServer embeddedServer = EmbeddedServers.create(id, null, null, false);
        assertNotNull(embeddedServer);
        embeddedServer.ignite("localhost", 0, null, 0, 0, 0);

        assertTrue(requestLogFile.exists());
        embeddedServer.extinguish();
        verify(serverFactory).create(0, 0, 0);
    }

    @Test
    public void testAdd_whenConfigureRoutes_createsCustomServer() throws Exception {
        String id = "custom";
        File requestLogDir = temporaryFolder.newFolder();
        File requestLogFile = new File(requestLogDir, "request.log");
        // Register custom server
        EmbeddedServers.add(EmbeddedServers.Identifiers.JETTY, new EmbeddedJettyFactory((i, j, k) -> {
            Server server = new Server();
            server.setRequestLog(new NCSARequestLog(requestLogFile.getAbsolutePath()));
            return server;
        }));

        get("/", (request, response) -> "OK");

        stop();
        assertTrue(requestLogFile.exists());

    }

}

@@ -134,16 +134,45 @@
 Server;%0A
+import org.junit.AfterClass;%0A
 import o
@@ -246,24 +246,44 @@
 aryFolder;%0A%0A
+import spark.Spark;%0A
 import spark
@@ -595,71 +595,8 @@
 hen;
-%0Aimport static spark.Spark.get;%0Aimport static spark.Spark.stop;
 %0A%0Apu
@@ -628,16 +628,17 @@
 sTest %7B%0A
+%0A
     @Rul
@@ -708,17 +708,16 @@
 der();%0A%0A
-%0A
     @Tes
@@ -1788,38 +1788,8 @@
 n %7B%0A
-        String id = %22custom%22;%0A
     
@@ -2219,25 +2219,24 @@
    %7D));%0A
-%0A
         
 get(%22/%22,
@@ -2227,16 +2227,22 @@
         
+Spark.
 get(%22/%22,
@@ -2272,25 +2272,24 @@
  %22OK%22);%0A
-%0A
         
 stop();%0A
@@ -2284,16 +2284,38 @@
     
-stop
+Spark.awaitInitialization
 ();%0A
+%0A
     
@@ -2355,16 +2355,96 @@
 sts());%0A
+    %7D%0A%0A    @AfterClass%0A    public static void tearDown() %7B%0A        Spark.stop();
 %0A    %7D%0A%0A

Fix EmbeddedServersTest