Skip to content

Commit c064e04

Browse files
committed
Add testing around dependency updating for JUnit5
1 parent 31e8bba commit c064e04

File tree

8 files changed

+440
-432
lines changed

8 files changed

+440
-432
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright 2020 the original author or authors.
3+
* <p>
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+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
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.openrewrite.java.testing.statik;
17+
18+
public interface UnitTest {}

src/before/java/org/openrewrite/java/testing/junit5/ExampleJunitTestClass.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,21 @@
2222
import org.junit.Rule;
2323
import org.junit.Test;
2424
import org.junit.Test;
25+
import org.junit.experimental.categories.Category;
2526
import org.junit.rules.ExpectedException;
2627
import org.junit.rules.TemporaryFolder;
2728
import org.junit.rules.Timeout;
2829
import org.mockito.Mock;
2930
import org.mockito.MockitoAnnotations;
31+
import org.openrewrite.java.testing.statik.UnitTest;
3032

3133
import java.io.File;
3234
import java.io.IOException;
3335
import java.util.List;
3436

3537
import static org.mockito.Mockito.*;
3638

39+
@Category(UnitTest.class)
3740
public class ExampleJunitTestClass {
3841

3942
// @Rule
@@ -104,17 +107,15 @@ public void assertsStuff() {
104107
Assert.fail("fail");
105108
}
106109

107-
@Test
108-
public void lambdaThrow() {
109-
110-
}
111-
112110
@Test(timeout = 500)
113111
public void bar() { }
114112

115113
@Test
116114
public void aTest() {
117-
String foo = mock(String.class);
118-
when(foo.concat(any())).then(invocation -> invocation.getArgumentAt(0, String.class));
115+
List<Integer> foo = mock(List.class);
116+
117+
when(foo.get(any())).then(invocation -> invocation.getArgumentAt(0, Integer.class));
118+
int one = foo.get(1);
119+
Assert.assertEquals(1, one);
119120
}
120121
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright 2020 the original author or authors.
3+
* <p>
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+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
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.openrewrite.java.testing.statik;
17+
18+
public interface UnitTest{}

src/main/java/org/openrewrite/java/testing/junit5/CategoryToTag.java

Lines changed: 83 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
*/
1616
package org.openrewrite.java.testing.junit5;
1717

18-
import com.fasterxml.jackson.databind.introspect.Annotated;
1918
import org.openrewrite.AutoConfigure;
19+
import org.openrewrite.Formatting;
2020
import org.openrewrite.java.AddAnnotation;
2121
import org.openrewrite.java.AutoFormat;
22+
import org.openrewrite.java.JavaIsoRefactorVisitor;
2223
import org.openrewrite.java.JavaRefactorVisitor;
2324
import org.openrewrite.java.tree.Expression;
2425
import org.openrewrite.java.tree.J;
@@ -30,117 +31,120 @@
3031
import java.util.stream.Collectors;
3132
import java.util.stream.Stream;
3233

34+
import static java.util.stream.Collectors.toList;
35+
import static org.openrewrite.Tree.randomId;
36+
3337
/**
3438
* Transforms the Junit4 @Category, which can list multiple categories, into one @Tag annotation per category listed
3539
*/
3640
@AutoConfigure
37-
public class CategoryToTag extends JavaRefactorVisitor {
41+
public class CategoryToTag extends JavaIsoRefactorVisitor {
3842
private static final String categoryAnnotation = "org.junit.experimental.categories.Category";
3943
private static final JavaType.Class tagType = JavaType.Class.build("org.junit.jupiter.api.Tag");
4044

4145
@Override
42-
public J visitClassDecl(J.ClassDecl classDecl) {
43-
J.ClassDecl cd = refactor(classDecl, super::visitClassDecl);
44-
if(!cd.findAnnotations("@" + categoryAnnotation).isEmpty()) {
46+
public J.ClassDecl visitClassDecl(J.ClassDecl classDecl) {
47+
J.ClassDecl cd = super.visitClassDecl(classDecl);
48+
List<J.Annotation> categoryAnnotations = cd.findAnnotationsOnClass("@" + categoryAnnotation);
49+
if(!categoryAnnotations.isEmpty()) {
4550
andThen(new Scoped(cd));
4651
}
4752
return cd;
4853
}
4954

5055
@Override
51-
public J visitMethod(J.MethodDecl method) {
52-
J.MethodDecl cd = refactor(method, super::visitMethod);
53-
if(!cd.findAnnotations("@" + categoryAnnotation).isEmpty()) {
54-
andThen(new Scoped(cd));
56+
public J.MethodDecl visitMethod(J.MethodDecl method) {
57+
J.MethodDecl m = super.visitMethod(method);
58+
List<J.Annotation> categoryAnnotations = m.findAnnotations("@" + categoryAnnotation);
59+
if(!categoryAnnotations.isEmpty()) {
60+
andThen(new Scoped(m));
5561
}
56-
return cd;
62+
return m;
5763
}
5864

59-
public static class Scoped extends JavaRefactorVisitor {
60-
final J statement;
61-
public Scoped(J statement) {
62-
this.statement = statement;
65+
public static class Scoped extends JavaIsoRefactorVisitor {
66+
final J scope;
67+
public Scoped(J scope) {
68+
this.scope = scope;
6369
}
6470

6571
@Override
66-
public J visitClassDecl(J.ClassDecl classDecl) {
67-
J.ClassDecl c = refactor(classDecl, super::visitClassDecl);
68-
69-
if (statement.isScope(classDecl) && !classDecl.findAnnotations("@" + categoryAnnotation).isEmpty()) {
70-
71-
classDecl.findAnnotations("@" + categoryAnnotation).stream().forEach( annot -> {
72-
Expression categoryArgs = annot.getArgs().getArgs().iterator().next();
73-
74-
Stream<Expression> categories = categoryArgs instanceof J.NewArray ?
75-
((J.NewArray) categoryArgs).getInitializer().getElements().stream() :
76-
Stream.of(categoryArgs);
77-
78-
categories.forEach(arg -> {
79-
andThen(new AddAnnotation.Scoped(
80-
classDecl,
81-
"org.junit.jupiter.api.Tag",
82-
arg.withPrefix("")));
83-
});
84-
}
85-
);
72+
public J.ClassDecl visitClassDecl(J.ClassDecl classDecl) {
73+
J.ClassDecl c = super.visitClassDecl(classDecl);
74+
if (scope.isScope(classDecl)) {
75+
AtomicInteger index = new AtomicInteger(0);
76+
c = c.withAnnotations(c.getAnnotations().stream()
77+
.flatMap(this::categoryAnnotationToTagAnnotations)
78+
.map(annotation -> {
79+
andThen(new AutoFormat(annotation));
80+
if (index.getAndIncrement() != 0) {
81+
return annotation.withPrefix("\n");
82+
} else {
83+
return annotation;
84+
}
85+
})
86+
.collect(Collectors.toList()));
8687

87-
c = c.withAnnotations(
88-
c.getAnnotations().stream()
89-
.filter(annot ->
90-
!TypeUtils.isOfClassType(annot.getAnnotationType().getType(), categoryAnnotation)
91-
)
92-
.collect(Collectors.toList())
93-
);
88+
maybeRemoveImport(categoryAnnotation);
89+
maybeAddImport(tagType);
9490
}
95-
maybeRemoveImport(categoryAnnotation);
96-
maybeAddImport(tagType);
9791

9892
return c;
9993
}
10094

10195
@Override
102-
public J visitMethod(J.MethodDecl methodDecl) {
103-
J.MethodDecl m = refactor(methodDecl, super::visitMethod);
104-
105-
if (statement.isScope(methodDecl) && !methodDecl.findAnnotations("@" + categoryAnnotation).isEmpty()) {
96+
public J.MethodDecl visitMethod(J.MethodDecl m) {
97+
if (scope.isScope(m)) {
98+
AtomicInteger index = new AtomicInteger(0);
10699
m = m.withAnnotations(m.getAnnotations().stream()
107-
.flatMap(annot -> {
108-
if(TypeUtils.isOfClassType(annot.getAnnotationType().getType(), categoryAnnotation)) {
109-
if(annot.getArgs() == null) return Stream.empty();
110-
111-
AtomicInteger index = new AtomicInteger(0);
112-
113-
Expression value = annot.getArgs().getArgs().iterator().next();
114-
//create stream of the values depending on what the value type is
115-
Stream<Expression> categories = value instanceof J.NewArray ?
116-
((J.NewArray) value).getInitializer().getElements().stream() :
117-
Stream.of(value);
118-
119-
return categories
120-
.map(arg -> {
121-
J.Annotation annotation =
122-
J.Annotation.buildAnnotation(
123-
annot.getFormatting(),
124-
tagType,
125-
Collections.singletonList(arg.withPrefix(""))
126-
);
127-
128-
if (index.getAndIncrement() != 0) {
129-
annotation = annotation.withPrefix("\n");
130-
andThen(new AutoFormat(annotation));
131-
}
132-
133-
return annotation;
134-
});
100+
.flatMap(this::categoryAnnotationToTagAnnotations)
101+
.map(annotation -> {
102+
andThen(new AutoFormat(annotation));
103+
if (index.getAndIncrement() != 0) {
104+
return annotation.withPrefix("\n");
105+
} else {
106+
return annotation;
135107
}
136-
return Stream.of(annot);
137108
})
138109
.collect(Collectors.toList()));
139-
}
140-
maybeRemoveImport(categoryAnnotation);
141-
maybeAddImport(tagType);
142110

111+
maybeRemoveImport(categoryAnnotation);
112+
maybeAddImport(tagType);
113+
}
143114
return m;
144115
}
116+
117+
private Stream<J.Annotation> categoryAnnotationToTagAnnotations(J.Annotation maybeCategory) {
118+
if(TypeUtils.isOfClassType(maybeCategory.getAnnotationType().getType(), categoryAnnotation)) {
119+
Expression annotationArgument = maybeCategory.getArgs().getArgs().iterator().next();
120+
121+
Stream<J.FieldAccess> categories;
122+
if (annotationArgument instanceof J.NewArray) {
123+
categories = ((J.NewArray) annotationArgument).getInitializer().getElements().stream()
124+
.map(J.FieldAccess.class::cast);
125+
} else {
126+
categories = Stream.of((J.FieldAccess) annotationArgument);
127+
}
128+
129+
return categories.map(category -> {
130+
String targetName = ((J.Ident) category.getTarget()).getSimpleName();
131+
J.Literal tagValue = new J.Literal(
132+
randomId(),
133+
targetName,
134+
"\"" + targetName + "\"",
135+
JavaType.Primitive.String,
136+
Formatting.EMPTY);
137+
J.Annotation tagAnnotation = J.Annotation.buildAnnotation(
138+
maybeCategory.getFormatting(),
139+
tagType,
140+
Collections.singletonList(tagValue)
141+
);
142+
maybeRemoveImport(TypeUtils.asFullyQualified(category.getTarget().getType()));
143+
return tagAnnotation;
144+
});
145+
} else {
146+
return Stream.of(maybeCategory);
147+
}
148+
}
145149
}
146150
}

src/main/resources/META-INF/rewrite/junit5.yml

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,30 +52,13 @@ visitors:
5252
- org.openrewrite.maven.RemoveDependency:
5353
groupId: junit
5454
artifactId: junit
55-
# Uncomment when a version of rewrite-maven has been published that has the fix for this issue:
56-
# https://github.com/openrewrite/rewrite/issues/92
57-
#---
58-
#type: specs.openrewrite.org/v1beta/visitor
59-
#name: org.openrewrite.java.testing.junit5.ExcludeJUnitVintageEngine
60-
#visitors:
61-
# - org.openrewrite.maven.ExcludeDependency:
62-
# groupId: org.junit.vintage
63-
# artifactId: junit-vintage-engine
64-
#---
65-
#type: specs.openrewrite.org/v1beta/visitor
66-
#name: org.openrewrite.java.testing.junit5.AddJunit5Dependency
67-
#visitors:
68-
# - org.openrewrite.maven.AddDependency:
69-
# groupId: org.junit.jupiter
70-
# artifactId: junit-jupiter-api
71-
# version: 5.7.0
72-
# scope: test
73-
# skipIfPresent: true
74-
# - org.openrewrite.maven.AddDependency:
75-
# groupId: org.junit.jupiter
76-
# artifactId: junit-jupiter-engine
77-
# version: 5.x
78-
# scope: test
55+
---
56+
type: specs.openrewrite.org/v1beta/visitor
57+
name: org.openrewrite.java.testing.junit5.ExcludeJUnitVintageEngine
58+
visitors:
59+
- org.openrewrite.maven.ExcludeDependency:
60+
groupId: org.junit.vintage
61+
artifactId: junit-vintage-engine
7962
---
8063
type: specs.openrewrite.org/v1beta/recipe
8164
name: org.openrewrite.java.testing.JUnit5Migration

0 commit comments

Comments
 (0)