Skip to content

Commit 607aea4

Browse files
committed
Clean-up eclipse-jdt tests.
1 parent ae5529e commit 607aea4

File tree

3 files changed

+61
-56
lines changed

3 files changed

+61
-56
lines changed

_ext/eclipse-jdt/src/test/java/com/diffplug/spotless/extra/eclipse/java/EclipseJdtFormatterStepImplTest.java

Lines changed: 48 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -15,96 +15,88 @@
1515
*/
1616
package com.diffplug.spotless.extra.eclipse.java;
1717

18-
import static com.diffplug.spotless.extra.eclipse.base.SpotlessEclipseFramework.LINE_DELIMITER;
1918
import static org.junit.jupiter.api.Assertions.*;
2019

20+
import java.io.File;
21+
import java.io.IOException;
2122
import java.util.Properties;
22-
import java.util.function.Consumer;
2323

2424
import org.eclipse.jdt.core.JavaCore;
2525
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
26+
import org.junit.jupiter.api.BeforeAll;
27+
import org.junit.jupiter.api.BeforeEach;
2628
import org.junit.jupiter.api.Test;
2729

30+
import com.diffplug.spotless.FormatterProperties;
31+
import com.diffplug.spotless.ResourceHarness;
32+
2833
/** Eclipse JDT wrapper integration tests */
29-
class EclipseJdtFormatterStepImplTest {
30-
31-
private final static String UNFORMATTED = "package com.diffplug.gradle.spotless;\n" +
32-
"public class C {\n" +
33-
" static void hello() {" +
34-
" System.out.println(\"Hello World!\");\n" +
35-
" }\n" +
36-
"}".replaceAll("\n", LINE_DELIMITER);
37-
private final static String FORMATTED = "package com.diffplug.gradle.spotless;\n" +
38-
"public class C {\n" +
39-
"\tstatic void hello() {\n" +
40-
"\t\tSystem.out.println(\"Hello World!\");\n" +
41-
"\t}\n" +
42-
"}".replaceAll("\n", LINE_DELIMITER);
43-
44-
private final static String PRE_UNFORMATTED = "/**<pre>void f(){}</pre>*/\n".replaceAll("\n", LINE_DELIMITER);
45-
private final static String PRE_FORMATTED = "/**\n * <pre>\n * void f() {\n * }\n * </pre>\n */\n".replaceAll("\n", LINE_DELIMITER);
34+
class EclipseJdtFormatterStepImplTest extends ResourceHarness {
35+
36+
private static String UNFORMATTED;
37+
private static String FORMATTED;
38+
39+
@BeforeAll
40+
static void beforeAll() throws IOException {
41+
UNFORMATTED = getTestResource("java/eclipse/JavaCodeUnformatted.test");
42+
FORMATTED = getTestResource("java/eclipse/JavaCodeFormatted.test");
43+
}
44+
45+
private Properties config;
46+
47+
@BeforeEach
48+
void beforeEach() throws IOException {
49+
File sttingsFile = createTestFile("java/eclipse/formatter.xml");
50+
config = FormatterProperties.from(sttingsFile).getProperties();
51+
}
4652

4753
private final static String ILLEGAL_CHAR = Character.toString((char) 254);
4854

4955
@Test
50-
void defaultFormat() throws Throwable {
51-
String output = format(UNFORMATTED, config -> {});
52-
assertEquals(FORMATTED,
53-
output, "Unexpected formatting with default preferences.");
56+
void nominal() throws Throwable {
57+
assertEquals(FORMATTED, format(UNFORMATTED));
5458
}
5559

56-
/**
57-
* The exception handling has changed sine about JDT 4.10.
58-
* Before that version, JDT caught very internal parser error.
59-
* The latest behavior is in line with Eclipse-Groovy.
60-
* CDT however (still) catches parser exceptions in the formatter step.
61-
* Spotless anyhow provides possibilities to change exception behavior.
62-
* Furthermore it is assumed that Spotless runs on compile-able code.
63-
*/
64-
public void invalidFormat() throws Throwable {
60+
@Test
61+
public void invalidSyntax() throws Throwable {
6562
try {
66-
String output = format(FORMATTED.replace("void hello() {", "void hello() "), config -> {});
67-
assertTrue(output.contains("void hello() " + LINE_DELIMITER), "Incomplete Java not formatted on best effort basis.");
63+
String invalidSyntax = FORMATTED.replace("{", "");
64+
assertEquals(invalidSyntax, format(invalidSyntax));
6865
} catch (IndexOutOfBoundsException e) {
6966
/*
7067
* Some JDT versions throw exception, but this changed again in later versions.
71-
* Anyhow, exceptions are acceptable, since Spotless should fromat valid Java code.
68+
* Anyhow, exceptions are acceptable, since Spotless should format valid Java code.
7269
*/
7370
}
7471
}
7572

7673
@Test
7774
void invalidCharater() throws Throwable {
78-
String output = format(FORMATTED.replace("void hello() {", "void hello()" + ILLEGAL_CHAR + " {"), config -> {});
79-
assertTrue(output.contains("void hello()" + ILLEGAL_CHAR + " {" + LINE_DELIMITER), "Invalid charater not formatted on best effort basis.");
75+
String invalidInput = UNFORMATTED + ILLEGAL_CHAR;
76+
assertEquals(invalidInput, format(invalidInput));
8077
}
8178

8279
@Test
8380
void invalidConfiguration() throws Throwable {
84-
String output = format(FORMATTED, config -> {
85-
config.setProperty(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, JavaCore.SPACE);
86-
config.setProperty(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, "noInteger");
87-
});
88-
assertEquals(FORMATTED.replace("\t", " "),
89-
output, "Invalid indentation configuration not replaced by default value (4 spaces)");
81+
config.setProperty(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, JavaCore.SPACE);
82+
config.setProperty(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, "noInteger");
83+
String defaultTabReplacement = " ";
84+
assertEquals(FORMATTED.replace("\t", defaultTabReplacement), format(FORMATTED));
9085
}
9186

9287
@Test
93-
/** Test that an internal code formatter can be created to format the Java code within HTML pre-tags. (see also Spotless issue #191) */
94-
void internalCodeFormatter() throws Throwable {
95-
String output = format(PRE_UNFORMATTED + UNFORMATTED, config -> {
96-
config.setProperty(
97-
DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_HEADER,
98-
DefaultCodeFormatterConstants.TRUE);
99-
});
100-
assertEquals(PRE_FORMATTED + FORMATTED,
101-
output, "Code within HTML <pre> tag not formatted.");
88+
void htmlPreTag() throws Throwable {
89+
config.clear();
90+
config.setProperty(
91+
DefaultCodeFormatterConstants.FORMATTER_COMMENT_FORMAT_HEADER,
92+
DefaultCodeFormatterConstants.TRUE);
93+
String formatted = getTestResource("java/eclipse/HtmlPreTagFormatted.test");
94+
String unformatted = getTestResource("java/eclipse/HtmlPreTagUnformatted.test");
95+
assertEquals(formatted, format(unformatted), "Failed to create internal code formatter. See Spotless issue #191");
10296
}
10397

104-
private static String format(final String input, final Consumer<Properties> config) throws Exception {
105-
Properties properties = new Properties();
106-
config.accept(properties);
107-
EclipseJdtFormatterStepImpl formatter = new EclipseJdtFormatterStepImpl(properties);
98+
private String format(final String input) throws Exception {
99+
EclipseJdtFormatterStepImpl formatter = new EclipseJdtFormatterStepImpl(config);
108100
return formatter.format(input);
109101
}
110102

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
public interface Java {
2+
/**
3+
* <pre>
4+
* void f() {
5+
* }
6+
* </pre>
7+
*/
8+
void f();
9+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public interface Java {
2+
/**<pre>void f(){}</pre>*/
3+
void f();
4+
}

0 commit comments

Comments
 (0)