Skip to content

Commit 9a74ac0

Browse files
committed
Add formatter integration tests for Java 8, 11 and 17
Closes gh-293
1 parent 3076583 commit 9a74ac0

File tree

5 files changed

+237
-3
lines changed

5 files changed

+237
-3
lines changed

pom.xml

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<checkstyle.version>8.45.1</checkstyle.version>
4242
<gradle.version>3.4</gradle.version>
4343
<groovy.version>2.4.21</groovy.version>
44+
<log4j.version>2.14.1</log4j.version>
4445
<maven-core.version>3.5.0</maven-core.version>
4546
<maven-plugin-api.version>${maven-core.version}</maven-plugin-api.version>
4647
<maven-plugin-annotations.version>3.5</maven-plugin-annotations.version>
@@ -49,6 +50,7 @@
4950
<javassist.version>3.21.0-GA</javassist.version>
5051
<picocontainer.version>1.2</picocontainer.version>
5152
<system-rules.version>1.16.0</system-rules.version>
53+
<testcontainers.version>1.16.0</testcontainers.version>
5254
<trove4j.version>3.0.3</trove4j.version>
5355
<tycho.version>2.4.0</tycho.version>
5456
<tycho-extras.version>2.4.0</tycho-extras.version>
@@ -445,9 +447,26 @@
445447
<dependencyManagement>
446448
<dependencies>
447449
<dependency>
448-
<groupId>org.junit.jupiter</groupId>
449-
<artifactId>junit-jupiter</artifactId>
450-
<version>${junit.version}</version>
450+
<groupId>org.apache.logging.log4j</groupId>
451+
<artifactId>log4j-api</artifactId>
452+
<version>${log4j.version}</version>
453+
</dependency>
454+
<dependency>
455+
<groupId>org.apache.logging.log4j</groupId>
456+
<artifactId>log4j-core</artifactId>
457+
<version>${log4j.version}</version>
458+
</dependency>
459+
<dependency>
460+
<groupId>org.apache.logging.log4j</groupId>
461+
<artifactId>log4j-slf4j-impl</artifactId>
462+
<version>${log4j.version}</version>
463+
</dependency>
464+
<dependency>
465+
<groupId>org.junit</groupId>
466+
<artifactId>junit-bom</artifactId>
467+
<version>5.8.1</version>
468+
<type>pom</type>
469+
<scope>import</scope>
451470
</dependency>
452471
<dependency>
453472
<groupId>com.puppycrawl.tools</groupId>
@@ -545,6 +564,16 @@
545564
<artifactId>picocontainer</artifactId>
546565
<version>${picocontainer.version}</version>
547566
</dependency>
567+
<dependency>
568+
<groupId>org.testcontainers</groupId>
569+
<artifactId>testcontainers</artifactId>
570+
<version>${testcontainers.version}</version>
571+
</dependency>
572+
<dependency>
573+
<groupId>org.testcontainers</groupId>
574+
<artifactId>junit-jupiter</artifactId>
575+
<version>${testcontainers.version}</version>
576+
</dependency>
548577
</dependencies>
549578
</dependencyManagement>
550579
<dependencies>

spring-javaformat/spring-javaformat-formatter/pom.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,31 @@
3232
<version>${project.version}</version>
3333
<scope>provided</scope>
3434
</dependency>
35+
<!-- Test -->
36+
<dependency>
37+
<groupId>org.apache.logging.log4j</groupId>
38+
<artifactId>log4j-api</artifactId>
39+
<scope>test</scope>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.apache.logging.log4j</groupId>
43+
<artifactId>log4j-core</artifactId>
44+
<scope>test</scope>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.apache.logging.log4j</groupId>
48+
<artifactId>log4j-slf4j-impl</artifactId>
49+
<scope>test</scope>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.testcontainers</groupId>
53+
<artifactId>testcontainers</artifactId>
54+
<scope>test</scope>
55+
</dependency>
56+
<dependency>
57+
<groupId>org.testcontainers</groupId>
58+
<artifactId>junit-jupiter</artifactId>
59+
<scope>test</scope>
60+
</dependency>
3561
</dependencies>
3662
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2017-2020 the original author or authors.
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+
* https://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+
17+
package io.spring.javaformat.formatter;
18+
19+
import org.eclipse.jface.text.BadLocationException;
20+
import org.eclipse.jface.text.Document;
21+
import org.eclipse.jface.text.IDocument;
22+
import org.eclipse.text.edits.TextEdit;
23+
24+
/**
25+
* Test app used to format something in a test container.
26+
*
27+
* @author Phillip Webb
28+
*/
29+
public final class FormatterApp {
30+
31+
private FormatterApp() {
32+
}
33+
34+
public static void main(String[] args) throws Exception, BadLocationException {
35+
Formatter formatter = new Formatter();
36+
String source = "public class Test {}";
37+
TextEdit edit = formatter.format(source);
38+
IDocument document = new Document(source);
39+
edit.apply(document);
40+
String formatted = document.get();
41+
System.out.println(formatted);
42+
}
43+
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright 2017-2020 the original author or authors.
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+
* https://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+
17+
package io.spring.javaformat.formatter;
18+
19+
import java.io.File;
20+
import java.io.IOException;
21+
import java.nio.file.Files;
22+
import java.nio.file.Path;
23+
import java.time.Duration;
24+
import java.util.ArrayList;
25+
import java.util.List;
26+
import java.util.stream.Collectors;
27+
import java.util.stream.Stream;
28+
29+
import org.junit.jupiter.params.ParameterizedTest;
30+
import org.junit.jupiter.params.provider.ValueSource;
31+
import org.testcontainers.containers.output.ToStringConsumer;
32+
import org.testcontainers.containers.startupcheck.OneShotStartupCheckStrategy;
33+
import org.testcontainers.junit.jupiter.Testcontainers;
34+
import org.testcontainers.utility.MountableFile;
35+
36+
import static org.assertj.core.api.Assertions.assertThat;
37+
38+
/**
39+
* Tests to ensure the formatter can run in different JVM versions.
40+
*
41+
* @author Phillip Webb
42+
*/
43+
@Testcontainers(disabledWithoutDocker = true)
44+
public class FormatterIntegrationTests {
45+
46+
@ParameterizedTest
47+
@ValueSource(strings = { "8", "11", "17" })
48+
void formatCode(String version) throws Exception {
49+
try (JavaContainer container = new JavaContainer(version)) {
50+
ToStringConsumer output = new ToStringConsumer();
51+
container.withStartupCheckStrategy(new OneShotStartupCheckStrategy().withTimeout(Duration.ofMinutes(5)));
52+
container.withLogConsumer(output);
53+
String classpath = withCopyClasspathToContainer(container);
54+
String applicationClassName = FormatterApp.class.getName();
55+
container.withCommand("java -cp " + classpath + " " + applicationClassName);
56+
container.start();
57+
assertThat(output.toUtf8String()).isEqualTo("public class Test {\n\n}\n");
58+
}
59+
}
60+
61+
private String withCopyClasspathToContainer(JavaContainer container) throws IOException {
62+
List<String> classpath = new ArrayList<>();
63+
for (String entry : System.getProperty("java.class.path").split(File.pathSeparator)) {
64+
if (entry.contains("spring-javaformat")) {
65+
classpath.add(withCopyClasspathEntryToContainer(container, entry));
66+
}
67+
}
68+
return classpath.stream().collect(Collectors.joining(":"));
69+
}
70+
71+
private String withCopyClasspathEntryToContainer(JavaContainer container, String entry) throws IOException {
72+
if (entry.endsWith(".jar")) {
73+
return withCopyClasspathJarToContainer(container, new File(entry));
74+
}
75+
return withCopyClasspathFolderToContainer(container, new File(entry));
76+
}
77+
78+
private String withCopyClasspathJarToContainer(JavaContainer container, File jarFile) {
79+
container.withCopyFileToContainer(MountableFile.forHostPath(jarFile.toPath()), "/app/" + jarFile.getName());
80+
return "/app/" + jarFile.getName();
81+
}
82+
83+
private String withCopyClasspathFolderToContainer(JavaContainer container, File classesFolder) throws IOException {
84+
String name = classesFolder.getName();
85+
Path source = classesFolder.toPath();
86+
try (Stream<Path> stream = Files.walk(source)) {
87+
stream.forEach((child) -> {
88+
try {
89+
Path relative = source.relativize(child);
90+
if (!child.toFile().isDirectory()) {
91+
String containerPath = "/app/" + name + "/" + relative;
92+
container.withCopyFileToContainer(MountableFile.forHostPath(child), containerPath);
93+
}
94+
}
95+
catch (Exception ex) {
96+
throw new IllegalStateException(ex);
97+
}
98+
});
99+
}
100+
return "/app/" + name;
101+
}
102+
103+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2017-2020 the original author or authors.
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+
* https://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+
17+
package io.spring.javaformat.formatter;
18+
19+
import org.testcontainers.containers.GenericContainer;
20+
21+
/**
22+
* {@link GenericContainer} for Java.
23+
*
24+
* @author Phillip Webb
25+
*/
26+
public class JavaContainer extends GenericContainer<JavaContainer> {
27+
28+
public JavaContainer(String version) {
29+
super("bellsoft/liberica-openjdk-debian:" + version);
30+
}
31+
32+
}

0 commit comments

Comments
 (0)