package com.insightfullogic.honest_profiler.core.collector;

import com.insightfullogic.honest_profiler.core.parser.Method;
import com.insightfullogic.honest_profiler.core.parser.StackFrame;
import com.insightfullogic.honest_profiler.core.parser.TraceStart;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertTrue;

public class LogCollectorTest {
    private static final int LINE = 123;

    @Test
    public void logCollectorShouldCopeWithUnexpectedFrames() {
        final List<Profile> found = new ArrayList<>();
        final LogCollector collector = new LogCollector(found::add, true);

        for (int i = 0; i < 10; i++) {
            collector.handle(new Method(i, "a", "Bass", "c" + i));
        }

        assertTrue("methods don't cause profiles", found.isEmpty());

        int threadId = 0;
        collector.handle(new TraceStart(2, ++threadId));

        assertTrue("nothing to profile still", found.isEmpty());

        collector.handle(new StackFrame(LINE, 0));
        collector.handle(new StackFrame(LINE, 1));
        // ..and one unexpected frame
        collector.handle(new StackFrame(LINE, 2));

        // normal method afterwards
        collector.handle(new TraceStart(2, ++threadId));
        collector.handle(new StackFrame(LINE, 6));
        collector.handle(new StackFrame(LINE, 7));

        // and continuation
        collector.handle(new TraceStart(20, ++threadId));

        assertArrayEquals(new long[] { 2, 7 }, idOfLastMethodInEachThread(mostRecentProfile(found)));
    }

    private long[] idOfLastMethodInEachThread(Profile profile) {
        return profile.getTrees().stream().mapToLong(x -> x.getRootNode().getMethod().getMethodId()).toArray();
    }

    private Profile mostRecentProfile(List<Profile> found) {
        return found.get(found.size() - 1);
    }
}

@@ -297,20 +297,21 @@
 il.Array
-List
+Deque
 ;%0Aimport
@@ -321,20 +321,21 @@
 va.util.
-List
+Deque
 ;%0A%0Aimpor
@@ -584,20 +584,21 @@
   final 
-List
+Deque
 %3CProfile
@@ -616,20 +616,21 @@
 ew Array
-List
+Deque
 %3C%3E();%0A  
@@ -1593,31 +1593,22 @@
 ead(
-mostRecentProfile(found
+found.getLast(
 )));
@@ -1801,119 +1801,7 @@
    %7D
-%0A%0A    private Profile mostRecentProfile(List%3CProfile%3E found) %7B%0A        return found.get(found.size() - 1);%0A    %7D
 %0A%7D%0A

simplify test