diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/AvoidInlineConditionalsCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/AvoidInlineConditionalsCheckTest.java index 8123101ff..ed34c92b8 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/AvoidInlineConditionalsCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/AvoidInlineConditionalsCheckTest.java @@ -21,6 +21,9 @@ package com.puppycrawl.tools.checkstyle.checks.coding; import static com.puppycrawl.tools.checkstyle.checks.coding.AvoidInlineConditionalsCheck.MSG_KEY; +import java.io.File; +import java.io.IOException; + import org.junit.Assert; import org.junit.Test; @@ -29,15 +32,21 @@ import com.puppycrawl.tools.checkstyle.DefaultConfiguration; public class AvoidInlineConditionalsCheckTest 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 = createCheckConfig(AvoidInlineConditionalsCheck.class); final String[] expected = { - "97:29: " + getCheckMessage(MSG_KEY), - "98:20: " + getCheckMessage(MSG_KEY), - "150:34: " + getCheckMessage(MSG_KEY), + "32:29: " + getCheckMessage(MSG_KEY), + "33:20: " + getCheckMessage(MSG_KEY), + "46:34: " + getCheckMessage(MSG_KEY), }; verify(checkConfig, getPath("InputWhitespace.java"), expected); } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/DeclarationOrderCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/DeclarationOrderCheckTest.java index 5f3630c5c..75232abaa 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/DeclarationOrderCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/DeclarationOrderCheckTest.java @@ -24,6 +24,9 @@ import static com.puppycrawl.tools.checkstyle.checks.coding.DeclarationOrderChec import static com.puppycrawl.tools.checkstyle.checks.coding.DeclarationOrderCheck.MSG_INSTANCE; import static com.puppycrawl.tools.checkstyle.checks.coding.DeclarationOrderCheck.MSG_STATIC; +import java.io.File; +import java.io.IOException; + import org.junit.Assert; import org.junit.Test; @@ -34,6 +37,12 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes; public class DeclarationOrderCheckTest extends BaseCheckTestSupport { + @Override + protected String getPath(String filename) throws IOException { + return super.getPath("checks" + File.separator + + "coding" + File.separator + filename); + } + @Test public void testDefault() throws Exception { final DefaultConfiguration checkConfig = @@ -68,7 +77,7 @@ public class DeclarationOrderCheckTest "178:5: " + getCheckMessage(MSG_INSTANCE), "182:9: " + getCheckMessage(MSG_ACCESS), }; - verify(checkConfig, getPath("coding/InputDeclarationOrder.java"), expected); + verify(checkConfig, getPath("InputDeclarationOrder.java"), expected); } @Test @@ -87,7 +96,7 @@ public class DeclarationOrderCheckTest "152:5: " + getCheckMessage(MSG_CONSTRUCTOR), "178:5: " + getCheckMessage(MSG_INSTANCE), }; - verify(checkConfig, getPath("coding/InputDeclarationOrder.java"), expected); + verify(checkConfig, getPath("InputDeclarationOrder.java"), expected); } @Test @@ -124,7 +133,7 @@ public class DeclarationOrderCheckTest "178:5: " + getCheckMessage(MSG_INSTANCE), "182:9: " + getCheckMessage(MSG_ACCESS), }; - verify(checkConfig, getPath("coding/InputDeclarationOrder.java"), expected); + verify(checkConfig, getPath("InputDeclarationOrder.java"), expected); } @Test @@ -162,5 +171,4 @@ public class DeclarationOrderCheckTest DeclarationOrderCheck check = new DeclarationOrderCheck(); check.visitToken(array); } - } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/EmptyStatementCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/EmptyStatementCheckTest.java index 84c0104e3..2c55ba1ef 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/EmptyStatementCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/EmptyStatementCheckTest.java @@ -21,6 +21,9 @@ package com.puppycrawl.tools.checkstyle.checks.coding; import static com.puppycrawl.tools.checkstyle.checks.coding.EmptyStatementCheck.MSG_KEY; +import java.io.File; +import java.io.IOException; + import org.junit.Assert; import org.junit.Test; @@ -29,6 +32,12 @@ import com.puppycrawl.tools.checkstyle.DefaultConfiguration; public class EmptyStatementCheckTest extends BaseCheckTestSupport { + @Override + protected String getPath(String filename) throws IOException { + return super.getPath("checks" + File.separator + + "coding" + File.separator + filename); + } + @Test public void testEmptyStatements() throws Exception { diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/EqualsHashCodeCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/EqualsHashCodeCheckTest.java index 37de5fd04..b412a915e 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/EqualsHashCodeCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/EqualsHashCodeCheckTest.java @@ -21,6 +21,9 @@ package com.puppycrawl.tools.checkstyle.checks.coding; import static com.puppycrawl.tools.checkstyle.checks.coding.EqualsHashCodeCheck.MSG_KEY; +import java.io.File; +import java.io.IOException; + import org.apache.commons.lang3.ArrayUtils; import org.junit.Assert; import org.junit.Test; @@ -30,14 +33,20 @@ import com.puppycrawl.tools.checkstyle.DefaultConfiguration; public class EqualsHashCodeCheckTest 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 = createCheckConfig(EqualsHashCodeCheck.class); final String[] expected = { - "126:9: " + getCheckMessage(MSG_KEY), - "163:13: " + getCheckMessage(MSG_KEY), - "191:9: " + getCheckMessage(MSG_KEY), + "57:9: " + getCheckMessage(MSG_KEY), + "94:13: " + getCheckMessage(MSG_KEY), + "122:9: " + getCheckMessage(MSG_KEY), }; verify(checkConfig, getPath("InputSemantic.java"), expected); } @@ -47,7 +56,7 @@ public class EqualsHashCodeCheckTest final DefaultConfiguration checkConfig = createCheckConfig(EqualsHashCodeCheck.class); final String[] expected = ArrayUtils.EMPTY_STRING_ARRAY; - verify(checkConfig, getPath("coding/InputEqualsHashCodeCheck.java"), expected); + verify(checkConfig, getPath("InputEqualsHashCodeCheck.java"), expected); } @Test diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/IllegalInstantiationCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/IllegalInstantiationCheckTest.java index 33e782d1e..1a065ab6f 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/IllegalInstantiationCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/IllegalInstantiationCheckTest.java @@ -44,18 +44,18 @@ public class IllegalInstantiationCheckTest checkConfig.addAttribute( "classes", "java.lang.Boolean," - + "com.puppycrawl.tools.checkstyle.InputModifier," + + "com.puppycrawl.tools.checkstyle.checks.coding.InputModifier," + "java.io.File," + "java.awt.Color"); final String[] expected = { "19:21: " + getCheckMessage(MSG_KEY, "java.lang.Boolean"), "24:21: " + getCheckMessage(MSG_KEY, "java.lang.Boolean"), "31:16: " + getCheckMessage(MSG_KEY, "java.lang.Boolean"), - "38:21: " + getCheckMessage(MSG_KEY, "com.puppycrawl.tools.checkstyle.InputModifier"), + "38:21: " + getCheckMessage(MSG_KEY, "com.puppycrawl.tools.checkstyle.checks.coding.InputModifier"), "41:18: " + getCheckMessage(MSG_KEY, "java.io.File"), "44:21: " + getCheckMessage(MSG_KEY, "java.awt.Color"), }; - verify(checkConfig, getPath("InputSemantic.java"), expected); + verify(checkConfig, getPath("checks/coding/InputSemantic.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 a5f4ddb23..7a29a02b1 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 @@ -124,9 +124,9 @@ public class OneTopLevelClassCheckTest extends BaseCheckTestSupport { final DefaultConfiguration checkConfig = createCheckConfig(OneTopLevelClassCheck.class); final String[] expected = { - "83: " + getCheckMessage(MSG_KEY, "InputDeclarationOrderEnum"), + "10: " + getCheckMessage(MSG_KEY, "InputDeclarationOrderEnum"), }; - verify(checkConfig, getPath("coding" + File.separator + "InputDeclarationOrder.java"), expected); + verify(checkConfig, getPath("checks" + File.separator + "design" + File.separator + "InputDeclarationOrder.java"), expected); } @Test diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/coding/InputDeclarationOrder.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputDeclarationOrder.java similarity index 98% rename from src/test/resources/com/puppycrawl/tools/checkstyle/coding/InputDeclarationOrder.java rename to src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputDeclarationOrder.java index c3aa136d4..90a65692f 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/coding/InputDeclarationOrder.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputDeclarationOrder.java @@ -1,4 +1,4 @@ -package com.puppycrawl.tools.checkstyle.coding; +package com.puppycrawl.tools.checkstyle.checks.coding; public class InputDeclarationOrder { diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputEmptyStatement.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputEmptyStatement.java new file mode 100644 index 000000000..2407c5001 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputEmptyStatement.java @@ -0,0 +1,83 @@ +package com.puppycrawl.tools.checkstyle.checks.coding; + +/** + * Input class for testing EmptyStatementCheck + * @author Rick Giles + * @version 5-May-2003 + */ +public class InputEmptyStatement +{ + public InputEmptyStatement() + { + ; + } + + public void EmptyMethod() + { + ; + } + + public void EmptyStatements(boolean cond) + { + for (;cond;); + + for (;cond;) + { + ; + } + + if (true); + + if (true) + { + ; + } + + if (cond) + { + int i; + } + + else + { + ; + } + + switch (1) + { + case 1 : + ; + default : + ; + } + + while (cond); + + while (cond) + { + ; + } + + do; + while (cond); + + do + { + ; + } + while (cond); + + try + { + ; + } + catch (Exception ex) + { + ; + } + finally + { + ; + } + } +} diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/coding/InputEqualsHashCodeCheck.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputEqualsHashCodeCheck.java similarity index 91% rename from src/test/resources/com/puppycrawl/tools/checkstyle/coding/InputEqualsHashCodeCheck.java rename to src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputEqualsHashCodeCheck.java index d3bfa457e..a6734fe11 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/coding/InputEqualsHashCodeCheck.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputEqualsHashCodeCheck.java @@ -1,4 +1,4 @@ -package com.puppycrawl.tools.checkstyle.coding; +package com.puppycrawl.tools.checkstyle.checks.coding; public class InputEqualsHashCodeCheck { public boolean notEquals() { diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputSemantic.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputSemantic.java new file mode 100644 index 000000000..2cd27f38d --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputSemantic.java @@ -0,0 +1,135 @@ +//////////////////////////////////////////////////////////////////////////////// +// Test case file for checkstyle. +// Created: 2001 +//////////////////////////////////////////////////////////////////////////////// +package com.puppycrawl.tools.checkstyle.checks.coding; + +import java.io.*; // star import for instantiation tests +import java.awt.Dimension; // explicit import for instantiation tests +import java.awt.Color; + +/** + * Test case for detecting simple semantic errors. + * @author Lars Kühne + **/ +class InputSemantic +{ + /* Boolean instantiation in a static initializer */ + static { + Boolean x = new Boolean(true); + } + + /* Boolean instantiation in a non-static initializer */ + { + Boolean x = new Boolean(true); + Boolean[] y = new Boolean[]{Boolean.TRUE, Boolean.FALSE}; + } + + /** fully qualified Boolean instantiation in a method. **/ + Boolean getBoolean() + { + return new java.lang.Boolean(true); + } + + void otherInstantiations() + { + // instantiation of classes in the same package + Object o1 = new InputBraces(); + Object o2 = new InputModifier(); + // classes in another package with .* import + ByteArrayOutputStream s = new ByteArrayOutputStream(); + File f = new File("/tmp"); + // classes in another package with explicit import + Dimension dim = new Dimension(); + Color col = new Color(0, 0, 0); + } + + public class EqualsVsHashCode1 + { + public boolean equals(int a) // wrong arg type, don't flag + { + return a == 1; + } + } + + public class EqualsVsHashCode2 + { + public boolean equals(String a) // flag + { + return true; + } + } + + public class EqualsVsHashCode3 + { + public boolean equals(Object a) // don't flag + { + return true; + } + + public int hashCode() + { + return 0; + } + } + + public class EqualsVsHashCode4 + { + // in anon inner class + ByteArrayOutputStream bos1 = new ByteArrayOutputStream() + { + public boolean equals(Object a) // don't flag + { + return true; + } + + public int hashCode() + { + return 0; + } + }; + + ByteArrayOutputStream bos2 = new ByteArrayOutputStream() + { + public boolean equals(Object a) // flag + { + return true; + } + }; + } + + public void triggerEmptyBlockWithoutBlock() + { + // an if statement without a block to increase test coverage + if (true) + return; + } + + // empty instance initializer + { + } + + public class EqualsVsHashCode5 + { + public boolean equals(int a) // wrong arg type, don't flag even with generics + { + return a == 1; + } + } + + public class EqualsVsHashCode6 + { + public boolean equals(Comparable a) // flag, weven with generics + { + return true; + } + } + + private class InputBraces { + + } + + private class InputModifier { + + } +} diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputWhitespace.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputWhitespace.java new file mode 100644 index 000000000..f7ba5f589 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputWhitespace.java @@ -0,0 +1,54 @@ +//////////////////////////////////////////////////////////////////////////////// +// Test case file for checkstyle. +// Created: 2001 +//////////////////////////////////////////////////////////////////////////////// +package com . puppycrawl + .tools. + checkstyle.checks.coding; + +/** + * Class for testing inline conditionals. + * error missing author tag + **/ +class InputWhitespace +{ + /** method **/ + void method1() + { + final int a = 1; + int b= 1; // Ignore 1 + b=1; // Ignore 1 + b+=1; // Ignore 1 + b -=- 1 + (+ b); // Ignore 2 + b = b ++ + b --; // Ignore 1 + b = ++ b - -- b; // Ignore 1 + } + + private int mVar4 = 1; + + /** test questions **/ + private void testQuestions() + { + boolean b = (1 == 2)?true:false; + b = (1==2) ? false : true; + } + + /** assert statement test */ + public void assertTest() + { + // OK + assert true; + + // OK + assert true : "Whups"; + + // evil colons, should be OK + assert "OK".equals(null) ? false : true : "Whups"; + + // missing WS around assert + assert(true); + + // missing WS around colon + assert true:"Whups"; + } +} diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/design/InputDeclarationOrder.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/design/InputDeclarationOrder.java new file mode 100644 index 000000000..d47b8e83c --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/design/InputDeclarationOrder.java @@ -0,0 +1,18 @@ +package com.puppycrawl.tools.checkstyle.checks.design; + +public class InputDeclarationOrder +{ + private class InnerClass + { + } +} + +enum InputDeclarationOrderEnum +{ + ENUM_VALUE_1; + + class AsyncProcess { + private final int startLogErrorsCnt = 0; + protected final int maxTotalConcurrentTasks = 0; + } +}