Skip to content

Commit e5b93b3

Browse files
committed
Make test Java 17 compatible
1 parent 6852124 commit e5b93b3

File tree

1 file changed

+78
-95
lines changed

1 file changed

+78
-95
lines changed

src/test/java/org/refactoringminer/test/TestRelatedStatementMappingsTest.java

Lines changed: 78 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,10 @@ public void testAssumptionIntroducingRefactoring(String url,String commit) throw
123123
})
124124
public void testAssertThrowsMappings(String url, String commit, String testResultFileName) {
125125
testRefactoringMappings(url, commit, testResultFileName, ref -> {
126-
switch (ref) {
127-
case AssertThrowsRefactoring a -> mapperInfo(a.getMappings(), a.getOperationBefore(), a.getOperationAfter());
128-
case AssertTimeoutRefactoring a -> mapperInfo(a.getMappings(), a.getOperationBefore(), a.getOperationAfter());
129-
default -> {}
130-
}
126+
if (ref instanceof AssertThrowsRefactoring a)
127+
mapperInfo(a.getMappings(), a.getOperationBefore(), a.getOperationAfter());
128+
else if (ref instanceof AssertTimeoutRefactoring a)
129+
mapperInfo(a.getMappings(), a.getOperationBefore(), a.getOperationAfter());
131130
});
132131
}
133132

@@ -152,11 +151,11 @@ void testExtractFixtureRefactoring(String url,String commit) {
152151
})
153152
public void testCustomRunnerMappings(String url, String commit, String testResultFileName) {
154153
testRefactoringMappings(url, commit, testResultFileName, ref -> {
155-
Set<Pair<UMLAnnotation, UMLAnnotation>> annotations = switch (ref) {
156-
case ModifyClassAnnotationRefactoring modifyRef -> Set.of(Pair.of(modifyRef.getAnnotationBefore(), modifyRef.getAnnotationAfter()));
157-
case AddClassAnnotationRefactoring addRef -> Set.of(Pair.of(null, addRef.getAnnotation()));
158-
default -> Set.of();
159-
};
154+
Set<Pair<UMLAnnotation, UMLAnnotation>> annotations = Set.of();
155+
if (ref instanceof ModifyClassAnnotationRefactoring modifyRef)
156+
annotations = Set.of(Pair.of(modifyRef.getAnnotationBefore(), modifyRef.getAnnotationAfter()));
157+
else if (ref instanceof AddClassAnnotationRefactoring addRef)
158+
annotations = Set.of(Pair.of(null, addRef.getAnnotation()));
160159
if (!annotations.isEmpty() && Set.of("RunWith", "ExtendWith").contains(((AnnotationRefactoring) ref).getAnnotation().getTypeName())) {
161160
mapperInfo(annotations, ((ClassLevelRefactoring) ref).getClassBefore(), ((ClassLevelRefactoring) ref).getClassAfter());
162161
}
@@ -230,16 +229,15 @@ public void testReplaceAssertionMappings(String url, String commit, String testR
230229
})
231230
public void testInlineFixtureMappings(String url, String commit, String testResultFileName) {
232231
testRefactoringMappings(url, commit, testResultFileName, ref -> {
233-
switch (ref) {
234-
case MoveCodeRefactoring moveCodeRefactoring -> mapperInfo(moveCodeRefactoring.getMappings(), moveCodeRefactoring.getSourceContainer(), moveCodeRefactoring.getTargetContainer());
235-
case RemoveMethodAnnotationRefactoring removeMethodAnnotationRefactoring -> {
236-
if (Set.of("Before", "BeforeEach", "BeforeAll", "BeforeClass","After", "AfterEach", "AfterAll", "AfterClass").contains(removeMethodAnnotationRefactoring.getAnnotation().getTypeName())) {
237-
mapperInfo(Set.of(Pair.of(removeMethodAnnotationRefactoring.getAnnotation(), null)), removeMethodAnnotationRefactoring.getOperationBefore(), removeMethodAnnotationRefactoring.getOperationAfter());
238-
}
232+
if (ref instanceof MoveCodeRefactoring moveCodeRefactoring)
233+
mapperInfo(moveCodeRefactoring.getMappings(), moveCodeRefactoring.getSourceContainer(), moveCodeRefactoring.getTargetContainer());
234+
else if(ref instanceof RemoveMethodAnnotationRefactoring removeMethodAnnotationRefactoring) {
235+
if (Set.of("Before", "BeforeEach", "BeforeAll", "BeforeClass","After", "AfterEach", "AfterAll", "AfterClass").contains(removeMethodAnnotationRefactoring.getAnnotation().getTypeName())) {
236+
mapperInfo(Set.of(Pair.of(removeMethodAnnotationRefactoring.getAnnotation(), null)), removeMethodAnnotationRefactoring.getOperationBefore(), removeMethodAnnotationRefactoring.getOperationAfter());
239237
}
240-
case InlineOperationRefactoring inlineOperationRefactoring -> mapperInfo(inlineOperationRefactoring.getBodyMapper().getMappings(), inlineOperationRefactoring.getInlinedOperation(), inlineOperationRefactoring.getTargetOperationAfterInline());
241-
default -> {}
242238
}
239+
else if (ref instanceof InlineOperationRefactoring inlineOperationRefactoring)
240+
mapperInfo(inlineOperationRefactoring.getBodyMapper().getMappings(), inlineOperationRefactoring.getInlinedOperation(), inlineOperationRefactoring.getTargetOperationAfterInline());
243241
});
244242
}
245243

@@ -255,14 +253,12 @@ public void testInlineFixtureMappings(String url, String commit, String testResu
255253
})
256254
public void testSplitTestMappings(String url, String commit, String testResultFileName) {
257255
testRefactoringMappings(url, commit, testResultFileName, ref -> {
258-
switch (ref) {
259-
case MoveCodeRefactoring moveCodeRefactoring -> mapperInfo(moveCodeRefactoring.getMappings(), moveCodeRefactoring.getSourceContainer(), moveCodeRefactoring.getTargetContainer());
260-
case SplitOperationRefactoring splitOperationRefactoring -> {
261-
for (UMLOperationBodyMapper methodMapping : splitOperationRefactoring.getMappers()) {
262-
mapperInfo(methodMapping.getMappings(), methodMapping.getOperation1(), methodMapping.getOperation2());
263-
}
256+
if (ref instanceof MoveCodeRefactoring moveCodeRefactoring)
257+
mapperInfo(moveCodeRefactoring.getMappings(), moveCodeRefactoring.getSourceContainer(), moveCodeRefactoring.getTargetContainer());
258+
else if (ref instanceof SplitOperationRefactoring splitOperationRefactoring) {
259+
for (UMLOperationBodyMapper methodMapping : splitOperationRefactoring.getMappers()) {
260+
mapperInfo(methodMapping.getMappings(), methodMapping.getOperation1(), methodMapping.getOperation2());
264261
}
265-
default -> {}
266262
}
267263
});
268264
}
@@ -290,14 +286,12 @@ public void testSpecializeExpectedExceptionMappings(String url, String commit, S
290286
})
291287
public void testMergeTestMappings(String url, String commit, String testResultFileName) {
292288
testRefactoringMappings(url, commit, testResultFileName, ref -> {
293-
switch (ref) {
294-
case MoveCodeRefactoring moveCodeRefactoring -> mapperInfo(moveCodeRefactoring.getMappings(), moveCodeRefactoring.getSourceContainer(), moveCodeRefactoring.getTargetContainer());
295-
case MergeOperationRefactoring mergeOperationRefactoring -> {
296-
for (UMLOperationBodyMapper methodMapping : mergeOperationRefactoring.getMappers()) {
297-
mapperInfo(methodMapping.getMappings(), methodMapping.getOperation1(), methodMapping.getOperation2());
298-
}
289+
if (ref instanceof MoveCodeRefactoring moveCodeRefactoring)
290+
mapperInfo(moveCodeRefactoring.getMappings(), moveCodeRefactoring.getSourceContainer(), moveCodeRefactoring.getTargetContainer());
291+
else if (ref instanceof MergeOperationRefactoring mergeOperationRefactoring) {
292+
for (UMLOperationBodyMapper methodMapping : mergeOperationRefactoring.getMappers()) {
293+
mapperInfo(methodMapping.getMappings(), methodMapping.getOperation1(), methodMapping.getOperation2());
299294
}
300-
default -> {}
301295
}
302296
});
303297
}
@@ -311,14 +305,12 @@ public void testMergeTestMappings(String url, String commit, String testResultFi
311305
})
312306
public void testSplitFixturesMappings(String url, String commit, String testResultFileName) {
313307
testRefactoringMappings(url, commit, testResultFileName, ref -> {
314-
switch (ref) {
315-
case MoveCodeRefactoring moveCodeRefactoring -> mapperInfo(moveCodeRefactoring.getMappings(), moveCodeRefactoring.getSourceContainer(), moveCodeRefactoring.getTargetContainer());
316-
case SplitOperationRefactoring splitOperationRefactoring -> {
317-
for (UMLOperationBodyMapper methodMapping : splitOperationRefactoring.getMappers()) {
318-
mapperInfo(methodMapping.getMappings(), methodMapping.getOperation1(), methodMapping.getOperation2());
319-
}
308+
if (ref instanceof MoveCodeRefactoring moveCodeRefactoring)
309+
mapperInfo(moveCodeRefactoring.getMappings(), moveCodeRefactoring.getSourceContainer(), moveCodeRefactoring.getTargetContainer());
310+
else if (ref instanceof SplitOperationRefactoring splitOperationRefactoring) {
311+
for (UMLOperationBodyMapper methodMapping : splitOperationRefactoring.getMappers()) {
312+
mapperInfo(methodMapping.getMappings(), methodMapping.getOperation1(), methodMapping.getOperation2());
320313
}
321-
default -> {}
322314
}
323315
});
324316
}
@@ -332,14 +324,12 @@ public void testSplitFixturesMappings(String url, String commit, String testResu
332324
})
333325
public void testMergeFixtureMappings(String url, String commit, String testResultFileName) {
334326
testRefactoringMappings(url, commit, testResultFileName, ref -> {
335-
switch (ref) {
336-
case MoveCodeRefactoring moveCodeRefactoring -> mapperInfo(moveCodeRefactoring.getMappings(), moveCodeRefactoring.getSourceContainer(), moveCodeRefactoring.getTargetContainer());
337-
case MergeOperationRefactoring mergeOperationRefactoring -> {
338-
for (UMLOperationBodyMapper methodMapping : mergeOperationRefactoring.getMappers()) {
339-
mapperInfo(methodMapping.getMappings(), methodMapping.getOperation1(), methodMapping.getOperation2());
340-
}
327+
if (ref instanceof MoveCodeRefactoring moveCodeRefactoring)
328+
mapperInfo(moveCodeRefactoring.getMappings(), moveCodeRefactoring.getSourceContainer(), moveCodeRefactoring.getTargetContainer());
329+
else if (ref instanceof MergeOperationRefactoring mergeOperationRefactoring) {
330+
for (UMLOperationBodyMapper methodMapping : mergeOperationRefactoring.getMappers()) {
331+
mapperInfo(methodMapping.getMappings(), methodMapping.getOperation1(), methodMapping.getOperation2());
341332
}
342-
default -> {}
343333
}
344334
});
345335
}
@@ -395,11 +385,11 @@ public void testMinimizeFixture(String url, String commit, String testResultFile
395385
})
396386
public void testAddAndRemoveMethodAnnotationMappings(String url, String commit, String testResultFileName) {
397387
testRefactoringMappings(url, commit, testResultFileName, ref -> {
398-
Set<Pair<UMLAnnotation, UMLAnnotation>> annotations = switch (ref.getRefactoringType()) {
399-
case RefactoringType.REMOVE_METHOD_ANNOTATION -> Set.of(Pair.of(((AnnotationRefactoring) ref).getAnnotation(), null));
400-
case RefactoringType.ADD_METHOD_ANNOTATION -> Set.of(Pair.of(null, ((AnnotationRefactoring) ref).getAnnotation()));
401-
default -> Set.of();
402-
};
388+
Set<Pair<UMLAnnotation, UMLAnnotation>> annotations = Set.of();
389+
if (ref instanceof RemoveMethodAnnotationRefactoring)
390+
annotations = Set.of(Pair.of(((AnnotationRefactoring) ref).getAnnotation(), null));
391+
else if (ref instanceof AddMethodAnnotationRefactoring)
392+
annotations = Set.of(Pair.of(null, ((AnnotationRefactoring) ref).getAnnotation()));
403393
if (!annotations.isEmpty()) {
404394
mapperInfo(annotations, ((MethodLevelRefactoring) ref).getOperationBefore(), ((MethodLevelRefactoring) ref).getOperationAfter());
405395
}
@@ -419,22 +409,17 @@ public void testAddAndRemoveMethodAnnotationMappings(String url, String commit,
419409
})
420410
public void testChangeTestAnnotationGranularityMappings(String url, String commit, String testResultFileName) {
421411
testRefactoringMappings(url, commit, testResultFileName, ref -> {
422-
Set<Pair<UMLAnnotation, UMLAnnotation>> annotations = switch (ref.getRefactoringType()) {
423-
case RefactoringType.REMOVE_METHOD_ANNOTATION, RefactoringType.REMOVE_CLASS_ANNOTATION -> Set.of(Pair.of(((AnnotationRefactoring) ref).getAnnotation(), null));
424-
case RefactoringType.ADD_METHOD_ANNOTATION, RefactoringType.ADD_CLASS_ANNOTATION -> Set.of(Pair.of(null, ((AnnotationRefactoring) ref).getAnnotation()));
425-
default -> Set.of();
426-
};
427-
if (!annotations.isEmpty() && "Test".equals(((AnnotationRefactoring) ref).getAnnotation().getTypeName())) {
428-
mapperInfo(annotations, switch (ref) {
429-
case MethodLevelRefactoring m -> m.getOperationBefore();
430-
case ClassLevelRefactoring c -> c.getClassBefore();
431-
default -> null;
432-
}, switch (ref) {
433-
case MethodLevelRefactoring m -> m.getOperationAfter();
434-
case ClassLevelRefactoring c -> c.getClassAfter();
435-
default -> null;
436-
});
437-
}
412+
Set<Pair<UMLAnnotation, UMLAnnotation>> annotations = Set.of();
413+
UMLAnnotation annotation = ref instanceof AnnotationRefactoring ? ((AnnotationRefactoring) ref).getAnnotation() : null;
414+
if (ref instanceof RemoveMethodAnnotationRefactoring || ref instanceof RemoveClassAnnotationRefactoring)
415+
annotations = Set.of(Pair.of(annotation, null));
416+
else if (ref instanceof AddMethodAnnotationRefactoring || ref instanceof AddClassAnnotationRefactoring)
417+
annotations = Set.of(Pair.of(null, annotation));
418+
if (!annotations.isEmpty() && "Test".equals(annotation.getTypeName()))
419+
if (ref instanceof MethodLevelRefactoring m)
420+
mapperInfo(annotations, m.getOperationBefore(), m.getOperationAfter());
421+
else if (ref instanceof ClassLevelRefactoring c)
422+
mapperInfo(annotations, c.getClassBefore(), c.getClassAfter());
438423
});
439424
}
440425

@@ -447,11 +432,10 @@ public void testChangeTestAnnotationGranularityMappings(String url, String commi
447432
})
448433
public void testRenameTestMappings(String url, String commit, String testResultFileName) {
449434
testRefactoringMappings(url, commit, testResultFileName, ref -> {
450-
switch (ref) {
451-
case RenameOperationRefactoring renameTestRefactoring -> mapperInfo(renameTestRefactoring.getBodyMapper().getMappings(), renameTestRefactoring.getOriginalOperation(), renameTestRefactoring.getRenamedOperation());
452-
case RenameClassRefactoring renameTestRefactoring -> mapperInfo(Collections.emptySet(), renameTestRefactoring.getOriginalClass(), renameTestRefactoring.getRenamedClass());
453-
default -> {}
454-
}
435+
if (ref instanceof RenameOperationRefactoring renameTestRefactoring)
436+
mapperInfo(renameTestRefactoring.getBodyMapper().getMappings(), renameTestRefactoring.getOriginalOperation(), renameTestRefactoring.getRenamedOperation());
437+
else if (ref instanceof RenameClassRefactoring renameTestRefactoring)
438+
mapperInfo(Collections.emptySet(), renameTestRefactoring.getOriginalClass(), renameTestRefactoring.getRenamedClass());
455439
});
456440
}
457441

@@ -473,23 +457,20 @@ public void testRenameTestMappings(String url, String commit, String testResultF
473457
})
474458
public void testParameterizedTestMigrationMappings(String url, String commit, String testResultFileName) {
475459
testRefactoringMappings(url, commit, testResultFileName, ref -> {
476-
Set<Pair<UMLAnnotation, UMLAnnotation>> annotations = switch (ref.getRefactoringType()) {
477-
case RefactoringType.REMOVE_METHOD_ANNOTATION, RefactoringType.REMOVE_ATTRIBUTE_ANNOTATION, RefactoringType.REMOVE_CLASS_ANNOTATION -> Set.of(Pair.of(((AnnotationRefactoring) ref).getAnnotation(), null));
478-
case RefactoringType.ADD_METHOD_ANNOTATION, RefactoringType.ADD_ATTRIBUTE_ANNOTATION, RefactoringType.ADD_CLASS_ANNOTATION -> Set.of(Pair.of(null, ((AnnotationRefactoring) ref).getAnnotation()));
479-
default -> Set.of();
480-
};
481-
if (!annotations.isEmpty() && Set.of("RunWith", "Parameterized.Parameters", "ParameterizedTest", "Test", "Parameters", "Parameter", "DataProvider", "ExtendWith", "ValueSource", "NullSource", "EmptySource", "NullAndEmptySource", "EnumSource", "MethodSource", "FieldSource", "CsvSource", "CsvFileSource", "ArgumentsSource").contains(((AnnotationRefactoring) ref).getAnnotation().getTypeName())) {
482-
mapperInfo(annotations, switch (ref) {
483-
case MethodLevelRefactoring m -> m.getOperationBefore();
484-
case ClassLevelRefactoring c -> c.getClassBefore();
485-
case AttributeLevelRefactoring a -> a.getAttributeBefore();
486-
default -> null;
487-
}, switch (ref) {
488-
case MethodLevelRefactoring m -> m.getOperationAfter();
489-
case ClassLevelRefactoring c -> c.getClassAfter();
490-
case AttributeLevelRefactoring a -> a.getAttributeAfter();
491-
default -> null;
492-
});
460+
Set<String> set = Set.of("RunWith", "Parameterized.Parameters", "ParameterizedTest", "Test", "Parameters", "Parameter", "DataProvider", "ExtendWith", "ValueSource", "NullSource", "EmptySource", "NullAndEmptySource", "EnumSource", "MethodSource", "FieldSource", "CsvSource", "CsvFileSource", "ArgumentsSource");
461+
UMLAnnotation annotation = ref instanceof AnnotationRefactoring ? ((AnnotationRefactoring) ref).getAnnotation() : null;
462+
Set<Pair<UMLAnnotation, UMLAnnotation>> annotations = Set.of();
463+
if (ref instanceof RemoveMethodAnnotationRefactoring || ref instanceof RemoveAttributeAnnotationRefactoring || ref instanceof RemoveClassAnnotationRefactoring)
464+
annotations = Set.of(Pair.of(annotation, null));
465+
else if (ref instanceof AddMethodAnnotationRefactoring || ref instanceof AddAttributeAnnotationRefactoring || ref instanceof AddClassAnnotationRefactoring)
466+
annotations = Set.of(Pair.of(null, annotation));
467+
if (!annotations.isEmpty() && set.contains(annotation.getTypeName())) {
468+
if (ref instanceof MethodLevelRefactoring m)
469+
mapperInfo(annotations, m.getOperationBefore(), m.getOperationAfter());
470+
else if (ref instanceof ClassLevelRefactoring c)
471+
mapperInfo(annotations, c.getClassBefore(), c.getClassAfter());
472+
else if (ref instanceof AttributeLevelRefactoring a)
473+
mapperInfo(annotations, a.getAttributeBefore(), a.getAttributeAfter());
493474
}
494475
});
495476
}
@@ -542,12 +523,14 @@ private void assertion(String testResultFileName) {
542523
private <T, Y> void mapperInfo(Set<Y> mappings, T before, T after) {
543524
actual.add(before + " -> " + after);
544525
for (var mapping : mappings) {
545-
switch (mapping) {
546-
case AbstractCodeMapping ac -> mapperInfo(ac);
547-
case Pair p -> mapperInfo(p);
548-
case AbstractCodeFragment frag -> mapperInfo(frag);
549-
default -> throw new IllegalArgumentException("Invalid mapping type: " + mapping.getClass());
550-
}
526+
if (mapping instanceof AbstractCodeMapping ac)
527+
mapperInfo(ac);
528+
else if (mapping instanceof Pair p)
529+
mapperInfo(p);
530+
else if (mapping instanceof AbstractCodeFragment frag)
531+
mapperInfo(frag);
532+
else
533+
throw new IllegalArgumentException("Invalid mapping type: " + mapping.getClass());
551534
}
552535
}
553536

0 commit comments

Comments
 (0)