|
15 | 15 | */ |
16 | 16 | package com.diffplug.spotless.extra.eclipse.java; |
17 | 17 |
|
18 | | -import static com.diffplug.spotless.extra.eclipse.base.SpotlessEclipseFramework.LINE_DELIMITER; |
19 | 18 | import static org.junit.jupiter.api.Assertions.*; |
20 | 19 |
|
| 20 | +import java.io.File; |
| 21 | +import java.io.IOException; |
21 | 22 | import java.util.Properties; |
22 | | -import java.util.function.Consumer; |
23 | 23 |
|
24 | 24 | import org.eclipse.jdt.core.JavaCore; |
25 | 25 | import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants; |
| 26 | +import org.junit.jupiter.api.BeforeAll; |
| 27 | +import org.junit.jupiter.api.BeforeEach; |
26 | 28 | import org.junit.jupiter.api.Test; |
27 | 29 |
|
| 30 | +import com.diffplug.spotless.FormatterProperties; |
| 31 | +import com.diffplug.spotless.ResourceHarness; |
| 32 | + |
28 | 33 | /** 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 | + } |
46 | 52 |
|
47 | 53 | private final static String ILLEGAL_CHAR = Character.toString((char) 254); |
48 | 54 |
|
49 | 55 | @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)); |
54 | 58 | } |
55 | 59 |
|
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 { |
65 | 62 | 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)); |
68 | 65 | } catch (IndexOutOfBoundsException e) { |
69 | 66 | /* |
70 | 67 | * 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. |
72 | 69 | */ |
73 | 70 | } |
74 | 71 | } |
75 | 72 |
|
76 | 73 | @Test |
77 | 74 | 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)); |
80 | 77 | } |
81 | 78 |
|
82 | 79 | @Test |
83 | 80 | 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)); |
90 | 85 | } |
91 | 86 |
|
92 | 87 | @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"); |
102 | 96 | } |
103 | 97 |
|
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); |
108 | 100 | return formatter.format(input); |
109 | 101 | } |
110 | 102 |
|
|
0 commit comments