1 /*
2  * Copyright (c) 2007 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 
6 package org.mockito.internal.invocation;
7 
8 import org.assertj.core.api.Assertions;
9 import org.junit.Before;
10 import org.junit.Test;
11 import org.mockito.Mock;
12 import org.mockito.internal.verification.InOrderContextImpl;
13 import org.mockito.internal.verification.api.InOrderContext;
14 import org.mockito.invocation.Invocation;
15 import org.mockito.invocation.Location;
16 import org.mockitousage.IMethods;
17 import org.mockitoutil.TestBase;
18 
19 import java.util.Arrays;
20 import java.util.Collections;
21 import java.util.LinkedList;
22 import java.util.List;
23 
24 import static org.junit.Assert.assertNull;
25 import static org.junit.Assert.assertSame;
26 import static org.junit.Assert.assertTrue;
27 
28 
29 public class InvocationsFinderTest extends TestBase {
30 
31     private LinkedList<Invocation> invocations = new LinkedList<Invocation>();
32     private Invocation simpleMethodInvocation;
33     private Invocation simpleMethodInvocationTwo;
34     private Invocation differentMethodInvocation;
35 
36 
37     private final InOrderContext context = new InOrderContextImpl();
38 
39     @Mock private IMethods mock;
40 
41     @Before
setup()42     public void setup() throws Exception {
43         simpleMethodInvocation = new InvocationBuilder().mock(mock).simpleMethod().seq(1).toInvocation();
44         simpleMethodInvocationTwo = new InvocationBuilder().mock(mock).simpleMethod().seq(2).toInvocation();
45         differentMethodInvocation = new InvocationBuilder().mock(mock).differentMethod().seq(3).toInvocation();
46         invocations.addAll(Arrays.asList(simpleMethodInvocation, simpleMethodInvocationTwo, differentMethodInvocation));
47 
48     }
49 
50     @Test
shouldFindActualInvocations()51     public void shouldFindActualInvocations() throws Exception {
52         List<Invocation> actual = InvocationsFinder.findInvocations(invocations, new InvocationMatcher(simpleMethodInvocation));
53         Assertions.assertThat(actual).containsSequence(simpleMethodInvocation, simpleMethodInvocationTwo);
54 
55         actual = InvocationsFinder.findInvocations(invocations, new InvocationMatcher(differentMethodInvocation));
56         Assertions.assertThat(actual).containsSequence(differentMethodInvocation);
57     }
58 
59     @Test
shouldFindFirstUnverifiedInvocation()60     public void shouldFindFirstUnverifiedInvocation() throws Exception {
61         assertSame(simpleMethodInvocation, InvocationsFinder.findFirstUnverified(invocations));
62 
63         simpleMethodInvocationTwo.markVerified();
64         simpleMethodInvocation.markVerified();
65 
66         assertSame(differentMethodInvocation, InvocationsFinder.findFirstUnverified(invocations));
67 
68         differentMethodInvocation.markVerified();
69         assertNull(InvocationsFinder.findFirstUnverified(invocations));
70     }
71 
72     @Test
shouldFindFirstUnverifiedInOrder()73     public void shouldFindFirstUnverifiedInOrder() throws Exception {
74         //given
75         InOrderContextImpl context = new InOrderContextImpl();
76         assertSame(simpleMethodInvocation, InvocationsFinder.findFirstUnverifiedInOrder(context, invocations));
77 
78         //when
79         context.markVerified(simpleMethodInvocationTwo);
80         context.markVerified(simpleMethodInvocation);
81 
82         //then
83         assertSame(differentMethodInvocation, InvocationsFinder.findFirstUnverifiedInOrder(context, invocations));
84 
85         //when
86         context.markVerified(differentMethodInvocation);
87 
88         //then
89         assertNull(InvocationsFinder.findFirstUnverifiedInOrder(context, invocations));
90     }
91 
92     @Test
shouldFindFirstUnverifiedInOrderAndRespectSequenceNumber()93     public void shouldFindFirstUnverifiedInOrderAndRespectSequenceNumber() throws Exception {
94         //given
95         InOrderContextImpl context = new InOrderContextImpl();
96         assertSame(simpleMethodInvocation, InvocationsFinder.findFirstUnverifiedInOrder(context, invocations));
97 
98         //when
99         //skipping verification of first invocation, then:
100         context.markVerified(simpleMethodInvocationTwo);
101         context.markVerified(differentMethodInvocation);
102 
103         //then
104         assertSame(null, InvocationsFinder.findFirstUnverifiedInOrder(context, invocations));
105     }
106 
107     @Test
shouldFindFirstUnverifiedInvocationOnMock()108     public void shouldFindFirstUnverifiedInvocationOnMock() throws Exception {
109         assertSame(simpleMethodInvocation, InvocationsFinder.findFirstUnverified(invocations, simpleMethodInvocation.getMock()));
110         assertNull(InvocationsFinder.findFirstUnverified(invocations, "different mock"));
111     }
112 
113     @Test
shouldFindFirstSimilarInvocationByName()114     public void shouldFindFirstSimilarInvocationByName() throws Exception {
115         Invocation overloadedSimpleMethod = new InvocationBuilder().mock(mock).simpleMethod().arg("test").toInvocation();
116 
117         Invocation found = InvocationsFinder.findSimilarInvocation(invocations, new InvocationMatcher(overloadedSimpleMethod));
118         assertSame(found, simpleMethodInvocation);
119     }
120 
121     @Test
shouldFindInvocationWithTheSameMethod()122     public void shouldFindInvocationWithTheSameMethod() throws Exception {
123         Invocation overloadedDifferentMethod = new InvocationBuilder().differentMethod().arg("test").toInvocation();
124 
125         invocations.add(overloadedDifferentMethod);
126 
127         Invocation found = InvocationsFinder.findSimilarInvocation(invocations, new InvocationMatcher(overloadedDifferentMethod));
128         assertSame(found, overloadedDifferentMethod);
129     }
130 
131     @Test
shouldGetLastStackTrace()132     public void shouldGetLastStackTrace() throws Exception {
133         Location last = InvocationsFinder.getLastLocation(invocations);
134         assertSame(differentMethodInvocation.getLocation(), last);
135 
136         assertNull(InvocationsFinder.getLastLocation(Collections.<Invocation>emptyList()));
137     }
138 
139     @Test
shouldFindAllMatchingUnverifiedChunks()140     public void shouldFindAllMatchingUnverifiedChunks() throws Exception {
141         List<Invocation> allMatching = InvocationsFinder.findAllMatchingUnverifiedChunks(invocations, new InvocationMatcher(simpleMethodInvocation), context);
142         Assertions.assertThat(allMatching).containsSequence(simpleMethodInvocation, simpleMethodInvocationTwo);
143 
144         context.markVerified(simpleMethodInvocation);
145         allMatching = InvocationsFinder.findAllMatchingUnverifiedChunks(invocations, new InvocationMatcher(simpleMethodInvocation), context);
146         Assertions.assertThat(allMatching).containsSequence(simpleMethodInvocationTwo);
147 
148         context.markVerified(simpleMethodInvocationTwo);
149         allMatching = InvocationsFinder.findAllMatchingUnverifiedChunks(invocations, new InvocationMatcher(simpleMethodInvocation), context);
150         assertTrue(allMatching.isEmpty());
151     }
152 
153     @Test
shouldFindMatchingChunk()154     public void shouldFindMatchingChunk() throws Exception {
155         List<Invocation> chunk = InvocationsFinder.findMatchingChunk(invocations, new InvocationMatcher(simpleMethodInvocation), 2, context);
156         Assertions.assertThat(chunk).containsSequence(simpleMethodInvocation, simpleMethodInvocationTwo);
157     }
158 
159     @Test
shouldReturnAllChunksWhenModeIsAtLeastOnce()160     public void shouldReturnAllChunksWhenModeIsAtLeastOnce() throws Exception {
161         Invocation simpleMethodInvocationThree = new InvocationBuilder().mock(mock).toInvocation();
162         invocations.add(simpleMethodInvocationThree);
163 
164         List<Invocation> chunk = InvocationsFinder.findMatchingChunk(invocations, new InvocationMatcher(simpleMethodInvocation), 1, context);
165         Assertions.assertThat(chunk).containsSequence(simpleMethodInvocation, simpleMethodInvocationTwo, simpleMethodInvocationThree);
166     }
167 
168     @Test
shouldReturnAllChunksWhenWantedCountDoesntMatch()169     public void shouldReturnAllChunksWhenWantedCountDoesntMatch() throws Exception {
170         Invocation simpleMethodInvocationThree = new InvocationBuilder().mock(mock).toInvocation();
171         invocations.add(simpleMethodInvocationThree);
172 
173         List<Invocation> chunk = InvocationsFinder.findMatchingChunk(invocations, new InvocationMatcher(simpleMethodInvocation), 1, context);
174         Assertions.assertThat(chunk).containsSequence(simpleMethodInvocation, simpleMethodInvocationTwo, simpleMethodInvocationThree);
175     }
176 
177     @Test
shouldFindPreviousInOrder()178     public void shouldFindPreviousInOrder() throws Exception {
179         Invocation previous = InvocationsFinder.findPreviousVerifiedInOrder(invocations, context);
180         assertNull(previous);
181 
182         context.markVerified(simpleMethodInvocation);
183         context.markVerified(simpleMethodInvocationTwo);
184 
185         previous = InvocationsFinder.findPreviousVerifiedInOrder(invocations, context);
186         assertSame(simpleMethodInvocationTwo, previous);
187     }
188 
189     @Test
shouldFindAllStackTraces()190     public void shouldFindAllStackTraces() {
191         List<Location> all = InvocationsFinder.getAllLocations(invocations);
192         Assertions.assertThat(all).contains(simpleMethodInvocation.getLocation(), simpleMethodInvocationTwo.getLocation(), differentMethodInvocation.getLocation());
193     }
194 
195     @Test
shouldNotFindLocationsForEmptyInvocationsList()196     public void shouldNotFindLocationsForEmptyInvocationsList() {
197         Assertions.assertThat(InvocationsFinder.getAllLocations(Collections.<Invocation>emptyList())).isEmpty();
198     }
199 }
200