Skip to content

Commit a08c667

Browse files
committed
extract for re-use
1 parent 6f98462 commit a08c667

File tree

7 files changed

+160
-1
lines changed

7 files changed

+160
-1
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2025 https://github.com/openapi-processor/openapi-processor-base
3+
* PDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.openapiprocessor.test
7+
8+
import groovy.transform.CompileStatic
9+
10+
import javax.tools.SimpleJavaFileObject
11+
12+
@CompileStatic
13+
class MemoryFile extends SimpleJavaFileObject {
14+
MemoryFile(String name, Kind kind) {
15+
super(URI.create("string:///${name}"), kind)
16+
}
17+
18+
OutputStream openOutputStream() {
19+
return new ByteArrayOutputStream()
20+
}
21+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2025 https://github.com/openapi-processor/openapi-processor-base
3+
* PDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.openapiprocessor.test
7+
8+
import groovy.transform.CompileStatic
9+
10+
import javax.tools.FileObject
11+
import javax.tools.ForwardingJavaFileManager
12+
import javax.tools.JavaFileObject
13+
import javax.tools.StandardJavaFileManager
14+
import java.nio.file.Path
15+
16+
@CompileStatic
17+
class MemoryFileManager extends ForwardingJavaFileManager<StandardJavaFileManager> {
18+
MemoryFileManager(StandardJavaFileManager fileManager) {
19+
super(fileManager)
20+
}
21+
22+
@Override
23+
JavaFileObject getJavaFileForOutput(
24+
Location location,
25+
String className,
26+
JavaFileObject.Kind kind,
27+
FileObject sibling
28+
) throws IOException {
29+
return new MemoryFile(className, kind)
30+
}
31+
32+
Iterable<? extends JavaFileObject> getJavaFileObjectsFromPaths(Iterable<Path> paths) {
33+
return fileManager.getJavaFileObjectsFromPaths(paths)
34+
}
35+
}

openapi-processor-test/src/main/groovy/io/openapiprocessor/test/ResourceReader.groovy

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
package io.openapiprocessor.test
77

8+
import groovy.transform.CompileStatic
9+
10+
@CompileStatic
811
class ResourceReader {
912
private Class resourceBase
1013

openapi-processor-test/src/main/groovy/io/openapiprocessor/test/TestFiles.groovy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ interface TestFiles {
1414

1515
Mapping getMapping(TestSet testSet)
1616
TestItems getOutputFiles(TestSet testSet)
17+
TestItems getCompileFiles(TestSet testSet)
1718

1819
Path getSourcePath(TestSet testSet, String file)
1920
Path getTargetPath(String file)

openapi-processor-test/src/main/groovy/io/openapiprocessor/test/TestFilesJimfs.groovy

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@ class TestFilesJimfs implements TestFiles {
6161
return new TestItemsReader(resource).read( "/tests/${testSet.name}", testSet.outputs)
6262
}
6363

64+
@Override
65+
TestItems getCompileFiles(TestSet testSet) {
66+
def itemsReader = new TestItemsReader(resource)
67+
def sourcePath = "/tests/${testSet.name}"
68+
69+
if (!itemsReader.exists(sourcePath, "compile.yaml")) {
70+
return null
71+
}
72+
73+
return new TestItemsReader(resource).read(sourcePath, "compile.yaml")
74+
}
75+
6476
@Override
6577
Path getSourcePath(TestSet testSet, String file) {
6678
return Paths.get(source.resolve("${testSet.expected}/${file}").toUri())

openapi-processor-test/src/main/groovy/io/openapiprocessor/test/TestFilesNative.groovy

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,19 @@ class TestFilesNative implements TestFiles {
4646

4747
@Override
4848
TestItems getOutputFiles(TestSet testSet) {
49-
return new TestItemsReader(resource).read( "/tests/${testSet.name}", testSet.outputs)
49+
return new TestItemsReader(resource).read("/tests/${testSet.name}", testSet.outputs)
50+
}
51+
52+
@Override
53+
TestItems getCompileFiles(TestSet testSet) {
54+
def itemsReader = new TestItemsReader(resource)
55+
def sourcePath = "/tests/${testSet.name}"
56+
57+
if (!itemsReader.exists(sourcePath, "compile.yaml")) {
58+
return null
59+
}
60+
61+
return new TestItemsReader(resource).read(sourcePath, "compile.yaml")
5062
}
5163

5264
@Override
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2025 https://github.com/openapi-processor/openapi-processor-base
3+
* PDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.openapiprocessor.test
7+
8+
import javax.tools.*
9+
import java.nio.file.Path
10+
11+
/**
12+
* used to compile test sets.
13+
*/
14+
class TestSetCompiler {
15+
TestSet testSet
16+
TestFiles testFiles
17+
18+
TestSetCompiler(TestSet testSet, TestFiles testFiles) {
19+
this.testSet = testSet
20+
this.testFiles = testFiles
21+
}
22+
23+
/**
24+
* compiles a test-set
25+
*
26+
* @return true on success, false on failure
27+
*/
28+
boolean run() {
29+
def sourcePath = "/tests/${testSet.name}"
30+
31+
def compilePaths = []
32+
33+
// stuff used by all tests
34+
compilePaths.add(Path.of("src/testInt/resources/compile/Generated.java"))
35+
compilePaths.add(Path.of("src/testInt/resources/compile/Mapping.java"))
36+
compilePaths.add(Path.of("src/testInt/resources/compile/Parameter.java"))
37+
38+
def expectedFiles = testFiles.getOutputFiles(testSet).items
39+
expectedFiles = expectedFiles.findAll { item -> !item.endsWith(".properties") }
40+
41+
def expectedFileNames = expectedFiles
42+
.collect { it.replaceFirst("<model>", "_${testSet.modelType}_") }
43+
44+
expectedFileNames.forEach {
45+
compilePaths.add(Path.of("src/testInt/resources${sourcePath}/$it"))
46+
}
47+
48+
def compileFiles = testFiles.getCompileFiles(testSet)
49+
if(compileFiles) {
50+
compileFiles.items.forEach {
51+
if (it.startsWith("not ")) {
52+
compilePaths.remove(Path.of("src/testInt/resources/${it.substring(4)}"))
53+
} else {
54+
compilePaths.add(Path.of("src/testInt/resources/$it"))
55+
}
56+
}
57+
}
58+
59+
def diagnostics = new DiagnosticCollector<JavaFileObject>()
60+
def compiler = ToolProvider.getSystemJavaCompiler()
61+
def manager = new MemoryFileManager(compiler.getStandardFileManager(diagnostics, null, null))
62+
63+
def options = []
64+
def compilationUnit = manager.getJavaFileObjectsFromPaths(compilePaths)
65+
def task = compiler.getTask(null, manager, diagnostics, options, null, compilationUnit)
66+
def success = task.call()
67+
if (!success) {
68+
for (diagnostic in diagnostics.diagnostics) {
69+
println("CompileSpec: compile error at ${diagnostic.source.name}:${diagnostic.lineNumber}, ${diagnostic.getMessage(Locale.ENGLISH)}")
70+
}
71+
}
72+
73+
return success
74+
}
75+
}

0 commit comments

Comments
 (0)