diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/regexp/RegexpCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/regexp/RegexpCheckTest.java index 76c307fb5..7f40212cf 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/regexp/RegexpCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/regexp/RegexpCheckTest.java @@ -24,6 +24,9 @@ import static com.puppycrawl.tools.checkstyle.checks.regexp.RegexpCheck.MSG_ILLE import static com.puppycrawl.tools.checkstyle.checks.regexp.RegexpCheck.MSG_REQUIRED_REGEXP; import static org.junit.Assert.assertArrayEquals; +import java.io.File; +import java.io.IOException; + import org.apache.commons.lang3.ArrayUtils; import org.junit.Test; @@ -31,6 +34,11 @@ import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport; import com.puppycrawl.tools.checkstyle.DefaultConfiguration; public class RegexpCheckTest extends BaseCheckTestSupport { + @Override + protected String getPath(String filename) throws IOException { + return super.getPath("checks" + File.separator + + "regexp" + File.separator + filename); + } @Test public void testGetRequiredTokens() { diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/regexp/RegexpMultilineCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/regexp/RegexpMultilineCheckTest.java index b08844281..093ff3fe2 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/regexp/RegexpMultilineCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/regexp/RegexpMultilineCheckTest.java @@ -25,6 +25,7 @@ import static com.puppycrawl.tools.checkstyle.checks.regexp.MultilineDetector.RE import static com.puppycrawl.tools.checkstyle.checks.regexp.MultilineDetector.STACKOVERFLOW; import java.io.File; +import java.io.IOException; import org.apache.commons.lang3.ArrayUtils; import org.junit.Before; @@ -47,6 +48,12 @@ public class RegexpMultilineCheckTest extends BaseFileSetCheckTestSupport { checkConfig = createCheckConfig(RegexpMultilineCheck.class); } + @Override + protected String getPath(String filename) throws IOException { + return super.getPath("checks" + File.separator + + "regexp" + File.separator + filename); + } + @Test public void testIt() throws Exception { final String illegal = "System\\.(out)|(err)\\.print(ln)?\\("; @@ -196,5 +203,4 @@ public class RegexpMultilineCheckTest extends BaseFileSetCheckTestSupport { final String[] expected = ArrayUtils.EMPTY_STRING_ARRAY; verify(checkConfig, getPath("InputSemantic.java"), expected); } - } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/regexp/RegexpSinglelineCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/regexp/RegexpSinglelineCheckTest.java index e60bfec86..6e2f9ac01 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/regexp/RegexpSinglelineCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/regexp/RegexpSinglelineCheckTest.java @@ -22,6 +22,9 @@ package com.puppycrawl.tools.checkstyle.checks.regexp; import static com.puppycrawl.tools.checkstyle.checks.regexp.MultilineDetector.REGEXP_EXCEEDED; import static com.puppycrawl.tools.checkstyle.checks.regexp.MultilineDetector.REGEXP_MINIMUM; +import java.io.File; +import java.io.IOException; + import org.apache.commons.lang3.ArrayUtils; import org.junit.Before; import org.junit.Test; @@ -37,6 +40,12 @@ public class RegexpSinglelineCheckTest extends BaseFileSetCheckTestSupport { checkConfig = createCheckConfig(RegexpSinglelineCheck.class); } + @Override + protected String getPath(String filename) throws IOException { + return super.getPath("checks" + File.separator + + "regexp" + File.separator + filename); + } + @Test public void testIt() throws Exception { final String illegal = "System\\.(out)|(err)\\.print(ln)?\\("; @@ -106,5 +115,4 @@ public class RegexpSinglelineCheckTest extends BaseFileSetCheckTestSupport { verify(checkConfig, getPath("InputSemantic.java"), expected); } - } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/regexp/RegexpSinglelineJavaCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/regexp/RegexpSinglelineJavaCheckTest.java index f10ddafad..e9a6facf0 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/regexp/RegexpSinglelineJavaCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/regexp/RegexpSinglelineJavaCheckTest.java @@ -23,6 +23,9 @@ import static com.puppycrawl.tools.checkstyle.checks.regexp.MultilineDetector.RE import static com.puppycrawl.tools.checkstyle.checks.regexp.MultilineDetector.REGEXP_MINIMUM; import static org.junit.Assert.assertArrayEquals; +import java.io.File; +import java.io.IOException; + import org.apache.commons.lang3.ArrayUtils; import org.junit.Before; import org.junit.Test; @@ -38,6 +41,12 @@ public class RegexpSinglelineJavaCheckTest extends BaseCheckTestSupport { checkConfig = createCheckConfig(RegexpSinglelineJavaCheck.class); } + @Override + protected String getPath(String filename) throws IOException { + return super.getPath("checks" + File.separator + + "regexp" + File.separator + filename); + } + @Test public void testGetRequiredTokens() { RegexpSinglelineJavaCheck checkObj = new RegexpSinglelineJavaCheck(); diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/InputStartingWithEmptyLine.java b/src/test/resources/com/puppycrawl/tools/checkstyle/InputStartingWithEmptyLine.java deleted file mode 100644 index 86c4296e8..000000000 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/InputStartingWithEmptyLine.java +++ /dev/null @@ -1,2 +0,0 @@ - -package com.puppycrawl.tools.checkstyle; diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/regexp/InputSemantic.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/regexp/InputSemantic.java new file mode 100644 index 000000000..fd1341969 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/regexp/InputSemantic.java @@ -0,0 +1,222 @@ +//////////////////////////////////////////////////////////////////////////////// +// Test case file for checkstyle. +// Created: 2001 +//////////////////////////////////////////////////////////////////////////////// +package com.puppycrawl.tools.checkstyle.checks.regexp; + +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); + } + + void exHandlerTest() + { + try { + ; // do stuff and don't handle exceptions in some cases + } + catch (IllegalStateException emptyCatchIsAlwaysAnError) { + } + catch (NullPointerException ex) { + // can never happen, but only commentig this is currently an error + // Possible future enhancement: allowEmptyCatch="commented" + } + catch (ArrayIndexOutOfBoundsException ex) { + ; + // can never happen, semicolon makes checkstyle happy + // this is a workaround for above problem + } + catch (NegativeArraySizeException ex) { + { + } + // can never happen, empty compound statement is another workaround + } + catch (UnsupportedOperationException handledException) { + System.out.println(handledException.getMessage()); + } + catch (SecurityException ex) { /* hello */ } + catch (StringIndexOutOfBoundsException ex) {} + catch (IllegalArgumentException ex) { } + + try { + } + finally { + } + try { + // something + } + finally { + // something + } + try { + ; // something + } + finally { + ; // statement + } + } + + /** test **/ + private static final long IGNORE = 666l + 666L; + + + + + + + + + + + + + + + + + + + + + + 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 { + + } + + synchronized void foo() { + synchronized (this) {} // not OK + synchronized (Class.class) { // OK + synchronized (new Object()) { + // not OK if checking statements + } + } + } + + + static { + + int a = 0;} + + static { + + } +} diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/regexp/InputStartingWithEmptyLine.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/regexp/InputStartingWithEmptyLine.java new file mode 100644 index 000000000..630e139ac --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/regexp/InputStartingWithEmptyLine.java @@ -0,0 +1,2 @@ + +package com.puppycrawl.tools.checkstyle.checks.regexp; diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/regexp/InputTrailingComment.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/regexp/InputTrailingComment.java new file mode 100644 index 000000000..9221a8ad2 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/regexp/InputTrailingComment.java @@ -0,0 +1,30 @@ +package com.puppycrawl.tools.checkstyle.checks.regexp; + +public class InputTrailingComment { + int i; // don't use trailing comments :) + // it fine to have comment w/o any statement + /* good c-style comment. */ + int j; /* bad c-style comment. */ + void method1() { /* some c-style multi-line + comment*/ + Runnable r = (new Runnable() { + public void run() { + } + }); /* we should allow this */ + } // we should allow this + /* + Let's check multi-line comments. + */ + /* c-style */ // cpp-style + /* c-style 1 */ /*c-style 2 */ + + void method2(long ms /* we should ignore this */) { + /* comment before text */int z; + /* int y */int y/**/; + } + + /** + * comment with trailing space + */ + final static public String NAME="Some Name"; // NOI18N +}