Skip to content

Commit

Permalink
issue #29: Hints for "Convert to assertTrue/assertFalse" should also …
Browse files Browse the repository at this point in the history
…support junit.framework.Assert
  • Loading branch information
markiewb committed Apr 13, 2014
1 parent 1638d5b commit ff0c2c8
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 0 deletions.
19 changes: 19 additions & 0 deletions nb-configuration.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project-shared-configuration>
<!--
This file contains additional configuration written by modules in the NetBeans IDE.
The configuration is intended to be shared among all the users of project and
therefore it is assumed to be part of version control checkout.
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
-->
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
<!--
Properties that influence various parts of the IDE, especially code formatting and the like.
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
That way multiple projects can share the same settings (useful for formatting rules for example).
Any value defined here will override the pom.xml file value but is only applicable to the current project.
-->
<netbeans.compile.on.save>all</netbeans.compile.on.save>
<netbeans.hint.jdkPlatform>JDK_1.7</netbeans.hint.jdkPlatform>
</properties>
</project-shared-configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
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 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");

}
}
15 changes: 15 additions & 0 deletions src/test/java/example/ConvertToAssertTrueFalseExample.java
Original file line number Diff line number Diff line change
@@ -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);
}

}

0 comments on commit ff0c2c8

Please sign in to comment.