Skip to content

Commit c2a03ae

Browse files
AmitKumarDeoghoriaromani
authored andcommitted
Issue #13345: Enable examples tests for IllegalInstantiationCheck
1 parent f75123e commit c2a03ae

File tree

9 files changed

+161
-120
lines changed

9 files changed

+161
-120
lines changed

src/xdocs-examples/java/com/puppycrawl/tools/checkstyle/checks/coding/IllegalInstantiationCheckExamplesTest.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@
1919

2020
package com.puppycrawl.tools.checkstyle.checks.coding;
2121

22-
import org.junit.jupiter.api.Disabled;
2322
import org.junit.jupiter.api.Test;
2423

2524
import com.puppycrawl.tools.checkstyle.AbstractExamplesModuleTestSupport;
25+
import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
2626

27-
@Disabled("until https://github.com/checkstyle/checkstyle/issues/13345")
2827
public class IllegalInstantiationCheckExamplesTest extends AbstractExamplesModuleTestSupport {
2928
@Override
3029
protected String getPackageLocation() {
@@ -33,28 +32,23 @@ protected String getPackageLocation() {
3332

3433
@Test
3534
public void testExample1() throws Exception {
36-
final String[] expected = {
37-
38-
};
39-
40-
verifyWithInlineConfigParser(getPath("Example1.txt"), expected);
35+
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
36+
verifyWithInlineConfigParser(getPath("Example1.java"), expected);
4137
}
4238

4339
@Test
4440
public void testExample2() throws Exception {
4541
final String[] expected = {
46-
42+
"27:27: " + getCheckMessage(IllegalInstantiationCheck.MSG_KEY, "java.lang.Boolean"),
43+
"29:17: " + getCheckMessage(IllegalInstantiationCheck.MSG_KEY, "java.lang.Integer"),
4744
};
4845

49-
verifyWithInlineConfigParser(getPath("Example2.txt"), expected);
46+
verifyWithInlineConfigParser(getPath("Example2.java"), expected);
5047
}
5148

5249
@Test
5350
public void testExample3() throws Exception {
54-
final String[] expected = {
55-
56-
};
57-
58-
verifyWithInlineConfigParser(getPath("Example3.txt"), expected);
51+
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
52+
verifyWithInlineConfigParser(getPath("Example3.java"), expected);
5953
}
6054
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*xml
2+
<module name="Checker">
3+
<module name="TreeWalker">
4+
<module name="IllegalInstantiation"/>
5+
</module>
6+
</module>
7+
*/
8+
package com.puppycrawl.tools.checkstyle.checks.coding.illegalinstantiation;
9+
10+
// xdoc section -- start
11+
class Example1 {
12+
class Boolean {
13+
boolean a;
14+
public Boolean (boolean a) { this.a = a; }
15+
}
16+
17+
void Example1 () {
18+
java.lang.Boolean[] newBoolArray = new java.lang.Boolean[]{true,true,false};
19+
Integer[] newIntArray = new Integer[]{1,2,3};
20+
}
21+
22+
void Example1 (boolean a, int b) {
23+
Boolean c = new Boolean(a);
24+
java.lang.Boolean d = new java.lang.Boolean(a);
25+
26+
Integer e = new Integer(b);
27+
28+
Integer f = Integer.valueOf(b);
29+
}
30+
}
31+
// xdoc section -- end

src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/coding/illegalinstantiation/Example1.txt

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*xml
2+
<module name="Checker">
3+
<module name="TreeWalker">
4+
<module name="IllegalInstantiation">
5+
<property name="classes"
6+
value="java.lang.Boolean, java.lang.Integer"/>
7+
</module>
8+
</module>
9+
</module>
10+
*/
11+
package com.puppycrawl.tools.checkstyle.checks.coding.illegalinstantiation;
12+
13+
// xdoc section -- start
14+
class Example2 {
15+
class Boolean {
16+
boolean a;
17+
public Boolean (boolean a) { this.a = a; }
18+
}
19+
20+
void Example2 () {
21+
java.lang.Boolean[] newBoolArray = new java.lang.Boolean[]{true,true,false};
22+
Integer[] newIntArray = new Integer[]{1,2,3};
23+
}
24+
25+
void Example2 (boolean a, int b) {
26+
Boolean c = new Boolean(a);
27+
java.lang.Boolean d = new java.lang.Boolean(a);
28+
// violation above, 'Instantiation of java.lang.Boolean should be avoided'
29+
Integer e = new Integer(b);
30+
// violation above, 'Instantiation of java.lang.Integer should be avoided'
31+
Integer f = Integer.valueOf(b);
32+
}
33+
}
34+
// xdoc section -- end

src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/coding/illegalinstantiation/Example2.txt

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*xml
2+
<module name="Checker">
3+
<module name="TreeWalker">
4+
<module name="IllegalInstantiation">
5+
<property name="classes"
6+
value="java.lang.Boolean[], Boolean[], java.lang.Integer[], Integer[]"/>
7+
</module>
8+
</module>
9+
</module>
10+
*/
11+
package com.puppycrawl.tools.checkstyle.checks.coding.illegalinstantiation;
12+
13+
// xdoc section -- start
14+
class Example3 {
15+
class Boolean {
16+
boolean a;
17+
public Boolean (boolean a) { this.a = a; }
18+
}
19+
20+
void Example3 () {
21+
java.lang.Boolean[] newBoolArray = new java.lang.Boolean[]{true,true,false};
22+
Integer[] newIntArray = new Integer[]{1,2,3};
23+
}
24+
25+
void Example3 (boolean a, int b) {
26+
Boolean c = new Boolean(a);
27+
java.lang.Boolean d = new java.lang.Boolean(a);
28+
29+
Integer e = new Integer(b);
30+
31+
Integer f = Integer.valueOf(b);
32+
}
33+
}
34+
// xdoc section -- end

src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/coding/illegalinstantiation/Example3.txt

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/xdocs/checks/coding/illegalinstantiation.xml

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,24 @@
7676
</source>
7777
<p id="Example1-code">Example:</p>
7878
<source>
79-
public class MyTest {
80-
public class Boolean {
79+
class Example1 {
80+
class Boolean {
8181
boolean a;
82-
8382
public Boolean (boolean a) { this.a = a; }
8483
}
8584

86-
public void myTest (boolean a, int b) {
87-
Boolean c = new Boolean(a); // OK
88-
java.lang.Boolean d = new java.lang.Boolean(a); // OK
85+
void Example1 () {
86+
java.lang.Boolean[] newBoolArray = new java.lang.Boolean[]{true,true,false};
87+
Integer[] newIntArray = new Integer[]{1,2,3};
88+
}
89+
90+
void Example1 (boolean a, int b) {
91+
Boolean c = new Boolean(a);
92+
java.lang.Boolean d = new java.lang.Boolean(a);
93+
94+
Integer e = new Integer(b);
8995

90-
Integer e = new Integer(b); // OK
91-
Integer f = Integer.valueOf(b); // OK
96+
Integer f = Integer.valueOf(b);
9297
}
9398
}
9499
</source>
@@ -100,29 +105,32 @@ public class MyTest {
100105
&lt;module name=&quot;Checker&quot;&gt;
101106
&lt;module name=&quot;TreeWalker&quot;&gt;
102107
&lt;module name=&quot;IllegalInstantiation&quot;&gt;
103-
&lt;property name=&quot;classes&quot; value=&quot;java.lang.Boolean,
104-
java.lang.Integer&quot;/&gt;
108+
&lt;property name=&quot;classes&quot;
109+
value=&quot;java.lang.Boolean, java.lang.Integer&quot;/&gt;
105110
&lt;/module&gt;
106111
&lt;/module&gt;
107112
&lt;/module&gt;
108113
</source>
109114
<p id="Example2-code">Example:</p>
110115
<source>
111-
public class MyTest {
112-
public class Boolean {
116+
class Example2 {
117+
class Boolean {
113118
boolean a;
114-
115119
public Boolean (boolean a) { this.a = a; }
116120
}
117121

118-
public void myTest (boolean a, int b) {
119-
Boolean c = new Boolean(a); // OK
120-
java.lang.Boolean d = new java.lang.Boolean(a); // violation, instantiation of
121-
// java.lang.Boolean should be avoided
122+
void Example2 () {
123+
java.lang.Boolean[] newBoolArray = new java.lang.Boolean[]{true,true,false};
124+
Integer[] newIntArray = new Integer[]{1,2,3};
125+
}
122126

123-
Integer e = new Integer(b); // violation, instantiation of
124-
// java.lang.Integer should be avoided
125-
Integer f = Integer.valueOf(b); // OK
127+
void Example2 (boolean a, int b) {
128+
Boolean c = new Boolean(a);
129+
java.lang.Boolean d = new java.lang.Boolean(a);
130+
// violation above, 'Instantiation of java.lang.Boolean should be avoided'
131+
Integer e = new Integer(b);
132+
// violation above, 'Instantiation of java.lang.Integer should be avoided'
133+
Integer f = Integer.valueOf(b);
126134
}
127135
}
128136
</source>
@@ -133,18 +141,32 @@ public class MyTest {
133141
&lt;module name=&quot;Checker&quot;&gt;
134142
&lt;module name=&quot;TreeWalker&quot;&gt;
135143
&lt;module name=&quot;IllegalInstantiation&quot;&gt;
136-
&lt;property name=&quot;classes&quot; value=&quot;java.lang.Boolean[],
137-
Boolean[], java.lang.Integer[], Integer[]&quot;/&gt;
144+
&lt;property name=&quot;classes&quot;
145+
value=&quot;java.lang.Boolean[], Boolean[], java.lang.Integer[], Integer[]&quot;/&gt;
138146
&lt;/module&gt;
139147
&lt;/module&gt;
140148
&lt;/module&gt;
141149
</source>
142150
<p id="Example3-code">Example:</p>
143151
<source>
144-
public class MyTest {
145-
public void myTest () {
146-
Boolean[] newBoolArray = new Boolean[]{true,true,false}; // OK
147-
Integer[] newIntArray = new Integer[]{1,2,3}; // OK
152+
class Example3 {
153+
class Boolean {
154+
boolean a;
155+
public Boolean (boolean a) { this.a = a; }
156+
}
157+
158+
void Example3 () {
159+
java.lang.Boolean[] newBoolArray = new java.lang.Boolean[]{true,true,false};
160+
Integer[] newIntArray = new Integer[]{1,2,3};
161+
}
162+
163+
void Example3 (boolean a, int b) {
164+
Boolean c = new Boolean(a);
165+
java.lang.Boolean d = new java.lang.Boolean(a);
166+
167+
Integer e = new Integer(b);
168+
169+
Integer f = Integer.valueOf(b);
148170
}
149171
}
150172
</source>

src/xdocs/checks/coding/illegalinstantiation.xml.template

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@
5757
</p>
5858
<macro name="example">
5959
<param name="path"
60-
value="resources/com/puppycrawl/tools/checkstyle/checks/coding/illegalinstantiation/Example1.txt"/>
60+
value="resources/com/puppycrawl/tools/checkstyle/checks/coding/illegalinstantiation/Example1.java"/>
6161
<param name="type" value="config"/>
6262
</macro>
6363
<p id="Example1-code">Example:</p>
6464
<macro name="example">
6565
<param name="path"
66-
value="resources/com/puppycrawl/tools/checkstyle/checks/coding/illegalinstantiation/Example1.txt"/>
66+
value="resources/com/puppycrawl/tools/checkstyle/checks/coding/illegalinstantiation/Example1.java"/>
6767
<param name="type" value="code"/>
6868
</macro>
6969
<p id="Example2-config">
@@ -72,27 +72,27 @@
7272
</p>
7373
<macro name="example">
7474
<param name="path"
75-
value="resources/com/puppycrawl/tools/checkstyle/checks/coding/illegalinstantiation/Example2.txt"/>
75+
value="resources/com/puppycrawl/tools/checkstyle/checks/coding/illegalinstantiation/Example2.java"/>
7676
<param name="type" value="config"/>
7777
</macro>
7878
<p id="Example2-code">Example:</p>
7979
<macro name="example">
8080
<param name="path"
81-
value="resources/com/puppycrawl/tools/checkstyle/checks/coding/illegalinstantiation/Example2.txt"/>
81+
value="resources/com/puppycrawl/tools/checkstyle/checks/coding/illegalinstantiation/Example2.java"/>
8282
<param name="type" value="code"/>
8383
</macro>
8484
<p id="Example3-config">
8585
Finally, there is a limitation that it is currently not possible to specify array classes:
8686
</p>
8787
<macro name="example">
8888
<param name="path"
89-
value="resources/com/puppycrawl/tools/checkstyle/checks/coding/illegalinstantiation/Example3.txt"/>
89+
value="resources/com/puppycrawl/tools/checkstyle/checks/coding/illegalinstantiation/Example3.java"/>
9090
<param name="type" value="config"/>
9191
</macro>
9292
<p id="Example3-code">Example:</p>
9393
<macro name="example">
9494
<param name="path"
95-
value="resources/com/puppycrawl/tools/checkstyle/checks/coding/illegalinstantiation/Example3.txt"/>
95+
value="resources/com/puppycrawl/tools/checkstyle/checks/coding/illegalinstantiation/Example3.java"/>
9696
<param name="type" value="code"/>
9797
</macro>
9898
</subsection>

0 commit comments

Comments
 (0)