Skip to content

Commit 4de7788

Browse files
committed
Add spotless plugin and reformat all code
1 parent 90c1f9a commit 4de7788

16 files changed

+936
-840
lines changed

pom.xml

Lines changed: 214 additions & 188 deletions
Large diffs are not rendered by default.

src/main/java/io/github/jsonSnapshot/DefaultConfig.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@
33
import lombok.Getter;
44

55
public class DefaultConfig implements SnapshotConfig {
6-
@Getter
7-
private String filePath = "src/test/java/";
6+
@Getter private String filePath = "src/test/java/";
87
}
Lines changed: 67 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,93 @@
11
package io.github.jsonSnapshot;
22

3-
import org.assertj.core.util.diff.DiffUtils;
4-
import org.assertj.core.util.diff.Patch;
5-
63
import java.lang.reflect.Method;
74
import java.util.Arrays;
85
import java.util.Collection;
96
import java.util.Set;
107
import java.util.function.Function;
118

9+
import org.assertj.core.util.diff.DiffUtils;
10+
import org.assertj.core.util.diff.Patch;
11+
1212
public class Snapshot {
1313

14-
private SnapshotFile snapshotFile;
14+
private SnapshotFile snapshotFile;
1515

16-
private Class clazz;
16+
private Class clazz;
1717

18-
private Method method;
18+
private Method method;
1919

20-
private Function<Object, String> jsonFunction;
20+
private Function<Object, String> jsonFunction;
2121

22-
private Object[] current;
22+
private Object[] current;
2323

24-
Snapshot(SnapshotFile snapshotFile, Class clazz, Method method, Function<Object, String> jsonFunction, Object... current) {
25-
this.current = current;
26-
this.snapshotFile = snapshotFile;
27-
this.clazz = clazz;
28-
this.method = method;
29-
this.jsonFunction = jsonFunction;
30-
}
24+
Snapshot(
25+
SnapshotFile snapshotFile,
26+
Class clazz,
27+
Method method,
28+
Function<Object, String> jsonFunction,
29+
Object... current) {
30+
this.current = current;
31+
this.snapshotFile = snapshotFile;
32+
this.clazz = clazz;
33+
this.method = method;
34+
this.jsonFunction = jsonFunction;
35+
}
3136

32-
public void toMatchSnapshot() {
37+
public void toMatchSnapshot() {
3338

34-
Set<String> rawSnapshots = snapshotFile.getRawSnapshots();
39+
Set<String> rawSnapshots = snapshotFile.getRawSnapshots();
3540

36-
String rawSnapshot = getRawSnapshot(rawSnapshots);
41+
String rawSnapshot = getRawSnapshot(rawSnapshots);
3742

38-
String currentObject = takeSnapshot();
43+
String currentObject = takeSnapshot();
3944

40-
// Match Snapshot
41-
if (rawSnapshot != null) {
42-
if (!rawSnapshot.trim().equals(currentObject.trim())) {
43-
throw generateDiffError(rawSnapshot, currentObject);
44-
}
45-
}
46-
// Create New Snapshot
47-
else {
48-
snapshotFile.push(currentObject);
49-
}
45+
// Match Snapshot
46+
if (rawSnapshot != null) {
47+
if (!rawSnapshot.trim().equals(currentObject.trim())) {
48+
throw generateDiffError(rawSnapshot, currentObject);
49+
}
5050
}
51-
52-
private SnapshotMatchException generateDiffError(String rawSnapshot, String currentObject) {
53-
//compute the patch: this is the diffutils part
54-
Patch<String> patch =
55-
DiffUtils.diff(
56-
Arrays.asList(rawSnapshot.trim().split("\n")),
57-
Arrays.asList(currentObject.trim().split("\n")));
58-
String error = "Error on: \n" + currentObject.trim() + "\n\n" + patch.getDeltas().stream().map(delta -> delta.toString() + "\n").reduce(String::concat).get();
59-
return new SnapshotMatchException(error);
51+
// Create New Snapshot
52+
else {
53+
snapshotFile.push(currentObject);
6054
}
61-
62-
private String getRawSnapshot(Collection<String> rawSnapshots) {
63-
for (String rawSnapshot : rawSnapshots) {
64-
65-
if (rawSnapshot.contains(getSnapshotName())) {
66-
return rawSnapshot;
67-
}
68-
}
69-
return null;
55+
}
56+
57+
private SnapshotMatchException generateDiffError(String rawSnapshot, String currentObject) {
58+
// compute the patch: this is the diffutils part
59+
Patch<String> patch =
60+
DiffUtils.diff(
61+
Arrays.asList(rawSnapshot.trim().split("\n")),
62+
Arrays.asList(currentObject.trim().split("\n")));
63+
String error =
64+
"Error on: \n"
65+
+ currentObject.trim()
66+
+ "\n\n"
67+
+ patch
68+
.getDeltas()
69+
.stream()
70+
.map(delta -> delta.toString() + "\n")
71+
.reduce(String::concat)
72+
.get();
73+
return new SnapshotMatchException(error);
74+
}
75+
76+
private String getRawSnapshot(Collection<String> rawSnapshots) {
77+
for (String rawSnapshot : rawSnapshots) {
78+
79+
if (rawSnapshot.contains(getSnapshotName())) {
80+
return rawSnapshot;
81+
}
7082
}
83+
return null;
84+
}
7185

72-
private String takeSnapshot() {
73-
return getSnapshotName() + jsonFunction.apply(current);
74-
}
86+
private String takeSnapshot() {
87+
return getSnapshotName() + jsonFunction.apply(current);
88+
}
7589

76-
public String getSnapshotName() {
77-
return clazz.getName() + "." + method.getName() + "=";
78-
}
90+
public String getSnapshotName() {
91+
return clazz.getName() + "." + method.getName() + "=";
92+
}
7993
}

src/main/java/io/github/jsonSnapshot/SnapshotCaptor.java

Lines changed: 77 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -5,100 +5,99 @@
55

66
public class SnapshotCaptor {
77

8-
private Class<?> parameterClass;
8+
private Class<?> parameterClass;
99

10-
private Class<?> argumentClass;
10+
private Class<?> argumentClass;
1111

12-
private String[] ignore;
12+
private String[] ignore;
1313

14-
public SnapshotCaptor(Class<?> parameterClass, String... ignore) {
15-
this.parameterClass = parameterClass;
16-
this.argumentClass = parameterClass;
17-
this.ignore = ignore;
18-
}
14+
public SnapshotCaptor(Class<?> parameterClass, String... ignore) {
15+
this.parameterClass = parameterClass;
16+
this.argumentClass = parameterClass;
17+
this.ignore = ignore;
18+
}
1919

20-
public SnapshotCaptor(Class<?> parameterClass, Class<?> argumentClass, String... ignore) {
21-
this.parameterClass = parameterClass;
22-
this.argumentClass = argumentClass;
23-
this.ignore = ignore;
24-
}
20+
public SnapshotCaptor(Class<?> parameterClass, Class<?> argumentClass, String... ignore) {
21+
this.parameterClass = parameterClass;
22+
this.argumentClass = argumentClass;
23+
this.ignore = ignore;
24+
}
2525

26-
public Class<?> getParameterClass() {
27-
return parameterClass;
28-
}
26+
public Class<?> getParameterClass() {
27+
return parameterClass;
28+
}
2929

30-
public Object removeIgnored(Object value) {
31-
Object newValue = value;
32-
if (ignore != null && ignore.length > 0) {
33-
newValue = shallowCopy(value);
34-
for (String each : ignore) {
35-
try {
36-
Field field = this.argumentClass.getDeclaredField(each);
37-
field.setAccessible(true);
38-
if (field.getType().isPrimitive()) {
39-
field.setByte(newValue, Integer.valueOf(0).byteValue());
40-
}
41-
else {
42-
field.set(newValue, null);
43-
}
44-
} catch (IllegalAccessException | NoSuchFieldException e) {
45-
throw new SnapshotMatchException("Invalid Ignore value " + each, e.getCause());
46-
}
47-
}
30+
public Object removeIgnored(Object value) {
31+
Object newValue = value;
32+
if (ignore != null && ignore.length > 0) {
33+
newValue = shallowCopy(value);
34+
for (String each : ignore) {
35+
try {
36+
Field field = this.argumentClass.getDeclaredField(each);
37+
field.setAccessible(true);
38+
if (field.getType().isPrimitive()) {
39+
field.setByte(newValue, Integer.valueOf(0).byteValue());
40+
} else {
41+
field.set(newValue, null);
42+
}
43+
} catch (IllegalAccessException | NoSuchFieldException e) {
44+
throw new SnapshotMatchException("Invalid Ignore value " + each, e.getCause());
4845
}
49-
return newValue;
46+
}
5047
}
48+
return newValue;
49+
}
5150

52-
private Object shallowCopy(Object value) {
51+
private Object shallowCopy(Object value) {
52+
try {
53+
Object newValue = constructCopy(this.argumentClass);
54+
55+
Field[] fields = this.argumentClass.getDeclaredFields();
56+
57+
for (Field field : fields) {
58+
field.setAccessible(true);
5359
try {
54-
Object newValue = constructCopy(this.argumentClass);
55-
56-
Field[] fields = this.argumentClass.getDeclaredFields();
57-
58-
for (Field field: fields) {
59-
field.setAccessible(true);
60-
try {
61-
field.set(newValue, field.get(value));
62-
}
63-
catch(Exception e) {
64-
//ignore
65-
}
66-
}
67-
return newValue;
68-
}
69-
catch (Exception e) {
70-
throw new SnapshotMatchException("Class "+ this.argumentClass.getSimpleName() + " must have a default empty constructor!");
60+
field.set(newValue, field.get(value));
61+
} catch (Exception e) {
62+
// ignore
7163
}
64+
}
65+
return newValue;
66+
} catch (Exception e) {
67+
throw new SnapshotMatchException(
68+
"Class "
69+
+ this.argumentClass.getSimpleName()
70+
+ " must have a default empty constructor!");
7271
}
72+
}
7373

74-
private Object constructCopy(Class<?> argumentClass)
75-
throws InstantiationException, IllegalAccessException, java.lang.reflect.InvocationTargetException, NoSuchMethodException {
74+
private Object constructCopy(Class<?> argumentClass)
75+
throws InstantiationException, IllegalAccessException,
76+
java.lang.reflect.InvocationTargetException, NoSuchMethodException {
7677

77-
try {
78-
return argumentClass.getDeclaredConstructor().newInstance();
79-
}
80-
catch (Exception e) {
81-
// Ignore - should log
82-
}
78+
try {
79+
return argumentClass.getDeclaredConstructor().newInstance();
80+
} catch (Exception e) {
81+
// Ignore - should log
82+
}
8383

84-
Constructor[] constructors = argumentClass.getDeclaredConstructors();
84+
Constructor[] constructors = argumentClass.getDeclaredConstructors();
8585

86-
if (constructors.length == 0) {
87-
return argumentClass.getDeclaredConstructor().newInstance();
88-
}
86+
if (constructors.length == 0) {
87+
return argumentClass.getDeclaredConstructor().newInstance();
88+
}
8989

90-
int i = 0;
91-
Class[] types = constructors[i].getParameterTypes();
92-
Object[] paramValues = new Object[types.length];
93-
94-
for (int j = 0; j < types.length; j++) {
95-
if (types[j].isPrimitive()) {
96-
paramValues[j] = Integer.valueOf(0).byteValue();
97-
}
98-
else {
99-
paramValues[j] = constructCopy(types[j]);
100-
}
101-
}
102-
return constructors[i].newInstance(paramValues);
90+
int i = 0;
91+
Class[] types = constructors[i].getParameterTypes();
92+
Object[] paramValues = new Object[types.length];
93+
94+
for (int j = 0; j < types.length; j++) {
95+
if (types[j].isPrimitive()) {
96+
paramValues[j] = Integer.valueOf(0).byteValue();
97+
} else {
98+
paramValues[j] = constructCopy(types[j]);
99+
}
103100
}
101+
return constructors[i].newInstance(paramValues);
102+
}
104103
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package io.github.jsonSnapshot;
22

33
public interface SnapshotConfig {
4-
String getFilePath();
4+
String getFilePath();
55
}

0 commit comments

Comments
 (0)