diff --git a/src/main/java/de/markiewb/netbeans/plugins/hints/assertfix/ConvertToAssertTrueFalseNull.java b/src/main/java/de/markiewb/netbeans/plugins/hints/assertfix/ConvertToAssertTrueFalseNull.java index 1679fc9..c238534 100644 --- a/src/main/java/de/markiewb/netbeans/plugins/hints/assertfix/ConvertToAssertTrueFalseNull.java +++ b/src/main/java/de/markiewb/netbeans/plugins/hints/assertfix/ConvertToAssertTrueFalseNull.java @@ -90,38 +90,6 @@ public static ErrorDescription computeAssertFalseWithMessage(HintContext ctx) { return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_computeAssertFalseWithMessage(), fix); } - @TriggerPattern(value = "junit.framework.Assert.assertEquals(true, $var)") - @Messages("ERR_computeAssertTrueWithoutMessage2=Replace with assertTrue") - public static ErrorDescription computeAssertTrueWithoutMessage2(HintContext ctx) { - Fix fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_computeAssertTrueWithoutMessage2(), ctx.getPath(), "junit.framework.Assert.assertTrue($var)"); - return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_computeAssertTrueWithoutMessage2(), fix); - } - - @TriggerPattern(value = "junit.framework.Assert.assertEquals(false, $var)") - @Messages("ERR_computeAssertFalseWithoutMessage2=Replace with assertFalse") - public static ErrorDescription computeAssertFalseWithoutMessage2(HintContext ctx) { - Fix fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_computeAssertFalseWithoutMessage2(), ctx.getPath(), "junit.framework.Assert.assertFalse($var)"); - return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_computeAssertFalseWithoutMessage2(), fix); - } - - @TriggerPattern(value = "junit.framework.Assert.assertEquals($msg, true, $var)", constraints = { - @ConstraintVariableType(variable = "$msg", type = "java.lang.String") - }) - @Messages("ERR_computeAssertTrueWithMessage2=Replace with assertTrue") - public static ErrorDescription computeAssertTrueWithMessage2(HintContext ctx) { - Fix fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_computeAssertTrueWithMessage2(), ctx.getPath(), "junit.framework.Assert.assertTrue($msg, $var)"); - return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_computeAssertTrueWithMessage2(), fix); - } - - @TriggerPattern(value = "junit.framework.Assert.assertEquals($msg, false, $var)", constraints = { - @ConstraintVariableType(variable = "$msg", type = "java.lang.String") - }) - @Messages("ERR_computeAssertFalseWithMessage2=Replace with assertFalse") - public static ErrorDescription computeAssertFalseWithMessage2(HintContext ctx) { - Fix fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_computeAssertFalseWithMessage2(), ctx.getPath(), "junit.framework.Assert.assertFalse($msg, $var)"); - return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_computeAssertFalseWithMessage2(), fix); - } - @TriggerPattern(value = "org.junit.Assert.assertEquals(null, $var)") @Messages("ERR_computeAssertNullWithoutMessage=Replace with assertNull") public static ErrorDescription computeAssertNullWithoutMessage(HintContext ctx) { @@ -138,20 +106,4 @@ public static ErrorDescription computeAssertNullWithMessage(HintContext ctx) { return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_computeAssertNullWithMessage(), fix); } - @TriggerPattern(value = "junit.framework.Assert.assertEquals(null, $var)") - @Messages("ERR_computeAssertNullWithoutMessage2=Replace with assertNull") - public static ErrorDescription computeAssertNullWithoutMessage2(HintContext ctx) { - Fix fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_computeAssertNullWithoutMessage2(), ctx.getPath(), "junit.framework.Assert.assertNull($var)"); - return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_computeAssertNullWithoutMessage2(), fix); - } - - @TriggerPattern(value = "junit.framework.Assert.assertEquals($msg, null, $var)", constraints = { - @ConstraintVariableType(variable = "$msg", type = "java.lang.String") - }) - @Messages("ERR_computeAssertNullWithMessage2=Replace with assertNull") - public static ErrorDescription computeAssertNullWithMessage2(HintContext ctx) { - Fix fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_computeAssertNullWithMessage2(), ctx.getPath(), "junit.framework.Assert.assertNull($msg, $var)"); - return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_computeAssertNullWithMessage2(), fix); - } - } diff --git a/src/main/java/de/markiewb/netbeans/plugins/hints/assertfix/UpgradeToOJAssert.java b/src/main/java/de/markiewb/netbeans/plugins/hints/assertfix/UpgradeToOJAssert.java new file mode 100644 index 0000000..50378b0 --- /dev/null +++ b/src/main/java/de/markiewb/netbeans/plugins/hints/assertfix/UpgradeToOJAssert.java @@ -0,0 +1,180 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2014 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2014 Sun Microsystems, Inc. + * Portions Copyrighted 2016 benno.markiewicz@googlemail.com + */ +package de.markiewb.netbeans.plugins.hints.assertfix; + +import com.sun.source.tree.CompilationUnitTree; +import com.sun.source.tree.ExpressionTree; +import com.sun.source.tree.ImportTree; +import com.sun.source.tree.MemberSelectTree; +import com.sun.source.tree.MethodInvocationTree; +import com.sun.source.tree.Tree; +import com.sun.source.util.TreePath; +import java.util.Collections; +import org.netbeans.api.java.source.TreeMaker; +import org.netbeans.api.java.source.TreePathHandle; +import org.netbeans.spi.editor.hints.ErrorDescription; +import org.netbeans.spi.editor.hints.Fix; +import org.netbeans.spi.editor.hints.Severity; +import org.netbeans.spi.java.hints.ErrorDescriptionFactory; +import org.netbeans.spi.java.hints.Hint; +import org.netbeans.spi.java.hints.HintContext; +import org.netbeans.spi.java.hints.JavaFix; +import org.netbeans.spi.java.hints.TriggerTreeKind; +import org.openide.util.NbBundle; + +/** + * Replace junit.framework.Assert with org.junit.Assert + * + * @author markiewb + * @since 1.6.0 + */ +@NbBundle.Messages({ + "ERR_UpgradeToOJAssert=Replace with org.junit.Assert", + "DESC_UpgradeToOJAssert=Replace usages of the deprecated junit.framework.Assert class to org.junit.Assert.

Provided by nb-additional-hints plugin

",}) +public class UpgradeToOJAssert { + + @TriggerTreeKind({Tree.Kind.IMPORT, Tree.Kind.METHOD_INVOCATION}) + @Hint(displayName = "#ERR_UpgradeToOJAssert", description = "#DESC_UpgradeToOJAssert", category = "testing", hintKind = Hint.Kind.INSPECTION, severity = Severity.WARNING) + public static ErrorDescription toFix(HintContext ctx) { + if (ctx.getPath().getLeaf().getKind() == Tree.Kind.METHOD_INVOCATION) { + MethodInvocationTree methodInvocationTree = (MethodInvocationTree) ctx.getPath().getLeaf(); + ExpressionTree methodSelect = methodInvocationTree.getMethodSelect(); + //junit.framework.Assert.assertEquals + if (methodSelect.getKind() == Tree.Kind.MEMBER_SELECT) { + MemberSelectTree methodSelect1 = (MemberSelectTree) methodSelect; + if (methodSelect1.getExpression().toString().equals("junit.framework.Assert")) { + Fix fix = new FixMethodInvocationImpl(TreePathHandle.create(ctx.getPath(), ctx.getInfo()), Bundle.ERR_UpgradeToOJAssert()).toEditorFix(); + ErrorDescription forTree = ErrorDescriptionFactory.forTree(ctx, ctx.getPath(), Bundle.ERR_UpgradeToOJAssert(), fix); + return forTree; + } + } + + } + //IMPORT + if (ctx.getPath().getLeaf().getKind() == Tree.Kind.IMPORT) { + TreePath importTP = ctx.getPath(); + ImportTree importTree = (ImportTree) importTP.getLeaf(); + MemberSelectTree importTree1 = (MemberSelectTree) importTree.getQualifiedIdentifier(); + if (importTree1.toString().startsWith("junit.framework.Assert.") || importTree1.toString().equals("junit.framework.Assert")) { + Fix fix = new FixImportImpl(TreePathHandle.create(importTP, ctx.getInfo()), Bundle.ERR_UpgradeToOJAssert()).toEditorFix(); + ErrorDescription forTree = ErrorDescriptionFactory.forTree(ctx, importTP, Bundle.ERR_UpgradeToOJAssert(), fix); + return forTree; + } + + } + return null; + } + + static class FixImportImpl extends JavaFix { + + private final String label; + + FixImportImpl(TreePathHandle handle, String label) { + super(handle, label); + this.label = label; + } + + @Override + protected String getText() { + + return label; + } + + @Override + protected void performRewrite(JavaFix.TransformationContext ctx) throws Exception { + TreePath path = ctx.getPath(); + + CompilationUnitTree cu = ctx.getWorkingCopy().getCompilationUnit(); + TreeMaker tm = ctx.getWorkingCopy().getTreeMaker(); + ImportTree importTree = (ImportTree) path.getLeaf(); + ImportTree Import; + String oldName = importTree.getQualifiedIdentifier().toString(); + if (oldName.equals("junit.framework.Assert")) { + Import = tm.Import(tm.QualIdent("org.junit.Assert"), importTree.isStatic()); + } else if (oldName.equals("junit.framework.Assert.*")) { + Import = tm.Import(tm.QualIdent("org.junit.Assert.*"), importTree.isStatic()); + } else if (oldName.startsWith("junit.framework.Assert.")) { + Import = tm.Import(tm.QualIdent("org.junit.Assert." + oldName.replace("junit.framework.Assert.", "")), importTree.isStatic()); + } else { + return; + } + CompilationUnitTree newCompilationUnit; + newCompilationUnit = tm.removeCompUnitImport(cu, importTree); + newCompilationUnit = tm.addCompUnitImport(newCompilationUnit, Import); + + ctx.getWorkingCopy().rewrite(cu, newCompilationUnit); + } + } + + static class FixMethodInvocationImpl extends JavaFix { + + private final String label; + + FixMethodInvocationImpl(TreePathHandle handle, String label) { + super(handle, label); + this.label = label; + } + + @Override + protected String getText() { + + return label; + } + + @Override + protected void performRewrite(JavaFix.TransformationContext ctx) throws Exception { + TreePath path = ctx.getPath(); + + TreeMaker tm = ctx.getWorkingCopy().getTreeMaker(); + + MethodInvocationTree oldInvocation = (MethodInvocationTree) path.getLeaf(); + MemberSelectTree selector = ((MemberSelectTree) oldInvocation.getMethodSelect()); + + ExpressionTree ms = tm.MemberSelect(tm.QualIdent("org.junit.Assert"), selector.getIdentifier()); + MethodInvocationTree newInvocation = tm.MethodInvocation(Collections.emptyList(), ms, oldInvocation.getArguments()); + + ctx.getWorkingCopy().rewrite(oldInvocation, newInvocation); + } + } + +} diff --git a/src/test/java/de/markiewb/netbeans/plugins/hints/assertfix/ConvertToAssertNull2Test.java b/src/test/java/de/markiewb/netbeans/plugins/hints/assertfix/ConvertToAssertNull2Test.java index 8b10a6b..95c2da0 100644 --- a/src/test/java/de/markiewb/netbeans/plugins/hints/assertfix/ConvertToAssertNull2Test.java +++ b/src/test/java/de/markiewb/netbeans/plugins/hints/assertfix/ConvertToAssertNull2Test.java @@ -17,17 +17,7 @@ public void testERR_computeAssertNullWithoutMessage() throws Exception { + "}\n") .classpath(FileUtil.getArchiveRoot(junit.framework.Assert.class.getProtectionDomain().getCodeSource().getLocation())) .run(ConvertToAssertTrueFalseNull.class) - .findWarning("3:31-3:43:hint:" + Bundle.ERR_computeAssertNullWithoutMessage2()) - .applyFix() - .assertCompilable() - .assertOutput("package test;\n" - + "import junit.framework.Assert;\n" - + "public class Test {\n" - + " public static void main(String[] args) {\n" - + " Assert.assertNull(new Long(0));\n" - + " }\n" - + "}\n"); - + .assertWarnings(); } @Test public void testERR_computeAssertNullWithMessage() throws Exception { @@ -40,16 +30,7 @@ public void testERR_computeAssertNullWithMessage() throws Exception { + "}\n") .classpath(FileUtil.getArchiveRoot(junit.framework.Assert.class.getProtectionDomain().getCodeSource().getLocation())) .run(ConvertToAssertTrueFalseNull.class) - .findWarning("3:31-3:43:hint:" + Bundle.ERR_computeAssertNullWithMessage2()) - .applyFix() - .assertCompilable() - .assertOutput("package test;\n" - + "import junit.framework.Assert;\n" - + "public class Test {\n" - + " public static void main(String[] args) {\n" - + " Assert.assertNull(\"expected different result\", new Long(0));\n" - + " }\n" - + "}\n"); + .assertWarnings(); } } diff --git a/src/test/java/de/markiewb/netbeans/plugins/hints/assertfix/ConvertToAssertTrueFalse2Test.java b/src/test/java/de/markiewb/netbeans/plugins/hints/assertfix/ConvertToAssertTrueFalse2Test.java index bafe96f..2f39b4a 100644 --- a/src/test/java/de/markiewb/netbeans/plugins/hints/assertfix/ConvertToAssertTrueFalse2Test.java +++ b/src/test/java/de/markiewb/netbeans/plugins/hints/assertfix/ConvertToAssertTrueFalse2Test.java @@ -22,16 +22,7 @@ public void testERR_computeAssertTrueWithoutMessage2() throws Exception { + "}\n") .classpath(FileUtil.getArchiveRoot(junit.framework.Assert.class.getProtectionDomain().getCodeSource().getLocation())) .run(ConvertToAssertTrueFalseNull.class) - .findWarning("3:31-3:43:hint:" + Bundle.ERR_computeAssertTrueWithoutMessage2()) - .applyFix() - .assertCompilable() - .assertOutput("package test;\n" - + "import junit.framework.Assert;\n" - + "public class Test {\n" - + " public static void main(String[] args) {\n" - + " Assert.assertTrue(false);\n" - + " }\n" - + "}\n"); + .assertWarnings(); } @Test @@ -45,16 +36,7 @@ public void testERR_computeAssertTrueWithMessage2() throws Exception { + "}\n") .classpath(FileUtil.getArchiveRoot(junit.framework.Assert.class.getProtectionDomain().getCodeSource().getLocation())) .run(ConvertToAssertTrueFalseNull.class) - .findWarning("3:31-3:43:hint:" + Bundle.ERR_computeAssertTrueWithMessage2()) - .applyFix() - .assertCompilable() - .assertOutput("package test;\n" - + "import junit.framework.Assert;\n" - + "public class Test {\n" - + " public static void main(String[] args) {\n" - + " Assert.assertTrue(\"expected different result\", false);\n" - + " }\n" - + "}\n"); + .assertWarnings(); } @Test @@ -68,16 +50,7 @@ public void testERR_computeAssertFalseWithoutMessage2() throws Exception { + "}\n") .classpath(FileUtil.getArchiveRoot(junit.framework.Assert.class.getProtectionDomain().getCodeSource().getLocation())) .run(ConvertToAssertTrueFalseNull.class) - .findWarning("3:31-3:43:hint:" + Bundle.ERR_computeAssertFalseWithoutMessage2()) - .applyFix() - .assertCompilable() - .assertOutput("package test;\n" - + "import junit.framework.Assert;\n" - + "public class Test {\n" - + " public static void main(String[] args) {\n" - + " Assert.assertFalse(false);\n" - + " }\n" - + "}\n"); + .assertWarnings(); } @Test @@ -91,16 +64,7 @@ public void testERR_computeAssertFalseWithMessage2() throws Exception { + "}\n") .classpath(FileUtil.getArchiveRoot(junit.framework.Assert.class.getProtectionDomain().getCodeSource().getLocation())) .run(ConvertToAssertTrueFalseNull.class) - .findWarning("3:31-3:43:hint:" + Bundle.ERR_computeAssertFalseWithMessage2()) - .applyFix() - .assertCompilable() - .assertOutput("package test;\n" - + "import junit.framework.Assert;\n" - + "public class Test {\n" - + " public static void main(String[] args) {\n" - + " Assert.assertFalse(\"expected different result\", true);\n" - + " }\n" - + "}\n"); + .assertWarnings(); } } diff --git a/src/test/java/de/markiewb/netbeans/plugins/hints/assertfix/UpgradeToOJAssertTest.java b/src/test/java/de/markiewb/netbeans/plugins/hints/assertfix/UpgradeToOJAssertTest.java new file mode 100644 index 0000000..d60b5e8 --- /dev/null +++ b/src/test/java/de/markiewb/netbeans/plugins/hints/assertfix/UpgradeToOJAssertTest.java @@ -0,0 +1,116 @@ +package de.markiewb.netbeans.plugins.hints.assertfix; + +import org.junit.Test; +import static org.junit.Assert.*; +import org.netbeans.modules.java.hints.test.api.HintTest; +import org.openide.filesystems.FileUtil; + +/** + * + * @author markiewb + */ +public class UpgradeToOJAssertTest { + + + + @Test + public void testAssertEquals_StaticStarImport() throws Exception { + HintTest.create() + .input("package test;\n" + + "import static junit.framework.Assert.*;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " assertEquals(false, false);\n" + + " }\n" + + "}\n") + .classpath(FileUtil.getArchiveRoot(junit.framework.Assert.class.getProtectionDomain().getCodeSource().getLocation())) + .run(UpgradeToOJAssert.class) + .findWarning("1:0-1:39:warning:" + Bundle.ERR_UpgradeToOJAssert()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "import static org.junit.Assert.*;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " assertEquals(false, false);\n" + + " }\n" + + "}\n"); + + } + + @Test + public void testAssertEquals_StaticImport() throws Exception { + HintTest.create() + .input("package test;\n" + + "import static junit.framework.Assert.assertEquals;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " assertEquals(false, false);\n" + + " }\n" + + "}\n") + .classpath(FileUtil.getArchiveRoot(junit.framework.Assert.class.getProtectionDomain().getCodeSource().getLocation())) + .run(UpgradeToOJAssert.class) + .findWarning("1:0-1:50:warning:" + Bundle.ERR_UpgradeToOJAssert()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "import static org.junit.Assert.assertEquals;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " assertEquals(false, false);\n" + + " }\n" + + "}\n"); + + } + + @Test + public void testAssertEquals_Import() throws Exception { + HintTest.create() + .input("package test;\n" + + "import junit.framework.Assert;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " Assert.assertEquals(false, false);\n" + + " }\n" + + "}\n") + .classpath(FileUtil.getArchiveRoot(junit.framework.Assert.class.getProtectionDomain().getCodeSource().getLocation())) + .run(UpgradeToOJAssert.class) + .findWarning("1:0-1:30:warning:" + Bundle.ERR_UpgradeToOJAssert()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "import org.junit.Assert;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " Assert.assertEquals(false, false);\n" + + " }\n" + + "}\n"); + + } + + @Test + public void testAssertEquals_FQNImport() throws Exception { + HintTest.create() + .input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " junit.framework.Assert.assertEquals(false, false);\n" + + " }\n" + + "}\n") + .classpath(FileUtil.getArchiveRoot(junit.framework.Assert.class.getProtectionDomain().getCodeSource().getLocation())) + .run(UpgradeToOJAssert.class) + .findWarning("3:8-3:57:warning:" + Bundle.ERR_UpgradeToOJAssert()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "import org.junit.Assert;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " Assert.assertEquals(false, false);\n" + + " }\n" + + "}\n"); + + } + + +} diff --git a/src/test/java/example/UpgradeToOJAssertTest.java b/src/test/java/example/UpgradeToOJAssertTest.java new file mode 100644 index 0000000..b2c975a --- /dev/null +++ b/src/test/java/example/UpgradeToOJAssertTest.java @@ -0,0 +1,20 @@ +package example; + +import static junit.framework.Assert.*; +import static junit.framework.Assert.assertEquals; +import junit.framework.Assert.*; +import junit.framework.Assert; + +/** + * + * @author markiewb + */ +public class UpgradeToOJAssertTest { + + public void testX() { + assertEquals(1, 2); + junit.framework.Assert.assertEquals(1, 2); + org.junit.Assert.assertEquals(1, 2); + } + +}