diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/DescendantTokenCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/DescendantTokenCheckTest.java index 1d418d435..1305e124d 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/DescendantTokenCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/DescendantTokenCheckTest.java @@ -158,7 +158,7 @@ public class DescendantTokenCheckTest extends BaseCheckTestSupport { "17:9: switch without \"default\" clause.", }; - verify(checkConfig, getPath("InputMissingSwitchDefault.java"), expected); + verify(checkConfig, getPath("checks/InputMissingSwitchDefault.java"), expected); } @Test @@ -172,11 +172,11 @@ public class DescendantTokenCheckTest extends BaseCheckTestSupport { checkConfig.addAttribute("maximumMessage", "Literal Strings should be compared using equals(), not ''==''."); final String[] expected = { - "11:18: Literal Strings should be compared using equals(), not '=='.", - "16:20: Literal Strings should be compared using equals(), not '=='.", - "21:22: Literal Strings should be compared using equals(), not '=='.", + "7:18: Literal Strings should be compared using equals(), not '=='.", + "12:20: Literal Strings should be compared using equals(), not '=='.", + "17:22: Literal Strings should be compared using equals(), not '=='.", }; - verify(checkConfig, getPath("coding" + File.separator + "InputStringLiteralEquality.java"), expected); + verify(checkConfig, getPath("checks" + File.separator + "InputStringLiteralEquality.java"), expected); } @Test diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/MissingSwitchDefaultCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/MissingSwitchDefaultCheckTest.java index c63e30b46..08d274ab6 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/MissingSwitchDefaultCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/MissingSwitchDefaultCheckTest.java @@ -21,6 +21,9 @@ package com.puppycrawl.tools.checkstyle.checks.coding; import static com.puppycrawl.tools.checkstyle.checks.coding.MissingSwitchDefaultCheck.MSG_KEY; +import java.io.File; +import java.io.IOException; + import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -37,6 +40,12 @@ public class MissingSwitchDefaultCheckTest checkConfig = createCheckConfig(MissingSwitchDefaultCheck.class); } + @Override + protected String getPath(String filename) throws IOException { + return super.getPath("checks" + File.separator + + "coding" + File.separator + filename); + } + @Test public void testMissingSwitchDefault() throws Exception { final String[] expected = { diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/NoCloneCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/NoCloneCheckTest.java index 09214a756..3b8c435c9 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/NoCloneCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/NoCloneCheckTest.java @@ -22,6 +22,7 @@ package com.puppycrawl.tools.checkstyle.checks.coding; import static com.puppycrawl.tools.checkstyle.checks.coding.NoCloneCheck.MSG_KEY; import java.io.File; +import java.io.IOException; import org.junit.Assert; import org.junit.Test; @@ -34,6 +35,12 @@ import com.puppycrawl.tools.checkstyle.DefaultConfiguration; */ public class NoCloneCheckTest extends BaseCheckTestSupport { + @Override + protected String getPath(String filename) throws IOException { + return super.getPath("checks" + File.separator + + "coding" + File.separator + filename); + } + @Test public void testHasClone() throws Exception { @@ -48,7 +55,7 @@ public class NoCloneCheckTest "60: " + getCheckMessage(MSG_KEY), "98: " + getCheckMessage(MSG_KEY), }; - verify(checkConfig, getPath("coding" + File.separator + "InputClone.java"), expected); + verify(checkConfig, getPath("InputClone.java"), expected); } @Test diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/RequireThisCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/RequireThisCheckTest.java index 3450f4aea..6afa8090c 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/RequireThisCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/RequireThisCheckTest.java @@ -23,6 +23,7 @@ import static com.puppycrawl.tools.checkstyle.checks.coding.RequireThisCheck.MSG import static com.puppycrawl.tools.checkstyle.checks.coding.RequireThisCheck.MSG_VARIABLE; import java.io.File; +import java.io.IOException; import org.apache.commons.lang3.ArrayUtils; import org.junit.Assert; @@ -32,6 +33,12 @@ import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport; import com.puppycrawl.tools.checkstyle.DefaultConfiguration; public class RequireThisCheckTest extends BaseCheckTestSupport { + @Override + protected String getPath(String filename) throws IOException { + return super.getPath("checks" + File.separator + + "coding" + File.separator + filename); + } + @Test public void testIt() throws Exception { final DefaultConfiguration checkConfig = @@ -44,7 +51,7 @@ public class RequireThisCheckTest extends BaseCheckTestSupport { "56:9: " + getCheckMessage(MSG_VARIABLE, "z", "\"this\""), }; verify(checkConfig, - getPath("coding" + File.separator + "InputRequireThis.java"), + getPath("InputRequireThis.java"), expected); } @@ -57,7 +64,7 @@ public class RequireThisCheckTest extends BaseCheckTestSupport { "17:9: " + getCheckMessage(MSG_METHOD, "method1", "\"this\""), }; verify(checkConfig, - getPath("coding" + File.separator + "InputRequireThis.java"), + getPath("InputRequireThis.java"), expected); } @@ -73,7 +80,7 @@ public class RequireThisCheckTest extends BaseCheckTestSupport { "56:9: " + getCheckMessage(MSG_VARIABLE, "z", "\"this\""), }; verify(checkConfig, - getPath("coding" + File.separator + "InputRequireThis.java"), + getPath("InputRequireThis.java"), expected); } @@ -94,7 +101,7 @@ public class RequireThisCheckTest extends BaseCheckTestSupport { "8:16: " + getCheckMessage(MSG_METHOD, "other", "\"this\""), }; verify(checkConfig, - getPath("coding" + File.separator + "InputRequireThis2.java"), + getPath("InputRequireThis2.java"), expected); } @@ -111,7 +118,7 @@ public class RequireThisCheckTest extends BaseCheckTestSupport { final DefaultConfiguration checkConfig = createCheckConfig(RequireThisCheck.class); final String[] expected = ArrayUtils.EMPTY_STRING_ARRAY; verify(checkConfig, - getPath("coding" + File.separator + "InputRequireThis3.java"), + getPath("InputRequireThis3.java"), expected); } } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/StringLiteralEqualityCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/StringLiteralEqualityCheckTest.java index f904c0d43..586136bb9 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/StringLiteralEqualityCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/StringLiteralEqualityCheckTest.java @@ -22,6 +22,7 @@ package com.puppycrawl.tools.checkstyle.checks.coding; import static com.puppycrawl.tools.checkstyle.checks.coding.StringLiteralEqualityCheck.MSG_KEY; import java.io.File; +import java.io.IOException; import org.junit.Assert; import org.junit.Test; @@ -31,6 +32,12 @@ import com.puppycrawl.tools.checkstyle.DefaultConfiguration; public class StringLiteralEqualityCheckTest extends BaseCheckTestSupport { + @Override + protected String getPath(String filename) throws IOException { + return super.getPath("checks" + File.separator + + "coding" + File.separator + filename); + } + @Test public void testIt() throws Exception { final DefaultConfiguration checkConfig = @@ -40,7 +47,7 @@ public class StringLiteralEqualityCheckTest "16:20: " + getCheckMessage(MSG_KEY, "=="), "21:22: " + getCheckMessage(MSG_KEY, "=="), }; - verify(checkConfig, getPath("coding" + File.separator + "InputStringLiteralEquality.java"), expected); + verify(checkConfig, getPath("InputStringLiteralEquality.java"), expected); } @Test diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/SuperCloneCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/SuperCloneCheckTest.java index c4c599a9d..6b3784927 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/SuperCloneCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/SuperCloneCheckTest.java @@ -40,7 +40,7 @@ public class SuperCloneCheckTest "35:19: " + getCheckMessage(MSG_KEY, "clone", "super.clone"), "60:48: " + getCheckMessage(MSG_KEY, "clone", "super.clone"), }; - verify(checkConfig, getPath("coding/InputClone.java"), expected); + verify(checkConfig, getPath("checks/coding/InputClone.java"), expected); } @Test diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/design/OneTopLevelClassCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/design/OneTopLevelClassCheckTest.java index 7a29a02b1..335b73ff0 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/design/OneTopLevelClassCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/design/OneTopLevelClassCheckTest.java @@ -109,14 +109,15 @@ public class OneTopLevelClassCheckTest extends BaseCheckTestSupport { createCheckConfig(OneTopLevelClassCheck.class); final String[] expected = { "25: " + getCheckMessage(MSG_KEY, "NoSuperClone"), - "33: " + getCheckMessage(MSG_KEY, "InnerClone"), - "50: " + getCheckMessage(MSG_KEY, "CloneWithTypeArguments"), - "58: " + getCheckMessage(MSG_KEY, "CloneWithTypeArgumentsAndNoSuper"), - "67: " + getCheckMessage(MSG_KEY, "MyClassWithGenericSuperMethod"), - "84: " + getCheckMessage(MSG_KEY, "AnotherClass"), - "97: " + getCheckMessage(MSG_KEY, "NativeTest"), + "29: " + getCheckMessage(MSG_KEY, "InnerClone"), + "33: " + getCheckMessage(MSG_KEY, "CloneWithTypeArguments"), + "37: " + getCheckMessage(MSG_KEY, "CloneWithTypeArgumentsAndNoSuper"), + "41: " + getCheckMessage(MSG_KEY, "MyClassWithGenericSuperMethod"), + "45: " + getCheckMessage(MSG_KEY, "AnotherClass"), + "48: " + getCheckMessage(MSG_KEY, "NativeTest"), }; - verify(checkConfig, getPath("coding" + File.separator + "InputClone.java"), expected); + verify(checkConfig, getPath("checks" + File.separator + "design" + + File.separator + "InputClone.java"), expected); } @Test diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/InputMissingSwitchDefault.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/InputMissingSwitchDefault.java similarity index 88% rename from src/test/resources/com/puppycrawl/tools/checkstyle/InputMissingSwitchDefault.java rename to src/test/resources/com/puppycrawl/tools/checkstyle/checks/InputMissingSwitchDefault.java index cd0c0a208..7db2bd5d8 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/InputMissingSwitchDefault.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/InputMissingSwitchDefault.java @@ -1,4 +1,4 @@ -package com.puppycrawl.tools.checkstyle; +package com.puppycrawl.tools.checkstyle.checks; public class InputMissingSwitchDefault { public void foo() { diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/InputStringLiteralEquality.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/InputStringLiteralEquality.java new file mode 100644 index 000000000..d6609d39a --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/InputStringLiteralEquality.java @@ -0,0 +1,43 @@ +package com.puppycrawl.tools.checkstyle.checks; + +public class InputStringLiteralEquality +{ + void foo(String name) + { + if (name == "Lars") + { + // flagged, should use equals + } + + if ("Oleg" == name) + { + // flagged, should use equals + } + + if ("Oliver" == "Oliver") + { + // doesn't make much sense because this can be evaluated + // to true at compile time, but is flagged anyway + } + + String compare = "Rick"; + if (name == compare) + { + // currently not flagged. + // + // Implementing this is very complicated, we would need + // - type info on the == operands + // - prevent false alarms where the user explicitly wants + // to compare object identities + // + // My current feeling is that we should leave finding + // this one to manual code inspections. After all MCI is + // what some of us get paid for :-) + } + + if ("Rick".toUpperCase() == "Rick".toLowerCase()) + { + // completly dynamic, don't flag + } + } +} diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/Input15Extensions.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/Input15Extensions.java new file mode 100644 index 000000000..11ec5dfb6 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/Input15Extensions.java @@ -0,0 +1,36 @@ +// someexamples of 1.5 extensions +package com.puppycrawl.tools.checkstyle.checks.coding; + +@interface MyAnnotation1 { + String name(); + int version(); +} + +@MyAnnotation1(name = "ABC", version = 1) +public class Input15Extensions +{ + +} + +enum Enum2 +{ + A, B, C; + Enum2() {} + public String toString() { + return ""; //some custom implementation + } +} + +interface TestRequireThisEnum +{ + enum DAY_OF_WEEK + { + SUNDAY, + MONDAY, + TUESDAY, + WEDNESDAY, + THURSDAY, + FRIDAY, + SATURDAY + } +} diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/coding/InputClone.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputClone.java similarity index 97% rename from src/test/resources/com/puppycrawl/tools/checkstyle/coding/InputClone.java rename to src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputClone.java index d21a4fb3a..d64148379 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/coding/InputClone.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputClone.java @@ -1,4 +1,4 @@ -package com.puppycrawl.tools.checkstyle.coding; +package com.puppycrawl.tools.checkstyle.checks.coding; public class InputClone {/* class body */ public InputClone() throws CloneNotSupportedException diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputMissingSwitchDefault.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputMissingSwitchDefault.java new file mode 100644 index 000000000..17a73a095 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputMissingSwitchDefault.java @@ -0,0 +1,22 @@ +package com.puppycrawl.tools.checkstyle.checks.coding; + +public class InputMissingSwitchDefault { + public void foo() { + int i = 1; + switch (i) { + case 1: i++; break; + case 2: i--; break; + default: return; + } + } +} + +class bad_test { + public void foo() { + int i = 1; + switch (i) { + case 1: i++; break; + case 2: i--; break; + } + } +} diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/coding/InputRequireThis.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputRequireThis.java similarity index 96% rename from src/test/resources/com/puppycrawl/tools/checkstyle/coding/InputRequireThis.java rename to src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputRequireThis.java index 14659958e..e8cdbb7b7 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/coding/InputRequireThis.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputRequireThis.java @@ -1,4 +1,4 @@ -package com.puppycrawl.tools.checkstyle.coding; +package com.puppycrawl.tools.checkstyle.checks.coding; import java.awt.Toolkit; import java.io.ByteArrayInputStream; diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/coding/InputRequireThis2.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputRequireThis2.java similarity index 79% rename from src/test/resources/com/puppycrawl/tools/checkstyle/coding/InputRequireThis2.java rename to src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputRequireThis2.java index 8de704356..032337fab 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/coding/InputRequireThis2.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputRequireThis2.java @@ -1,4 +1,4 @@ -package com.puppycrawl.tools.checkstyle.coding; +package com.puppycrawl.tools.checkstyle.checks.coding; public class InputRequireThis2 { private final int number = 1; diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/coding/InputRequireThis3.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputRequireThis3.java similarity index 88% rename from src/test/resources/com/puppycrawl/tools/checkstyle/coding/InputRequireThis3.java rename to src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputRequireThis3.java index d4bc4931a..4d326a652 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/coding/InputRequireThis3.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputRequireThis3.java @@ -1,4 +1,4 @@ -package com.puppycrawl.tools.checkstyle.coding; +package com.puppycrawl.tools.checkstyle.checks.coding; public class InputRequireThis3 { interface AnonWithEmpty { diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/coding/InputStringLiteralEquality.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputStringLiteralEquality.java similarity index 95% rename from src/test/resources/com/puppycrawl/tools/checkstyle/coding/InputStringLiteralEquality.java rename to src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputStringLiteralEquality.java index 0b008c259..8a8f13af7 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/coding/InputStringLiteralEquality.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputStringLiteralEquality.java @@ -1,4 +1,4 @@ -package com.puppycrawl.tools.checkstyle.coding; +package com.puppycrawl.tools.checkstyle.checks.coding; /** * Input file for the StringLiteralEqualityCheck diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/design/InputClone.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/design/InputClone.java new file mode 100644 index 000000000..d70c8595e --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/design/InputClone.java @@ -0,0 +1,50 @@ +package com.puppycrawl.tools.checkstyle.checks.design; +public class InputClone +{/* class body */ + public InputClone() throws CloneNotSupportedException + { //constructor body + super.equals(new String()); + super.clone(); + } + + public Object clone() throws CloneNotSupportedException + { + return super.clone(); + } + + public void method() throws CloneNotSupportedException + { + super.clone(); + } + + { + super.clone(); + } +} + +class NoSuperClone +{ +} + +class InnerClone +{ +} + +class CloneWithTypeArguments extends CloneWithTypeArgumentsAndNoSuper +{ +} + +class CloneWithTypeArgumentsAndNoSuper +{ +} + +class MyClassWithGenericSuperMethod +{ +} + +class AnotherClass { +} + +class NativeTest { + public native Object clone(); +}