package uk.co.real_logic.aeron.util.event;

import org.junit.Test;
import uk.co.real_logic.aeron.util.concurrent.AtomicBuffer;

import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat;

public class EventCodecTest
{

    private static final String MESSAGE = "End of the world!";
    public static final String DECLARING_CLASS = EventCodecTest.class.getName();
    public static final String METHOD = "someMethod";
    public static final String FILE = EventCodecTest.class.getSimpleName() + ".java";
    public static final int lineNumber = 10;

    private static final int BUFFER_SIZE = 1024 * 10;

    private AtomicBuffer buffer = new AtomicBuffer(new byte[BUFFER_SIZE]);

    @Test
    public void dissectAsExceptionShouldContainTheValuesEncoded()
    {
        final Exception ex = new Exception(MESSAGE);
        ex.fillInStackTrace();

        final int size = EventCodec.encode(buffer, ex);
        final String written = EventCodec.dissectAsException(EventCode.EXCEPTION, buffer, 0, size);

        assertThat(written, containsString(MESSAGE));
        assertThat(written, containsString(ex.getClass().getName()));
        assertThat(written, containsString(getClass().getName()));
        assertThat(written, containsString(getClass().getSimpleName() + ".java"));
        assertThat(written, containsString("dissectAsExceptionShouldContainTheValuesEncoded"));
        // Line number of ex.fillInStackTrace();
        assertThat(written, containsString(":26"));
    }

    @Test
    public void dissectAsInvocationShouldContainTheValuesEncoded()
    {
        StackTraceElement element = new StackTraceElement(DECLARING_CLASS, METHOD, FILE, lineNumber);

        final int size = EventCodec.encode(buffer, element);
        final String written = EventCodec.dissectAsInvocation(EventCode.EXCEPTION, buffer, 0, size);

        assertThat(written, containsString(DECLARING_CLASS));
        assertThat(written, containsString(METHOD));
        assertThat(written, containsString(FILE));
        assertThat(written, containsString(":" + lineNumber));
    }

}

@@ -247,28 +247,26 @@
 cTest%0A%7B%0A
-%0A
     p
-rivate
+ublic
  static 
@@ -558,26 +558,27 @@
 nal int 
-lineNumber
+LINE_NUMBER
  = 10;%0A%0A
@@ -1406,57 +1406,8 @@
 ));%0A
-        // Line number of ex.fillInStackTrace();%0A
     
@@ -1452,13 +1452,59 @@
 (%22:2
-6
+5
 %22));
+ // Line number of ex.fillInStackTrace() above
 %0A   
@@ -1598,16 +1598,22 @@
         
+final 
 StackTra
@@ -1685,26 +1685,27 @@
 , FILE, 
-lineNumber
+LINE_NUMBER
 );%0A%0A    
@@ -2082,18 +2082,19 @@
 %22 + 
-lineNumber
+LINE_NUMBER
 ));%0A
@@ -2099,11 +2099,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

Clean up warnings.