36
36
import org .junit .Test ;
37
37
38
38
/**
39
- * Test case for {@link UseStringIsEmptyRule}.
39
+ * Test case for {@link com.qulice.pmd.rules. UseStringIsEmptyRule}.
40
40
* @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.
44
50
*/
51
+ @ SuppressWarnings ("PMD.TooManyMethods" )
45
52
public final class UseStringIsEmptyRuleTest {
46
53
47
54
/**
@@ -50,6 +57,36 @@ public final class UseStringIsEmptyRuleTest {
50
57
private static final String ERR_MESSAGE =
51
58
"Use String.isEmpty() when checking for empty string" ;
52
59
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
+
53
90
/**
54
91
* UseStringIsEmptyRule can detect when String.length() is compared to 0.
55
92
* @throws Exception If something goes wrong
@@ -64,6 +101,36 @@ public void detectsLengthEqualsZero() throws Exception {
64
101
).validate ();
65
102
}
66
103
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
+
67
134
/**
68
135
* UseStringIsEmptyRule can detect when String.length() >= 1,
69
136
* when the String is returned by a method.
@@ -72,22 +139,22 @@ public void detectsLengthEqualsZero() throws Exception {
72
139
@ Test
73
140
public void detectsLengthGreaterOrEqualOne () throws Exception {
74
141
new PmdAssert (
75
- "StringFromMethodLength .java" , Matchers .is (false ),
142
+ "StringLengthGreaterOrEqualOne .java" , Matchers .is (false ),
76
143
Matchers .containsString (
77
144
UseStringIsEmptyRuleTest .ERR_MESSAGE
78
145
)
79
146
).validate ();
80
147
}
81
148
82
149
/**
83
- * UseStringIsEmptyRule can detect when String.length() < 1 ,
150
+ * UseStringIsEmptyRule can detect when String.length() <= 0 ,
84
151
* when the String is a local variable.
85
152
* @throws Exception If something goes wrong
86
153
*/
87
154
@ Test
88
- public void detectsLengthLessThanOne () throws Exception {
155
+ public void detectsLengthLessOrEqualZero () throws Exception {
89
156
new PmdAssert (
90
- "LocalStringLength .java" , Matchers .is (false ),
157
+ "StringLengthLessOrEqualZero .java" , Matchers .is (false ),
91
158
Matchers .containsString (
92
159
UseStringIsEmptyRuleTest .ERR_MESSAGE
93
160
)
0 commit comments