From 396f3ebbbf18838cd78e621cfb9cd82c35ceddbc Mon Sep 17 00:00:00 2001
From: Timur Tibeyev <timurtibeyev@gmail.com>
Date: Sun, 19 Jul 2020 19:25:45 +0600
Subject: [PATCH] Issue #7: add example for MethodLength

---
 .../SuppressionPatchXpathFilterTest.java        | 17 +++++++++++++++++
 .../MethodLength/context/Test.java              |  8 ++++++++
 .../MethodLength/context/defaultContext.patch   | 11 +++++++++++
 .../context/defaultContextConfig.xml            | 16 ++++++++++++++++
 .../MethodLength/context/expected.txt           |  0
 .../MethodLength/contexttwo/Test.java           |  7 +++++++
 .../contexttwo/defaultContext.patch             | 11 +++++++++++
 .../contexttwo/defaultContextConfig.xml         | 16 ++++++++++++++++
 .../MethodLength/contexttwo/expected.txt        |  1 +
 9 files changed, 87 insertions(+)
 create mode 100644 src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchxpathfilter/MethodLength/context/Test.java
 create mode 100644 src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchxpathfilter/MethodLength/context/defaultContext.patch
 create mode 100644 src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchxpathfilter/MethodLength/context/defaultContextConfig.xml
 create mode 100644 src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchxpathfilter/MethodLength/context/expected.txt
 create mode 100644 src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchxpathfilter/MethodLength/contexttwo/Test.java
 create mode 100644 src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchxpathfilter/MethodLength/contexttwo/defaultContext.patch
 create mode 100644 src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchxpathfilter/MethodLength/contexttwo/defaultContextConfig.xml
 create mode 100644 src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchxpathfilter/MethodLength/contexttwo/expected.txt

diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/filters/SuppressionPatchXpathFilterTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/filters/SuppressionPatchXpathFilterTest.java
index a45b5273..08f4f007 100644
--- a/src/test/java/com/puppycrawl/tools/checkstyle/filters/SuppressionPatchXpathFilterTest.java
+++ b/src/test/java/com/puppycrawl/tools/checkstyle/filters/SuppressionPatchXpathFilterTest.java
@@ -122,4 +122,21 @@ public void testMemberName() throws Exception {
         testByConfig(
                 "MemberName/patchedline/defaultContextConfig.xml");
     }
+
+    @Test
+    @Ignore("MethodLength should have a violation when method length"
+            + " is more than threshold, but not. It should be solved by"
+            + "context strategy.")
+    public void testMethodLengthContextStrategy() throws Exception {
+        testByConfig(
+                "MethodLength/contexttwo/defaultContextConfig.xml");
+    }
+
+    @Test
+    @Ignore("MethodLength should have no violations after patching,"
+            + " but should show violations before patching.")
+    public void testMethodLengthViolationShouldBeSuppressedAfterPatch() throws Exception {
+        testByConfig(
+                "MethodLength/context/defaultContextConfig.xml");
+    }
 }
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchxpathfilter/MethodLength/context/Test.java b/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchxpathfilter/MethodLength/context/Test.java
new file mode 100644
index 00000000..e2c61d7e
--- /dev/null
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchxpathfilter/MethodLength/context/Test.java
@@ -0,0 +1,8 @@
+public class Test {
+    private String name;
+
+    public void test() { // violation context, should be suppressed after patching
+        int a = 123;
+        int b = 234;
+    }
+}
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchxpathfilter/MethodLength/context/defaultContext.patch b/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchxpathfilter/MethodLength/context/defaultContext.patch
new file mode 100644
index 00000000..b645aa9d
--- /dev/null
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchxpathfilter/MethodLength/context/defaultContext.patch
@@ -0,0 +1,11 @@
+diff --git a/Test.java b/Test.java
+index d85e94c..bcd3588 100644
+--- a/Test.java
++++ b/Test.java
+@@ -1,4 +1,6 @@
+ public class Test {
++    private String name;
++
+     public void test() {
+         int a = 123;
+         int b = 234;
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchxpathfilter/MethodLength/context/defaultContextConfig.xml b/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchxpathfilter/MethodLength/context/defaultContextConfig.xml
new file mode 100644
index 00000000..7ba5ae11
--- /dev/null
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchxpathfilter/MethodLength/context/defaultContextConfig.xml
@@ -0,0 +1,16 @@
+<?xml version="1.0"?>
+<!DOCTYPE module PUBLIC
+          "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
+          "https://checkstyle.org/dtds/configuration_1_3.dtd">
+<module name="Checker">
+    <module name="TreeWalker">
+        <module name="MethodLength">
+            <property name="max" value="3"/>
+        </module>
+
+        <module name="com.puppycrawl.tools.checkstyle.filters.SuppressionPatchXpathFilter">
+            <property name="file" value="${tp}/defaultContext.patch" />
+            <property name="strategy" value="context" />
+        </module>
+    </module>
+</module>
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchxpathfilter/MethodLength/context/expected.txt b/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchxpathfilter/MethodLength/context/expected.txt
new file mode 100644
index 00000000..e69de29b
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchxpathfilter/MethodLength/contexttwo/Test.java b/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchxpathfilter/MethodLength/contexttwo/Test.java
new file mode 100644
index 00000000..fcb758ef
--- /dev/null
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchxpathfilter/MethodLength/contexttwo/Test.java
@@ -0,0 +1,7 @@
+public class Test {
+    public void test() { // violation context
+        int a = 123;
+        int b = 234;
+        int c = 345;
+    }
+}
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchxpathfilter/MethodLength/contexttwo/defaultContext.patch b/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchxpathfilter/MethodLength/contexttwo/defaultContext.patch
new file mode 100644
index 00000000..df24c340
--- /dev/null
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchxpathfilter/MethodLength/contexttwo/defaultContext.patch
@@ -0,0 +1,11 @@
+diff --git a/Test.java b/Test.java
+index d85e94c..db1f8c2 100644
+--- a/Test.java
++++ b/Test.java
+@@ -2,6 +2,7 @@ public class Test {
+     public void test() {
+         int a = 123;
+         int b = 234;
++        int c = 345;
+     }
+ }
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchxpathfilter/MethodLength/contexttwo/defaultContextConfig.xml b/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchxpathfilter/MethodLength/contexttwo/defaultContextConfig.xml
new file mode 100644
index 00000000..7ba5ae11
--- /dev/null
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchxpathfilter/MethodLength/contexttwo/defaultContextConfig.xml
@@ -0,0 +1,16 @@
+<?xml version="1.0"?>
+<!DOCTYPE module PUBLIC
+          "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
+          "https://checkstyle.org/dtds/configuration_1_3.dtd">
+<module name="Checker">
+    <module name="TreeWalker">
+        <module name="MethodLength">
+            <property name="max" value="3"/>
+        </module>
+
+        <module name="com.puppycrawl.tools.checkstyle.filters.SuppressionPatchXpathFilter">
+            <property name="file" value="${tp}/defaultContext.patch" />
+            <property name="strategy" value="context" />
+        </module>
+    </module>
+</module>
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchxpathfilter/MethodLength/contexttwo/expected.txt b/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchxpathfilter/MethodLength/contexttwo/expected.txt
new file mode 100644
index 00000000..7823c0f8
--- /dev/null
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/filters/suppressionpatchxpathfilter/MethodLength/contexttwo/expected.txt
@@ -0,0 +1 @@
+Test.java:2:5: Method length is 5 lines (max allowed is 3). [MethodLength]
