<commit_before>package com.farpost.intellij.remotecall.notifier;

import com.farpost.intellij.remotecall.handler.MessageHandler;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull;

@Test
public class SocketMessageNotifierTest {

	private Thread notifierThread;
	private StubMessageHandler messageHandler = new StubMessageHandler();
	private ServerSocket socket;

	@BeforeMethod
	public void setUp() throws IOException {
		MessageNotifier notifier = createNotifier();
		notifier.addMessageHandler(messageHandler);

		notifierThread = new Thread(notifier);
		notifierThread.start();
	}

	@AfterMethod
	public void tearDown() throws IOException {
		messageHandler.clear();
		notifierThread.interrupt();
		disposeNotifier();
	}

	@Test
	public void notifierShouldCallHandlerOnMessageReceived() throws IOException {
		sendMessage("GET /?message=HelloFile.java");
		assertEquals(messageHandler.getLastMessage(), "HelloFile.java");
	}

	@Test
	public void notifierShouldSkipEmptyMessages() throws IOException {
		sendMessage("");
		assertNull(messageHandler.getLastMessage(),
			"Received " + messageHandler.getLastMessage() + ". Null expected");
		messageHandler.clear();
	}

	@Test
	public void notifierShouldReceiveOnlyGetRequests() throws IOException {
		sendMessage("GET /?message=foo");
		assertEquals(messageHandler.getLastMessage(), "foo");
		messageHandler.clear();

		sendMessage("POST /\r\n\r\nmessage=bar");
		assertNull(messageHandler.getLastMessage(),
			"Received " + messageHandler.getLastMessage() + ". Null expected");
		messageHandler.clear();

		sendMessage("DELETE /?message=bar");
		assertNull(messageHandler.getLastMessage(),
			"Received " + messageHandler.getLastMessage() + ". Null expected");
		messageHandler.clear();
	}

	private MessageNotifier createNotifier() throws IOException {
		socket = new ServerSocket();
		socket.bind(new InetSocketAddress("localhost", 62775));
		return new SocketMessageNotifier(socket);
	}

	private void sendMessage(String message) throws IOException {
		Socket client = new Socket("localhost", 62775);<commit_msg>Extract PORT constant<commit_after>package com.farpost.intellij.remotecall.notifier;

import com.farpost.intellij.remotecall.handler.MessageHandler;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull;

@Test
public class SocketMessageNotifierTest {

	private final int PORT = 62775;
	private Thread notifierThread;
	private StubMessageHandler messageHandler = new StubMessageHandler();
	private ServerSocket socket;

	@BeforeMethod
	public void setUp() throws IOException {
		MessageNotifier notifier = createNotifier();
		notifier.addMessageHandler(messageHandler);

		notifierThread = new Thread(notifier);
		notifierThread.start();
	}

	@AfterMethod
	public void tearDown() throws IOException {
		messageHandler.clear();
		notifierThread.interrupt();
		disposeNotifier();
	}

	@Test
	public void notifierShouldCallHandlerOnMessageReceived() throws IOException {
		sendMessage("GET /?message=HelloFile.java");
		assertEquals(messageHandler.getLastMessage(), "HelloFile.java");
	}

	@Test
	public void notifierShouldSkipEmptyMessages() throws IOException {
		sendMessage("");
		assertNull(messageHandler.getLastMessage(),
			"Received " + messageHandler.getLastMessage() + ". Null expected");
		messageHandler.clear();
	}

	@Test
	public void notifierShouldReceiveOnlyGetRequests() throws IOException {
		sendMessage("GET /?message=foo");
		assertEquals(messageHandler.getLastMessage(), "foo");
		messageHandler.clear();

		sendMessage("POST /\r\n\r\nmessage=bar");
		assertNull(messageHandler.getLastMessage(),
			"Received " + messageHandler.getLastMessage() + ". Null expected");
		messageHandler.clear();

		sendMessage("DELETE /?message=bar");
		assertNull(messageHandler.getLastMessage(),
			"Received " + messageHandler.getLastMessage() + ". Null expected");
		messageHandler.clear();
	}

	private MessageNotifier createNotifier() throws IOException {
		socket = new ServerSocket();
		socket.bind(new InetSocketAddress("localhost", PORT));
		return new SocketMessageNotifier(socket);
	}

	private void sendMessage(String message) throws IOException {
		Socket client = new Socket("localhost", PORT);
{'fraction_non_alphanumeric': 0.11051051051051052, 'fraction_numerical': 0.003003003003003003, 'mean_word_length': 6.261627906976744, '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': '683728', 'n_tokens_mistral': 1483, 'n_tokens_neox': 1390, 'n_words': 292}