Skip to content

Commit 326e621

Browse files
authored
JUnit5 Extension for unit testing application parts (#31)
1 parent 269b30e commit 326e621

File tree

108 files changed

+2897
-97
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+2897
-97
lines changed

annotation-processor-common/src/testFixtures/java/ru/tinkoff/kora/annotation/processor/common/TestUtils.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,35 @@ public static ClassLoader annotationProcess(List<Class<?>> targetClasses, Proces
107107
return annotationProcessFiles(files, processors);
108108
}
109109

110+
public static ClassLoader annotationProcess(List<Class<?>> targetClasses, List<Processor> processors) throws Exception {
111+
var files = targetClasses.stream()
112+
.map(targetClass -> {
113+
var targetFile = targetClass.getName().replace('.', '/') + ".java";
114+
var root = "src/test/java/";
115+
return root + targetFile;
116+
})
117+
.toList();
118+
119+
return annotationProcessFiles(files, true, processors);
120+
}
121+
110122
public static ClassLoader annotationProcessFiles(List<String> targetFiles, Processor... processors) throws Exception {
111123
return annotationProcessFiles(targetFiles, true, processors);
112124
}
113125

114-
public static ClassLoader annotationProcessFiles(List<String> targetFiles, boolean clearClasses, Processor... processors) throws Exception {
126+
public static ClassLoader annotationProcessFiles(List<String> targetFiles, boolean clearClasses, Processor ... processors) throws Exception {
127+
return annotationProcessFiles(targetFiles, clearClasses, List.of(processors));
128+
}
129+
130+
public static ClassLoader annotationProcessFiles(List<String> targetFiles, boolean clearClasses, List<Processor> processors) throws Exception {
131+
return annotationProcessFiles(targetFiles, List.of(), clearClasses, processors);
132+
}
133+
134+
public static ClassLoader annotationProcessFiles(List<String> targetFiles, List<String> targetClasses, boolean clearClasses, List<Processor> processors) throws Exception {
135+
return annotationProcessFiles(targetFiles, targetClasses, clearClasses, p -> true, processors);
136+
}
137+
138+
public static ClassLoader annotationProcessFiles(List<String> targetFiles, List<String> targetClasses, boolean clearClasses, Predicate<Path> clearClassesPredicate, List<Processor> processors) throws Exception {
115139
var compiler = ToolProvider.getSystemJavaCompiler();
116140
var out = new StringWriter();
117141
var diagnostics = new ArrayList<Diagnostic<? extends JavaFileObject>>();
@@ -126,7 +150,7 @@ public static ClassLoader annotationProcessFiles(List<String> targetFiles, boole
126150
if (clearClasses) {
127151
try (var s = Files.walk(outClasses)) {
128152
s.forEach(p -> {
129-
if (!Files.isDirectory(p)) {
153+
if (!Files.isDirectory(p) && clearClassesPredicate.test(p)) {
130154
try {
131155
Files.delete(p);
132156
} catch (IOException e) {
@@ -157,8 +181,8 @@ public static ClassLoader annotationProcessFiles(List<String> targetFiles, boole
157181
cp.add(outClasses);
158182
standardFileManager.setLocationFromPaths(StandardLocation.CLASS_PATH, cp);
159183

160-
var task = compiler.getTask(out, standardFileManager, l, List.of("-parameters", "-g", "--enable-preview", "--source", "17", "-XprintRounds"), List.of(), inputSourceFiles);
161-
task.setProcessors(List.of(processors));
184+
var task = compiler.getTask(out, standardFileManager, l, List.of("-parameters", "-g", "--enable-preview", "--source", "17", "-XprintRounds"), targetClasses, inputSourceFiles);
185+
task.setProcessors(processors);
162186
try {
163187
task.call();
164188
if (diagnostics.stream().noneMatch(d -> d.getKind() == Diagnostic.Kind.ERROR)) {

application-graph/src/main/java/ru/tinkoff/kora/application/graph/ApplicationGraphDraw.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.TreeMap;
1111

1212
public class ApplicationGraphDraw {
13+
1314
private final List<Node<?>> graphNodes = new ArrayList<>();
1415
private final Class<?> root;
1516

@@ -21,11 +22,11 @@ public Class<?> getRoot() {
2122
return root;
2223
}
2324

24-
public <T> Node<T> addNode0(Type type, Class<?>[] tags, Graph.Factory<T> factory, Node<?>... dependencies) {
25+
public <T> Node<T> addNode0(Type type, Class<?>[] tags, Graph.Factory<? extends T> factory, Node<?>... dependencies) {
2526
return this.addNode0(type, tags, factory, List.of(), dependencies);
2627
}
2728

28-
public <T> Node<T> addNode0(Type type, Class<?>[] tags, Graph.Factory<T> factory, List<Node<? extends GraphInterceptor<T>>> interceptors, Node<?>... dependencies) {
29+
public <T> Node<T> addNode0(Type type, Class<?>[] tags, Graph.Factory<? extends T> factory, List<Node<? extends GraphInterceptor<T>>> interceptors, Node<?>... dependencies) {
2930
for (var dependency : dependencies) {
3031
if (dependency.index >= 0 && dependency.graphDraw != this) {
3132
throw new IllegalArgumentException("Dependency is from another graph");
@@ -73,7 +74,7 @@ public Node<?> findNodeByType(Type type) {
7374
return null;
7475
}
7576

76-
public <T> void replaceNode(Node<T> node, Graph.Factory<T> factory) {
77+
public <T> void replaceNode(Node<T> node, Graph.Factory<? extends T> factory) {
7778
this.graphNodes.set(node.index, new Node<T>(
7879
this, node.index, factory, node.type(), List.of(), List.of(), node.tags()
7980
));

application-graph/src/main/java/ru/tinkoff/kora/application/graph/Node.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
public final class Node<T> {
99
final ApplicationGraphDraw graphDraw;
1010
final int index;
11-
final Graph.Factory<T> factory;
11+
final Graph.Factory<? extends T> factory;
1212
// leaks for the test purposes
1313
private final Type type;
1414
private final Class<?>[] tags;
@@ -18,7 +18,7 @@ public final class Node<T> {
1818
private final List<Node<?>> dependentNodes;
1919
private final boolean isValueOf;
2020

21-
Node(ApplicationGraphDraw graphDraw, int index, Graph.Factory<T> factory, Type type, List<Node<?>> dependencyNodes, List<Node<? extends GraphInterceptor<T>>> interceptors, Class<?>[] tags) {
21+
Node(ApplicationGraphDraw graphDraw, int index, Graph.Factory<? extends T> factory, Type type, List<Node<?>> dependencyNodes, List<Node<? extends GraphInterceptor<T>>> interceptors, Class<?>[] tags) {
2222
this.graphDraw = graphDraw;
2323
this.index = index;
2424
this.factory = factory;
@@ -31,7 +31,7 @@ public final class Node<T> {
3131
this.tags = tags;
3232
}
3333

34-
private Node(ApplicationGraphDraw graphDraw, int index, Graph.Factory<T> factory, Type type, List<Node<?>> dependencyNodes, List<Node<? extends GraphInterceptor<T>>> interceptors, List<Node<?>> dependentNodes, List<Node<?>> intercepts, boolean isValueOf, Class<?>[] tags) {
34+
private Node(ApplicationGraphDraw graphDraw, int index, Graph.Factory<? extends T> factory, Type type, List<Node<?>> dependencyNodes, List<Node<? extends GraphInterceptor<T>>> interceptors, List<Node<?>> dependentNodes, List<Node<?>> intercepts, boolean isValueOf, Class<?>[] tags) {
3535
this.graphDraw = graphDraw;
3636
this.index = index;
3737
this.factory = factory;

build.gradle

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ subprojects {
4848

4949
// config test libraries
5050
subprojects {
51-
if (project.parent.name != "test") {
51+
if (project.parent.name != "internal") {
5252
return
5353
}
5454
dependencies {
@@ -61,10 +61,10 @@ def isPublishedLibrary = { Project p ->
6161
if (!p.childProjects.isEmpty()) {
6262
return false
6363
}
64-
if (p.parent.name == "test") {
64+
if (p.parent.name == "internal") {
6565
return false
6666
}
67-
if (p.name == 'maven-parent') {
67+
if (p.name == "maven-parent") {
6868
return false
6969
}
7070
return true
@@ -226,7 +226,7 @@ subprojects {
226226
dependencies {
227227
api libs.jsr305
228228

229-
testImplementation project(":test:test-logging")
229+
testImplementation project(":internal:test-logging")
230230
testImplementation libs.junit.jupiter
231231
testImplementation libs.mockito.core
232232
testImplementation libs.assertj
@@ -330,7 +330,7 @@ def isOtherTest = { Project p ->
330330
if (p.name.startsWith("http") || p.name.startsWith("kafka") || p.name.startsWith("database")) {
331331
return false
332332
}
333-
if (p.parent.name == "test") {
333+
if (p.parent.name == "internal") {
334334
return false
335335
}
336336
return !kspProjects.contains(p.name)

cache/cache-annotation-processor/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ dependencies {
99
implementation libs.logback.classic
1010

1111
testImplementation testFixtures(project(":annotation-processor-common"))
12-
testImplementation project(":test:test-logging")
12+
testImplementation project(":internal:test-logging")
1313
testImplementation "com.google.testing.compile:compile-testing:0.19"
1414
}
1515

cache/cache-caffeine/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ dependencies {
1111
testImplementation project(":cache:cache-annotation-processor")
1212
testImplementation project(":config:config-annotation-processor")
1313
testImplementation project(":kora-app-annotation-processor")
14-
testImplementation project(":test:test-logging")
14+
testImplementation project(":internal:test-logging")
1515
}
1616

1717
apply from: '../../in-test-generated.gradle'

cache/cache-common/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ dependencies {
33
api project(":common")
44
api project(":logging:logging-common")
55

6-
testImplementation project(":test:test-logging")
6+
testImplementation project(":internal:test-logging")
77
}

cache/cache-redis/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ dependencies {
1616
testImplementation project(":config:config-annotation-processor")
1717
testImplementation project(":kora-app-annotation-processor")
1818
testImplementation project(":json:json-annotation-processor")
19-
testImplementation project(":test:test-logging")
20-
testImplementation project(":test:test-redis")
19+
testImplementation project(":internal:test-logging")
20+
testImplementation project(":internal:test-redis")
2121

2222
testImplementation libs.testcontainers.junit.jupiter
2323
}

cache/cache-symbol-processor/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ dependencies {
1414
implementation libs.kotlinpoet
1515
implementation libs.kotlinpoet.ksp
1616

17-
testImplementation project(":test:test-logging")
17+
testImplementation project(":internal:test-logging")
1818
testImplementation testFixtures(project(":symbol-processor-common"))
1919
testImplementation(libs.kotlin.stdlib.lib)
2020
testImplementation(libs.kotlin.coroutines.reactor)
Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,10 @@
11
package ru.tinkoff.kora.common;
22

3-
import reactor.core.publisher.Mono;
4-
import ru.tinkoff.kora.application.graph.Lifecycle;
5-
63
import java.lang.annotation.ElementType;
74
import java.lang.annotation.Retention;
85
import java.lang.annotation.RetentionPolicy;
96
import java.lang.annotation.Target;
107

11-
@Retention(RetentionPolicy.SOURCE)
8+
@Retention(RetentionPolicy.CLASS)
129
@Target(ElementType.TYPE)
13-
public @interface KoraApp {
14-
}
15-
16-
17-
18-
interface TestApp {
19-
default Lifecycle someLifecycle() {
20-
return new Lifecycle() {
21-
@Override
22-
public Mono<?> init() {
23-
return null;
24-
}
25-
26-
@Override
27-
public Mono<?> release() {
28-
return null;
29-
}
30-
};
31-
}
32-
}
10+
public @interface KoraApp { }

0 commit comments

Comments
 (0)