Skip to content

Commit

Permalink
issue #13: Hints for "Convert to assertTrue/assertFalse"
Browse files Browse the repository at this point in the history
  • Loading branch information
markiewb committed Oct 6, 2013
1 parent 426681b commit c11439a
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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.netbeans.spi.editor.hints.ErrorDescription;
import org.netbeans.spi.editor.hints.Fix;
import org.netbeans.spi.java.hints.ConstraintVariableType;
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.TriggerPattern;
import org.openide.util.NbBundle.Messages;

@Messages({
"DN_SimplifyAssertMethods=Convert to assertTrue/assertFalse",
"DESC_SimplifyAssertMethods=Converts <tt>assertEquals</tt> expressions to their <tt>assertTrue/assertFalse</tt> counterparts. <br>For example: <tt>org.junit.Assert.assertEquals($msg, true, $var)</tt> will be transformed to <tt>org.junit.Assert.assertTrue($msg, $var)</tt>",
})
@Hint(displayName = "#DN_SimplifyAssertMethods", description = "#DESC_SimplifyAssertMethods", category = "testing")
public class ConvertToAssertTrueFalse {

@TriggerPattern(value = "org.junit.Assert.assertEquals(true, $var)")
@Messages("ERR_computeAssertTrueWithoutMessage=Replace with assertTrue")
public static ErrorDescription computeAssertTrueWithoutMessage(HintContext ctx) {
Fix fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_computeAssertTrueWithoutMessage(), ctx.getPath(), "org.junit.Assert.assertTrue($var)");
return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_computeAssertTrueWithoutMessage(), fix);
}

@TriggerPattern(value = "org.junit.Assert.assertEquals(false, $var)")
@Messages("ERR_computeAssertFalseWithoutMessage=Replace with assertFalse")
public static ErrorDescription computeAssertFalseWithoutMessage(HintContext ctx) {
Fix fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_computeAssertFalseWithoutMessage(), ctx.getPath(), "org.junit.Assert.assertFalse($var)");
return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_computeAssertFalseWithoutMessage(), fix);
}

@TriggerPattern(value = "org.junit.Assert.assertEquals($msg, true, $var)", constraints = {
@ConstraintVariableType(variable = "$msg", type = "java.lang.String")
})
@Messages("ERR_computeAssertTrueWithMessage=Replace with assertTrue")
public static ErrorDescription computeAssertTrueWithMessage(HintContext ctx) {
Fix fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_computeAssertTrueWithMessage(), ctx.getPath(), "org.junit.Assert.assertTrue($msg, $var)");
return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_computeAssertTrueWithMessage(), fix);
}

@TriggerPattern(value = "org.junit.Assert.assertEquals($msg, false, $var)", constraints = {
@ConstraintVariableType(variable = "$msg", type = "java.lang.String")
})
@Messages("ERR_computeAssertFalseWithMessage=Replace with assertFalse")
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);
}
}
Original file line number Diff line number Diff line change
@@ -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 ConvertToAssertTrueFalseTest {

@Test
public void testERR_computeAssertTrueWithoutMessage() throws Exception {
HintTest.create()
.input("package test;\n"
+ "public class Test {\n"
+ " public static void main(String[] args) {\n"
+ " org.junit.Assert.assertEquals(true, false);\n"
+ " }\n"
+ "}\n")
.classpath(FileUtil.getArchiveRoot(org.junit.Assert.class.getProtectionDomain().getCodeSource().getLocation()))
.run(ConvertToAssertTrueFalse.class)
.findWarning("3:25-3:37:verifier:" + Bundle.ERR_computeAssertTrueWithoutMessage())
.applyFix()
.assertCompilable()
.assertOutput("package test;\n"
+ "import org.junit.Assert;\n"
+ "public class Test {\n"
+ " public static void main(String[] args) {\n"
+ " Assert.assertTrue(false);\n"
+ " }\n"
+ "}\n");

}
@Test
public void testERR_computeAssertTrueWithMessage() throws Exception {
HintTest.create()
.input("package test;\n"
+ "public class Test {\n"
+ " public static void main(String[] args) {\n"
+ " org.junit.Assert.assertEquals(\"expected different result\", true, false);\n"
+ " }\n"
+ "}\n")
.classpath(FileUtil.getArchiveRoot(org.junit.Assert.class.getProtectionDomain().getCodeSource().getLocation()))
.run(ConvertToAssertTrueFalse.class)
.findWarning("3:25-3:37:verifier:" + Bundle.ERR_computeAssertTrueWithMessage())
.applyFix()
.assertCompilable()
.assertOutput("package test;\n"
+ "import org.junit.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_computeAssertFalseWithoutMessage() throws Exception {
HintTest.create()
.input("package test;\n"
+ "public class Test {\n"
+ " public static void main(String[] args) {\n"
+ " org.junit.Assert.assertEquals(false, false);\n"
+ " }\n"
+ "}\n")
.classpath(FileUtil.getArchiveRoot(org.junit.Assert.class.getProtectionDomain().getCodeSource().getLocation()))
.run(ConvertToAssertTrueFalse.class)
.findWarning("3:25-3:37:verifier:" + Bundle.ERR_computeAssertFalseWithoutMessage())
.applyFix()
.assertCompilable()
.assertOutput("package test;\n"
+ "import org.junit.Assert;\n"
+ "public class Test {\n"
+ " public static void main(String[] args) {\n"
+ " Assert.assertFalse(false);\n"
+ " }\n"
+ "}\n");

}
@Test
public void testERR_computeAssertFalseWithMessage() throws Exception {
HintTest.create()
.input("package test;\n"
+ "public class Test {\n"
+ " public static void main(String[] args) {\n"
+ " org.junit.Assert.assertEquals(\"expected different result\", false, true);\n"
+ " }\n"
+ "}\n")
.classpath(FileUtil.getArchiveRoot(org.junit.Assert.class.getProtectionDomain().getCodeSource().getLocation()))
.run(ConvertToAssertTrueFalse.class)
.findWarning("3:25-3:37:verifier:" + Bundle.ERR_computeAssertFalseWithMessage())
.applyFix()
.assertCompilable()
.assertOutput("package test;\n"
+ "import org.junit.Assert;\n"
+ "public class Test {\n"
+ " public static void main(String[] args) {\n"
+ " Assert.assertFalse(\"expected different result\", true);\n"
+ " }\n"
+ "}\n");

}
}

0 comments on commit c11439a

Please sign in to comment.