1
1
package com .zenika .snapshotmatcher ;
2
2
3
- import static com .zenika .snapshotmatcher .Configuration .getRootPackageName ;
4
3
import static java .util .Arrays .asList ;
5
4
6
5
import java .io .BufferedReader ;
12
11
import java .nio .file .Paths ;
13
12
import java .util .ArrayList ;
14
13
import java .util .List ;
15
- import java .util .Optional ;
16
- import java .util .Properties ;
17
14
import java .util .stream .Collectors ;
18
15
import java .util .stream .Stream ;
19
16
@@ -37,12 +34,7 @@ private SnapshotMatcher() {
37
34
38
35
@ Override
39
36
public boolean matchesSafely (T o ) {
40
- Path snapshotPath ;
41
- try {
42
- snapshotPath = getPath ();
43
- } catch (MatcherException e ) {
44
- return false ;
45
- }
37
+ Path snapshotPath = getPath ();
46
38
47
39
if (Files .exists (snapshotPath )) {
48
40
// File exists => Compare snapshot file to given object
@@ -101,14 +93,10 @@ private boolean compareSnapshot(T o, Path snapshotPath) {
101
93
102
94
@ Override
103
95
public void describeTo (Description description ) {
104
- try {
105
- description .appendText ("Object should match snapshot at " + getPath ().toString ());
106
- } catch (MatcherException e ) {
107
- e .printStackTrace ();
108
- }
96
+ description .appendText ("Object should match snapshot at " + getPath ().toString ());
109
97
}
110
98
111
- private Path getPath () throws MatcherException {
99
+ private Path getPath () {
112
100
StackTraceElement caller = getCaller ();
113
101
114
102
String callerClassName = caller .getClassName ();
@@ -117,16 +105,18 @@ private Path getPath() throws MatcherException {
117
105
return Paths .get (String .format ("src/test/resources/snapshots/%s/%s.json" , callerClassName , callerMethodName ));
118
106
}
119
107
120
- private StackTraceElement getCaller () throws MatcherException {
108
+ private StackTraceElement getCaller () {
121
109
final StackTraceElement [] stackTraceElements = Thread .currentThread ().getStackTrace ();
122
- final String rootPackageName = getRootPackageName ();
123
110
124
111
return Stream .of (stackTraceElements )
125
- .filter (stackTraceElement ->
126
- stackTraceElement .getClassName ().startsWith (rootPackageName )
127
- && !stackTraceElement .getClassName ().equals (getClass ().getName ()))
112
+ // Filter out java.lang package
113
+ .filter (stackTraceElement -> !stackTraceElement .getClassName ().startsWith (Thread .class .getPackage ().getName ()))
114
+ // Filter out org.hamcrest package
115
+ .filter (stackTraceElement -> !stackTraceElement .getClassName ().startsWith (TypeSafeMatcher .class .getPackage ().getName ()))
116
+ // Filter out current class
117
+ .filter (stackTraceElement -> !stackTraceElement .getClassName ().equals (SnapshotMatcher .class .getName ()))
128
118
.findFirst ()
129
- .orElseThrow ( MatcherException :: new );
119
+ .orElse ( null );
130
120
}
131
121
132
122
}
0 commit comments