package net.openhft.chronicle.wire;

import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.core.Mocker;
import org.junit.Test;

import java.io.StringWriter;

import static junit.framework.TestCase.assertFalse;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/*
 * Created by Peter Lawrey on 17/05/2017.
 */
public class MethodWriterTest {
    @Test
    public void testSubclasses() {
        Wire wire = new TextWire(Bytes.elasticHeapByteBuffer(256));
        Event writer = wire.methodWriterBuilder(Event.class).genericEvent("event").build();
        writer.event("top", new MethodReaderTest.MRT1("one"));
        writer.event("top", new MethodReaderTest.MRT2("one", "two"));
        writer.event("mid", new MethodReaderTest.MRT1("1"));
        writer.event("mid", new MethodReaderTest.MRT2("1", "2"));

        StringWriter sw = new StringWriter();
        MethodReader reader = wire.methodReader(Mocker.logging(MethodReaderTest.MRTListener.class, "subs ", sw));
        for (int i = 0; i < 4; i++) {
            assertTrue(reader.readOne());
        }
        assertFalse(reader.readOne());
        String expected = "subs top[!net.openhft.chronicle.wire.MethodReaderTest$MRT1 {\n" +
                "  field1: one,\n" +
                "  value: a\n" +
                "}\n" +
                "]\n" +
                "subs top[!net.openhft.chronicle.wire.MethodReaderTest$MRT2 {\n" +
                "  field1: one,\n" +
                "  value: a,\n" +
                "  field2: two\n" +
                "}\n" +
                "]\n" +
                "subs mid[!net.openhft.chronicle.wire.MethodReaderTest$MRT1 {\n" +
                "  field1: \"1\",\n" +
                "  value: a\n" +
                "}\n" +
                "]\n" +
                "subs mid[!net.openhft.chronicle.wire.MethodReaderTest$MRT2 {\n" +
                "  field1: \"1\",\n" +
                "  value: a,\n" +
                "  field2: \"2\"\n" +
                "}\n" +
                "]\n";
        String actual = sw.toString().replace("\r", "");
        assertEquals(expected, actual);
    }

    @Test
    public void testDefault() {
        Wire wire = new TextWire(Bytes.elasticHeapByteBuffer(256));
        HasDefault writer = wire.methodWriter(HasDefault.class);
        writer.callsMethod("hello,world,bye");
        assertEquals("method: [\n" +
                "  hello,\n" +
                "  world,\n" +
                "  bye\n" +
                "]\n" +
                "---\n", wire.toString());

    }

    interface Event {
        void event(String eventName, Object o);
    }

    public interface HasDefault {
        default void callsMethod(String args) {
            method(args.split(","));
        }

        void method(String... args);
    }
}


@@ -2317,24 +2317,25 @@
 class);%0A
+%0A
         
 writer.c
@@ -2330,192 +2330,435 @@
     
-writer.callsMethod(%22hello,world,bye%22);%0A        assertEquals(%22method: %5B%5Cn%22 +%0A                %22  hello,%5Cn%22 +%0A                %22  world,%5Cn%22 +%0A                %22  bye%5Cn%22 +%0A                %22%5D
+// MethodWriter records an invocation on the default method%0A        // callsMethod of the _proxy_, not the interface%0A        // this should work for replaying events back through a MethodReader.%0A        // Change made in Chronicle-Core https://github.com/OpenHFT/Chronicle-Core/commit/86c532d20a304c990cb2a82ebd84ffb355660fd3%0A        writer.callsMethod(%22hello,world,bye%22);%0A        assertEquals(%22callsMethod: %5C%22hello,world,bye%5C%22
 %5Cn%22 

update test to reflect new behaviour introduced in core