Skip to content

Commit f838d2a

Browse files
committed
Refactor the legacy EclipseResourceHarness to use StepHarness.
1 parent 6173676 commit f838d2a

File tree

2 files changed

+15
-51
lines changed

2 files changed

+15
-51
lines changed
Lines changed: 6 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 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.
@@ -21,8 +21,8 @@
2121
import java.util.Arrays;
2222

2323
import com.diffplug.spotless.FormatterStep;
24-
import com.diffplug.spotless.LineEnding;
2524
import com.diffplug.spotless.ResourceHarness;
25+
import com.diffplug.spotless.StepHarnessWithFile;
2626
import com.diffplug.spotless.extra.EclipseBasedStepBuilder;
2727

2828
/**
@@ -43,57 +43,19 @@
4343
*/
4444
public class EclipseResourceHarness extends ResourceHarness {
4545
private final EclipseBasedStepBuilder stepBuilder;
46-
private final String fileName;
47-
private final String input;
48-
private final String expected;
4946

5047
/**
5148
* Create harness to be used for several versions of the formatter step
5249
* @param builder Eclipse Formatter step builder
53-
* @param unformatted Simple unformatted input
54-
* @param formatted Expected formatted output
5550
*/
56-
public EclipseResourceHarness(EclipseBasedStepBuilder builder, String unformatted, String formatted) {
57-
this(builder, "someSourceFile", unformatted, formatted);
51+
public EclipseResourceHarness(EclipseBasedStepBuilder builder) {
52+
this.stepBuilder = builder;
5853
}
5954

60-
/**
61-
* Create harness to be used for several versions of the formatter step
62-
* @param builder Eclipse Formatter step builder
63-
* @param sourceFileName File name of the source file
64-
* @param unformatted Simple unformatted input
65-
* @param formatted Expected formatted output
66-
*/
67-
public EclipseResourceHarness(EclipseBasedStepBuilder builder, String sourceFileName, String unformatted, String formatted) {
68-
stepBuilder = builder;
69-
fileName = sourceFileName;
70-
input = unformatted;
71-
expected = formatted;
72-
}
73-
74-
/**
75-
* Assert that formatting input results in expected output
76-
* @param formatterVersion Formatter version
77-
* @param settingsFiles Formatter settings
78-
* @return Formatted string
79-
*/
80-
protected String assertFormatted(String formatterVersion, File... settingsFiles) throws Exception {
81-
String output = format(formatterVersion, settingsFiles);
82-
assertThat(output).isEqualTo(expected);
83-
return output;
84-
}
85-
86-
/**
87-
* Formatting input results and returns output
88-
* @param formatterVersion Formatter version
89-
* @param settingsFiles Formatter settings
90-
* @return Formatted string
91-
*/
92-
protected String format(String formatterVersion, File... settingsFiles) throws Exception {
93-
File inputFile = setFile(fileName).toContent(input);
55+
protected StepHarnessWithFile harnessFor(String formatterVersion, File... settingsFiles) throws Exception {
9456
stepBuilder.setVersion(formatterVersion);
9557
stepBuilder.setPreferences(Arrays.asList(settingsFiles));
9658
FormatterStep step = stepBuilder.build();
97-
return LineEnding.toUnix(step.format(input, inputFile));
59+
return StepHarnessWithFile.forStep(this, step);
9860
}
9961
}

lib-extra/src/test/java/com/diffplug/spotless/extra/wtp/EclipseWtpFormatterStepTest.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 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.
@@ -15,8 +15,6 @@
1515
*/
1616
package com.diffplug.spotless.extra.wtp;
1717

18-
import static org.assertj.core.api.Assertions.assertThat;
19-
2018
import java.io.File;
2119
import java.io.FileOutputStream;
2220
import java.io.IOException;
@@ -38,14 +36,18 @@ public class EclipseWtpFormatterStepTest {
3836
private final static Jvm.Support<String> JVM_SUPPORT = Jvm.<String> support("Oldest Version").add(8, "4.8.0");
3937

4038
private static class NestedTests extends EclipseResourceHarness {
39+
private final String unformatted, formatted;
40+
4141
public NestedTests(String unformatted, String formatted, EclipseWtpFormatterStep kind) {
42-
super(kind.createBuilder(TestProvisioner.mavenCentral()), unformatted, formatted);
42+
super(kind.createBuilder(TestProvisioner.mavenCentral()));
43+
this.unformatted = unformatted;
44+
this.formatted = formatted;
4345
}
4446

4547
@ParameterizedTest
4648
@MethodSource
4749
void formatWithVersion(String version) throws Exception {
48-
assertFormatted(version);
50+
harnessFor(version).test("someFilename", unformatted, formatted);
4951
}
5052

5153
private static Stream<String> formatWithVersion() {
@@ -67,8 +69,8 @@ void multipleConfigurations() throws Exception {
6769
config.setProperty("indentationChar", "space");
6870
config.setProperty("indentationSize", "5");
6971
});
70-
String defaultFormatted = assertFormatted(EclipseWtpFormatterStep.defaultVersion(), tabPropertyFile);
71-
assertThat(format(EclipseWtpFormatterStep.defaultVersion(), spacePropertyFile)).as("Space formatting output unexpected").isEqualTo(defaultFormatted.replace("\t", " "));
72+
harnessFor(EclipseWtpFormatterStep.defaultVersion(), tabPropertyFile).test("someFilename", unformatted, formatted);
73+
harnessFor(EclipseWtpFormatterStep.defaultVersion(), spacePropertyFile).test("someFilename", unformatted, formatted.replace("\t", " "));
7274
}
7375

7476
private File createPropertyFile(Consumer<Properties> config) throws IOException {

0 commit comments

Comments
 (0)