package net.logstash.logging.handler;

import static org.junit.Assert.assertTrue;

import java.io.ByteArrayOutputStream;
import java.util.logging.Level;

import org.jboss.logmanager.ExtLogRecord;
import org.jboss.logmanager.handlers.ConsoleHandler;
import org.junit.Test;

import net.logstash.logging.formatter.LogstashUtilFormatter;
import net.logstash.logging.handler.SocketHandler.Protocol;

public class SockerHandlerTest {

  @Test
  public void testSocketHandler() throws Exception {
    try (SocketHandler handler = new SocketHandler(Protocol.TCP, "127.0.0.1", 5555)) {
      ByteArrayOutputStream os = new ByteArrayOutputStream();
      handler.setLevel(Level.FINE);
      handler.setAutoFlush(true);
      handler.setFormatter(new LogstashUtilFormatter());

      ConsoleHandler target = new ConsoleHandler();
      target.setAutoFlush(true);
      target.setLevel(Level.ALL);
      target.setOutputStream(os);
      target.setFormatter(new LogstashUtilFormatter());
      handler.setSubHandler(target);

      ExtLogRecord record = new ExtLogRecord(Level.FINE, "test", this.getClass().getName());
      record.setLoggerName("testSocketHandler");
      handler.doPublish(record);
      String log = new String(os.toByteArray());
      assertTrue(log.length() > 0);
      assertTrue(log.contains("\"@message\":\"test\""));
      assertTrue(log.contains("\"@source\":\"testSocketHandler\""));

      os.reset();
      record = new ExtLogRecord(Level.FINE, null, this.getClass().getName());
      record.setLoggerName("testSocketHandler");
      handler.doPublish(record);
      log = new String(os.toByteArray());
      assertTrue(log.length() == 0);

      os.reset();
      record = new ExtLogRecord(Level.FINE, "", this.getClass().getName());
      record.setLoggerName("testSocketHandler");
      handler.doPublish(record);
      log = new String(os.toByteArray());
      assertTrue(log.length() > 0);
      assertTrue(log.contains("\"@message\":\"\""));
      assertTrue(log.contains("\"@source\":\"testSocketHandler\""));
    }

  }

}

@@ -63,26 +63,17 @@
 .Assert.
-assertTrue
+*
 ;%0A%0Aimpor
@@ -749,16 +749,271 @@
 tter());
+%0A      %0A      assertEquals(Protocol.TCP, handler.getProtocol());%0A      assertEquals(5555, handler.getPort());%0A      assertEquals(%22127.0.0.1%22, handler.getAddress().getHostAddress());%0A      handler.setPort(5556);%0A      assertEquals(5556, handler.getPort());
 %0A%0A      
@@ -1251,16 +1251,69 @@
 target);
+%0A      assertEquals(target, handler.getSubHandler());
 %0A%0A      

Improve test