Skip to content

Commit eb0df83

Browse files
committed
StepHarness (and WithFile) now have a supportsRoundTrip method so we can start testing round trip serialization on steps.
1 parent ac2e90d commit eb0df83

File tree

4 files changed

+67
-26
lines changed

4 files changed

+67
-26
lines changed

testlib/src/main/java/com/diffplug/spotless/SerializableEqualityTester.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016 DiffPlug
2+
* Copyright 2016-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -71,7 +71,7 @@ public void areDifferentThan() {
7171
}
7272

7373
@SuppressWarnings("unchecked")
74-
private static <T extends Serializable> T reserialize(T input) {
74+
static <T extends Serializable> T reserialize(T input) {
7575
byte[] asBytes = LazyForwardingEquality.toBytes(input);
7676
ByteArrayInputStream byteInput = new ByteArrayInputStream(asBytes);
7777
try (ObjectInputStream objectInput = new ObjectInputStream(byteInput)) {

testlib/src/main/java/com/diffplug/spotless/StepHarness.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,14 @@
2121
import java.nio.charset.StandardCharsets;
2222
import java.nio.file.Paths;
2323
import java.util.Arrays;
24-
import java.util.Objects;
2524

2625
import org.assertj.core.api.AbstractStringAssert;
2726
import org.assertj.core.api.Assertions;
2827

2928
/** An api for testing a {@code FormatterStep} that doesn't depend on the File path. DO NOT ADD FILE SUPPORT TO THIS, use {@link StepHarnessWithFile} if you need that. */
30-
public class StepHarness implements AutoCloseable {
31-
private final Formatter formatter;
32-
29+
public class StepHarness extends StepHarnessBase<StepHarness> {
3330
private StepHarness(Formatter formatter) {
34-
this.formatter = Objects.requireNonNull(formatter);
31+
super(formatter);
3532
}
3633

3734
/** Creates a harness for testing steps which don't depend on the file. */
@@ -57,14 +54,14 @@ public static StepHarness forFormatter(Formatter formatter) {
5754

5855
/** Asserts that the given element is transformed as expected, and that the result is idempotent. */
5956
public StepHarness test(String before, String after) {
60-
String actual = formatter.compute(LineEnding.toUnix(before), new File(""));
57+
String actual = formatter().compute(LineEnding.toUnix(before), new File(""));
6158
assertEquals(after, actual, "Step application failed");
6259
return testUnaffected(after);
6360
}
6461

6562
/** Asserts that the given element is idempotent w.r.t the step under test. */
6663
public StepHarness testUnaffected(String idempotentElement) {
67-
String actual = formatter.compute(LineEnding.toUnix(idempotentElement), new File(""));
64+
String actual = formatter().compute(LineEnding.toUnix(idempotentElement), new File(""));
6865
assertEquals(idempotentElement, actual, "Step is not idempotent");
6966
return this;
7067
}
@@ -88,7 +85,7 @@ public AbstractStringAssert<?> testResourceExceptionMsg(String resourceBefore) {
8885

8986
public AbstractStringAssert<?> testExceptionMsg(String before) {
9087
try {
91-
formatter.compute(LineEnding.toUnix(before), Formatter.NO_FILE_SENTINEL);
88+
formatter().compute(LineEnding.toUnix(before), Formatter.NO_FILE_SENTINEL);
9289
throw new SecurityException("Expected exception");
9390
} catch (Throwable e) {
9491
if (e instanceof SecurityException) {
@@ -105,9 +102,4 @@ public AbstractStringAssert<?> testExceptionMsg(String before) {
105102
}
106103
}
107104
}
108-
109-
@Override
110-
public void close() {
111-
formatter.close();
112-
}
113105
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2023 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless;
17+
18+
import java.util.Objects;
19+
20+
import org.assertj.core.api.Assertions;
21+
22+
class StepHarnessBase<T extends StepHarnessBase<?>> implements AutoCloseable {
23+
private final Formatter formatter;
24+
private Formatter roundTripped;
25+
private boolean supportsRoundTrip = false;
26+
27+
protected StepHarnessBase(Formatter formatter) {
28+
this.formatter = Objects.requireNonNull(formatter);
29+
}
30+
31+
public T supportsRoundTrip(boolean supportsRoundTrip) {
32+
this.supportsRoundTrip = supportsRoundTrip;
33+
return (T) this;
34+
}
35+
36+
protected Formatter formatter() {
37+
if (!supportsRoundTrip) {
38+
return formatter;
39+
} else {
40+
if (roundTripped == null) {
41+
roundTripped = SerializableEqualityTester.reserialize(formatter);
42+
Assertions.assertThat(roundTripped).isEqualTo(formatter);
43+
}
44+
return roundTripped;
45+
}
46+
}
47+
48+
@Override
49+
public void close() {
50+
formatter.close();
51+
if (roundTripped != null) {
52+
roundTripped.close();
53+
}
54+
}
55+
}

testlib/src/main/java/com/diffplug/spotless/StepHarnessWithFile.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,12 @@
2626
import org.assertj.core.api.Assertions;
2727

2828
/** An api for testing a {@code FormatterStep} that depends on the File path. */
29-
public class StepHarnessWithFile implements AutoCloseable {
30-
private final Formatter formatter;
29+
public class StepHarnessWithFile extends StepHarnessBase<StepHarnessWithFile> {
3130
private final ResourceHarness harness;
3231

3332
private StepHarnessWithFile(ResourceHarness harness, Formatter formatter) {
33+
super(formatter);
3434
this.harness = Objects.requireNonNull(harness);
35-
this.formatter = Objects.requireNonNull(formatter);
3635
}
3736

3837
/** Creates a harness for testing steps which do depend on the file. */
@@ -54,14 +53,14 @@ public static StepHarnessWithFile forFormatter(ResourceHarness harness, Formatte
5453

5554
/** Asserts that the given element is transformed as expected, and that the result is idempotent. */
5655
public StepHarnessWithFile test(File file, String before, String after) {
57-
String actual = formatter.compute(LineEnding.toUnix(before), file);
56+
String actual = formatter().compute(LineEnding.toUnix(before), file);
5857
assertEquals(after, actual, "Step application failed");
5958
return testUnaffected(file, after);
6059
}
6160

6261
/** Asserts that the given element is idempotent w.r.t the step under test. */
6362
public StepHarnessWithFile testUnaffected(File file, String idempotentElement) {
64-
String actual = formatter.compute(LineEnding.toUnix(idempotentElement), file);
63+
String actual = formatter().compute(LineEnding.toUnix(idempotentElement), file);
6564
assertEquals(idempotentElement, actual, "Step is not idempotent");
6665
return this;
6766
}
@@ -96,7 +95,7 @@ public AbstractStringAssert<?> testResourceExceptionMsg(String filename, String
9695

9796
public AbstractStringAssert<?> testExceptionMsg(File file, String before) {
9897
try {
99-
formatter.compute(LineEnding.toUnix(before), file);
98+
formatter().compute(LineEnding.toUnix(before), file);
10099
throw new SecurityException("Expected exception");
101100
} catch (Throwable e) {
102101
if (e instanceof SecurityException) {
@@ -110,9 +109,4 @@ public AbstractStringAssert<?> testExceptionMsg(File file, String before) {
110109
}
111110
}
112111
}
113-
114-
@Override
115-
public void close() {
116-
formatter.close();
117-
}
118112
}

0 commit comments

Comments
 (0)