Skip to content

Commit 544edec

Browse files
committed
Move integration test input sources into resources directory
1 parent cfe23d9 commit 544edec

File tree

20 files changed

+330
-192
lines changed

20 files changed

+330
-192
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (C) 2022 - 2023, 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+
* 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.tests.integration;
17+
18+
import java.nio.file.Files;
19+
import java.nio.file.Path;
20+
21+
/**
22+
* Abstract base class for integration tests to use.
23+
*
24+
* @author Ashley Scopes
25+
*/
26+
public abstract class AbstractIntegrationTest {
27+
28+
/**
29+
* Get the resources directory for this test. This will be a directory named the full
30+
* canonical class name.
31+
*
32+
* @return the resource directory path.
33+
*/
34+
protected Path resourcesDirectory() {
35+
var path = Path.of("src", "test", "resources");
36+
for (var part : getClass().getCanonicalName().split("[./]")) {
37+
path = path.resolve(part);
38+
}
39+
40+
if (!Files.isDirectory(path)) {
41+
throw new IllegalStateException(
42+
"Please ensure the directory " + path + " exists, and try again"
43+
);
44+
}
45+
46+
return path;
47+
}
48+
}

java-compiler-testing/src/test/java/io/github/ascopes/jct/tests/integration/compilation/BasicLegacyCompilationIntegrationTest.java

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import io.github.ascopes.jct.compilers.JctCompiler;
2121
import io.github.ascopes.jct.junit.JavacCompilerTest;
22+
import io.github.ascopes.jct.tests.integration.AbstractIntegrationTest;
2223
import io.github.ascopes.jct.workspaces.PathStrategy;
2324
import io.github.ascopes.jct.workspaces.Workspaces;
2425
import javax.tools.StandardLocation;
@@ -30,23 +31,16 @@
3031
* @author Ashley Scopes
3132
*/
3233
@DisplayName("Basic legacy compilation integration tests")
33-
class BasicLegacyCompilationIntegrationTest {
34+
class BasicLegacyCompilationIntegrationTest extends AbstractIntegrationTest {
3435

3536
@DisplayName("I can compile a 'Hello, World!' program using a RAM directory")
3637
@JavacCompilerTest
3738
void helloWorldJavacRamDirectory(JctCompiler<?, ?> compiler) {
3839
try (var workspace = Workspaces.newWorkspace(PathStrategy.RAM_DIRECTORIES)) {
3940
workspace
4041
.createPackage(StandardLocation.SOURCE_PATH)
41-
.createFile("com", "example", "HelloWorld.java").withContents(
42-
"package com.example;",
43-
"public class HelloWorld {",
44-
" public static void main(String[] args) {",
45-
" System.out.println(\"Hello, World\");",
46-
" }",
47-
"}"
48-
);
49-
42+
.createDirectory("com", "example")
43+
.copyContentsFrom(resourcesDirectory());
5044
var compilation = compiler.compile(workspace);
5145

5246
assertThatCompilation(compilation)
@@ -65,14 +59,8 @@ void helloWorldJavacTempDirectory(JctCompiler<?, ?> compiler) {
6559
try (var workspace = Workspaces.newWorkspace(PathStrategy.TEMP_DIRECTORIES)) {
6660
workspace
6761
.createPackage(StandardLocation.SOURCE_PATH)
68-
.createFile("com", "example", "HelloWorld.java").withContents(
69-
"package com.example;",
70-
"public class HelloWorld {",
71-
" public static void main(String[] args) {",
72-
" System.out.println(\"Hello, World\");",
73-
" }",
74-
"}"
75-
);
62+
.createDirectory("com", "example")
63+
.copyContentsFrom(resourcesDirectory());
7664

7765
var compilation = compiler.compile(workspace);
7866

java-compiler-testing/src/test/java/io/github/ascopes/jct/tests/integration/compilation/BasicModuleCompilationIntegrationTest.java

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import io.github.ascopes.jct.compilers.JctCompiler;
2121
import io.github.ascopes.jct.junit.JavacCompilerTest;
22+
import io.github.ascopes.jct.tests.integration.AbstractIntegrationTest;
2223
import io.github.ascopes.jct.workspaces.PathStrategy;
2324
import io.github.ascopes.jct.workspaces.Workspaces;
2425
import org.junit.jupiter.api.DisplayName;
@@ -29,7 +30,7 @@
2930
* @author Ashley Scopes
3031
*/
3132
@DisplayName("Basic module compilation integration tests")
32-
class BasicModuleCompilationIntegrationTest {
33+
class BasicModuleCompilationIntegrationTest extends AbstractIntegrationTest {
3334

3435
@DisplayName("I can compile a 'Hello, World!' module program using a RAM disk")
3536
@JavacCompilerTest(minVersion = 9)
@@ -38,20 +39,7 @@ void helloWorldRamDisk(JctCompiler<?, ?> compiler) {
3839
// Given
3940
workspace
4041
.createSourcePathPackage()
41-
.createFile("com", "example", "HelloWorld.java").withContents(
42-
"package com.example;",
43-
"public class HelloWorld {",
44-
" public static void main(String[] args) {",
45-
" System.out.println(\"Hello, World\");",
46-
" }",
47-
"}"
48-
)
49-
.and().createFile("module-info.java").withContents(
50-
"module hello.world {",
51-
" requires java.base;",
52-
" exports com.example;",
53-
"}"
54-
);
42+
.copyContentsFrom(resourcesDirectory());
5543

5644
// When
5745
var compilation = compiler.compile(workspace);
@@ -81,20 +69,7 @@ void helloWorldUsingTempDirectory(JctCompiler<?, ?> compiler) {
8169
// Given
8270
workspace
8371
.createSourcePathPackage()
84-
.createFile("com", "example", "HelloWorld.java").withContents(
85-
"package com.example;",
86-
"public class HelloWorld {",
87-
" public static void main(String[] args) {",
88-
" System.out.println(\"Hello, World\");",
89-
" }",
90-
"}"
91-
)
92-
.and().createFile("module-info.java").withContents(
93-
"module hello.world {",
94-
" requires java.base;",
95-
" exports com.example;",
96-
"}"
97-
);
72+
.copyContentsFrom(resourcesDirectory());
9873

9974
// When
10075
var compilation = compiler.compile(workspace);

java-compiler-testing/src/test/java/io/github/ascopes/jct/tests/integration/compilation/BasicMultiModuleCompilationIntegrationTest.java

Lines changed: 20 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import io.github.ascopes.jct.compilers.JctCompiler;
2121
import io.github.ascopes.jct.junit.JavacCompilerTest;
22+
import io.github.ascopes.jct.tests.integration.AbstractIntegrationTest;
2223
import io.github.ascopes.jct.workspaces.PathStrategy;
2324
import io.github.ascopes.jct.workspaces.Workspaces;
2425
import org.junit.jupiter.api.DisplayName;
@@ -29,28 +30,16 @@
2930
* @author Ashley Scopes
3031
*/
3132
@DisplayName("Basic multi-module compilation integration tests")
32-
class BasicMultiModuleCompilationIntegrationTest {
33+
class BasicMultiModuleCompilationIntegrationTest extends AbstractIntegrationTest {
3334

3435
@DisplayName("I can compile a single module using multi-module layout using a RAM disk")
3536
@JavacCompilerTest(minVersion = 9)
3637
void singleModuleInMultiModuleLayoutRamDisk(JctCompiler<?, ?> compiler) {
3738
try (var workspace = Workspaces.newWorkspace(PathStrategy.RAM_DIRECTORIES)) {
3839
// Given
3940
workspace
40-
.createSourcePathModule("hello.world")
41-
.createFile("com", "example", "HelloWorld.java").withContents(
42-
"package com.example;",
43-
"public class HelloWorld {",
44-
" public static void main(String[] args) {",
45-
" System.out.println(\"Hello, World\");",
46-
" }",
47-
"}"
48-
)
49-
.and().createFile("module-info.java").withContents(
50-
"module hello.world {",
51-
" exports com.example;",
52-
"}"
53-
);
41+
.createSourcePathModule("hello.world.singlemodule")
42+
.copyContentsFrom(resourcesDirectory().resolve("hello.world.singlemodule"));
5443

5544
// When
5645
var compilation = compiler.compile(workspace);
@@ -61,12 +50,12 @@ void singleModuleInMultiModuleLayoutRamDisk(JctCompiler<?, ?> compiler) {
6150

6251
assertThatCompilation(compilation)
6352
.classOutput().modules()
64-
.moduleExists("hello.world")
53+
.moduleExists("hello.world.singlemodule")
6554
.fileExists("com", "example", "HelloWorld.class").isNotEmptyFile();
6655

6756
assertThatCompilation(compilation)
6857
.classOutput().modules()
69-
.moduleExists("hello.world")
58+
.moduleExists("hello.world.singlemodule")
7059
.fileExists("module-info.class").isNotEmptyFile();
7160
}
7261
}
@@ -77,20 +66,8 @@ void singleModuleInMultiModuleLayoutTempDirectory(JctCompiler<?, ?> compiler) {
7766
try (var workspace = Workspaces.newWorkspace(PathStrategy.TEMP_DIRECTORIES)) {
7867
// Given
7968
workspace
80-
.createSourcePathModule("hello.world")
81-
.createFile("com", "example", "HelloWorld.java").withContents(
82-
"package com.example;",
83-
"public class HelloWorld {",
84-
" public static void main(String[] args) {",
85-
" System.out.println(\"Hello, World\");",
86-
" }",
87-
"}"
88-
)
89-
.and().createFile("module-info.java").withContents(
90-
"module hello.world {",
91-
" exports com.example;",
92-
"}"
93-
);
69+
.createSourcePathModule("hello.world.singlemodule")
70+
.copyContentsFrom(resourcesDirectory().resolve("hello.world.singlemodule"));
9471

9572
// When
9673
var compilation = compiler.compile(workspace);
@@ -101,12 +78,12 @@ void singleModuleInMultiModuleLayoutTempDirectory(JctCompiler<?, ?> compiler) {
10178

10279
assertThatCompilation(compilation)
10380
.classOutput().modules()
104-
.moduleExists("hello.world")
81+
.moduleExists("hello.world.singlemodule")
10582
.fileExists("com", "example", "HelloWorld.class").isNotEmptyFile();
10683

10784
assertThatCompilation(compilation)
10885
.classOutput().modules()
109-
.moduleExists("hello.world")
86+
.moduleExists("hello.world.singlemodule")
11087
.fileExists("module-info.class").isNotEmptyFile();
11188
}
11289
}
@@ -117,38 +94,12 @@ void multipleModulesInMultiModuleLayoutRamDisk(JctCompiler<?, ?> compiler) {
11794
try (var workspace = Workspaces.newWorkspace(PathStrategy.RAM_DIRECTORIES)) {
11895
// Given
11996
workspace
120-
.createSourcePathModule("hello.world")
121-
.createFile("com", "example", "HelloWorld.java").withContents(
122-
"package com.example;",
123-
"import com.example.greeter.Greeter;",
124-
"public class HelloWorld {",
125-
" public static void main(String[] args) {",
126-
" System.out.println(Greeter.greet(\"World\"));",
127-
" }",
128-
"}"
129-
)
130-
.and().createFile("module-info.java").withContents(
131-
"module hello.world {",
132-
" requires greeter;",
133-
" exports com.example;",
134-
"}"
135-
);
97+
.createSourcePathModule("hello.world.crossmodule")
98+
.copyContentsFrom(resourcesDirectory().resolve("hello.world.crossmodule"));
13699

137100
workspace
138101
.createSourcePathModule("greeter")
139-
.createFile("com", "example", "greeter", "Greeter.java").withContents(
140-
"package com.example.greeter;",
141-
"public class Greeter {",
142-
" public static String greet(String name) {",
143-
" return \"Hello, \" + name + \"!\";",
144-
" }",
145-
"}"
146-
)
147-
.and().createFile("module-info.java").withContents(
148-
"module greeter {",
149-
" exports com.example.greeter;",
150-
"}"
151-
);
102+
.copyContentsFrom(resourcesDirectory().resolve("greeter"));
152103

153104
// When
154105
var compilation = compiler.compile(workspace);
@@ -159,12 +110,12 @@ void multipleModulesInMultiModuleLayoutRamDisk(JctCompiler<?, ?> compiler) {
159110

160111
assertThatCompilation(compilation)
161112
.classOutput().modules()
162-
.moduleExists("hello.world")
113+
.moduleExists("hello.world.crossmodule")
163114
.fileExists("com", "example", "HelloWorld.class").isNotEmptyFile();
164115

165116
assertThatCompilation(compilation)
166117
.classOutput().modules()
167-
.moduleExists("hello.world")
118+
.moduleExists("hello.world.crossmodule")
168119
.fileExists("module-info.class").isNotEmptyFile();
169120

170121
assertThatCompilation(compilation)
@@ -185,38 +136,12 @@ void multipleModulesInMultiModuleLayoutTempDirectory(JctCompiler<?, ?> compiler)
185136
try (var workspace = Workspaces.newWorkspace(PathStrategy.TEMP_DIRECTORIES)) {
186137
// Given
187138
workspace
188-
.createSourcePathModule("hello.world")
189-
.createFile("com", "example", "HelloWorld.java").withContents(
190-
"package com.example;",
191-
"import com.example.greeter.Greeter;",
192-
"public class HelloWorld {",
193-
" public static void main(String[] args) {",
194-
" System.out.println(Greeter.greet(\"World\"));",
195-
" }",
196-
"}"
197-
)
198-
.and().createFile("module-info.java").withContents(
199-
"module hello.world {",
200-
" requires greeter;",
201-
" exports com.example;",
202-
"}"
203-
);
139+
.createSourcePathModule("hello.world.crossmodule")
140+
.copyContentsFrom(resourcesDirectory().resolve("hello.world.crossmodule"));
204141

205142
workspace
206143
.createSourcePathModule("greeter")
207-
.createFile("com", "example", "greeter", "Greeter.java").withContents(
208-
"package com.example.greeter;",
209-
"public class Greeter {",
210-
" public static String greet(String name) {",
211-
" return \"Hello, \" + name + \"!\";",
212-
" }",
213-
"}"
214-
)
215-
.and().createFile("module-info.java").withContents(
216-
"module greeter {",
217-
" exports com.example.greeter;",
218-
"}"
219-
);
144+
.copyContentsFrom(resourcesDirectory().resolve("greeter"));
220145

221146
// When
222147
var compilation = compiler.compile(workspace);
@@ -226,12 +151,12 @@ void multipleModulesInMultiModuleLayoutTempDirectory(JctCompiler<?, ?> compiler)
226151

227152
assertThatCompilation(compilation)
228153
.classOutput().modules()
229-
.moduleExists("hello.world")
154+
.moduleExists("hello.world.crossmodule")
230155
.fileExists("com", "example", "HelloWorld.class").isNotEmptyFile();
231156

232157
assertThatCompilation(compilation)
233158
.classOutput().modules()
234-
.moduleExists("hello.world")
159+
.moduleExists("hello.world.crossmodule")
235160
.fileExists("module-info.class").isNotEmptyFile();
236161

237162
assertThatCompilation(compilation)

java-compiler-testing/src/test/java/io/github/ascopes/jct/tests/integration/compilation/CompilingSpecificClassesIntegrationTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import io.github.ascopes.jct.compilers.JctCompiler;
2121
import io.github.ascopes.jct.junit.JavacCompilerTest;
22+
import io.github.ascopes.jct.tests.integration.AbstractIntegrationTest;
2223
import io.github.ascopes.jct.workspaces.Workspaces;
2324
import org.junit.jupiter.api.DisplayName;
2425

@@ -28,14 +29,15 @@
2829
* @author Ashley Scopes
2930
*/
3031
@DisplayName("Compiling specific classes integration tests")
31-
class CompilingSpecificClassesIntegrationTest {
32+
class CompilingSpecificClassesIntegrationTest extends AbstractIntegrationTest {
33+
3234
@DisplayName("Only the classes that I specify get compiled")
3335
@JavacCompilerTest
3436
void onlyTheClassesSpecifiedGetCompiled(JctCompiler<?, ?> compiler) {
3537
try (var workspace = Workspaces.newWorkspace()) {
3638
workspace
3739
.createSourcePathPackage()
38-
.copyContentsFrom("src", "test", "resources", "integration", "specificclasses");
40+
.copyContentsFrom(resourcesDirectory());
3941

4042
var compilation = compiler.compile(workspace, "Fibonacci", "HelloWorld");
4143

0 commit comments

Comments
 (0)