aboutsummaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorPiyush Sharma <ps26oct@gmail.com>2017-03-09 00:56:21 +0530
committerRoman Ivanov <romani@users.noreply.github.com>2017-04-12 18:09:39 -0700
commitdb64216a19b480855058d85f489d22fface4ff13 (patch)
tree533c0ffdba4b02fcfd0213036ab5b3bb2de8bc47 /src/test
parent7533f022a71e932d3fb2600c91b8463171a2caae (diff)
downloadcheckstyle-db64216a19b480855058d85f489d22fface4ff13.tar.gz
Issue #3423: Allow local vars without any unnecessary violations from RequireThisCheck. Added UT. Fixed related UT
Diffstat (limited to 'src/test')
-rw-r--r--src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/RequireThisCheckTest.java19
-rw-r--r--src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputAllowLocalVars.java49
-rw-r--r--src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputValidateOnlyOverlappingFalse.java8
3 files changed, 69 insertions, 7 deletions
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 3cf4dbbd3..bdd7eeb2d 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
@@ -191,8 +191,6 @@ public class RequireThisCheckTest extends BaseCheckTestSupport {
"185:9: " + getCheckMessage(MSG_VARIABLE, "field1", ""),
"189:9: " + getCheckMessage(MSG_VARIABLE, "field1", ""),
"210:9: " + getCheckMessage(MSG_VARIABLE, "field1", ""),
- "215:9: " + getCheckMessage(MSG_VARIABLE, "field1", ""),
- "225:21: " + getCheckMessage(MSG_VARIABLE, "field1", ""),
"228:21: " + getCheckMessage(MSG_VARIABLE, "field1", ""),
"238:9: " + getCheckMessage(MSG_VARIABLE, "field1", ""),
"253:9: " + getCheckMessage(MSG_VARIABLE, "booleanField", ""),
@@ -204,7 +202,6 @@ public class RequireThisCheckTest extends BaseCheckTestSupport {
"340:9: " + getCheckMessage(MSG_VARIABLE, "field1", ""),
"374:40: " + getCheckMessage(MSG_METHOD, "getServletRelativeAction", ""),
"376:20: " + getCheckMessage(MSG_METHOD, "processAction", ""),
- "383:9: " + getCheckMessage(MSG_VARIABLE, "servletRelativeAction", ""),
"384:16: " + getCheckMessage(MSG_METHOD, "processAction", ""),
};
verify(checkConfig, getPath("InputValidateOnlyOverlappingFalse.java"), expected);
@@ -261,4 +258,20 @@ public class RequireThisCheckTest extends BaseCheckTestSupport {
};
verify(checkConfig, getPath("InputRequireThisMetodReferences.java"), expected);
}
+
+ @Test
+ public void testAllowLocalVars() throws Exception {
+ final DefaultConfiguration checkConfig = createCheckConfig(RequireThisCheck.class);
+ checkConfig.addAttribute("validateOnlyOverlapping", "false");
+ checkConfig.addAttribute("checkMethods", "false");
+ final String[] expected = {
+ "14:9: " + getCheckMessage(MSG_VARIABLE, "s1", ""),
+ "22:9: " + getCheckMessage(MSG_VARIABLE, "s1", ""),
+ "35:9: " + getCheckMessage(MSG_VARIABLE, "s2", ""),
+ "40:9: " + getCheckMessage(MSG_VARIABLE, "s2", ""),
+ "46:9: " + getCheckMessage(MSG_VARIABLE, "s2", ""),
+ "47:16: " + getCheckMessage(MSG_VARIABLE, "s1", ""),
+ };
+ verify(checkConfig, getPath("InputAllowLocalVars.java"), expected);
+ }
}
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputAllowLocalVars.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputAllowLocalVars.java
new file mode 100644
index 000000000..339bf90fa
--- /dev/null
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputAllowLocalVars.java
@@ -0,0 +1,49 @@
+/*
+Input test file for RequireThisCheck.
+Created: 2017
+ */
+
+package com.puppycrawl.tools.checkstyle.checks.coding;
+
+class InputAllowLocalVars {
+
+ private String s1 = "foo1";
+ String s2 = "foo2";
+
+ InputAllowLocalVars() {
+ s1 = "bar1"; // Violation. Requires "this".
+ String s2;
+ s2 = "bar2"; // No violation. Local var allowed.
+ }
+
+ public int getS1() {
+ String s1 = null;
+ s1 = "bar"; // No violation
+ s1 = s1; // Violation. "this" required here to resolve any confusion due to overlapping.
+ return 1;
+ }
+
+ public String getS1(String param) {
+ String s1 = null;
+ s1 = param; // No violation
+ s1 += s1; // No violation. s1 is being returned.
+ return s1; // No violation
+ }
+
+ String getS2() {
+ String s2 = null;
+ s2+=s2; // Violation. "this" required here to resolve any confusion due to overlapping.
+ return "return";
+ }
+
+ String getS2(String s2) {
+ s2 = null; // Violation. Requires "this". s2 is a param not a local var.
+ return s2; // No violation. param is returned.
+ }
+
+ String getS2(int a) {
+ String s2 = " ";
+ s2 += s2; // Violation. "this" required here to resolve any confusion due to overlapping.
+ return s1; // Violation. Requires "this".
+ }
+}
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputValidateOnlyOverlappingFalse.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputValidateOnlyOverlappingFalse.java
index 8765ebf99..54fc5ca38 100644
--- a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputValidateOnlyOverlappingFalse.java
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputValidateOnlyOverlappingFalse.java
@@ -212,7 +212,7 @@ public class InputValidateOnlyOverlappingFalse {
void foo24() {
String field1 = "Hello";
- field1 = "Java"; // violation
+ field1 = "Java"; // No violation. Local var allowed
this.booleanField = true;
this.booleanField = booleanField;
}
@@ -222,10 +222,10 @@ public class InputValidateOnlyOverlappingFalse {
if (true) {
String field1 = "Hello, World!";
if (true) {
- field1 = new String(); // violation
+ field1 = new String(); // No violation. Local var allowed
}
else {
- field1 = new String(); // violation
+ field1 += field1; // violation
}
}
}
@@ -380,7 +380,7 @@ public class InputValidateOnlyOverlappingFalse {
servletRelativeAction = "" + servletRelativeAction;
}
}
- servletRelativeAction = "servletRelativeAction"; // violation
+ servletRelativeAction = "servletRelativeAction"; // No violation. Local var allowed
return processAction(servletRelativeAction); // violation (Method call to 'processAction' needs "this.".)
}