Repository navigation
Expand file tree
/
Copy pathAotIntegrationTest.java
More file actions
98 lines (86 loc) · 4.54 KB
/
Copy pathAotIntegrationTest.java
File metadata and controls
98 lines (86 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package org.kohsuke.github;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import static org.junit.Assert.fail;
/**
* AOT test to check if the required classes are registered for reflections / serialization. WARNING: This test needs to
* be run with maven as a plugin is required to generate the AOT information first.
*/
@SpringBootTest
public class AotIntegrationTest {
/**
* Create default AotIntegrationTest instance
*/
public AotIntegrationTest() {
}
/**
* Test to check if all required classes are registered for AOT.
*
* @throws IOException
* if the files to test can not be read
*/
@Test
public void testIfAllRequiredClassesAreRegisteredForAot() throws IOException {
String artifactId = System.getProperty("test.projectArtifactId", "default");
Stream<String> providedReflectionConfigStreamOfNames = readAotConfigToStreamOfClassNames(
"./target/classes/META-INF/native-image/org.kohsuke/" + artifactId + "/reflect-config.json");
Stream<String> providedNoReflectStreamOfNames = Files
.lines(Path.of("./target/test-classes/no-reflect-and-serialization-list"));
Stream<String> providedSerializationStreamOfNames = readAotConfigToStreamOfClassNames(
"./target/classes/META-INF/native-image/org.kohsuke/" + artifactId + "/serialization-config.json");
Stream<String> providedAotConfigClassNamesPart = Stream
.concat(providedSerializationStreamOfNames,
Stream.concat(providedReflectionConfigStreamOfNames, providedNoReflectStreamOfNames))
.distinct();
List<String> providedReflectionAndNoReflectionConfigNames = providedAotConfigClassNamesPart
.collect(Collectors.toList());
Stream<String> generatedReflectConfigStreamOfClassNames = readAotConfigToStreamOfClassNames(
"./target/spring-aot/test/resources/META-INF/native-image/org.kohsuke/" + artifactId
+ "/reflect-config.json");
Stream<String> generatedSerializationStreamOfNames = readAotConfigToStreamOfClassNames(
"./target/spring-aot/test/resources/META-INF/native-image/org.kohsuke/" + artifactId
+ "/serialization-config.json");
Stream<String> generatedAotConfigClassNames = Stream.concat(generatedReflectConfigStreamOfClassNames,
generatedSerializationStreamOfNames);
List<String> missingClasses = generatedAotConfigClassNames
.filter(name -> !providedReflectionAndNoReflectionConfigNames.contains(name))
.collect(Collectors.toList());
if (!missingClasses.isEmpty()) {
try {
String errorTemplate = Files
.readString(Path.of("./target/test-classes/reflection-and-serialization-test-error-message"));
String classList = missingClasses.stream().map(name -> " - " + name).collect(Collectors.joining("\n"));
fail(String.format(errorTemplate, classList));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
private Stream<String> readAotConfigToStreamOfClassNames(String reflectionConfig) throws IOException {
byte[] reflectionConfigFileAsBytes = Files.readAllBytes(Path.of(reflectionConfig));
// Test methods are allowed to directly use whatever jackson is available.
ArrayNode reflectConfigJsonArray = (ArrayNode) JsonMapper.builder()
.build()
.readTree(reflectionConfigFileAsBytes);
return StreamSupport
.stream(Spliterators.spliteratorUnknownSize(reflectConfigJsonArray.iterator(), Spliterator.ORDERED),
false)
.map(jsonNode -> jsonNode.get("name"))
.map(JsonNode::toString)
.map(reflectConfigEntryClassName -> reflectConfigEntryClassName.replace("\"", ""))
.filter(x -> x.contains("org.kohsuke.github"))
.filter(x -> !x.contains("org.kohsuke.github.AotTest"));
}
}