Skip to content

Commit 907384b

Browse files
committed
Remove configuration to autodetect caller package name
1 parent 0a04270 commit 907384b

File tree

3 files changed

+11
-51
lines changed

3 files changed

+11
-51
lines changed

snapshot-matcher-example/src/test/resources/snapshotmatcher.properties

Lines changed: 0 additions & 1 deletion
This file was deleted.

snapshot-matcher/src/main/java/com/zenika/snapshotmatcher/Configuration.java

Lines changed: 0 additions & 29 deletions
This file was deleted.

snapshot-matcher/src/main/java/com/zenika/snapshotmatcher/SnapshotMatcher.java

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.zenika.snapshotmatcher;
22

3-
import static com.zenika.snapshotmatcher.Configuration.getRootPackageName;
43
import static java.util.Arrays.asList;
54

65
import java.io.BufferedReader;
@@ -12,8 +11,6 @@
1211
import java.nio.file.Paths;
1312
import java.util.ArrayList;
1413
import java.util.List;
15-
import java.util.Optional;
16-
import java.util.Properties;
1714
import java.util.stream.Collectors;
1815
import java.util.stream.Stream;
1916

@@ -37,12 +34,7 @@ private SnapshotMatcher() {
3734

3835
@Override
3936
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();
4638

4739
if (Files.exists(snapshotPath)) {
4840
// File exists => Compare snapshot file to given object
@@ -101,14 +93,10 @@ private boolean compareSnapshot(T o, Path snapshotPath) {
10193

10294
@Override
10395
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());
10997
}
11098

111-
private Path getPath() throws MatcherException {
99+
private Path getPath() {
112100
StackTraceElement caller = getCaller();
113101

114102
String callerClassName = caller.getClassName();
@@ -117,16 +105,18 @@ private Path getPath() throws MatcherException {
117105
return Paths.get(String.format("src/test/resources/snapshots/%s/%s.json", callerClassName, callerMethodName));
118106
}
119107

120-
private StackTraceElement getCaller() throws MatcherException {
108+
private StackTraceElement getCaller() {
121109
final StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
122-
final String rootPackageName = getRootPackageName();
123110

124111
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()))
128118
.findFirst()
129-
.orElseThrow(MatcherException::new);
119+
.orElse(null);
130120
}
131121

132122
}

0 commit comments

Comments
 (0)