<commit_before>package com.thoughtworks.frankenstein.application;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.util.List;

import org.jmock.MockObjectTestCase;

import com.thoughtworks.frankenstein.common.Constants;
import com.thoughtworks.frankenstein.events.DefaultEventRegistry;
import com.thoughtworks.frankenstein.events.EnterTextEvent;
import com.thoughtworks.frankenstein.recorders.ScriptListener;
import com.thoughtworks.frankenstein.script.Script;
import com.thoughtworks.frankenstein.script.TestReporter;

/**
 * Ensures behaviour of socket listener
 */
public class SocketListenerTest extends MockObjectTestCase {

    public void testAcceptsTextFromSocket() throws IOException, InterruptedException {
        MockFrankensteinRecorder recorder = new MockFrankensteinRecorder();
        SocketListener listener = new SocketListener(recorder);
        listener.start(Constants.LISTEN_PORT);
        Socket socket = new Socket(InetAddress.getLocalHost(), Constants.LISTEN_PORT);

        BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        writer.write("EnterText \"textField\" \"abc\"\n");
        writer.write("EnterText \"def\" \"def\"\n");
        writer.write(Constants.END_OF_SCRIPT + "\n");
        writer.flush();

        synchronized (MockFrankensteinRecorder.RECORDER_LOCK) {
            MockFrankensteinRecorder.RECORDER_LOCK.wait(10000);
        }

        assertNotNull(recorder.events);
        assertEquals(2, recorder.events.size());
        assertEquals(new EnterTextEvent("textField", "abc"), recorder.events.get(0));
        assertEquals(new EnterTextEvent("def", "def"), recorder.events.get(1));

        listener.scriptCompleted(true);

        String testStatus = reader.readLine();
        assertEquals(Constants.SCRIPT_PASSED, testStatus);

        String eof = reader.readLine();
        assertNull(eof);

        reader.close();
        writer.close();
        socket.close();
        listener.stop();
    }

    private static class MockFrankensteinRecorder implements FrankensteinRecorder {
        List events;
        static final Object RECORDER_LOCK = new Object();

        public void start() {
        }

        public void stop() {
        }<commit_msg>Stabilizing SocketListenerTest<commit_after>package com.thoughtworks.frankenstein.application;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.util.List;

import org.jmock.MockObjectTestCase;

import com.thoughtworks.frankenstein.common.Constants;
import com.thoughtworks.frankenstein.events.DefaultEventRegistry;
import com.thoughtworks.frankenstein.events.EnterTextEvent;
import com.thoughtworks.frankenstein.recorders.ScriptListener;
import com.thoughtworks.frankenstein.script.Script;
import com.thoughtworks.frankenstein.script.TestReporter;

/**
 * Ensures behaviour of socket listener
 */
public class SocketListenerTest extends MockObjectTestCase {

    public void testAcceptsTextFromSocket() throws IOException, InterruptedException {
        MockFrankensteinRecorder recorder = new MockFrankensteinRecorder();
        SocketListener listener = new SocketListener(recorder);
        listener.start(Constants.LISTEN_PORT);
        Socket socket = new Socket(InetAddress.getLocalHost(), Constants.LISTEN_PORT);
        BufferedReader reader;
        BufferedWriter writer;
        synchronized (MockFrankensteinRecorder.RECORDER_LOCK) {
            reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            writer.write("EnterText \"textField\" \"abc\"\n");
            writer.write("EnterText \"def\" \"def\"\n");
            writer.write(Constants.END_OF_SCRIPT + "\n");
            writer.flush();
            MockFrankensteinRecorder.RECORDER_LOCK.wait(10000);
        }

        assertNotNull(recorder.events);
        assertEquals(2, recorder.events.size());
        assertEquals(new EnterTextEvent("textField", "abc"), recorder.events.get(0));
        assertEquals(new EnterTextEvent("def", "def"), recorder.events.get(1));

        listener.scriptCompleted(true);

        String testStatus = reader.readLine();
        assertEquals(Constants.SCRIPT_PASSED, testStatus);

        String eof = reader.readLine();
        assertNull(eof);

        reader.close();
        writer.close();
        socket.close();
        listener.stop();
    }

    private static class MockFrankensteinRecorder implements FrankensteinRecorder {
        List events;
        static final Object RECORDER_LOCK = new Object();

        public void start() {
        }

        public void stop() {
        }
{'fraction_non_alphanumeric': 0.10439447080668454, 'fraction_numerical': 0.003301010934598721, 'mean_word_length': 3.5649717514124295, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '2277979', 'n_tokens_mistral': 1347, 'n_tokens_neox': 1249, 'n_words': 245}