Skip to content

Commit af3c9ca

Browse files
author
Botond Somogyi
committed
#none: spotlessJavaCheck format
1 parent 97abfe6 commit af3c9ca

10 files changed

Lines changed: 179 additions & 176 deletions

File tree

‎java-snapshot-testing-core/src/main/java/au/com/origin/snapshots/SnapshotFile.java‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.io.InputStreamReader;
99
import java.nio.charset.StandardCharsets;
1010
import java.nio.file.Files;
11+
import java.nio.file.NoSuchFileException;
1112
import java.nio.file.Path;
1213
import java.nio.file.Paths;
1314
import java.nio.file.StandardOpenOption;
@@ -155,13 +156,17 @@ public void cleanup() {
155156

156157
@SneakyThrows
157158
private boolean snapshotsAreTheSame() {
158-
Path path = Paths.get(this.getDebugFilename());
159-
if (Files.exists(path)) {
159+
Path debugPath = Paths.get(this.getDebugFilename());
160+
if (Files.exists(debugPath)) {
160161
List<String> snapshotFileContent =
161162
Files.readAllLines(Paths.get(this.fileName), StandardCharsets.UTF_8);
162-
List<String> debugSnapshotFileContent =
163-
Files.readAllLines(Paths.get(this.getDebugFilename()), StandardCharsets.UTF_8);
164-
return Objects.equals(snapshotFileContent, debugSnapshotFileContent);
163+
try {
164+
List<String> debugSnapshotFileContent =
165+
Files.readAllLines(debugPath, StandardCharsets.UTF_8);
166+
return Objects.equals(snapshotFileContent, debugSnapshotFileContent);
167+
} catch (NoSuchFileException e) {
168+
return false;
169+
}
165170
}
166171

167172
return false;

‎java-snapshot-testing-core/src/test/java/au/com/origin/snapshots/SnapshotCaptor.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,10 @@ private Object shallowCopy(Object value) {
7373
}
7474

7575
private Object constructCopy(Class<?> argumentClass)
76-
throws InstantiationException, IllegalAccessException,
77-
java.lang.reflect.InvocationTargetException, NoSuchMethodException {
76+
throws InstantiationException,
77+
IllegalAccessException,
78+
java.lang.reflect.InvocationTargetException,
79+
NoSuchMethodException {
7880

7981
try {
8082
return argumentClass.getDeclaredConstructor().newInstance();

‎java-snapshot-testing-spock/src/main/groovy/au/com/origin/snapshots/spock/EnableSnapshots.groovy‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package au.com.origin.snapshots.spock
22

3-
import org.spockframework.runtime.extension.ExtensionAnnotation
4-
53
import java.lang.annotation.ElementType
64
import java.lang.annotation.Retention
75
import java.lang.annotation.RetentionPolicy
86
import java.lang.annotation.Target
7+
import org.spockframework.runtime.extension.ExtensionAnnotation
98

109
@Retention(RetentionPolicy.RUNTIME)
1110
@Target([ElementType.TYPE, ElementType.METHOD])

‎java-snapshot-testing-spock/src/main/groovy/au/com/origin/snapshots/spock/SnapshotExtension.groovy‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ import org.spockframework.runtime.model.SpecInfo
99

1010
class SnapshotExtension extends AbstractAnnotationDrivenExtension<EnableSnapshots> implements SnapshotConfigInjector {
1111

12-
SnapshotVerifier snapshotVerifier;
12+
SnapshotVerifier snapshotVerifier;
1313

14-
void visitSpecAnnotation(EnableSnapshots annotation, SpecInfo spec) {
15-
this.snapshotVerifier = new SnapshotVerifier(getSnapshotConfig(), spec.reflection, false)
16-
}
14+
void visitSpecAnnotation(EnableSnapshots annotation, SpecInfo spec) {
15+
this.snapshotVerifier = new SnapshotVerifier(getSnapshotConfig(), spec.reflection, false)
16+
}
1717

18-
void visitSpec(SpecInfo spec) {
19-
def snapshotMethodInterceptor = new SnapshotMethodInterceptor(snapshotVerifier)
20-
spec.allFeatures.featureMethod*.addInterceptor(snapshotMethodInterceptor)
21-
spec.addCleanupSpecInterceptor(snapshotMethodInterceptor)
22-
}
18+
void visitSpec(SpecInfo spec) {
19+
def snapshotMethodInterceptor = new SnapshotMethodInterceptor(snapshotVerifier)
20+
spec.allFeatures.featureMethod*.addInterceptor(snapshotMethodInterceptor)
21+
spec.addCleanupSpecInterceptor(snapshotMethodInterceptor)
22+
}
2323

24-
@Override
25-
SnapshotConfig getSnapshotConfig() {
26-
return new PropertyResolvingSnapshotConfig()
27-
}
24+
@Override
25+
SnapshotConfig getSnapshotConfig() {
26+
return new PropertyResolvingSnapshotConfig()
27+
}
2828
}
Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,57 @@
11
package au.com.origin.snapshots.spock
22

33
import au.com.origin.snapshots.Expect
4-
import au.com.origin.snapshots.utils.ReflectionUtils
54
import au.com.origin.snapshots.SnapshotVerifier
65
import au.com.origin.snapshots.logging.LoggingHelper
6+
import au.com.origin.snapshots.utils.ReflectionUtils
7+
import java.lang.reflect.Method
78
import org.slf4j.LoggerFactory
89
import org.spockframework.runtime.extension.AbstractMethodInterceptor
910
import org.spockframework.runtime.extension.IMethodInvocation
1011

11-
import java.lang.reflect.Method
12-
1312
// Based on this issue: https://github.com/spockframework/spock/issues/652
1413
class SnapshotMethodInterceptor extends AbstractMethodInterceptor {
15-
private log = LoggerFactory.getLogger( SnapshotMethodInterceptor.class )
16-
private final SnapshotVerifier snapshotVerifier;
17-
18-
SnapshotMethodInterceptor(SnapshotVerifier snapshotVerifier) {
19-
this.snapshotVerifier = snapshotVerifier
20-
}
21-
22-
@Override
23-
void interceptFeatureMethod(IMethodInvocation invocation) throws Throwable {
24-
updateInstanceVariable(invocation.instance, invocation.feature.featureMethod.reflection)
25-
26-
def parameterCount = invocation.method.reflection.parameterCount
27-
if (parameterCount > invocation.arguments.length) {
28-
def newArguments = new Object[parameterCount]
29-
System.arraycopy invocation.arguments, 0, newArguments, 0, invocation.arguments.length
30-
invocation.arguments = newArguments
31-
}
32-
invocation.method.reflection.parameterTypes.eachWithIndex { type, i ->
33-
if (Expect.class == type) {
34-
LoggingHelper.deprecatedV5(log, "Injecting 'Expect' via method a argument is no longer recommended. Consider using instance variable injection instead.")
35-
invocation.arguments[i] = new Expect(snapshotVerifier, invocation.feature.featureMethod.reflection)
36-
}
37-
}
38-
invocation.proceed()
39-
}
40-
41-
private void updateInstanceVariable(Object testInstance, Method testMethod) {
42-
ReflectionUtils.findFieldByPredicate(testInstance.class, { field -> field.getType() == Expect.class })
43-
.ifPresent({ field ->
44-
Expect expect = Expect.of(snapshotVerifier, testMethod);
45-
ReflectionUtils.makeAccessible(field);
46-
try {
47-
field.set(testInstance, expect);
48-
} catch (IllegalAccessException e) {
49-
throw new RuntimeException(e);
50-
}
51-
});
52-
}
53-
54-
@Override
55-
void interceptCleanupSpecMethod(IMethodInvocation invocation) throws Throwable {
56-
this.snapshotVerifier.validateSnapshots();
57-
}
14+
private log = LoggerFactory.getLogger( SnapshotMethodInterceptor.class )
15+
private final SnapshotVerifier snapshotVerifier;
16+
17+
SnapshotMethodInterceptor(SnapshotVerifier snapshotVerifier) {
18+
this.snapshotVerifier = snapshotVerifier
19+
}
20+
21+
@Override
22+
void interceptFeatureMethod(IMethodInvocation invocation) throws Throwable {
23+
updateInstanceVariable(invocation.instance, invocation.feature.featureMethod.reflection)
24+
25+
def parameterCount = invocation.method.reflection.parameterCount
26+
if (parameterCount > invocation.arguments.length) {
27+
def newArguments = new Object[parameterCount]
28+
System.arraycopy invocation.arguments, 0, newArguments, 0, invocation.arguments.length
29+
invocation.arguments = newArguments
30+
}
31+
invocation.method.reflection.parameterTypes.eachWithIndex { type, i ->
32+
if (Expect.class == type) {
33+
LoggingHelper.deprecatedV5(log, "Injecting 'Expect' via method a argument is no longer recommended. Consider using instance variable injection instead.")
34+
invocation.arguments[i] = new Expect(snapshotVerifier, invocation.feature.featureMethod.reflection)
35+
}
36+
}
37+
invocation.proceed()
38+
}
39+
40+
private void updateInstanceVariable(Object testInstance, Method testMethod) {
41+
ReflectionUtils.findFieldByPredicate(testInstance.class, { field -> field.getType() == Expect.class })
42+
.ifPresent({ field ->
43+
Expect expect = Expect.of(snapshotVerifier, testMethod);
44+
ReflectionUtils.makeAccessible(field);
45+
try {
46+
field.set(testInstance, expect);
47+
} catch (IllegalAccessException e) {
48+
throw new RuntimeException(e);
49+
}
50+
});
51+
}
52+
53+
@Override
54+
void interceptCleanupSpecMethod(IMethodInvocation invocation) throws Throwable {
55+
this.snapshotVerifier.validateSnapshots();
56+
}
5857
}

‎java-snapshot-testing-spock/src/test/groovy/au/com/origin/snapshots/SpecificationBase.groovy‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ import spock.lang.Specification
66

77
@RunWith(Sputnik.class)
88
class SpecificationBase extends Specification {
9-
Expect expect;
9+
Expect expect;
1010
}

‎java-snapshot-testing-spock/src/test/groovy/au/com/origin/snapshots/SpockExtensionUsedSpec.groovy‎

Lines changed: 67 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -8,72 +8,71 @@ import spock.lang.Unroll
88
@EnableSnapshots
99
class SpockExtensionUsedSpec extends Specification {
1010

11-
Expect expect
12-
13-
@SnapshotName("Should use extension")
14-
def "Should use extension"(Expect expect) {
15-
when:
16-
expect.toMatchSnapshot("Hello World")
17-
18-
then:
19-
true
20-
}
21-
22-
@SnapshotName("Should use extension again")
23-
def "Should use extension again"(Expect expect) {
24-
when:
25-
expect.toMatchSnapshot("Hello World")
26-
27-
then:
28-
true
29-
}
30-
31-
@SnapshotName("Should use extension via instance variable")
32-
def "Should use extension via instance variable"() {
33-
when:
34-
expect.toMatchSnapshot("Hello World")
35-
36-
then:
37-
true
38-
}
39-
40-
@SnapshotName("DataTable example 1")
41-
@Unroll
42-
def 'DataTable example 1: #letter'(def letter) {
43-
given: 'I use an @Unroll function'
44-
String result = letter.toUpperCase()
45-
46-
when: 'I snapshot the letter'
47-
expect.scenario("letter $letter").toMatchSnapshot(result)
48-
49-
then:
50-
true
51-
52-
where:
53-
[letter] << [['A'],['B'],['C']]
54-
}
55-
56-
57-
@SnapshotName("DataTable example 2")
58-
def 'DataTable example 2: #scenario to uppercase'() {
59-
when: 'I convert to uppercase'
60-
String result = value.toUpperCase();
61-
then: 'Should convert letters to uppercase'
62-
// Check you snapshot against your output using a unique scenario
63-
expect.scenario(scenario).toMatchSnapshot(result)
64-
where:
65-
scenario | value
66-
'letter' | 'a'
67-
'number' | '1'
68-
}
69-
70-
@SnapshotName("Can run a non snapshot test")
71-
def "Can run a non snapshot test"() {
72-
when:
73-
def isTrue = true
74-
75-
then:
76-
isTrue
77-
}
78-
11+
Expect expect
12+
13+
@SnapshotName("Should use extension")
14+
def "Should use extension"(Expect expect) {
15+
when:
16+
expect.toMatchSnapshot("Hello World")
17+
18+
then:
19+
true
20+
}
21+
22+
@SnapshotName("Should use extension again")
23+
def "Should use extension again"(Expect expect) {
24+
when:
25+
expect.toMatchSnapshot("Hello World")
26+
27+
then:
28+
true
29+
}
30+
31+
@SnapshotName("Should use extension via instance variable")
32+
def "Should use extension via instance variable"() {
33+
when:
34+
expect.toMatchSnapshot("Hello World")
35+
36+
then:
37+
true
38+
}
39+
40+
@SnapshotName("DataTable example 1")
41+
@Unroll
42+
def 'DataTable example 1: #letter'(def letter) {
43+
given: 'I use an @Unroll function'
44+
String result = letter.toUpperCase()
45+
46+
when: 'I snapshot the letter'
47+
expect.scenario("letter $letter").toMatchSnapshot(result)
48+
49+
then:
50+
true
51+
52+
where:
53+
[letter] << [['A'], ['B'], ['C']]
54+
}
55+
56+
57+
@SnapshotName("DataTable example 2")
58+
def 'DataTable example 2: #scenario to uppercase'() {
59+
when: 'I convert to uppercase'
60+
String result = value.toUpperCase();
61+
then: 'Should convert letters to uppercase'
62+
// Check you snapshot against your output using a unique scenario
63+
expect.scenario(scenario).toMatchSnapshot(result)
64+
where:
65+
scenario | value
66+
'letter' | 'a'
67+
'number' | '1'
68+
}
69+
70+
@SnapshotName("Can run a non snapshot test")
71+
def "Can run a non snapshot test"() {
72+
when:
73+
def isTrue = true
74+
75+
then:
76+
isTrue
77+
}
7978
}

‎java-snapshot-testing-spock/src/test/groovy/au/com/origin/snapshots/TestBaseSpec.groovy‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ import au.com.origin.snapshots.spock.EnableSnapshots
66
@EnableSnapshots
77
class TestBaseSpec extends SpecificationBase {
88

9-
@SnapshotName("Should use extension")
10-
def "Should use extension"() {
11-
when:
12-
expect.toMatchSnapshot("Hello World")
13-
14-
then:
15-
true
16-
}
9+
@SnapshotName("Should use extension")
10+
def "Should use extension"() {
11+
when:
12+
expect.toMatchSnapshot("Hello World")
1713

14+
then:
15+
true
16+
}
1817
}

0 commit comments

Comments
 (0)