Skip to content

Commit

Permalink
#1031.Test case, String[].length, for UseStringIsEmptyRule
Browse files Browse the repository at this point in the history
  • Loading branch information
zCRUSADERz committed May 1, 2019
1 parent a57c230 commit 9197c99
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
Expand Up @@ -33,6 +33,9 @@
import org.hamcrest.core.CombinableMatcher;
import org.hamcrest.core.IsEqual;
import org.hamcrest.core.StringContains;
import org.hamcrest.text.IsEmptyString;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

Expand Down Expand Up @@ -72,6 +75,21 @@ public void detectLengthComparisons(final String file) throws Exception {
).validate();
}

/**
* UseStringIsEmpty not detect when used String[].length, when checking for
* empty string.
* @throws Exception If something goes wrong.
*/
@Disabled
@Test
public void notDetectOnArrayOfStrings() throws Exception {
new PmdAssert(
"ArrayOfStringsLengthGreaterThanZero.java",
new IsEqual<>(true),
new IsEmptyString()
).validate();
}

/**
* Constructs StringContains matcher for error message.
* @param file File name.
Expand Down
@@ -0,0 +1,27 @@
package foo;

public final class ArrayOfStringsLengthGreaterThanZero {

private final String[] strings;

public ArrayOfStringsLengthGreaterThanZero(final String... args) {
this.strings = args.clone();
}

public String[] args() {
return this.strings.clone();
}

public boolean arrayFromArgs(final String... args) {
return args.length > 0;
}

public boolean arrayFromField() {
return this.strings.length > 0;
}

public boolean arrayFromMethod() {
return this.args().length > 0;
}

}

0 comments on commit 9197c99

Please sign in to comment.