Skip to content

Commit 1369cc8

Browse files
committed
Add error-prone acceptance test
1 parent 589e02f commit 1369cc8

File tree

8 files changed

+318
-0
lines changed

8 files changed

+318
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>io.github.ascopes.jct</groupId>
9+
<artifactId>acceptance-tests</artifactId>
10+
<version>0.0.1-SNAPSHOT</version>
11+
<relativePath>../pom.xml</relativePath>
12+
</parent>
13+
14+
<artifactId>acceptance-tests-error-prone</artifactId>
15+
16+
<properties>
17+
<argLine>
18+
--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED
19+
--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED
20+
--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED
21+
--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED
22+
--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED
23+
--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED
24+
--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED
25+
--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
26+
--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED
27+
--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED
28+
</argLine>
29+
30+
<error-prone.version>2.16</error-prone.version>
31+
</properties>
32+
33+
<dependencies>
34+
<dependency>
35+
<groupId>${project.groupId}</groupId>
36+
<artifactId>java-compiler-testing</artifactId>
37+
<scope>test</scope>
38+
</dependency>
39+
40+
<dependency>
41+
<groupId>com.google.errorprone</groupId>
42+
<artifactId>error_prone_core</artifactId>
43+
<version>${error-prone.version}</version>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>org.apache.groovy</groupId>
48+
<artifactId>groovy</artifactId>
49+
<scope>test</scope>
50+
</dependency>
51+
52+
<dependency>
53+
<groupId>org.junit.jupiter</groupId>
54+
<artifactId>junit-jupiter</artifactId>
55+
<scope>test</scope>
56+
</dependency>
57+
58+
<dependency>
59+
<groupId>org.slf4j</groupId>
60+
<artifactId>slf4j-simple</artifactId>
61+
<scope>test</scope>
62+
</dependency>
63+
</dependencies>
64+
65+
<build>
66+
<plugins>
67+
<plugin>
68+
<groupId>org.codehaus.gmavenplus</groupId>
69+
<artifactId>gmavenplus-plugin</artifactId>
70+
</plugin>
71+
72+
<plugin>
73+
<groupId>org.apache.maven.plugins</groupId>
74+
<artifactId>maven-surefire-plugin</artifactId>
75+
76+
<configuration>
77+
<failIfNoTests>true</failIfNoTests>
78+
</configuration>
79+
</plugin>
80+
</plugins>
81+
</build>
82+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright (C) 2022 - 2022 Ashley Scopes
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 io.github.ascopes.jct.acceptancetests.errorprone
17+
18+
import io.github.ascopes.jct.compilers.JctCompiler
19+
import io.github.ascopes.jct.junit.JavacCompilerTest
20+
import io.github.ascopes.jct.workspaces.Workspaces
21+
import org.junit.jupiter.api.DisplayName
22+
23+
import static io.github.ascopes.jct.assertions.JctAssertions.assertThatCompilation
24+
25+
@DisplayName("Error-prone acceptance tests")
26+
class ErrorProneTest {
27+
28+
@DisplayName("Happy paths work as expected")
29+
@JavacCompilerTest
30+
void happyPathsWorkAsExpected(JctCompiler compiler) {
31+
try (def workspace = Workspaces.newWorkspace()) {
32+
// Given
33+
workspace
34+
.createSourcePathPackage()
35+
.createDirectory("org", "example")
36+
.copyContentsFrom("src", "test", "resources", "code", "nullness", "happy")
37+
38+
// When
39+
def compilation = compiler
40+
.addCompilerOptions(
41+
"-Xplugin:ErrorProne",
42+
"-XDcompilePolicy=simple",
43+
)
44+
.compile(workspace)
45+
46+
// Then
47+
assertThatCompilation(compilation)
48+
.isSuccessfulWithoutWarnings()
49+
}
50+
}
51+
52+
@DisplayName("Sad paths fail as expected")
53+
@JavacCompilerTest
54+
void sadPathsFailAsExpected(JctCompiler compiler) {
55+
try (def workspace = Workspaces.newWorkspace()) {
56+
// Given
57+
workspace
58+
.createSourcePathPackage()
59+
.createDirectory("org", "example")
60+
.copyContentsFrom("src", "test", "resources", "code", "nullness", "sad")
61+
62+
// When
63+
def compilation = compiler
64+
.addCompilerOptions(
65+
"-Xplugin:ErrorProne",
66+
"-XDcompilePolicy=simple",
67+
)
68+
.compile(workspace)
69+
70+
// Then
71+
assertThatCompilation(compilation)
72+
.isFailure()
73+
.diagnostics()
74+
.errors()
75+
.singleElement()
76+
.message()
77+
.startsWith(
78+
"[MustBeClosedChecker] This method returns a resource which must be managed "
79+
+ "carefully, not just left for garbage collection."
80+
)
81+
}
82+
}
83+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (C) 2022 - 2022 Ashley Scopes
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 org.example;
17+
18+
import com.google.errorprone.annotations.MustBeClosed;
19+
import java.io.IOException;
20+
import java.io.InputStream;
21+
import java.nio.file.Files;
22+
import java.nio.file.Path;
23+
24+
/**
25+
* This should pass error-prone because it always closes an auto-closeable resource.
26+
*/
27+
public class HappyCase {
28+
29+
/**
30+
* Do something.
31+
*
32+
* @param args arguments.
33+
* @throws IOException any exception.
34+
*/
35+
public static void main(String[] args) throws IOException {
36+
try (InputStream inputStream = inputStream()) {
37+
inputStream.read();
38+
}
39+
}
40+
41+
@MustBeClosed
42+
private static InputStream inputStream() throws IOException {
43+
Path tempFile = Files.createTempFile("foo", "bar");
44+
return Files.newInputStream(tempFile);
45+
}
46+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (C) 2022 - 2022 Ashley Scopes
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 org.example;
17+
18+
import com.google.errorprone.annotations.MustBeClosed;
19+
import java.io.IOException;
20+
import java.io.InputStream;
21+
import java.nio.file.Files;
22+
import java.nio.file.Path;
23+
24+
/**
25+
* This should fail error-prone because it never closes an auto-closeable resource.
26+
*/
27+
public class SadCase {
28+
29+
/**
30+
* Do something.
31+
*
32+
* @param args arguments.
33+
* @throws IOException any exception.
34+
*/
35+
public static void main(String[] args) throws IOException {
36+
InputStream inputStream = inputStream();
37+
inputStream.read();
38+
}
39+
40+
@MustBeClosed
41+
private static InputStream inputStream() throws IOException {
42+
Path tempFile = Files.createTempFile("foo", "bar");
43+
return Files.newInputStream(tempFile);
44+
}
45+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
junit.jupiter.execution.parallel.enabled=true
2+
junit.jupiter.execution.parallel.mode.classes.default=SAME_THREAD
3+
junit.jupiter.execution.parallel.mode.default=CONCURRENT
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (C) 2022 - 2022 Ashley Scopes
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 org.example;
17+
18+
import org.checkerframework.checker.nullness.qual.Nullable;
19+
20+
/**
21+
* This should pass checkerframework.
22+
*/
23+
public class HappyCase {
24+
25+
public static void main(String[] args) {
26+
@Nullable String foo = null;
27+
System.out.println(foo);
28+
}
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (C) 2022 - 2022 Ashley Scopes
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 org.example;
17+
18+
import org.checkerframework.checker.nullness.qual.NonNull;
19+
20+
/**
21+
* This should fail checkerframework because it assigns a null value to a non-null holder.
22+
*/
23+
public class SadCase {
24+
25+
public static void main(String[] args) {
26+
@NonNull String foo = null;
27+
System.out.println(foo);
28+
}
29+
}

acceptance-tests/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<modules>
1818
<module>acceptance-tests-checkerframework</module>
1919
<module>acceptance-tests-dagger</module>
20+
<module>acceptance-tests-error-prone</module>
2021
<module>acceptance-tests-google-auto-factory</module>
2122
<module>acceptance-tests-google-auto-service</module>
2223
<module>acceptance-tests-google-auto-value</module>

0 commit comments

Comments
 (0)