diff --git a/sandbox_common/src/org/sandbox/jdt/internal/corext/fix2/MYCleanUpConstants.java b/sandbox_common/src/org/sandbox/jdt/internal/corext/fix2/MYCleanUpConstants.java index 5873996d..38c0c3bf 100644 --- a/sandbox_common/src/org/sandbox/jdt/internal/corext/fix2/MYCleanUpConstants.java +++ b/sandbox_common/src/org/sandbox/jdt/internal/corext/fix2/MYCleanUpConstants.java @@ -123,6 +123,10 @@ public class MYCleanUpConstants { * */ public static final String JUNIT_CLEANUP_4_EXTERNALRESOURCE= "cleanup.junitcleanup_4_externalresource"; //$NON-NLS-1$ + /** + * + */ + public static final String JUNIT_CLEANUP_4_RULEEXTERNALRESOURCE= "cleanup.junitcleanup_4_ruleexternalresource"; //$NON-NLS-1$ /** * */ diff --git a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/AbstractTool.java b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/AbstractTool.java index 90eee4ab..c12c77b0 100644 --- a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/AbstractTool.java +++ b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/AbstractTool.java @@ -124,6 +124,7 @@ public abstract class AbstractTool { protected static final String ORG_JUNIT_JUPITER_API_ASSUMPTIONS= "org.junit.jupiter.api.Assumptions"; protected static final String ORG_JUNIT_ASSUME= "org.junit.Assume"; protected static final String ASSUMPTIONS= "Assumptions"; + protected static final String ORG_JUNIT_CLASS_RULE= "org.junit.ClassRule"; public static Collection getUsedVariableNames(ASTNode node) { CompilationUnit root= (CompilationUnit) node.getRoot(); @@ -140,45 +141,15 @@ protected static boolean isOfType(ITypeBinding typeBinding, String typename) { return typeBinding.getQualifiedName().equals(typename); } - private void adaptBeforeAfterCallsInTestClass(TypeDeclaration testClass, String fieldName, ASTRewrite rewriter, - AST ast, TextEditGroup group) { - testClass.accept(new ASTVisitor() { - @Override - public boolean visit(MethodInvocation node) { - if (node.getExpression() instanceof SimpleName - && ((SimpleName) node.getExpression()).getIdentifier().equals(fieldName)) { - String methodName= node.getName().getIdentifier(); - if (METHOD_BEFORE.equals(methodName)) { - // Ersetze "before()" durch "beforeEach(context)" - rewriter.replace(node.getName(), ast.newSimpleName(METHOD_BEFORE_EACH), group); - addContextParameterIfMissing(node, rewriter, ast, group); - } else if (METHOD_AFTER.equals(methodName)) { - // Ersetze "after()" durch "afterEach()" - rewriter.replace(node.getName(), ast.newSimpleName(METHOD_AFTER_EACH), group); - } - } - return super.visit(node); - } - }); - } - - private void addContextParameterIfMissing(MethodInvocation node, ASTRewrite rewriter, AST ast, - TextEditGroup group) { - if (node.arguments().isEmpty()) { - ListRewrite argsRewrite= rewriter.getListRewrite(node, MethodInvocation.ARGUMENTS_PROPERTY); - argsRewrite.insertFirst(ast.newSimpleName("context"), group); - } - } - protected Optional getInnerTypeDeclaration(FieldDeclaration fieldDeclaration) { for (Object fragment : fieldDeclaration.fragments()) { if (fragment instanceof VariableDeclarationFragment) { - VariableDeclarationFragment variableFragment = (VariableDeclarationFragment) fragment; + VariableDeclarationFragment variableFragment= (VariableDeclarationFragment) fragment; // Prüfen, ob die Initialisierung eine anonyme Klasse ist - Expression initializer = variableFragment.getInitializer(); + Expression initializer= variableFragment.getInitializer(); if (initializer instanceof ClassInstanceCreation) { - ClassInstanceCreation classInstance = (ClassInstanceCreation) initializer; + ClassInstanceCreation classInstance= (ClassInstanceCreation) initializer; // Anonyme Klasse gefunden if (classInstance.getAnonymousClassDeclaration() != null) { @@ -186,14 +157,14 @@ protected Optional getInnerTypeDeclaration(FieldDeclaration fieldDeclar } // Falls keine anonyme Klasse, den Typ der inneren Klasse prüfen - ITypeBinding typeBinding = classInstance.getType().resolveBinding(); + ITypeBinding typeBinding= classInstance.getType().resolveBinding(); if (typeBinding != null && typeBinding.isClass() && typeBinding.getJavaElement() instanceof IType) { - IType type = (IType) typeBinding.getJavaElement(); - IJavaProject javaProject = type.getJavaProject(); - String typeName = type.getElementName(); + IType type= (IType) typeBinding.getJavaElement(); + IJavaProject javaProject= type.getJavaProject(); + String typeName= type.getElementName(); // Verwende nun den Projektnamen und den Typnamen - TypeDeclaration innerTypeDecl = findTypeDeclaration(javaProject, typeName); + TypeDeclaration innerTypeDecl= findTypeDeclaration(javaProject, typeName); if (innerTypeDecl != null) { return Optional.of(innerTypeDecl); } @@ -204,44 +175,21 @@ protected Optional getInnerTypeDeclaration(FieldDeclaration fieldDeclar return Optional.empty(); // Keine innere oder anonyme Klasse gefunden } - protected void modifyExternalResourceClass(TypeDeclaration node, ASTRewrite rewriter, AST ast, TextEditGroup group, - ImportRewrite importRewriter) { - ITypeBinding binding= node.resolveBinding(); - if (binding == null || !isExternalResource(binding)) { - return; - } - if (isDirectlyExtendingExternalResource(binding)) { - refactorToImplementCallbacks(node, rewriter, ast, group, importRewriter); + private void addContextArgumentIfMissing(ASTNode node, ASTRewrite rewriter, AST ast, TextEditGroup group) { + ListRewrite argsRewrite; + if (node instanceof MethodInvocation) { + argsRewrite= rewriter.getListRewrite(node, MethodInvocation.ARGUMENTS_PROPERTY); + } else if (node instanceof SuperMethodInvocation) { + argsRewrite= rewriter.getListRewrite(node, SuperMethodInvocation.ARGUMENTS_PROPERTY); + } else { + return; // Unterstützt nur MethodInvocation und SuperMethodInvocation } - for (MethodDeclaration method : node.getMethods()) { - if (isLifecycleMethod(method, METHOD_BEFORE) || isLifecycleMethod(method, METHOD_AFTER)) { - setPublicVisibilityIfProtected(method, rewriter, ast, group); - String replacement= METHOD_BEFORE.equals(method.getName().getIdentifier()) ? METHOD_BEFORE_EACH - : METHOD_AFTER_EACH; - adaptSuperBeforeCalls(method.getName().getIdentifier(), replacement, method, rewriter, ast, group); - if (METHOD_BEFORE.equals(method.getName().getIdentifier())) { - removeThrowsThrowable(method, rewriter, group); - } - refactorMethod(rewriter, ast, method, replacement, group, importRewriter); - } - } -// importRewriter.removeImport(ORG_JUNIT_RULE); - } - private void updateLifecycleMethods(TypeDeclaration typeDecl, ASTRewrite rewrite, AST ast, TextEditGroup group, - ImportRewrite importRewrite) { - for (MethodDeclaration method : typeDecl.getMethods()) { - String methodName= method.getName().getIdentifier(); - if (METHOD_BEFORE.equals(methodName) || METHOD_AFTER.equals(methodName)) { - setPublicVisibilityIfProtected(method, rewrite, ast, group); - String replacement= METHOD_BEFORE.equals(methodName) ? METHOD_BEFORE_EACH : METHOD_AFTER_EACH; - adaptSuperBeforeCalls(methodName, replacement, method, rewrite, ast, group); - if (METHOD_BEFORE.equals(methodName)) { - removeThrowsThrowable(method, rewrite, group); - } - rewrite.replace(method.getName(), ast.newSimpleName(replacement), group); - ensureExtensionContextParameter(method, rewrite, ast, group, importRewrite); - } + boolean hasContextArgument= argsRewrite.getRewrittenList().stream().anyMatch( + arg -> arg instanceof SimpleName && ((SimpleName) arg).getIdentifier().equals(VARIABLE_NAME_CONTEXT)); + + if (!hasContextArgument) { + argsRewrite.insertFirst(ast.newSimpleName(VARIABLE_NAME_CONTEXT), group); } } @@ -278,6 +226,103 @@ private TypeDeclaration findTypeDeclarationInProject(ITypeBinding typeBinding) { return type != null ? findTypeDeclaration(type.getJavaProject(), type.getElementName()) : null; } + private void processLifecycleMethod(MethodDeclaration method, ASTRewrite rewriter, AST ast, TextEditGroup group, + ImportRewrite importRewriter, boolean isBefore) { + String oldName= isBefore ? METHOD_BEFORE : METHOD_AFTER; + String newName= isBefore ? METHOD_BEFORE_EACH : METHOD_AFTER_EACH; + + setPublicVisibilityIfProtected(method, rewriter, ast, group); + adaptSuperBeforeCalls(oldName, newName, method, rewriter, ast, group); + + if (isBefore) { + removeThrowsThrowable(method, rewriter, group); + } + + rewriter.replace(method.getName(), ast.newSimpleName(newName), group); + ensureExtensionContextParameter(method, rewriter, ast, group, importRewriter); + } + + private void processMethodInvocation(MethodInvocation node, String fieldName, ASTRewrite rewriter, AST ast, + TextEditGroup group) { + if (node.getExpression() instanceof SimpleName + && ((SimpleName) node.getExpression()).getIdentifier().equals(fieldName)) { + String methodName= node.getName().getIdentifier(); + if (METHOD_BEFORE.equals(methodName)) { + rewriter.replace(node.getName(), ast.newSimpleName(METHOD_BEFORE_EACH), group); + addContextArgumentIfMissing(node, rewriter, ast, group); + } else if (METHOD_AFTER.equals(methodName)) { + rewriter.replace(node.getName(), ast.newSimpleName(METHOD_AFTER_EACH), group); + } + } + } + + private void adaptBeforeAfterCallsInTestClass(TypeDeclaration testClass, String fieldName, ASTRewrite rewriter, + AST ast, TextEditGroup group) { + testClass.accept(new ASTVisitor() { + @Override + public boolean visit(MethodInvocation node) { + processMethodInvocation(node, fieldName, rewriter, ast, group); + return super.visit(node); + } + }); + } + + protected void modifyExternalResourceClass(TypeDeclaration node, ASTRewrite rewriter, AST ast, TextEditGroup group, + ImportRewrite importRewriter) { + if (!shouldProcessNode(node)) { + return; + } + + if (isDirectlyExtendingExternalResource(node.resolveBinding())) { + refactorToImplementCallbacks(node, rewriter, ast, group, importRewriter); + } + + updateLifecycleMethodsInClass(node, rewriter, ast, group, importRewriter); + } + + private boolean shouldProcessNode(TypeDeclaration node) { + ITypeBinding binding= node.resolveBinding(); + return binding != null && isExternalResource(binding); + } + + private void processBeforeMethod(MethodDeclaration method, ASTRewrite rewriter, AST ast, TextEditGroup group, + ImportRewrite importRewriter) { + setPublicVisibilityIfProtected(method, rewriter, ast, group); + adaptSuperBeforeCalls(METHOD_BEFORE, METHOD_BEFORE_EACH, method, rewriter, ast, group); + removeThrowsThrowable(method, rewriter, group); + rewriter.replace(method.getName(), ast.newSimpleName(METHOD_BEFORE_EACH), group); + ensureExtensionContextParameter(method, rewriter, ast, group, importRewriter); + } + + private void processAfterMethod(MethodDeclaration method, ASTRewrite rewriter, AST ast, TextEditGroup group, + ImportRewrite importRewriter) { + setPublicVisibilityIfProtected(method, rewriter, ast, group); + adaptSuperBeforeCalls(METHOD_AFTER, METHOD_AFTER_EACH, method, rewriter, ast, group); + rewriter.replace(method.getName(), ast.newSimpleName(METHOD_AFTER_EACH), group); + ensureExtensionContextParameter(method, rewriter, ast, group, importRewriter); + } + + private void updateLifecycleMethodsInClass(TypeDeclaration node, ASTRewrite rewriter, AST ast, TextEditGroup group, + ImportRewrite importRewriter) { + for (MethodDeclaration method : node.getMethods()) { + if (isLifecycleMethod(method, METHOD_BEFORE)) { + processBeforeMethod(method, rewriter, ast, group, importRewriter); + } else if (isLifecycleMethod(method, METHOD_AFTER)) { + processAfterMethod(method, rewriter, ast, group, importRewriter); + } + } + } + + private void updateLifecycleMethods(TypeDeclaration typeDecl, ASTRewrite rewrite, AST ast, TextEditGroup group, + ImportRewrite importRewrite) { + for (MethodDeclaration method : typeDecl.getMethods()) { + if (isLifecycleMethod(method, METHOD_BEFORE) || isLifecycleMethod(method, METHOD_AFTER)) { + processLifecycleMethod(method, rewrite, ast, group, importRewrite, + METHOD_BEFORE.equals(method.getName().getIdentifier())); + } + } + } + private void adaptTypeDeclaration(TypeDeclaration typeDecl, ASTRewrite rewrite, AST ast, ImportRewrite importRewrite, TextEditGroup group) { removeSuperclassType(typeDecl, rewrite, group); @@ -287,6 +332,24 @@ private void adaptTypeDeclaration(TypeDeclaration typeDecl, ASTRewrite rewrite, importRewrite.addImport(ORG_JUNIT_JUPITER_API_EXTENSION_AFTER_EACH_CALLBACK); } + private void refactorToImplementCallbacks(TypeDeclaration node, ASTRewrite rewriter, AST ast, TextEditGroup group, + ImportRewrite importRewriter) { + rewriter.remove(node.getSuperclassType(), group); + importRewriter.removeImport(ORG_JUNIT_RULES_EXTERNAL_RESOURCE); + ListRewrite listRewrite= rewriter.getListRewrite(node, TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY); + addInterfaceCallback(listRewrite, ast, BEFORE_EACH_CALLBACK, group); + addInterfaceCallback(listRewrite, ast, AFTER_EACH_CALLBACK, group); + importRewriter.addImport(ORG_JUNIT_JUPITER_API_EXTENSION_BEFORE_EACH_CALLBACK); + importRewriter.addImport(ORG_JUNIT_JUPITER_API_EXTENSION_AFTER_EACH_CALLBACK); + } + + private void addBeforeAndAfterEachCallbacks(TypeDeclaration typeDecl, ASTRewrite rewrite, AST ast, + ImportRewrite importRewrite, TextEditGroup group) { + ListRewrite listRewrite= rewrite.getListRewrite(typeDecl, TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY); + listRewrite.insertLast(ast.newSimpleType(ast.newName(BEFORE_EACH_CALLBACK)), group); + listRewrite.insertLast(ast.newSimpleType(ast.newName(AFTER_EACH_CALLBACK)), group); + } + private void adaptSuperBeforeCalls(String vorher, String nachher, MethodDeclaration method, ASTRewrite rewriter, AST ast, TextEditGroup group) { method.accept(new ASTVisitor() { @@ -294,28 +357,13 @@ private void adaptSuperBeforeCalls(String vorher, String nachher, MethodDeclarat public boolean visit(SuperMethodInvocation node) { if (vorher.equals(node.getName().getIdentifier())) { rewriter.replace(node.getName(), ast.newSimpleName(nachher), group); - addContextArgumentIfAbsent(node, rewriter, ast, group); + addContextArgumentIfMissing(node, rewriter, ast, group); } return super.visit(node); } }); } - private void addContextArgumentIfAbsent(SuperMethodInvocation node, ASTRewrite rewriter, AST ast, - TextEditGroup group) { - if (node.arguments().isEmpty()) { - rewriter.getListRewrite(node, SuperMethodInvocation.ARGUMENTS_PROPERTY) - .insertFirst(ast.newSimpleName(VARIABLE_NAME_CONTEXT), group); - } - } - - private void addBeforeAndAfterEachCallbacks(TypeDeclaration typeDecl, ASTRewrite rewrite, AST ast, - ImportRewrite importRewrite, TextEditGroup group) { - ListRewrite listRewrite= rewrite.getListRewrite(typeDecl, TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY); - listRewrite.insertLast(ast.newSimpleType(ast.newName(BEFORE_EACH_CALLBACK)), group); - listRewrite.insertLast(ast.newSimpleType(ast.newName(AFTER_EACH_CALLBACK)), group); - } - protected void addExtendWithAnnotation(ASTRewrite rewrite, AST ast, TextEditGroup group, ImportRewrite importRewriter, String className, FieldDeclaration field) { TypeDeclaration parentClass= getParentTypeDeclaration(field); @@ -347,33 +395,78 @@ protected Name addImport(String typeName, final CompilationUnitRewrite cuRewrite return ast.newName(importedName); } + private void manageImport(ImportRewrite importRewriter, String typeName, boolean add) { + if (add) { + importRewriter.addImport(typeName); + } else { + importRewriter.removeImport(typeName); + } + } + private void addInterfaceCallback(ListRewrite listRewrite, AST ast, String callbackName, TextEditGroup group) { - listRewrite.insertLast(ast.newSimpleType(ast.newName(callbackName)), group); + // Prüfen, ob das Interface bereits in der Liste existiert + boolean hasCallback= listRewrite.getRewrittenList().stream().anyMatch(type -> type instanceof SimpleType + && ((SimpleType) type).getName().getFullyQualifiedName().equals(callbackName)); + + if (!hasCallback) { + // Interface hinzufügen, wenn es noch nicht existiert + listRewrite.insertLast(ast.newSimpleType(ast.newName(callbackName)), group); + } } private void addRegisterExtensionAnnotation(FieldDeclaration field, ASTRewrite rewrite, AST ast, ImportRewrite importRewrite, TextEditGroup group) { - MarkerAnnotation registerExtensionAnnotation= ast.newMarkerAnnotation(); - registerExtensionAnnotation.setTypeName(ast.newName(ANNOTATION_REGISTER_EXTENSION)); - rewrite.getListRewrite(field, FieldDeclaration.MODIFIERS2_PROPERTY).insertFirst(registerExtensionAnnotation, - group); + // Prüfen, ob die Annotation bereits existiert + boolean hasRegisterExtension= field.modifiers().stream().anyMatch(modifier -> modifier instanceof Annotation + && ((Annotation) modifier).getTypeName().getFullyQualifiedName().equals(ANNOTATION_REGISTER_EXTENSION)); + + // Prüfen, ob die Annotation bereits im Rewrite hinzugefügt wurde + ListRewrite listRewrite= rewrite.getListRewrite(field, FieldDeclaration.MODIFIERS2_PROPERTY); + boolean hasPendingRegisterExtension= listRewrite.getRewrittenList().stream() + .anyMatch(rewritten -> rewritten instanceof MarkerAnnotation && ((MarkerAnnotation) rewritten) + .getTypeName().getFullyQualifiedName().equals(ANNOTATION_REGISTER_EXTENSION)); + + if (!hasRegisterExtension && !hasPendingRegisterExtension) { + // Annotation hinzufügen, wenn sie weder im AST noch im Rewrite existiert + MarkerAnnotation registerExtensionAnnotation= ast.newMarkerAnnotation(); + registerExtensionAnnotation.setTypeName(ast.newName(ANNOTATION_REGISTER_EXTENSION)); + listRewrite.insertFirst(registerExtensionAnnotation, group); + + // Import hinzufügen + importRewrite.addImport(ORG_JUNIT_JUPITER_API_EXTENSION_REGISTER_EXTENSION); + } } private void ensureExtensionContextParameter(MethodDeclaration method, ASTRewrite rewrite, AST ast, TextEditGroup group, ImportRewrite importRewrite) { + + // Prüfen, ob ExtensionContext bereits existiert (im AST oder im Rewrite) boolean hasExtensionContext= method.parameters().stream() .anyMatch(param -> param instanceof SingleVariableDeclaration - && ((SingleVariableDeclaration) param).getType().toString().equals(EXTENSION_CONTEXT)); + && isExtensionContext((SingleVariableDeclaration) param)) + || rewrite.getListRewrite(method, MethodDeclaration.PARAMETERS_PROPERTY).getRewrittenList().stream() + .anyMatch(param -> param instanceof SingleVariableDeclaration + && ((SingleVariableDeclaration) param).getType().toString().equals(EXTENSION_CONTEXT)); + if (!hasExtensionContext) { + // Neuen Parameter hinzufügen SingleVariableDeclaration newParam= ast.newSingleVariableDeclaration(); newParam.setType(ast.newSimpleType(ast.newName(EXTENSION_CONTEXT))); newParam.setName(ast.newSimpleName(VARIABLE_NAME_CONTEXT)); ListRewrite listRewrite= rewrite.getListRewrite(method, MethodDeclaration.PARAMETERS_PROPERTY); listRewrite.insertLast(newParam, group); + + // Import hinzufügen importRewrite.addImport(ORG_JUNIT_JUPITER_API_EXTENSION_EXTENSION_CONTEXT); } } + // Hilfsmethode zum Vergleich des Typs + private boolean isExtensionContext(SingleVariableDeclaration param) { + ITypeBinding binding= param.getType().resolveBinding(); + return binding != null && ORG_JUNIT_JUPITER_API_EXTENSION_EXTENSION_CONTEXT.equals(binding.getQualifiedName()); + } + public String extractClassNameFromField(FieldDeclaration field) { for (Object fragmentObj : field.fragments()) { VariableDeclarationFragment fragment= (VariableDeclarationFragment) fragmentObj; @@ -504,10 +597,12 @@ private boolean isStringType(Expression expression) { return typeBinding != null && String.class.getCanonicalName().equals(typeBinding.getQualifiedName()); } - public void migrateRuleToRegisterExtensionAndAdaptHierarchy(Optional innerTypeDeclaration, TypeDeclaration testClass, ASTRewrite rewrite, AST ast, - ImportRewrite importRewrite, TextEditGroup group, String varname) { - if(innerTypeDeclaration.isPresent() && innerTypeDeclaration.get() instanceof TypeDeclaration) { - adaptBeforeAfterCallsInTestClass((TypeDeclaration) innerTypeDeclaration.get(), varname, rewrite, ast, group); + public void migrateRuleToRegisterExtensionAndAdaptHierarchy(Optional innerTypeDeclaration, + TypeDeclaration testClass, ASTRewrite rewrite, AST ast, ImportRewrite importRewrite, TextEditGroup group, + String varname) { + if (innerTypeDeclaration.isPresent() && innerTypeDeclaration.get() instanceof TypeDeclaration) { + adaptBeforeAfterCallsInTestClass((TypeDeclaration) innerTypeDeclaration.get(), varname, rewrite, ast, + group); } for (FieldDeclaration field : testClass.getFields()) { if (isAnnotatedWithRule(field) && isExternalResource(field)) { @@ -548,23 +643,6 @@ public void process(Annotation node, IJavaProject jproject, ASTRewrite rewrite, importRewriter.removeImport(ORG_JUNIT_RULES_EXTERNAL_RESOURCE); } - private void refactorMethod(ASTRewrite rewriter, AST ast, MethodDeclaration method, String newMethodName, - TextEditGroup group, ImportRewrite importRewriter) { - rewriter.replace(method.getName(), ast.newSimpleName(newMethodName), group); - ensureExtensionContextParameter(method, rewriter, ast, group, importRewriter); - } - - private void refactorToImplementCallbacks(TypeDeclaration node, ASTRewrite rewriter, AST ast, TextEditGroup group, - ImportRewrite importRewriter) { - rewriter.remove(node.getSuperclassType(), group); - importRewriter.removeImport(ORG_JUNIT_RULES_EXTERNAL_RESOURCE); - ListRewrite listRewrite= rewriter.getListRewrite(node, TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY); - addInterfaceCallback(listRewrite, ast, BEFORE_EACH_CALLBACK, group); - addInterfaceCallback(listRewrite, ast, AFTER_EACH_CALLBACK, group); - importRewriter.addImport(ORG_JUNIT_JUPITER_API_EXTENSION_BEFORE_EACH_CALLBACK); - importRewriter.addImport(ORG_JUNIT_JUPITER_API_EXTENSION_AFTER_EACH_CALLBACK); - } - private void removeRuleAnnotation(BodyDeclaration declaration, ASTRewrite rewriter, TextEditGroup group, ImportRewrite importRewriter) { List modifiers= declaration.modifiers(); diff --git a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/fix/JUnitCleanUpCore.java b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/fix/JUnitCleanUpCore.java index fdfe5c53..de86cc96 100644 --- a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/fix/JUnitCleanUpCore.java +++ b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/fix/JUnitCleanUpCore.java @@ -72,8 +72,7 @@ public ICleanUpFix createFix(final CleanUpContext context) throws CoreException return null; } Set operations= new LinkedHashSet<>(); - Set nodesprocessed= new HashSet<>(); - computeFixSet.forEach(i -> i.findOperations(compilationUnit, operations, nodesprocessed)); + computeFixSet.forEach(i -> i.findOperations(compilationUnit, operations, new HashSet())); if (operations.isEmpty()) { return null; } @@ -105,17 +104,20 @@ private EnumSet computeFixSet() { EnumSet fixSet = isEnabled(JUNIT_CLEANUP) ? EnumSet.allOf(JUnitCleanUpFixCore.class) : EnumSet.noneOf(JUnitCleanUpFixCore.class); - Map cleanupMappings = Map.of( - MYCleanUpConstants.JUNIT_CLEANUP_4_ASSERT, JUnitCleanUpFixCore.ASSERT, - MYCleanUpConstants.JUNIT_CLEANUP_4_ASSUME, JUnitCleanUpFixCore.ASSUME, - MYCleanUpConstants.JUNIT_CLEANUP_4_AFTER, JUnitCleanUpFixCore.AFTER, - MYCleanUpConstants.JUNIT_CLEANUP_4_BEFORE, JUnitCleanUpFixCore.BEFORE, - MYCleanUpConstants.JUNIT_CLEANUP_4_AFTERCLASS, JUnitCleanUpFixCore.AFTERCLASS, - MYCleanUpConstants.JUNIT_CLEANUP_4_BEFORECLASS, JUnitCleanUpFixCore.BEFORECLASS, - MYCleanUpConstants.JUNIT_CLEANUP_4_TEST, JUnitCleanUpFixCore.TEST, - MYCleanUpConstants.JUNIT_CLEANUP_4_IGNORE, JUnitCleanUpFixCore.IGNORE, - MYCleanUpConstants.JUNIT_CLEANUP_4_RULETEMPORARYFOLDER, JUnitCleanUpFixCore.RULETEMPORARYFOLDER, - MYCleanUpConstants.JUNIT_CLEANUP_4_RULETESTNAME, JUnitCleanUpFixCore.RULETESTNAME + Map cleanupMappings = Map.ofEntries( + Map.entry(MYCleanUpConstants.JUNIT_CLEANUP_4_ASSERT, JUnitCleanUpFixCore.ASSERT), + Map.entry(MYCleanUpConstants.JUNIT_CLEANUP_4_ASSUME, JUnitCleanUpFixCore.ASSUME), + Map.entry(MYCleanUpConstants.JUNIT_CLEANUP_4_AFTER, JUnitCleanUpFixCore.AFTER), + Map.entry(MYCleanUpConstants.JUNIT_CLEANUP_4_BEFORE, JUnitCleanUpFixCore.BEFORE), + Map.entry(MYCleanUpConstants.JUNIT_CLEANUP_4_AFTERCLASS, JUnitCleanUpFixCore.AFTERCLASS), + Map.entry(MYCleanUpConstants.JUNIT_CLEANUP_4_BEFORECLASS, JUnitCleanUpFixCore.BEFORECLASS), + Map.entry(MYCleanUpConstants.JUNIT_CLEANUP_4_TEST, JUnitCleanUpFixCore.TEST), + Map.entry(MYCleanUpConstants.JUNIT_CLEANUP_4_IGNORE, JUnitCleanUpFixCore.IGNORE), + Map.entry(MYCleanUpConstants.JUNIT_CLEANUP_4_RUNWITH, JUnitCleanUpFixCore.RULETEMPORARYFOLDER), + Map.entry(MYCleanUpConstants.JUNIT_CLEANUP_4_EXTERNALRESOURCE, JUnitCleanUpFixCore.EXTERNALRESOURCE), + Map.entry(MYCleanUpConstants.JUNIT_CLEANUP_4_RULETEMPORARYFOLDER, JUnitCleanUpFixCore.RUNWITH), + Map.entry(MYCleanUpConstants.JUNIT_CLEANUP_4_RULETESTNAME, JUnitCleanUpFixCore.RULETESTNAME), + Map.entry(MYCleanUpConstants.JUNIT_CLEANUP_4_RULEEXTERNALRESOURCE, JUnitCleanUpFixCore.RULEEXTERNALRESOURCE) ); cleanupMappings.forEach((config, fix) -> { if (!isEnabled(config)) { diff --git a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/preferences/cleanup/CleanUpMessages.java b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/preferences/cleanup/CleanUpMessages.java index 16702af2..26970dbc 100644 --- a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/preferences/cleanup/CleanUpMessages.java +++ b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/preferences/cleanup/CleanUpMessages.java @@ -29,6 +29,7 @@ public class CleanUpMessages { public static String JavaFeatureTabPage_CheckboxName_JUNIT_CLEANUP_AFTERCLASS; public static String JavaFeatureTabPage_CheckboxName_JUNIT_CLEANUP_RULETEMPORARYFOLDER; public static String JavaFeatureTabPage_CheckboxName_JUNIT_CLEANUP_RULETESTNAME; + public static String JavaFeatureTabPage_CheckboxName_JUNIT_CLEANUP_RULEEXTERNALRESOURCE; public static String JavaFeatureTabPage_CheckboxName_JUNIT_CLEANUP_EXTERNALRESOURCE; public static String JavaFeatureTabPage_CheckboxName_JUNIT_CLEANUP_RUNWITH; diff --git a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/preferences/cleanup/CleanUpMessages.properties b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/preferences/cleanup/CleanUpMessages.properties index 3e8c0052..07dcb574 100644 --- a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/preferences/cleanup/CleanUpMessages.properties +++ b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/preferences/cleanup/CleanUpMessages.properties @@ -9,6 +9,7 @@ JavaFeatureTabPage_CheckboxName_JUNIT_CLEANUP_BEFORECLASS=BeforeClass JavaFeatureTabPage_CheckboxName_JUNIT_CLEANUP_AFTERCLASS=AfterClass JavaFeatureTabPage_CheckboxName_JUNIT_CLEANUP_RULETEMPORARYFOLDER=Rule TemporaryFolder JavaFeatureTabPage_CheckboxName_JUNIT_CLEANUP_RULETESTNAME=Rule Testname +JavaFeatureTabPage_CheckboxName_JUNIT_CLEANUP_RULEEXTERNALRESOURCE=RuleExternalResource JavaFeatureTabPage_CheckboxName_JUNIT_CLEANUP_EXTERNALRESOURCE=ExternalResource JavaFeatureTabPage_CheckboxName_JUNIT_CLEANUP_RUNWITH=RunWith JavaFeatureTabPage_GroupName_JUnit=JUnit \ No newline at end of file diff --git a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/preferences/cleanup/DefaultCleanUpOptionsInitializer.java b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/preferences/cleanup/DefaultCleanUpOptionsInitializer.java index ec7a8f4e..1d9745dc 100644 --- a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/preferences/cleanup/DefaultCleanUpOptionsInitializer.java +++ b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/preferences/cleanup/DefaultCleanUpOptionsInitializer.java @@ -32,6 +32,7 @@ public void setDefaultOptions(CleanUpOptions options) { options.setOption(MYCleanUpConstants.JUNIT_CLEANUP_4_TEST, CleanUpOptions.FALSE); options.setOption(MYCleanUpConstants.JUNIT_CLEANUP_4_RULETESTNAME, CleanUpOptions.FALSE); options.setOption(MYCleanUpConstants.JUNIT_CLEANUP_4_RULETEMPORARYFOLDER, CleanUpOptions.FALSE); + options.setOption(MYCleanUpConstants.JUNIT_CLEANUP_4_RULEEXTERNALRESOURCE, CleanUpOptions.FALSE); options.setOption(MYCleanUpConstants.JUNIT_CLEANUP_4_EXTERNALRESOURCE, CleanUpOptions.FALSE); options.setOption(MYCleanUpConstants.JUNIT_CLEANUP_4_RUNWITH, CleanUpOptions.FALSE); } diff --git a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/preferences/cleanup/SandboxCodeTabPage.java b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/preferences/cleanup/SandboxCodeTabPage.java index 2b6f209f..5125362d 100644 --- a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/preferences/cleanup/SandboxCodeTabPage.java +++ b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/preferences/cleanup/SandboxCodeTabPage.java @@ -89,6 +89,10 @@ protected void doCreatePreferences(Composite composite, int numColumns) { CleanUpMessages.JavaFeatureTabPage_CheckboxName_JUNIT_CLEANUP_EXTERNALRESOURCE, MYCleanUpConstants.JUNIT_CLEANUP_4_EXTERNALRESOURCE, FALSE_TRUE); intent(junitGroup); + final CheckboxPreference junit_ruleexternalresource= createCheckboxPref(junitGroup, numColumns-1, + CleanUpMessages.JavaFeatureTabPage_CheckboxName_JUNIT_CLEANUP_RULEEXTERNALRESOURCE, MYCleanUpConstants.JUNIT_CLEANUP_4_RULEEXTERNALRESOURCE, + FALSE_TRUE); + intent(junitGroup); final CheckboxPreference junit_runwith= createCheckboxPref(junitGroup, numColumns-1, CleanUpMessages.JavaFeatureTabPage_CheckboxName_JUNIT_CLEANUP_RUNWITH, MYCleanUpConstants.JUNIT_CLEANUP_4_RUNWITH, FALSE_TRUE); @@ -103,6 +107,7 @@ protected void doCreatePreferences(Composite composite, int numColumns) { junit_afterclass, junit_ruletempfolder, junit_ruletestname, + junit_ruleexternalresource, junit_externalresource, junit_runwith}); intent(junitGroup); diff --git a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/preferences/cleanup/SaveActionCleanUpOptionsInitializer.java b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/preferences/cleanup/SaveActionCleanUpOptionsInitializer.java index f24a4f33..93dd7c76 100644 --- a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/preferences/cleanup/SaveActionCleanUpOptionsInitializer.java +++ b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/preferences/cleanup/SaveActionCleanUpOptionsInitializer.java @@ -32,6 +32,7 @@ public void setDefaultOptions(CleanUpOptions options) { options.setOption(MYCleanUpConstants.JUNIT_CLEANUP_4_TEST, CleanUpOptions.FALSE); options.setOption(MYCleanUpConstants.JUNIT_CLEANUP_4_RULETESTNAME, CleanUpOptions.FALSE); options.setOption(MYCleanUpConstants.JUNIT_CLEANUP_4_RULETEMPORARYFOLDER, CleanUpOptions.FALSE); + options.setOption(MYCleanUpConstants.JUNIT_CLEANUP_4_RULEEXTERNALRESOURCE, CleanUpOptions.FALSE); options.setOption(MYCleanUpConstants.JUNIT_CLEANUP_4_EXTERNALRESOURCE, CleanUpOptions.FALSE); options.setOption(MYCleanUpConstants.JUNIT_CLEANUP_4_RUNWITH, CleanUpOptions.FALSE); } diff --git a/sandbox_junit_cleanup_test/src/org/eclipse/jdt/ui/tests/quickfix/Java8/JUnitCleanupCases.java b/sandbox_junit_cleanup_test/src/org/eclipse/jdt/ui/tests/quickfix/Java8/JUnitCleanupCases.java new file mode 100644 index 00000000..0391e3d1 --- /dev/null +++ b/sandbox_junit_cleanup_test/src/org/eclipse/jdt/ui/tests/quickfix/Java8/JUnitCleanupCases.java @@ -0,0 +1,969 @@ +package org.eclipse.jdt.ui.tests.quickfix.Java8; + +enum JUnitCleanupCases{ + PositiveCase(""" +package test; + +import org.hamcrest.CoreMatchers; +import org.junit.Assert; +import org.junit.Assume; +import org.junit.Before; +import org.junit.After; +import org.junit.BeforeClass; +import org.junit.AfterClass; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +@RunWith(Suite.class) +@Suite.SuiteClasses({ + MyTest.class +}) +public class MyTest { + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + // Setup vor allen Tests + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + // Aufräumen nach allen Tests + } + + @Before + public void setUp() throws Exception { + // Setup vor jedem Test + } + + @After + public void tearDown() throws Exception { + // Aufräumen nach jedem Test + } + + @Ignore("Ignored with message") + @Test + public void ignoredTestWithMessage() { + Assert.fail("This test is ignored with a message."); + } + + @Ignore + @Test + public void ignoredTestWithoutMessage() { + Assert.fail("This test is ignored without a message."); + } + + @Test + public void testBasicAssertions() { + Assert.assertEquals("Values should match", 42, 42); + Assert.assertEquals(42, 42); + + Assert.assertNotEquals("Values should not match", 42, 43); + Assert.assertNotEquals(42, 43); + + Object o = "test"; + Assert.assertSame("Objects should be same", o, o); + Assert.assertSame(o, o); + + Assert.assertNotSame("Objects should not be same", "test1", "test2"); + Assert.assertNotSame("test1", "test2"); + + Assert.assertNull("Should be null", null); + Assert.assertNull(null); + + Assert.assertNotNull("Should not be null", new Object()); + Assert.assertNotNull(new Object()); + + Assert.assertTrue("Condition should be true", true); + Assert.assertTrue(true); + + Assert.assertFalse("Condition should be false", false); + Assert.assertFalse(false); + } + + @Test + public void testArrayAssertions() { + int[] expected = {1, 2, 3}; + int[] actual = {1, 2, 3}; + + Assert.assertArrayEquals("Arrays should match", expected, actual); + Assert.assertArrayEquals(expected, actual); + } + + @Test + public void testWithAssume() { + Assume.assumeTrue("Precondition failed", true); + Assume.assumeTrue(true); + + Assume.assumeFalse("Precondition not met", false); + Assume.assumeFalse(false); + + Assume.assumeNotNull("Value should not be null", new Object()); + Assume.assumeNotNull(new Object()); + + Assume.assumeThat("Value should match condition", 42, CoreMatchers.is(42)); + Assume.assumeThat(42, CoreMatchers.is(42)); + } + + @Test + public void testAssertThat() { + Assert.assertThat("Value should match", 42, CoreMatchers.is(42)); + Assert.assertThat(42, CoreMatchers.is(42)); + + Assert.assertThat("String should contain value", "Hello, world!", CoreMatchers.containsString("world")); + Assert.assertThat("Hello, world!", CoreMatchers.containsString("world")); + } + + @Test + public void testEqualityWithDelta() { + Assert.assertEquals("Floating point equality with delta", 0.1 + 0.2, 0.3, 0.0001); + Assert.assertEquals(0.1 + 0.2, 0.3, 0.0001); + + Assert.assertNotEquals("Floating point inequality", 0.1 + 0.2, 0.4, 0.0001); + Assert.assertNotEquals(0.1 + 0.2, 0.4, 0.0001); + } + + @Test + public void testFail() { + // Testfälle für Assert.fail + Assert.fail("This test should fail with a message."); + Assert.fail(); + } +} +""", //$NON-NLS-1$ + +""" +package test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.junit.MatcherAssume.assumeThat; + +import org.hamcrest.CoreMatchers; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.junit.platform.suite.api.SelectClasses; +import org.junit.platform.suite.api.Suite; + +@Suite +@SelectClasses({ + MyTest.class +}) +public class MyTest { + + @BeforeAll + public static void setUpBeforeClass() throws Exception { + // Setup vor allen Tests + } + + @AfterAll + public static void tearDownAfterClass() throws Exception { + // Aufräumen nach allen Tests + } + + @BeforeEach + public void setUp() throws Exception { + // Setup vor jedem Test + } + + @AfterEach + public void tearDown() throws Exception { + // Aufräumen nach jedem Test + } + + @Disabled("Ignored with message") + @Test + public void ignoredTestWithMessage() { + Assertions.fail("This test is ignored with a message."); + } + + @Disabled + @Test + public void ignoredTestWithoutMessage() { + Assertions.fail("This test is ignored without a message."); + } + + @Test + public void testBasicAssertions() { + Assertions.assertEquals(42, 42, "Values should match"); + Assertions.assertEquals(42, 42); + + Assertions.assertNotEquals(42, 43, "Values should not match"); + Assertions.assertNotEquals(42, 43); + + Object o = "test"; + Assertions.assertSame(o, o, "Objects should be same"); + Assertions.assertSame(o, o); + + Assertions.assertNotSame("test1", "test2", "Objects should not be same"); + Assertions.assertNotSame("test1", "test2"); + + Assertions.assertNull(null, "Should be null"); + Assertions.assertNull(null); + + Assertions.assertNotNull(new Object(), "Should not be null"); + Assertions.assertNotNull(new Object()); + + Assertions.assertTrue(true, "Condition should be true"); + Assertions.assertTrue(true); + + Assertions.assertFalse(false, "Condition should be false"); + Assertions.assertFalse(false); + } + + @Test + public void testArrayAssertions() { + int[] expected = {1, 2, 3}; + int[] actual = {1, 2, 3}; + + Assertions.assertArrayEquals(expected, actual, "Arrays should match"); + Assertions.assertArrayEquals(expected, actual); + } + + @Test + public void testWithAssume() { + Assumptions.assumeTrue(true, "Precondition failed"); + Assumptions.assumeTrue(true); + + Assumptions.assumeFalse(false, "Precondition not met"); + Assumptions.assumeFalse(false); + + Assumptions.assumeNotNull(new Object(), "Value should not be null"); + Assumptions.assumeNotNull(new Object()); + + assumeThat("Value should match condition", 42, CoreMatchers.is(42)); + assumeThat(42, CoreMatchers.is(42)); + } + + @Test + public void testAssertThat() { + assertThat("Value should match", 42, CoreMatchers.is(42)); + assertThat(42, CoreMatchers.is(42)); + + assertThat("String should contain value", "Hello, world!", CoreMatchers.containsString("world")); + assertThat("Hello, world!", CoreMatchers.containsString("world")); + } + + @Test + public void testEqualityWithDelta() { + Assertions.assertEquals(0.1 + 0.2, 0.3, 0.0001, "Floating point equality with delta"); + Assertions.assertEquals(0.1 + 0.2, 0.3, 0.0001); + + Assertions.assertNotEquals(0.1 + 0.2, 0.4, 0.0001, "Floating point inequality"); + Assertions.assertNotEquals(0.1 + 0.2, 0.4, 0.0001); + } + + @Test + public void testFail() { + // Testfälle für Assert.fail + Assertions.fail("This test should fail with a message."); + Assertions.fail(); + } +} +"""), + AlreadyJunit5Case( +""" +package test; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; +/** + * + */ +public class MyTest { + + @Test + public void test3() { + assertEquals("expected", "actual"); + } +} +""", //$NON-NLS-1$ +""" +package test; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; +/** + * + */ +public class MyTest { + + @Test + public void test3() { + assertEquals("expected", "actual"); + } +} +"""), + StaticImportCase(""" + package test; + import static org.junit.Assert.*; + + import org.junit.After; + import org.junit.AfterClass; + import org.junit.Before; + import org.junit.BeforeClass; + import org.junit.Ignore; + import org.junit.Test; + import org.junit.runner.RunWith; + import org.junit.runners.Suite; + + /** + * + */ + @RunWith(Suite.class) + @Suite.SuiteClasses({ + MyTest.class + }) + public class MyTest { + + /** + * @throws java.lang.Exception + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { + } + + @Ignore + @Test + public void test() { + fail("Not yet implemented"); + } + + @Ignore("not implemented") + @Test + public void test2() { + fail("Not yet implemented"); + } + + @Test + public void test3() { + assertEquals("expected", "actual"); + } + + @Test + public void test4() { + assertEquals("failuremessage", "expected", "actual"); + int result=5; + assertEquals(5, result); // expected = 5, actual = result + assertNotEquals("failuremessage",5, result); // expected = 5, actual = result + assertTrue("failuremessage",false); + assertFalse("failuremessage",false); + assertTrue(false); + assertFalse(false); + } + } + """, //$NON-NLS-1$ + + """ + package test; + import static org.junit.jupiter.api.Assertions.*; + + import org.junit.jupiter.api.AfterAll; + import org.junit.jupiter.api.AfterEach; + import org.junit.jupiter.api.BeforeAll; + import org.junit.jupiter.api.BeforeEach; + import org.junit.jupiter.api.Disabled; + import org.junit.jupiter.api.Test; + import org.junit.platform.suite.api.SelectClasses; + import org.junit.platform.suite.api.Suite; + + /** + * + */ + @Suite + @SelectClasses({ + MyTest.class + }) + public class MyTest { + + /** + * @throws java.lang.Exception + */ + @BeforeAll + public static void setUpBeforeClass() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @AfterAll + public static void tearDownAfterClass() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @BeforeEach + public void setUp() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @AfterEach + public void tearDown() throws Exception { + } + + @Disabled + @Test + public void test() { + fail("Not yet implemented"); + } + + @Disabled("not implemented") + @Test + public void test2() { + fail("Not yet implemented"); + } + + @Test + public void test3() { + assertEquals("expected", "actual"); + } + + @Test + public void test4() { + assertEquals("expected", "actual", "failuremessage"); + int result=5; + assertEquals(5, result); // expected = 5, actual = result + assertNotEquals(5,result, "failuremessage"); // expected = 5, actual = result + assertTrue(false,"failuremessage"); + assertFalse(false,"failuremessage"); + assertTrue(false); + assertFalse(false); + } + } + """), + StaticExplicitImportCase(""" + package test; + import static org.junit.Assert.fail; + import static org.junit.Assert.assertEquals; + import static org.junit.Assert.assertNotEquals; + import static org.junit.Assert.assertTrue; + import static org.junit.Assert.assertFalse; + + import org.junit.After; + import org.junit.AfterClass; + import org.junit.Before; + import org.junit.BeforeClass; + import org.junit.Ignore; + import org.junit.Test; + import org.junit.runner.RunWith; + import org.junit.runners.Suite; + + /** + * + */ + @RunWith(Suite.class) + @Suite.SuiteClasses({ + MyTest.class + }) + public class MyTest { + + /** + * @throws java.lang.Exception + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { + } + + @Ignore + @Test + public void test() { + fail("Not yet implemented"); + } + + @Ignore("not implemented") + @Test + public void test2() { + fail("Not yet implemented"); + } + + @Test + public void test3() { + assertEquals("expected", "actual"); + } + + @Test + public void test4() { + assertEquals("failuremessage", "expected", "actual"); + int result=5; + assertEquals(5, result); // expected = 5, actual = result + assertNotEquals("failuremessage",5, result); // expected = 5, actual = result + assertTrue("failuremessage",false); + assertFalse("failuremessage",false); + assertTrue(false); + assertFalse(false); + } + } + """, //$NON-NLS-1$ + + """ + package test; + import static org.junit.jupiter.api.Assertions.assertEquals; + import static org.junit.jupiter.api.Assertions.assertFalse; + import static org.junit.jupiter.api.Assertions.assertNotEquals; + import static org.junit.jupiter.api.Assertions.assertTrue; + import static org.junit.jupiter.api.Assertions.fail; + + import org.junit.jupiter.api.AfterAll; + import org.junit.jupiter.api.AfterEach; + import org.junit.jupiter.api.BeforeAll; + import org.junit.jupiter.api.BeforeEach; + import org.junit.jupiter.api.Disabled; + import org.junit.jupiter.api.Test; + import org.junit.platform.suite.api.SelectClasses; + import org.junit.platform.suite.api.Suite; + + /** + * + */ + @Suite + @SelectClasses({ + MyTest.class + }) + public class MyTest { + + /** + * @throws java.lang.Exception + */ + @BeforeAll + public static void setUpBeforeClass() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @AfterAll + public static void tearDownAfterClass() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @BeforeEach + public void setUp() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @AfterEach + public void tearDown() throws Exception { + } + + @Disabled + @Test + public void test() { + fail("Not yet implemented"); + } + + @Disabled("not implemented") + @Test + public void test2() { + fail("Not yet implemented"); + } + + @Test + public void test3() { + assertEquals("expected", "actual"); + } + + @Test + public void test4() { + assertEquals("expected", "actual", "failuremessage"); + int result=5; + assertEquals(5, result); // expected = 5, actual = result + assertNotEquals(5,result, "failuremessage"); // expected = 5, actual = result + assertTrue(false,"failuremessage"); + assertFalse(false,"failuremessage"); + assertTrue(false); + assertFalse(false); + } + } + """), + RuleAnonymousExternalResource( +""" +package test; +import org.junit.Test; +import org.junit.Rule; +import org.junit.rules.ExternalResource; +/** + * + */ +public class MyTest { + + @Rule + public ExternalResource er= new ExternalResource() { + @Override + protected void before() throws Throwable { + }; + + @Override + protected void after() { + }; + }; + + @Test + public void test3() { + } +} +""", //$NON-NLS-1$ +""" +package test; +import org.junit.Rule; +import org.junit.jupiter.api.Test; +import org.junit.rules.ExternalResource; +/** + * + */ +public class MyTest { + + @Rule + public ExternalResource er= new ExternalResource() { + @Override + protected void before() throws Throwable { + }; + + @Override + protected void after() { + }; + }; + + @Test + public void test3() { + } +} +"""), +RuleNestedExternalResource( +""" +package test; + +import org.junit.Test; +import org.junit.Rule; +import org.junit.ClassRule; +import org.junit.rules.ExternalResource; + +public class MyTest { + + // Final abgeleitete Klasse + final class MyExternalResource extends ExternalResource { + @Override + protected void before() throws Throwable { + super.before(); + int i = 4; + } + + @Override + protected void after() { + super.after(); + } + } + + @Rule + public ExternalResource er = new MyExternalResource(); + + // Anonyme Klasse als ExternalResource + @Rule + public ExternalResource anonymousRule = new ExternalResource() { + @Override + protected void before() throws Throwable { + System.out.println("Anonymous rule before"); + } + + @Override + protected void after() { + System.out.println("Anonymous rule after"); + } + }; + + // Statische Klasse für ClassRule + static class StaticExternalResource extends ExternalResource { + @Override + protected void before() throws Throwable { + System.out.println("Static resource before"); + } + + @Override + protected void after() { + System.out.println("Static resource after"); + } + } + + @ClassRule + public static ExternalResource staticResource = new StaticExternalResource(); + + // Klasse mit Konstruktor + final class ConstructedExternalResource extends ExternalResource { + private final String resourceName; + + public ConstructedExternalResource(String resourceName) { + this.resourceName = resourceName; + } + + @Override + protected void before() throws Throwable { + System.out.println("Resource setup: " + resourceName); + } + + @Override + protected void after() { + System.out.println("Resource cleanup: " + resourceName); + } + } + + @Rule + public ExternalResource constructedResource = new ConstructedExternalResource("TestResource"); + + // Zweite Regel + @Rule + public ExternalResource secondRule = new ExternalResource() { + @Override + protected void before() throws Throwable { + System.out.println("Second rule before"); + } + + @Override + protected void after() { + System.out.println("Second rule after"); + } + }; + + // Testfälle + @Test + public void testWithExternalResource() { + System.out.println("Test with external resource"); + } + + @Test + public void testWithMultipleResources() { + System.out.println("Test with multiple resources"); + } +} +""", //$NON-NLS-1$ +""" +package test; + +import org.junit.ClassRule; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.AfterEachCallback; +import org.junit.jupiter.api.extension.BeforeEachCallback; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.RegisterExtension; + +public class MyTest { + + // Final abgeleitete Klasse + final class MyExternalResource implements BeforeEachCallback, AfterEachCallback { + @Override + public void beforeEach(ExtensionContext context) { + super.beforeEach(context); + int i = 4; + } + + @Override + public void afterEach(ExtensionContext context) { + super.afterEach(context); + } + } + + @RegisterExtension + public ExternalResource er = new MyExternalResource(); + + // Anonyme Klasse als ExternalResource + @RegisterExtension + public ExternalResource anonymousRule = new ExternalResource() { + @Override + protected void before() throws Throwable { + System.out.println("Anonymous rule before"); + } + + @Override + protected void after() { + System.out.println("Anonymous rule after"); + } + }; + + // Statische Klasse für ClassRule + static class StaticExternalResource implements BeforeEachCallback, AfterEachCallback { + @Override + public void beforeEach(ExtensionContext context) { + System.out.println("Static resource before"); + } + + @Override + public void afterEach(ExtensionContext context) { + System.out.println("Static resource after"); + } + } + + @ClassRule + public static ExternalResource staticResource = new StaticExternalResource(); + + // Klasse mit Konstruktor + final class ConstructedExternalResource implements BeforeEachCallback, AfterEachCallback { + private final String resourceName; + + public ConstructedExternalResource(String resourceName) { + this.resourceName = resourceName; + } + + @Override + public void beforeEach(ExtensionContext context) { + System.out.println("Resource setup: " + resourceName); + } + + @Override + public void afterEach(ExtensionContext context) { + System.out.println("Resource cleanup: " + resourceName); + } + } + + @RegisterExtension + public ExternalResource constructedResource = new ConstructedExternalResource("TestResource"); + + // Zweite Regel + @RegisterExtension + public ExternalResource secondRule = new ExternalResource() { + @Override + protected void before() throws Throwable { + System.out.println("Second rule before"); + } + + @Override + protected void after() { + System.out.println("Second rule after"); + } + }; + + // Testfälle + @Test + public void testWithExternalResource() { + System.out.println("Test with external resource"); + } + + @Test + public void testWithMultipleResources() { + System.out.println("Test with multiple resources"); + } +} +"""), + TestnameRule( +""" +package test; +import java.io.File; +import java.io.IOException; +import org.junit.Test; +import org.junit.Rule; +import org.junit.rules.TestName; +import org.junit.rules.TemporaryFolder; +/** + * + */ +public class MyTest { + + private static final String SRC= "src"; + + @Rule + public TemporaryFolder tempFolder = new TemporaryFolder(); + + @Rule + public TestName tn = new TestName(); + + @Test + public void test3() throws IOException{ + System.out.println("Test name: " + tn.getMethodName()); + File newFile = tempFolder.newFile("myfile.txt"); + } +} +""", //$NON-NLS-1$ +""" +package test; +import java.io.File; +import java.io.IOException; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; +import org.junit.jupiter.api.io.TempDir; +/** + * + */ +public class MyTest { + + @TempDir + Path tempFolder; + + private String testName; + + @BeforeEach + void init(TestInfo testInfo) { + this.testName = testInfo.getDisplayName(); + } + + private static final String SRC= "src"; + + @Test + public void test3() throws IOException{ + System.out.println("Test name: " + testName); + File newFile = tempFolder.resolve("myfile.txt").toFile(); + } +} +"""); //$NON-NLS-1$ + + String given; + String expected; + + JUnitCleanupCases(String given, String expected) { + this.given=given; + this.expected=expected; + } + } \ No newline at end of file diff --git a/sandbox_junit_cleanup_test/src/org/eclipse/jdt/ui/tests/quickfix/Java8/JUnitMigrationCleanUpTest.java b/sandbox_junit_cleanup_test/src/org/eclipse/jdt/ui/tests/quickfix/Java8/JUnitMigrationCleanUpTest.java index f3b294eb..a31b47fa 100644 --- a/sandbox_junit_cleanup_test/src/org/eclipse/jdt/ui/tests/quickfix/Java8/JUnitMigrationCleanUpTest.java +++ b/sandbox_junit_cleanup_test/src/org/eclipse/jdt/ui/tests/quickfix/Java8/JUnitMigrationCleanUpTest.java @@ -45,759 +45,6 @@ public void setup() throws CoreException { fRootJUnit5= context4junit5.createClasspathForJUnit(JUnitCore.JUNIT5_CONTAINER_PATH); } - enum JUnitCleanupCases{ - PositiveCase(""" -package test; -import org.junit.Assert; - -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Ignore; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Suite; -import org.junit.Assume; -import static org.junit.Assume.assumeTrue; -import org.hamcrest.CoreMatchers; - -/** - * - */ -@RunWith(Suite.class) -@Suite.SuiteClasses({ - MyTest.class -}) -public class MyTest { - - /** - * @throws java.lang.Exception - */ - @BeforeClass - public static void setUpBeforeClass() throws Exception { - } - - /** - * @throws java.lang.Exception - */ - @AfterClass - public static void tearDownAfterClass() throws Exception { - } - - /** - * @throws java.lang.Exception - */ - @Before - public void setUp() throws Exception { - } - - /** - * @throws java.lang.Exception - */ - @After - public void tearDown() throws Exception { - } - - @Ignore - @Test - public void test() { - Assert.fail("Not yet implemented"); - Object o1,o2; - o1="foo"; - o2=o1; - Assert.assertSame("ohno", o1, o2); - } - - @Ignore("not implemented") - @Test - public void test2() { - Assert.fail("Not yet implemented"); - } - - @Test - public void test3() { - boolean condition=true; - Assume.assumeFalse("Bedingung nicht erfüllt", condition); - Assume.assumeFalse(condition); - assumeTrue("Bedingung nicht erfüllt", condition); - assumeTrue(condition); - Assert.assertEquals("expected", "actual"); - Assume.assumeNotNull(" "); - Assume.assumeThat(1, CoreMatchers.is(1)); - Assert.assertThat(1, CoreMatchers.is(1)); - } - - @Test - public void test4() { - Assert.assertEquals("failuremessage", "expected", "actual"); - int result=5; - Assert.assertEquals(5, result); // expected = 5, actual = result - Assert.assertNotEquals("failuremessage",5, result); // expected = 5, actual = result - Assert.assertNotEquals(5, result); // expected = 5, actual = result - Assert.assertTrue("failuremessage",false); - Assert.assertTrue(false); - Assert.assertFalse("failuremessage",false); - Assert.assertFalse(false); - Assert.assertNull("failuremessage", null); - Assert.assertNull(null); - } -} -""", //$NON-NLS-1$ - -""" -package test; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.junit.MatcherAssume.assumeThat; -import static org.junit.jupiter.api.Assumptions.assumeTrue; - -import org.hamcrest.CoreMatchers; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Assumptions; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; -import org.junit.platform.suite.api.SelectClasses; -import org.junit.platform.suite.api.Suite; - -/** - * - */ -@Suite -@SelectClasses({ - MyTest.class -}) -public class MyTest { - - /** - * @throws java.lang.Exception - */ - @BeforeAll - public static void setUpBeforeClass() throws Exception { - } - - /** - * @throws java.lang.Exception - */ - @AfterAll - public static void tearDownAfterClass() throws Exception { - } - - /** - * @throws java.lang.Exception - */ - @BeforeEach - public void setUp() throws Exception { - } - - /** - * @throws java.lang.Exception - */ - @AfterEach - public void tearDown() throws Exception { - } - - @Disabled - @Test - public void test() { - Assertions.fail("Not yet implemented"); - Object o1,o2; - o1="foo"; - o2=o1; - Assertions.assertSame(o1, o2, "ohno"); - } - - @Disabled("not implemented") - @Test - public void test2() { - Assertions.fail("Not yet implemented"); - } - - @Test - public void test3() { - boolean condition=true; - Assumptions.assumeFalse(condition, "Bedingung nicht erfüllt"); - Assumptions.assumeFalse(condition); - assumeTrue(condition, "Bedingung nicht erfüllt"); - assumeTrue(condition); - Assertions.assertEquals("expected", "actual"); - Assumptions.assumeNotNull(" "); - assumeThat(1, CoreMatchers.is(1)); - assertThat(1, CoreMatchers.is(1)); - } - - @Test - public void test4() { - Assertions.assertEquals("expected", "actual", "failuremessage"); - int result=5; - Assertions.assertEquals(5, result); // expected = 5, actual = result - Assertions.assertNotEquals(5,result, "failuremessage"); // expected = 5, actual = result - Assertions.assertNotEquals(5, result); // expected = 5, actual = result - Assertions.assertTrue(false,"failuremessage"); - Assertions.assertTrue(false); - Assertions.assertFalse(false,"failuremessage"); - Assertions.assertFalse(false); - Assertions.assertNull(null, "failuremessage"); - Assertions.assertNull(null); - } -} -"""), - AlreadyJunit5Case( -""" -package test; -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.junit.jupiter.api.Test; -/** - * - */ -public class MyTest { - - @Test - public void test3() { - assertEquals("expected", "actual"); - } -} -""", //$NON-NLS-1$ -""" -package test; -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.junit.jupiter.api.Test; -/** - * - */ -public class MyTest { - - @Test - public void test3() { - assertEquals("expected", "actual"); - } -} -"""), - StaticImportCase(""" - package test; - import static org.junit.Assert.*; - - import org.junit.After; - import org.junit.AfterClass; - import org.junit.Before; - import org.junit.BeforeClass; - import org.junit.Ignore; - import org.junit.Test; - import org.junit.runner.RunWith; - import org.junit.runners.Suite; - - /** - * - */ - @RunWith(Suite.class) - @Suite.SuiteClasses({ - MyTest.class - }) - public class MyTest { - - /** - * @throws java.lang.Exception - */ - @BeforeClass - public static void setUpBeforeClass() throws Exception { - } - - /** - * @throws java.lang.Exception - */ - @AfterClass - public static void tearDownAfterClass() throws Exception { - } - - /** - * @throws java.lang.Exception - */ - @Before - public void setUp() throws Exception { - } - - /** - * @throws java.lang.Exception - */ - @After - public void tearDown() throws Exception { - } - - @Ignore - @Test - public void test() { - fail("Not yet implemented"); - } - - @Ignore("not implemented") - @Test - public void test2() { - fail("Not yet implemented"); - } - - @Test - public void test3() { - assertEquals("expected", "actual"); - } - - @Test - public void test4() { - assertEquals("failuremessage", "expected", "actual"); - int result=5; - assertEquals(5, result); // expected = 5, actual = result - assertNotEquals("failuremessage",5, result); // expected = 5, actual = result - assertTrue("failuremessage",false); - assertFalse("failuremessage",false); - assertTrue(false); - assertFalse(false); - } - } - """, //$NON-NLS-1$ - - """ - package test; - import static org.junit.jupiter.api.Assertions.*; - - import org.junit.jupiter.api.AfterAll; - import org.junit.jupiter.api.AfterEach; - import org.junit.jupiter.api.BeforeAll; - import org.junit.jupiter.api.BeforeEach; - import org.junit.jupiter.api.Disabled; - import org.junit.jupiter.api.Test; - import org.junit.platform.suite.api.SelectClasses; - import org.junit.platform.suite.api.Suite; - - /** - * - */ - @Suite - @SelectClasses({ - MyTest.class - }) - public class MyTest { - - /** - * @throws java.lang.Exception - */ - @BeforeAll - public static void setUpBeforeClass() throws Exception { - } - - /** - * @throws java.lang.Exception - */ - @AfterAll - public static void tearDownAfterClass() throws Exception { - } - - /** - * @throws java.lang.Exception - */ - @BeforeEach - public void setUp() throws Exception { - } - - /** - * @throws java.lang.Exception - */ - @AfterEach - public void tearDown() throws Exception { - } - - @Disabled - @Test - public void test() { - fail("Not yet implemented"); - } - - @Disabled("not implemented") - @Test - public void test2() { - fail("Not yet implemented"); - } - - @Test - public void test3() { - assertEquals("expected", "actual"); - } - - @Test - public void test4() { - assertEquals("expected", "actual", "failuremessage"); - int result=5; - assertEquals(5, result); // expected = 5, actual = result - assertNotEquals(5,result, "failuremessage"); // expected = 5, actual = result - assertTrue(false,"failuremessage"); - assertFalse(false,"failuremessage"); - assertTrue(false); - assertFalse(false); - } - } - """), - StaticExplicitImportCase(""" - package test; - import static org.junit.Assert.fail; - import static org.junit.Assert.assertEquals; - import static org.junit.Assert.assertNotEquals; - import static org.junit.Assert.assertTrue; - import static org.junit.Assert.assertFalse; - - import org.junit.After; - import org.junit.AfterClass; - import org.junit.Before; - import org.junit.BeforeClass; - import org.junit.Ignore; - import org.junit.Test; - import org.junit.runner.RunWith; - import org.junit.runners.Suite; - - /** - * - */ - @RunWith(Suite.class) - @Suite.SuiteClasses({ - MyTest.class - }) - public class MyTest { - - /** - * @throws java.lang.Exception - */ - @BeforeClass - public static void setUpBeforeClass() throws Exception { - } - - /** - * @throws java.lang.Exception - */ - @AfterClass - public static void tearDownAfterClass() throws Exception { - } - - /** - * @throws java.lang.Exception - */ - @Before - public void setUp() throws Exception { - } - - /** - * @throws java.lang.Exception - */ - @After - public void tearDown() throws Exception { - } - - @Ignore - @Test - public void test() { - fail("Not yet implemented"); - } - - @Ignore("not implemented") - @Test - public void test2() { - fail("Not yet implemented"); - } - - @Test - public void test3() { - assertEquals("expected", "actual"); - } - - @Test - public void test4() { - assertEquals("failuremessage", "expected", "actual"); - int result=5; - assertEquals(5, result); // expected = 5, actual = result - assertNotEquals("failuremessage",5, result); // expected = 5, actual = result - assertTrue("failuremessage",false); - assertFalse("failuremessage",false); - assertTrue(false); - assertFalse(false); - } - } - """, //$NON-NLS-1$ - - """ - package test; - import static org.junit.jupiter.api.Assertions.assertEquals; - import static org.junit.jupiter.api.Assertions.assertFalse; - import static org.junit.jupiter.api.Assertions.assertNotEquals; - import static org.junit.jupiter.api.Assertions.assertTrue; - import static org.junit.jupiter.api.Assertions.fail; - - import org.junit.jupiter.api.AfterAll; - import org.junit.jupiter.api.AfterEach; - import org.junit.jupiter.api.BeforeAll; - import org.junit.jupiter.api.BeforeEach; - import org.junit.jupiter.api.Disabled; - import org.junit.jupiter.api.Test; - import org.junit.platform.suite.api.SelectClasses; - import org.junit.platform.suite.api.Suite; - - /** - * - */ - @Suite - @SelectClasses({ - MyTest.class - }) - public class MyTest { - - /** - * @throws java.lang.Exception - */ - @BeforeAll - public static void setUpBeforeClass() throws Exception { - } - - /** - * @throws java.lang.Exception - */ - @AfterAll - public static void tearDownAfterClass() throws Exception { - } - - /** - * @throws java.lang.Exception - */ - @BeforeEach - public void setUp() throws Exception { - } - - /** - * @throws java.lang.Exception - */ - @AfterEach - public void tearDown() throws Exception { - } - - @Disabled - @Test - public void test() { - fail("Not yet implemented"); - } - - @Disabled("not implemented") - @Test - public void test2() { - fail("Not yet implemented"); - } - - @Test - public void test3() { - assertEquals("expected", "actual"); - } - - @Test - public void test4() { - assertEquals("expected", "actual", "failuremessage"); - int result=5; - assertEquals(5, result); // expected = 5, actual = result - assertNotEquals(5,result, "failuremessage"); // expected = 5, actual = result - assertTrue(false,"failuremessage"); - assertFalse(false,"failuremessage"); - assertTrue(false); - assertFalse(false); - } - } - """), - RuleAnonymousExternalResource( -""" -package test; -import org.junit.Test; -import org.junit.Rule; -import org.junit.rules.ExternalResource; -/** - * - */ -public class MyTest { - - @Rule - public ExternalResource er= new ExternalResource() { - @Override - protected void before() throws Throwable { - }; - - @Override - protected void after() { - }; - }; - - @Test - public void test3() { - } -} -""", //$NON-NLS-1$ -""" -package test; -import org.junit.Rule; -import org.junit.jupiter.api.Test; -import org.junit.rules.ExternalResource; -/** - * - */ -public class MyTest { - - @Rule - public ExternalResource er= new ExternalResource() { - @Override - protected void before() throws Throwable { - }; - - @Override - protected void after() { - }; - }; - - @Test - public void test3() { - } -} -"""), -RuleNestedExternalResource( -""" -package test; -import org.junit.Test; -import org.junit.Rule; -import org.junit.rules.ExternalResource; -/** - * - */ -public class MyTest { - - final class MyExternalResource extends ExternalResource { - @Override - protected void before() throws Throwable { - super.before(); - int i=4; - } - - @Override - protected void after() { - super.after(); - } - } - - @Rule - public ExternalResource er= new MyExternalResource(); - - @Test - public void test3() { - } -} -""", //$NON-NLS-1$ -""" -package test; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.AfterEachCallback; -import org.junit.jupiter.api.extension.BeforeEachCallback; -import org.junit.jupiter.api.extension.ExtensionContext; -import org.junit.jupiter.api.extension.RegisterExtension; -/** - * - */ -public class MyTest { - - final class MyExternalResource implements BeforeEachCallback, AfterEachCallback { - @Override - public void beforeEach(ExtensionContext context) { - super.beforeEach(context); - int i=4; - } - - @Override - public void afterEach(ExtensionContext context) { - super.afterEach(context); - } - } - - @RegisterExtension - public ExternalResource er= new MyExternalResource(); - - @Test - public void test3() { - } -} -"""), - TestnameRule( -""" -package test; -import java.io.File; -import java.io.IOException; -import org.junit.Test; -import org.junit.Rule; -import org.junit.rules.TestName; -import org.junit.rules.TemporaryFolder; -/** - * - */ -public class MyTest { - - private static final String SRC= "src"; - - @Rule - public TemporaryFolder tempFolder = new TemporaryFolder(); - - @Rule - public TestName tn = new TestName(); - - @Test - public void test3() throws IOException{ - System.out.println("Test name: " + tn.getMethodName()); - File newFile = tempFolder.newFile("myfile.txt"); - } -} -""", //$NON-NLS-1$ -""" -package test; -import java.io.File; -import java.io.IOException; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.junit.jupiter.api.io.TempDir; -/** - * - */ -public class MyTest { - - @TempDir - Path tempFolder; - - private String testName; - - @BeforeEach - void init(TestInfo testInfo) { - this.testName = testInfo.getDisplayName(); - } - - private static final String SRC= "src"; - - @Test - public void test3() throws IOException{ - System.out.println("Test name: " + testName); - File newFile = tempFolder.resolve("myfile.txt").toFile(); - } -} -"""); //$NON-NLS-1$ - - String given; - String expected; - - JUnitCleanupCases(String given, String expected) { - this.given=given; - this.expected=expected; - } - } - @ParameterizedTest @EnumSource(JUnitCleanupCases.class) public void testJUnitCleanupParametrized(JUnitCleanupCases test) throws CoreException { @@ -815,6 +62,7 @@ public void testJUnitCleanupParametrized(JUnitCleanupCases test) throws CoreExce context4junit4.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_TEST); context4junit4.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_RULETEMPORARYFOLDER); context4junit4.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_RULETESTNAME); + context4junit4.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_RULEEXTERNALRESOURCE); context4junit4.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_EXTERNALRESOURCE); context4junit4.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_RUNWITH); context4junit4.assertRefactoringResultAsExpected(new ICompilationUnit[] {cu}, new String[] {test.expected}, null); @@ -864,6 +112,19 @@ public void testJUnitCleanupdonttouch(NOJUnitCleanupCases test) throws CoreExcep IPackageFragment pack= fRootJUnit5.createPackageFragment("test", true, null); ICompilationUnit cu= pack.createCompilationUnit("MyTest.java",test.given,false, null); //$NON-NLS-1$ context4junit5.enable(MYCleanUpConstants.JUNIT_CLEANUP); + context4junit5.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_ASSERT); + context4junit5.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_ASSUME); + context4junit5.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_SUITE); + context4junit5.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_BEFORE); + context4junit5.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_AFTER); + context4junit5.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_BEFORECLASS); + context4junit5.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_AFTERCLASS); + context4junit5.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_IGNORE); + context4junit5.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_TEST); + context4junit5.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_RULETEMPORARYFOLDER); + context4junit5.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_RULETESTNAME); + context4junit5.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_EXTERNALRESOURCE); + context4junit5.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_RUNWITH); context4junit5.assertRefactoringHasNoChange(new ICompilationUnit[] { cu }); } @@ -927,6 +188,7 @@ public start(){ context4junit4.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_TEST); context4junit4.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_RULETEMPORARYFOLDER); context4junit4.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_RULETESTNAME); + context4junit4.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_RULEEXTERNALRESOURCE); context4junit4.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_EXTERNALRESOURCE); context4junit4.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_RUNWITH); context4junit4.assertRefactoringResultAsExpected(new ICompilationUnit[] {cu,cu2}, new String[] { @@ -1133,6 +395,7 @@ protected void after() { context4junit4.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_TEST); context4junit4.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_RULETEMPORARYFOLDER); context4junit4.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_RULETESTNAME); + context4junit4.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_RULEEXTERNALRESOURCE); context4junit4.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_EXTERNALRESOURCE); context4junit4.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_RUNWITH); context4junit4.assertRefactoringResultAsExpected(new ICompilationUnit[] {cu,cu2,cu3}, new String[] { @@ -1192,4 +455,28 @@ public void afterEach(ExtensionContext context) { """ }, null); } + + + @ParameterizedTest + @EnumSource(value=JUnitCleanupCases.class, names= {"RuleNestedExternalResource"}) + public void testJUnitCleanupSelectedCase(JUnitCleanupCases test) throws CoreException { + IPackageFragment pack= fRootJUnit4.createPackageFragment("test", true, null); + ICompilationUnit cu= pack.createCompilationUnit("MyTest.java", test.given, true, null); //$NON-NLS-1$ + context4junit4.enable(MYCleanUpConstants.JUNIT_CLEANUP); +// context4junit4.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_ASSERT); +// context4junit4.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_ASSUME); +// context4junit4.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_SUITE); +// context4junit4.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_BEFORE); +// context4junit4.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_AFTER); +// context4junit4.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_BEFORECLASS); +// context4junit4.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_AFTERCLASS); +// context4junit4.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_IGNORE); + context4junit4.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_TEST); +// context4junit4.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_RULETEMPORARYFOLDER); +// context4junit4.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_RULETESTNAME); + context4junit4.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_RULEEXTERNALRESOURCE); + context4junit4.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_EXTERNALRESOURCE); +// context4junit4.enable(MYCleanUpConstants.JUNIT_CLEANUP_4_RUNWITH); + context4junit4.assertRefactoringResultAsExpected(new ICompilationUnit[] {cu}, new String[] {test.expected}, null); + } }