<commit_before>package spark;

import org.junit.Test;
import org.powermock.reflect.Whitebox;
import spark.ssl.SslStores;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class SparkInstanceTest {

    @Test(expected = HaltException.class)
    public void testHalt_withOutParameters_thenThrowHaltException(){
        SparkInstance sparkInstance = new SparkInstance();
        sparkInstance.halt();
    }

    @Test(expected = HaltException.class)
    public void testHalt_withStatusCode_thenThrowHaltException(){
        SparkInstance sparkInstance = new SparkInstance();
        sparkInstance.halt(100);
    }

    @Test(expected = HaltException.class)
    public void testHalt_withBodyContent_thenThrowHaltException(){
        SparkInstance sparkInstance = new SparkInstance();
        sparkInstance.halt("error");
    }

    @Test(expected = HaltException.class)
    public void testHalt_withStatusCodeAndBodyContent_thenThrowHaltException(){
        SparkInstance sparkInstance = new SparkInstance();
        sparkInstance.halt(100, "error");
    }

    @Test
    public void testIpAddress_withInitializedFalse(){
        SparkInstance sparkInstance = new SparkInstance();
        sparkInstance.ipAddress("127.0.0.1");

        String ipAddress = Whitebox.getInternalState(sparkInstance, "ipAddress");
        assertEquals("Value is not equal", "127.0.0.1", ipAddress);
    }

    @Test(expected = IllegalStateException.class)
    public void testIpAddress_withInitializedTrue_thenThrowIllegalStateException(){
        SparkInstance sparkInstance = new SparkInstance();
        Whitebox.setInternalState(sparkInstance, "initialized", true);
        sparkInstance.ipAddress("127.0.0.1");
    }

    @Test
    public void testSetIpAddress_withInitializedFalse(){
        SparkInstance sparkInstance = new SparkInstance();
        sparkInstance.setIpAddress("127.0.0.1");

        String ipAddress = Whitebox.getInternalState(sparkInstance, "ipAddress");
        assertEquals("Value is not equal", "127.0.0.1", ipAddress);
    }

    @Test(expected = IllegalStateException.class)
    public void testSetIpAddress_withInitializedTrue_thenThrowIllegalStateException(){
        SparkInstance sparkInstance = new SparkInstance();
        Whitebox.setInternalState(sparkInstance, "initialized", true);
        sparkInstance.setIpAddress("127.0.0.1");
    }

    @Test
    public void testPort_withInitializedFalse(){
        SparkInstance sparkInstance = new SparkInstance();
        sparkInstance.port(8080);

        int port = Whitebox.getInternalState(sparkInstance, "port");
        assertEquals("Value is not equal", 8080, port);
    }

    @Test(expected = IllegalStateException.class)
    public void testPort_withInitializedTrue_thenThrowIllegalStateException(){
        SparkInstance sparkInstance = new SparkInstance();
        Whitebox.setInternalState(sparkInstance, "initialized", true);
        sparkInstance.port(8080);
    }

    @Test
    public void testSetPort_withInitializedFalse(){
        SparkInstance sparkInstance = new SparkInstance();
        sparkInstance.setPort(8080);

        int port = Whitebox.getInternalState(sparkInstance, "port");
        assertEquals("Value is not equal", 8080, port);
    }

    @Test(expected = IllegalStateException.class)
    public void testSetPort_withInitializedTrue_thenThrowIllegalStateException(){
        SparkInstance sparkInstance = new SparkInstance();
        Whitebox.setInternalState(sparkInstance, "initialized", true);
        sparkInstance.setPort(8080);
    }

    @Test
    public void testThreadPool_withOnlyMaxThreads(){
        SparkInstance sparkInstance = new SparkInstance();
        sparkInstance.threadPool(100);
        int maxThreads = Whitebox.getInternalState(sparkInstance, "maxThreads");
        int minThreads = Whitebox.getInternalState(sparkInstance, "minThreads");
        int threadIdleTimeoutMillis = Whitebox.getInternalState(sparkInstance, "threadIdleTimeoutMillis");
        assertEquals("Value is not equal", 100, maxThreads);
        assertEquals("Value is not equal", -1, minThreads);
        assertEquals("Value is not equal", -1, threadIdleTimeoutMillis);
    }

    @Test
    public void testThreadPool_withMaxMinAndTimeoutParameters(){
        SparkInstance sparkInstance = new SparkInstance();
        sparkInstance.threadPool(100, 50, 75);
        int maxThreads = Whitebox.getInternalState(sparkInstance, "maxThreads");
        int minThreads = Whitebox.getInternalState(sparkInstance, "minThreads");
        int threadIdleTimeoutMillis = Whitebox.getInternalState(sparkInstance, "threadIdleTimeoutMillis");
        assertEquals("Value is not equal", 100, maxThreads);
        assertEquals("Value is not equal", 50, minThreads);
        assertEquals("Value is not equal", 75, threadIdleTimeoutMillis);
    }

    @Test(expected = IllegalStateException.class)
    public void testThreadPool_withMaxMinAndTimeoutParameters_thenThrowIllegalStateException(){
        SparkInstance sparkInstance = new SparkInstance();
        Whitebox.setInternalState(sparkInstance, "initialized", true);
        sparkInstance.threadPool(100, 50, 75);
    }

    @Test
    public void testSecure_thenReturnNewSslStores(){
        SparkInstance sparkInstance = new SparkInstance();
        sparkInstance.secure("keyfile", "keypassword", "truststorefile", "truststorepassword");
        SslStores sslStores = Whitebox.getInternalState(sparkInstance, "sslStores");
        assertNotNull("SslStores is null", sslStores);
        assertEquals("keystoreFile is not equal", "keyfile", sslStores.keystoreFile());
        assertEquals("keystorePassword is not equal", "keypassword", sslStores.keystorePassword());
        assertEquals("trustStoreFile is not equal", "truststorefile", sslStores.trustStoreFile());
        assertEquals("trustStorePassword is not equal", "truststorepassword", sslStores.trustStorePassword());
    }

    @Test(expected = IllegalStateException.class)
    public void testSecure_withInitializedTrue_thenThrowIllegalStateException(){
        SparkInstance sparkInstance = new SparkInstance();
        Whitebox.setInternalState(sparkInstance, "initialized", true);
        sparkInstance.secure(null, null, null, null);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testSecure_withInitializedFalse_thenThrowIllegalArgumentException(){
        SparkInstance sparkInstance = new SparkInstance();
        sparkInstance.secure(null, null, null, null);
    }

    @Test(expected = IllegalStateException.class)
    public void testWebSocketIdleTimeoutMillis_withInitializedTrue_thenThrowIllegalStateException(){
        SparkInstance sparkInstance = new SparkInstance();
        Whitebox.setInternalState(sparkInstance, "initialized", true);
        sparkInstance.webSocketIdleTimeoutMillis(100);
    }

    @Test(expected = IllegalStateException.class)
    public void testWebSocket_withInitializedTrue_thenThrowIllegalStateException(){
        SparkInstance sparkInstance = new SparkInstance();
        Whitebox.setInternalState(sparkInstance, "initialized", true);
        sparkInstance.webSocket("/", Object.class);<commit_msg>UTS-192 -  Modified test case for SparkInstanceTest addressing PR comment<commit_after>package spark;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.powermock.reflect.Whitebox;
import spark.ssl.SslStores;

import javax.servlet.http.HttpServletResponse;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class SparkInstanceTest {

    private static final String IP_ADDRESS = "127.0.0.1";
    private static final int NOT_FOUND_STATUS_CODE = HttpServletResponse.SC_NOT_FOUND;

    private SparkInstance sparkInstance;

    @Rule
    public ExpectedException thrown = ExpectedException.none();

    @Before
    public void test(){
        sparkInstance = new SparkInstance();
    }

    @Test(expected = HaltException.class)
    public void testHalt_whenOutParameters_thenThrowHaltException(){
        sparkInstance.halt();
    }

    @Test(expected = HaltException.class)
    public void testHalt_whenStatusCode_thenThrowHaltException(){
        sparkInstance.halt(NOT_FOUND_STATUS_CODE);
    }

    @Test(expected = HaltException.class)
    public void testHalt_whenBodyContent_thenThrowHaltException(){
        sparkInstance.halt("error");
    }

    @Test(expected = HaltException.class)
    public void testHalt_whenStatusCodeAndBodyContent_thenThrowHaltException(){
        sparkInstance.halt(NOT_FOUND_STATUS_CODE, "error");
    }

    @Test
    public void testIpAddress_whenInitializedFalse(){
        sparkInstance.ipAddress(IP_ADDRESS);

        String ipAddress = Whitebox.getInternalState(sparkInstance, "ipAddress");
        assertEquals("IP address should be set to the IP address that was specified", IP_ADDRESS, ipAddress);
    }

    @Test
    public void testIpAddress_whenInitializedTrue_thenThrowIllegalStateException(){
        thrown.expect(IllegalStateException.class);
        thrown.expectMessage("This must be done before route mapping has begun");

        Whitebox.setInternalState(sparkInstance, "initialized", true);
        sparkInstance.ipAddress(IP_ADDRESS);
    }

    @Test
    public void testSetIpAddress_whenInitializedFalse(){
        sparkInstance.setIpAddress(IP_ADDRESS);

        String ipAddress = Whitebox.getInternalState(sparkInstance, "ipAddress");
        assertEquals("IP address should be set to the IP address that was specified", IP_ADDRESS, ipAddress);
    }

    @Test
    public void testSetIpAddress_whenInitializedTrue_thenThrowIllegalStateException(){
        thrown.expect(IllegalStateException.class);
        thrown.expectMessage("This must be done before route mapping has begun");

        Whitebox.setInternalState(sparkInstance, "initialized", true);
        sparkInstance.setIpAddress(IP_ADDRESS);
    }

    @Test
    public void testPort_whenInitializedFalse(){
        sparkInstance.port(8080);

        int port = Whitebox.getInternalState(sparkInstance, "port");
        assertEquals("Port should be set to the Port that was specified", 8080, port);
    }

    @Test
    public void testPort_whenInitializedTrue_thenThrowIllegalStateException(){
        thrown.expect(IllegalStateException.class);
        thrown.expectMessage("This must be done before route mapping has begun");

        Whitebox.setInternalState(sparkInstance, "initialized", true);
        sparkInstance.port(8080);
    }

    @Test
    public void testSetPort_whenInitializedFalse(){
        sparkInstance.setPort(8080);

        int port = Whitebox.getInternalState(sparkInstance, "port");
        assertEquals("Port should be set to the Port that was specified", 8080, port);
    }

    @Test
    public void testSetPort_whenInitializedTrue_thenThrowIllegalStateException(){
        thrown.expect(IllegalStateException.class);
        thrown.expectMessage("This must be done before route mapping has begun");

        Whitebox.setInternalState(sparkInstance, "initialized", true);
        sparkInstance.setPort(8080);
    }

    @Test
    public void testThreadPool_whenOnlyMaxThreads(){
        sparkInstance.threadPool(100);
        int maxThreads = Whitebox.getInternalState(sparkInstance, "maxThreads");
        int minThreads = Whitebox.getInternalState(sparkInstance, "minThreads");
        int threadIdleTimeoutMillis = Whitebox.getInternalState(sparkInstance, "threadIdleTimeoutMillis");
        assertEquals("Should return maxThreads specified", 100, maxThreads);
        assertEquals("Should return minThreads specified", -1, minThreads);
        assertEquals("Should return threadIdleTimeoutMillis specified", -1, threadIdleTimeoutMillis);
    }

    @Test
    public void testThreadPool_whenMaxMinAndTimeoutParameters(){
        sparkInstance.threadPool(100, 50, 75);
        int maxThreads = Whitebox.getInternalState(sparkInstance, "maxThreads");
        int minThreads = Whitebox.getInternalState(sparkInstance, "minThreads");
        int threadIdleTimeoutMillis = Whitebox.getInternalState(sparkInstance, "threadIdleTimeoutMillis");
        assertEquals("Should return maxThreads specified", 100, maxThreads);
        assertEquals("Should return minThreads specified", 50, minThreads);
        assertEquals("Should return threadIdleTimeoutMillis specified", 75, threadIdleTimeoutMillis);
    }

    @Test
    public void testThreadPool_whenMaxMinAndTimeoutParameters_thenThrowIllegalStateException(){
        thrown.expect(IllegalStateException.class);
        thrown.expectMessage("This must be done before route mapping has begun");

        Whitebox.setInternalState(sparkInstance, "initialized", true);
        sparkInstance.threadPool(100, 50, 75);
    }

    @Test
    public void testSecure_thenReturnNewSslStores(){
        sparkInstance.secure("keyfile", "keypassword", "truststorefile", "truststorepassword");
        SslStores sslStores = Whitebox.getInternalState(sparkInstance, "sslStores");
        assertNotNull("Should return a SslStores because we configured it to have one", sslStores);
        assertEquals("Should return keystoreFile from SslStores", "keyfile", sslStores.keystoreFile());
        assertEquals("Should return keystorePassword from SslStores", "keypassword", sslStores.keystorePassword());
        assertEquals("Should return trustStoreFile from SslStores", "truststorefile", sslStores.trustStoreFile());
        assertEquals("Should return trustStorePassword from SslStores", "truststorepassword", sslStores.trustStorePassword());
    }

    @Test
    public void testSecure_whenInitializedTrue_thenThrowIllegalStateException(){
        thrown.expect(IllegalStateException.class);
        thrown.expectMessage("This must be done before route mapping has begun");

        Whitebox.setInternalState(sparkInstance, "initialized", true);
        sparkInstance.secure(null, null, null, null);
    }

    @Test
    public void testSecure_whenInitializedFalse_thenThrowIllegalArgumentException(){
        thrown.expect(IllegalArgumentException.class);
        thrown.expectMessage("Must provide a keystore file to run secured");

        sparkInstance.secure(null, null, null, null);
    }

    @Test
    public void testWebSocketIdleTimeoutMillis_whenInitializedTrue_thenThrowIllegalStateException(){
        thrown.expect(IllegalStateException.class);
        thrown.expectMessage("This must be done before route mapping has begun");

        Whitebox.setInternalState(sparkInstance, "initialized", true);
        sparkInstance.webSocketIdleTimeoutMillis(100);
    }

    @Test
    public void testWebSocket_whenInitializedTrue_thenThrowIllegalStateException(){
        thrown.expect(IllegalStateException.class);
        thrown.expectMessage("This must be done before route mapping has begun");

        Whitebox.setInternalState(sparkInstance, "initialized", true);
        sparkInstance.webSocket("/", Object.class);
{'fraction_non_alphanumeric': 0.0891029900332226, 'fraction_numerical': 0.010830564784053157, 'mean_word_length': 4.195374525371074, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 7, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5096846', 'n_tokens_mistral': 4108, 'n_tokens_neox': 3895, 'n_words': 844}