• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockitousage.internal.debugging;
6 
7 import static org.assertj.core.api.Assertions.assertThat;
8 import static org.junit.Assert.assertEquals;
9 
10 import java.util.ArrayList;
11 import java.util.List;
12 
13 import org.junit.Test;
14 import org.mockito.internal.debugging.LocationImpl;
15 import org.mockito.internal.exceptions.stacktrace.StackTraceFilter;
16 import org.mockitoutil.TestBase;
17 
18 @SuppressWarnings("serial")
19 public class LocationImplTest extends TestBase {
20 
21     @Test
shouldLocationNotContainGetStackTraceMethod()22     public void shouldLocationNotContainGetStackTraceMethod() {
23         assertThat(new LocationImpl().toString())
24                 .contains("shouldLocationNotContainGetStackTraceMethod");
25     }
26 
27     @Test
shouldBeSafeInCaseForSomeReasonFilteredStackTraceIsEmpty()28     public void shouldBeSafeInCaseForSomeReasonFilteredStackTraceIsEmpty() {
29         // given
30         StackTraceFilter filterReturningEmptyArray =
31                 new StackTraceFilter() {
32                     @Override
33                     public StackTraceElement[] filter(StackTraceElement[] target, boolean keepTop) {
34                         return new StackTraceElement[0];
35                     }
36 
37                     @Override
38                     public StackTraceElement filterFirst(Throwable target, boolean isInline) {
39                         return null;
40                     }
41                 };
42 
43         // when
44         String loc = new LocationImpl(filterReturningEmptyArray).toString();
45 
46         // then
47         assertEquals("-> at <<unknown line>>", loc);
48     }
49 
50     @Test
provides_location_class()51     public void provides_location_class() {
52         // when
53         final List<String> files = new ArrayList<String>();
54         new Runnable() { // anonymous inner class adds stress to the check
55             public void run() {
56                 files.add(new LocationImpl().getSourceFile());
57             }
58         }.run();
59 
60         // then
61         assertEquals("LocationImplTest.java", files.get(0));
62     }
63 }
64