/*
 * Copyright 2015-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.glowroot.agent.model;

import com.google.common.base.Ticker;
import org.junit.Test;

import org.glowroot.agent.plugin.api.MessageSupplier;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

public class TraceEntryComponentTest {

    @Test
    public void testTooManyPops() {
        // given
        ThreadContextImpl threadContext = mock(ThreadContextImpl.class);
        MessageSupplier messageSupplier = mock(MessageSupplier.class);
        TimerImpl timer = mock(TimerImpl.class);
        Ticker ticker = mock(Ticker.class);
        TraceEntryComponent traceEntryComponent =
                new TraceEntryComponent(threadContext, messageSupplier, timer, 0, ticker);
        // when
        traceEntryComponent.popEntry(traceEntryComponent.getRootEntry(), 0);
        traceEntryComponent.popEntry(traceEntryComponent.getRootEntry(), 0);
        // then ok
    }

    @Test
    public void testMissedPop() {
        // given
        ThreadContextImpl threadContext = mock(ThreadContextImpl.class);
        MessageSupplier messageSupplier1 = mock(MessageSupplier.class);
        MessageSupplier messageSupplier2 = mock(MessageSupplier.class);
        TimerImpl timer1 = mock(TimerImpl.class);
        TimerImpl timer2 = mock(TimerImpl.class);
        Ticker ticker = mock(Ticker.class);
        TraceEntryComponent traceEntryComponent =
                new TraceEntryComponent(threadContext, messageSupplier1, timer1, 0, ticker);
        // when
        traceEntryComponent.pushEntry(0, messageSupplier2, timer2, null, null, 0);
        traceEntryComponent.popEntry(traceEntryComponent.getRootEntry(), 0);
        // then
        assertThat(traceEntryComponent.isCompleted()).isTrue();
    }

    @Test
    public void testPopNonExisting() {
        // given
        ThreadContextImpl threadContext = mock(ThreadContextImpl.class);
        MessageSupplier messageSupplier1 = mock(MessageSupplier.class);
        MessageSupplier messageSupplier2 = mock(MessageSupplier.class);
        TimerImpl timer1 = mock(TimerImpl.class);
        TimerImpl timer2 = mock(TimerImpl.class);
        Ticker ticker = mock(Ticker.class);
        TraceEntryComponent traceEntryComponent =
                new TraceEntryComponent(threadContext, messageSupplier1, timer1, 0, ticker);
        // when
        traceEntryComponent.pushEntry(0, messageSupplier2, timer2, null, null, 0);
        traceEntryComponent.popEntry(mock(TraceEntryImpl.class), 0);
        // then
        assertThat(traceEntryComponent.isCompleted()).isFalse();
    }
}

@@ -706,16 +706,62 @@
 .Test;%0A%0A
+import org.glowroot.agent.plugin.api.Message;%0A
 import o
@@ -806,16 +806,16 @@
 pplier;%0A
-
 %0Aimport 
@@ -904,16 +904,56 @@
 to.mock;
+%0Aimport static org.mockito.Mockito.when;
 %0A%0Apublic
@@ -1185,32 +1185,101 @@
 upplier.class);%0A
+        when(messageSupplier.get()).thenReturn(Message.from(%22abc%22));%0A
         TimerImp
@@ -1962,32 +1962,172 @@
 upplier.class);%0A
+        when(messageSupplier1.get()).thenReturn(Message.from(%22abc%22));%0A        when(messageSupplier2.get()).thenReturn(Message.from(%22xyz%22));%0A
         TimerImp
@@ -2863,32 +2863,32 @@
 upplier.class);%0A
-
         MessageS
@@ -2935,32 +2935,172 @@
 upplier.class);%0A
+        when(messageSupplier1.get()).thenReturn(Message.from(%22abc%22));%0A        when(messageSupplier2.get()).thenReturn(Message.from(%22xyz%22));%0A
         TimerImp

Clean up test