Skip to content

Commit 12b3e74

Browse files
committedJan 25, 2019
For #1000: Added String.length comparison check tests
1 parent 93f22f1 commit 12b3e74

9 files changed

+134
-25
lines changed
 

‎qulice-pmd/src/test/java/com/qulice/pmd/UseStringIsEmptyRuleTest.java

+75-8
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,19 @@
3636
import org.junit.Test;
3737

3838
/**
39-
* Test case for {@link UseStringIsEmptyRule}.
39+
* Test case for {@link com.qulice.pmd.rules.UseStringIsEmptyRule}.
4040
* @since 0.18
41-
* @todo #950:30min UseStringIsEmptyRuleTest is not comparing all possible
42-
* targets. Implement the tests for the remaining targets (see complete list in
43-
* UseStringIsEmptyRule#getComparisonTargets).
41+
* @todo #1000:30min UseStringIsEmptyRuleTest is not comparing all possible
42+
* targets. Implement the tests for the remaining targets (see
43+
* complete list in UseStringIsEmptyRule#getComparisonTargets). Conditions
44+
* that must be tested are checking string length when the String is the
45+
* result of a method (this.method().length()) and when the String is a class
46+
* field (this.somestring.length()). Use the same name and file pattern used
47+
* in the test methods, appending 'This' or 'Method' suffix to indicate
48+
* which access type the test relates to (field or method), renaming the
49+
* tests if needed.
4450
*/
51+
@SuppressWarnings("PMD.TooManyMethods")
4552
public final class UseStringIsEmptyRuleTest {
4653

4754
/**
@@ -50,6 +57,36 @@ public final class UseStringIsEmptyRuleTest {
5057
private static final String ERR_MESSAGE =
5158
"Use String.isEmpty() when checking for empty string";
5259

60+
/**
61+
* UseStringIsEmptyRule can detect when String.length() < 1,
62+
* when the String is a local variable.
63+
* @throws Exception If something goes wrong
64+
*/
65+
@Test
66+
public void detectsLengthLessThanOne() throws Exception {
67+
new PmdAssert(
68+
"StringLengthLessThanOne.java", Matchers.is(false),
69+
Matchers.containsString(
70+
UseStringIsEmptyRuleTest.ERR_MESSAGE
71+
)
72+
).validate();
73+
}
74+
75+
/**
76+
* UseStringIsEmptyRule can detect when String.length() > 0,
77+
* when the String is a local variable.
78+
* @throws Exception If something goes wrong
79+
*/
80+
@Test
81+
public void detectsLengthGreaterThanZero() throws Exception {
82+
new PmdAssert(
83+
"StringLengthGreaterThanZero.java", Matchers.is(false),
84+
Matchers.containsString(
85+
UseStringIsEmptyRuleTest.ERR_MESSAGE
86+
)
87+
).validate();
88+
}
89+
5390
/**
5491
* UseStringIsEmptyRule can detect when String.length() is compared to 0.
5592
* @throws Exception If something goes wrong
@@ -64,6 +101,36 @@ public void detectsLengthEqualsZero() throws Exception {
64101
).validate();
65102
}
66103

104+
/**
105+
* UseStringIsEmptyRule can detect when String.length() != 1,
106+
* when the String is a local variable.
107+
* @throws Exception If something goes wrong
108+
*/
109+
@Test
110+
public void detectsLengthNotEqualsZero() throws Exception {
111+
new PmdAssert(
112+
"StringLengthNotEqualsZero.java", Matchers.is(false),
113+
Matchers.containsString(
114+
UseStringIsEmptyRuleTest.ERR_MESSAGE
115+
)
116+
).validate();
117+
}
118+
119+
/**
120+
* UseStringIsEmptyRule can detect when String.length() >= 0,
121+
* when the String is returned by a method.
122+
* @throws Exception If something goes wrong
123+
*/
124+
@Test
125+
public void detectsLengthGreaterOrEqualZero() throws Exception {
126+
new PmdAssert(
127+
"StringLengthGreaterOrEqualZero.java", Matchers.is(false),
128+
Matchers.containsString(
129+
UseStringIsEmptyRuleTest.ERR_MESSAGE
130+
)
131+
).validate();
132+
}
133+
67134
/**
68135
* UseStringIsEmptyRule can detect when String.length() >= 1,
69136
* when the String is returned by a method.
@@ -72,22 +139,22 @@ public void detectsLengthEqualsZero() throws Exception {
72139
@Test
73140
public void detectsLengthGreaterOrEqualOne() throws Exception {
74141
new PmdAssert(
75-
"StringFromMethodLength.java", Matchers.is(false),
142+
"StringLengthGreaterOrEqualOne.java", Matchers.is(false),
76143
Matchers.containsString(
77144
UseStringIsEmptyRuleTest.ERR_MESSAGE
78145
)
79146
).validate();
80147
}
81148

82149
/**
83-
* UseStringIsEmptyRule can detect when String.length() < 1,
150+
* UseStringIsEmptyRule can detect when String.length() <= 0,
84151
* when the String is a local variable.
85152
* @throws Exception If something goes wrong
86153
*/
87154
@Test
88-
public void detectsLengthLessThanOne() throws Exception {
155+
public void detectsLengthLessOrEqualZero() throws Exception {
89156
new PmdAssert(
90-
"LocalStringLength.java", Matchers.is(false),
157+
"StringLengthLessOrEqualZero.java", Matchers.is(false),
91158
Matchers.containsString(
92159
UseStringIsEmptyRuleTest.ERR_MESSAGE
93160
)

‎qulice-pmd/src/test/resources/com/qulice/pmd/LocalStringLength.java

-10
This file was deleted.

‎qulice-pmd/src/test/resources/com/qulice/pmd/StringLengthEqualsZero.java

+1-7
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,7 @@
22

33
public final class StringLengthEqualsZero {
44

5-
private final String somestring;
6-
7-
public StringLengthEqualsZero(final String str) {
8-
this.somestring = str;
9-
}
10-
11-
public boolean sizeIsZero() {
5+
public boolean sizeIsZero(final String somestring) {
126
return somestring.length() == 0;
137
}
148

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package foo;
2+
3+
public final class StringLengthGreaterOrEqualOne {
4+
5+
public boolean sizeGreaterOrEqualOne(final String somestring) {
6+
return somestring.length() >= 1;
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package foo;
2+
3+
public final class StringLengthGreaterOrEqualOne {
4+
5+
public boolean sizeGreaterOrEqualOne(final String somestring) {
6+
return somestring.length() >= 0;
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package foo;
2+
3+
public final class StringLengthGreaterThanZero {
4+
5+
public boolean sizeGreaterThanZero(final String somestring) {
6+
return somestring.length() > 0;
7+
}
8+
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package foo;
2+
3+
public final class StringLengthMinorOrEqualsThanZero {
4+
5+
private final String somestring;
6+
7+
public StringLengthMinorOrEqualsThanZero(final String str) {
8+
this.somestring = str;
9+
}
10+
11+
public boolean sizeMinorOrEqualsThanZero() {
12+
return somestring.length() <= 0;
13+
}
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package foo;
2+
3+
public final class StringLengthLessThanOne {
4+
5+
public boolean sizeLessThanOne(final String somestring) {
6+
return somestring.length() < 1;
7+
}
8+
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package foo;
2+
3+
public final class StringLengthNotEqualsZero {
4+
5+
public boolean sizeIsNotZero(final String somestring) {
6+
return somestring.length() != 0;
7+
}
8+
9+
}

0 commit comments

Comments
 (0)
Please sign in to comment.