diff --git a/nb-configuration.xml b/nb-configuration.xml new file mode 100644 index 0000000..c6ce562 --- /dev/null +++ b/nb-configuration.xml @@ -0,0 +1,19 @@ + + + + + + all + JDK_1.7 + + diff --git a/src/main/java/de/markiewb/netbeans/plugins/hints/assertfix/ConvertToAssertTrueFalse.java b/src/main/java/de/markiewb/netbeans/plugins/hints/assertfix/ConvertToAssertTrueFalse.java index 98b9ee9..c6af942 100644 --- a/src/main/java/de/markiewb/netbeans/plugins/hints/assertfix/ConvertToAssertTrueFalse.java +++ b/src/main/java/de/markiewb/netbeans/plugins/hints/assertfix/ConvertToAssertTrueFalse.java @@ -53,4 +53,36 @@ public static ErrorDescription computeAssertFalseWithMessage(HintContext ctx) { Fix fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_computeAssertFalseWithMessage(), ctx.getPath(), "org.junit.Assert.assertFalse($msg, $var)"); 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); + } } 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 new file mode 100644 index 0000000..173e6ce --- /dev/null +++ b/src/test/java/de/markiewb/netbeans/plugins/hints/assertfix/ConvertToAssertTrueFalse2Test.java @@ -0,0 +1,106 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package de.markiewb.netbeans.plugins.hints.assertfix; + +import org.junit.Test; +import org.netbeans.modules.java.hints.test.api.HintTest; +import org.openide.filesystems.FileUtil; + +public class ConvertToAssertTrueFalse2Test { + + @Test + public void testERR_computeAssertTrueWithoutMessage2() throws Exception { + HintTest.create() + .input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " junit.framework.Assert.assertEquals(true, false);\n" + + " }\n" + + "}\n") + .classpath(FileUtil.getArchiveRoot(junit.framework.Assert.class.getProtectionDomain().getCodeSource().getLocation())) + .run(ConvertToAssertTrueFalse.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"); + + } + @Test + public void testERR_computeAssertTrueWithMessage2() throws Exception { + HintTest.create() + .input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " junit.framework.Assert.assertEquals(\"expected different result\", true, false);\n" + + " }\n" + + "}\n") + .classpath(FileUtil.getArchiveRoot(junit.framework.Assert.class.getProtectionDomain().getCodeSource().getLocation())) + .run(ConvertToAssertTrueFalse.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"); + + } + @Test + public void testERR_computeAssertFalseWithoutMessage2() 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(ConvertToAssertTrueFalse.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"); + + } + @Test + public void testERR_computeAssertFalseWithMessage2() throws Exception { + HintTest.create() + .input("package test;\n" + + "public class Test {\n" + + " public static void main(String[] args) {\n" + + " junit.framework.Assert.assertEquals(\"expected different result\", false, true);\n" + + " }\n" + + "}\n") + .classpath(FileUtil.getArchiveRoot(junit.framework.Assert.class.getProtectionDomain().getCodeSource().getLocation())) + .run(ConvertToAssertTrueFalse.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"); + + } +} diff --git a/src/test/java/example/ConvertToAssertTrueFalseExample.java b/src/test/java/example/ConvertToAssertTrueFalseExample.java new file mode 100644 index 0000000..d9e202d --- /dev/null +++ b/src/test/java/example/ConvertToAssertTrueFalseExample.java @@ -0,0 +1,15 @@ +package example; + +public class ConvertToAssertTrueFalseExample { + + public static void main(String[] args) { + boolean actual = false; + org.junit.Assert.assertEquals("Not correct", true, actual); + junit.framework.Assert.assertEquals("Not correct", true, actual); + org.junit.Assert.assertEquals(true, actual); + junit.framework.Assert.assertEquals(true, actual); + org.junit.Assert.assertEquals(false, actual); + junit.framework.Assert.assertEquals(false, actual); + } + +}