Skip to content

Commit 1125324

Browse files
pway99TeamModerne
andauthored
refactor: Code cleanup (#165)
* refactor: Code cleanup Co-authored-by: Moderne <[email protected]> * remove unnecessary cast Co-authored-by: Moderne <[email protected]>
1 parent 3982f0e commit 1125324

10 files changed

+22
-22
lines changed

src/main/java/org/openrewrite/java/testing/assertj/JUnitAssertArrayEqualsToAssertThat.java

100755100644
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ private static boolean isFloatingPointType(Expression expression) {
151151
JavaType.FullyQualified fullyQualified = TypeUtils.asFullyQualified(expression.getType());
152152
if (fullyQualified != null) {
153153
String typeName = fullyQualified.getFullyQualifiedName();
154-
return (typeName.equals("java.lang.Double") || typeName.equals("java.lang.Float"));
154+
return ("java.lang.Double".equals(typeName) || "java.lang.Float".equals(typeName));
155155
}
156156

157157
JavaType.Primitive parameterType = TypeUtils.asPrimitive(expression.getType());

src/main/java/org/openrewrite/java/testing/assertj/JUnitAssertEqualsToAssertThat.java

100755100644
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ private static boolean isFloatingPointType(Expression expression) {
145145
JavaType.FullyQualified fullyQualified = TypeUtils.asFullyQualified(expression.getType());
146146
if (fullyQualified != null) {
147147
String typeName = fullyQualified.getFullyQualifiedName();
148-
return (typeName.equals("java.lang.Double") || typeName.equals("java.lang.Float"));
148+
return ("java.lang.Double".equals(typeName) || "java.lang.Float".equals(typeName));
149149
}
150150

151151
JavaType.Primitive parameterType = TypeUtils.asPrimitive(expression.getType());

src/main/java/org/openrewrite/java/testing/assertj/JUnitAssertNotEqualsToAssertThat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ private static boolean isFloatingPointType(Expression expression) {
144144
JavaType.FullyQualified fullyQualified = TypeUtils.asFullyQualified(expression.getType());
145145
if (fullyQualified != null) {
146146
String typeName = fullyQualified.getFullyQualifiedName();
147-
return (typeName.equals("java.lang.Double") || typeName.equals("java.lang.Float"));
147+
return ("java.lang.Double".equals(typeName) || "java.lang.Float".equals(typeName));
148148
}
149149

150150
JavaType.Primitive parameterType = TypeUtils.asPrimitive(expression.getType());

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

100755100644
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
6767
Expression firstArg = args.get(0);
6868
// Suppress arg-switching for Assertions.assertEquals(String, String)
6969
if (args.size() == 2) {
70-
if (m.getSimpleName().equals("assertSame")
71-
|| m.getSimpleName().equals("assertNotSame")
72-
|| m.getSimpleName().equals("assertEquals")
73-
|| m.getSimpleName().equals("assertNotEquals")) {
70+
if ("assertSame".equals(m.getSimpleName())
71+
|| "assertNotSame".equals(m.getSimpleName())
72+
|| "assertEquals".equals(m.getSimpleName())
73+
|| "assertNotEquals".equals(m.getSimpleName())) {
7474
return m;
7575
}
7676
}
@@ -101,7 +101,7 @@ private boolean isStringArgument(Expression arg) {
101101

102102
private static boolean isJunitAssertMethod(J.MethodInvocation method) {
103103
if (method.getType() != null && TypeUtils.isAssignableTo(ASSERTION_TYPE, method.getType().getDeclaringType())) {
104-
return !method.getSimpleName().equals("assertThat");
104+
return !"assertThat".equals(method.getSimpleName());
105105
}
106106
if (!(method.getSelect() instanceof J.Identifier)) {
107107
return false;
@@ -111,7 +111,7 @@ private static boolean isJunitAssertMethod(J.MethodInvocation method) {
111111
return false;
112112
}
113113
JavaType.FullyQualified receiverType = (JavaType.FullyQualified) receiver.getType();
114-
return receiverType.getFullyQualifiedName().equals("org.junit.Assert");
114+
return "org.junit.Assert".equals(receiverType.getFullyQualifiedName());
115115
}
116116
}
117117
}

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

100755100644
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDecl
137137
Expression expectMethodArg = args.get(0);
138138
isExpectArgAMatcher = isHamcrestMatcher(expectMethodArg);
139139
JavaType.FullyQualified argType = TypeUtils.asFullyQualified(expectMethodArg.getType());
140-
if (!isExpectArgAMatcher && (argType == null || !argType.getFullyQualifiedName().equals("java.lang.Class"))) {
140+
if (!isExpectArgAMatcher && (argType == null || !"java.lang.Class".equals(argType.getFullyQualifiedName()))) {
141141
return m;
142142
}
143143
}
@@ -235,7 +235,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDecl
235235

236236
@Override
237237
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
238-
if (method.getType() != null && method.getType().getDeclaringType().getFullyQualifiedName().equals("org.junit.rules.ExpectedException")) {
238+
if (method.getType() != null && "org.junit.rules.ExpectedException".equals(method.getType().getDeclaringType().getFullyQualifiedName())) {
239239
switch (method.getSimpleName()) {
240240
case "expect":
241241
getCursor().putMessageOnFirstEnclosing(J.MethodDeclaration.class, "expectedExceptionMethodInvocation", method);

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
9999
Cursor classDeclCursor = getCursor().dropParentUntil(J.ClassDeclaration.class::isInstance);
100100
// methods having names starting with parametersFor... are init methods
101101
if (m.getSimpleName().startsWith(PARAMETERS_FOR_PREFIX)) {
102-
((Set<String>) classDeclCursor.computeMessageIfAbsent(INIT_METHOD_REFERENCES, v -> new HashSet<String>())).add(m.getSimpleName());
102+
classDeclCursor.computeMessageIfAbsent(INIT_METHOD_REFERENCES, v -> new HashSet<>()).add(m.getSimpleName());
103103
}
104104
return m;
105105
}
@@ -112,7 +112,7 @@ public J.Annotation visitAnnotation(J.Annotation annotation, ExecutionContext ex
112112
String annotationArgumentValue = getAnnotationArgumentForInitMethod(anno, "method", "named");
113113
if (annotationArgumentValue != null) {
114114
for (String method : annotationArgumentValue.split(",")) {
115-
((Set<String>) classDeclCursor.computeMessageIfAbsent(INIT_METHOD_REFERENCES, v -> new HashSet<String>())).add(method);
115+
classDeclCursor.computeMessageIfAbsent(INIT_METHOD_REFERENCES, v -> new HashSet<>()).add(method);
116116
}
117117
} else if (anno.getArguments() != null && !anno.getArguments().isEmpty()) {
118118
// This conversion is not supported add a comment to the annotation and the method name to the not supported list
@@ -130,15 +130,15 @@ public J.Annotation visitAnnotation(J.Annotation annotation, ExecutionContext ex
130130
String namedInitMethod = getLiteralAnnotationArgumentValue(annotation);
131131
if (namedInitMethod != null) {
132132
J.MethodDeclaration m = getCursor().dropParentUntil(J.MethodDeclaration.class::isInstance).getValue();
133-
((Set<String>) classDeclCursor.computeMessageIfAbsent(INIT_METHOD_REFERENCES, v -> new HashSet<String>())).add(m.getSimpleName());
134-
classDeclCursor.computeMessageIfAbsent(INIT_METHODS_MAP, v -> new HashMap<String, String>()).put(namedInitMethod, m.getSimpleName());
133+
classDeclCursor.computeMessageIfAbsent(INIT_METHOD_REFERENCES, v -> new HashSet<>()).add(m.getSimpleName());
134+
classDeclCursor.computeMessageIfAbsent(INIT_METHODS_MAP, v -> new HashMap<>()).put(namedInitMethod, m.getSimpleName());
135135
}
136136
} else if (TEST_CASE_NAME_MATCHER.matches(anno)) {
137137
// test name for ParameterizedTest argument
138138
Object testNameArg = getLiteralAnnotationArgumentValue(anno);
139139
String testName = testNameArg != null ? testNameArg.toString() : "{method}({params}) [{index}]";
140140
J.MethodDeclaration md = getCursor().dropParentUntil(J.MethodDeclaration.class::isInstance).getValue();
141-
classDeclCursor.computeMessageIfAbsent(INIT_METHODS_MAP, v -> new HashMap<String, String>()).put(md.getSimpleName(), testName);
141+
classDeclCursor.computeMessageIfAbsent(INIT_METHODS_MAP, v -> new HashMap<>()).put(md.getSimpleName(), testName);
142142
}
143143
return anno;
144144
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
146146
J.MethodDeclaration md = super.visitMethodDeclaration(method, executionContext);
147147
if (md.getSimpleName().startsWith("test") && md.getLeadingAnnotations().stream().noneMatch(JUNIT_TEST_ANNOTATION_MATCHER::matches)) {
148148
md = updateMethodDeclarationAnnotationAndModifier(md, "@Test", "org.junit.jupiter.api.Test");
149-
} else if (md.getSimpleName().equals("setUp")) {
149+
} else if ("setUp".equals(md.getSimpleName())) {
150150
md = updateMethodDeclarationAnnotationAndModifier(md, "@BeforeEach", "org.junit.jupiter.api.BeforeEach");
151-
} else if (md.getSimpleName().equals("tearDown")) {
151+
} else if ("tearDown".equals(md.getSimpleName())) {
152152
md = updateMethodDeclarationAnnotationAndModifier(md, "@AfterEach", "org.junit.jupiter.api.AfterEach");
153153
}
154154
return md;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
131131
variableForInitMethod = variableForInitMethod.withTypeExpression(variableForInitMethod.getTypeExpression().withPrefix(Space.EMPTY).withComments(new ArrayList<>()));
132132
}
133133
//noinspection unchecked
134-
((TreeMap<Integer, Statement>) params.computeIfAbsent(FIELD_INJECTION_ARGUMENTS, v -> new TreeMap<Integer, Statement>())).put(position, variableForInitMethod);
134+
((TreeMap<Integer, Statement>) params.computeIfAbsent(FIELD_INJECTION_ARGUMENTS, v -> new TreeMap<>())).put(position, variableForInitMethod);
135135

136136
}
137137
return variableDeclarations;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
161161
JavaType newFolderMethodDeclaration = methods
162162
.filter(m -> {
163163
List<Statement> params = m.getParameters();
164-
return m.getSimpleName().equals("newFolder")
164+
return "newFolder".equals(m.getSimpleName())
165165
&& params.size() == 2
166166
&& params.get(0).hasClassType(FILE_TYPE)
167167
&& params.get(1).hasClassType(STRING_TYPE);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
127127
String assignParamName = ((J.Identifier) assign.getVariable()).getSimpleName();
128128
Expression e = assign.getAssignment();
129129

130-
if (assignParamName.equals("expected")) {
130+
if ("expected".equals(assignParamName)) {
131131
assert e instanceof J.FieldAccess;
132132

133133
m = m.withTemplate(JavaTemplate.builder(this::getCursor, "Object o = () -> #{}").build(),
@@ -146,7 +146,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
146146
e, lambda);
147147

148148
maybeAddImport("org.junit.jupiter.api.Assertions", "assertThrows");
149-
} else if (assignParamName.equals("timeout")) {
149+
} else if ("timeout".equals(assignParamName)) {
150150
doAfterVisit(new AddTimeoutAnnotationStep(m, e));
151151
}
152152
}

0 commit comments

Comments
 (0)