[BAEL-3019] Add sample code for empty and blank string checks (#7176)
* [BAEL-3019] Add sample code for empty and blank string checks * [BAEL-3019] Add article link to module README * [BAEL-3019] Remove blank line in the beginning of certain tests * [BAEL-3019] Unroll tests and adhere to givenXXX_thenXXX naming convention * Revert "[BAEL-3019] Add article link to module README" This reverts commit 675295fafa53861d2bd3eb0e766665a58adda97d. * [BAEL-3019] Bump guava version for 'java-strings-2' to 28.0-jre
This commit is contained in:
parent
d67b026d29
commit
c5179b6ec8
|
@ -57,6 +57,26 @@
|
|||
<artifactId>commons-text</artifactId>
|
||||
<version>${commons-text.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.validation</groupId>
|
||||
<artifactId>validation-api</artifactId>
|
||||
<version>2.0.0.Final</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate.validator</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
<version>6.0.2.Final</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.el</groupId>
|
||||
<artifactId>javax.el-api</artifactId>
|
||||
<version>3.0.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.web</groupId>
|
||||
<artifactId>javax.el</artifactId>
|
||||
<version>2.2.6</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
@ -86,7 +106,7 @@
|
|||
<properties>
|
||||
<commons-lang3.version>3.8.1</commons-lang3.version>
|
||||
<icu4j.version>61.1</icu4j.version>
|
||||
<guava.version>27.0.1-jre</guava.version>
|
||||
<guava.version>28.0-jre</guava.version>
|
||||
<commons-text.version>1.4</commons-text.version>
|
||||
</properties>
|
||||
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.string.emptystrings;
|
||||
|
||||
class EmptyStringCheck {
|
||||
|
||||
boolean isEmptyString(String string) {
|
||||
return string == null || string.isEmpty();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.string.emptystrings;
|
||||
|
||||
class Java5EmptyStringCheck {
|
||||
|
||||
boolean isEmptyString(String string) {
|
||||
return string == null || string.length() == 0;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.string.emptystrings;
|
||||
|
||||
class PlainJavaBlankStringCheck {
|
||||
|
||||
boolean isBlankString(String string) {
|
||||
return string == null || string.trim().isEmpty();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.string.emptystrings;
|
||||
|
||||
import javax.validation.constraints.Pattern;
|
||||
|
||||
class SomeClassWithValidations {
|
||||
|
||||
@Pattern(regexp = "\\A(?!\\s*\\Z).+")
|
||||
private String someString;
|
||||
|
||||
SomeClassWithValidations setSomeString(String someString) {
|
||||
this.someString = someString;
|
||||
return this;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,142 @@
|
|||
package com.baeldung.string.emptystrings;
|
||||
|
||||
import static org.hamcrest.Matchers.iterableWithSize;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
import javax.validation.ValidatorFactory;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
|
||||
public class EmptyStringsUnitTest {
|
||||
|
||||
private String emptyString = "";
|
||||
private String blankString = " \n\t ";
|
||||
private String nonEmptyString = " someString ";
|
||||
|
||||
/*
|
||||
* EmptyStringCheck
|
||||
*/
|
||||
@Test
|
||||
public void givenSomeEmptyString_thenEmptyStringCheckIsEmptyStringReturnsTrue() {
|
||||
assertTrue(new EmptyStringCheck().isEmptyString(emptyString));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSomeNonEmptyString_thenEmptyStringCheckIsEmptyStringReturnsFalse() {
|
||||
assertFalse(new EmptyStringCheck().isEmptyString(nonEmptyString));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSomeBlankString_thenEmptyStringCheckIsEmptyStringReturnsFalse() {
|
||||
assertFalse(new EmptyStringCheck().isEmptyString(blankString));
|
||||
}
|
||||
|
||||
/*
|
||||
* Java5EmptyStringCheck
|
||||
*/
|
||||
@Test
|
||||
public void givenSomeEmptyString_thenJava5EmptyStringCheckIsEmptyStringReturnsTrue() {
|
||||
assertTrue(new Java5EmptyStringCheck().isEmptyString(emptyString));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSomeNonEmptyString_thenJava5EmptyStringCheckIsEmptyStringReturnsFalse() {
|
||||
assertFalse(new Java5EmptyStringCheck().isEmptyString(nonEmptyString));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSomeBlankString_thenJava5EmptyStringCheckIsEmptyStringReturnsFalse() {
|
||||
assertFalse(new Java5EmptyStringCheck().isEmptyString(blankString));
|
||||
}
|
||||
|
||||
/*
|
||||
* PlainJavaBlankStringCheck
|
||||
*/
|
||||
@Test
|
||||
public void givenSomeEmptyString_thenPlainJavaBlankStringCheckIsBlankStringReturnsTrue() {
|
||||
assertTrue(new PlainJavaBlankStringCheck().isBlankString(emptyString));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSomeNonEmptyString_thenPlainJavaBlankStringCheckIsBlankStringReturnsFalse() {
|
||||
assertFalse(new PlainJavaBlankStringCheck().isBlankString(nonEmptyString));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSomeBlankString_thenPlainJavaBlankStringCheckIsBlankStringReturnsTrue() {
|
||||
assertTrue(new PlainJavaBlankStringCheck().isBlankString(blankString));
|
||||
}
|
||||
|
||||
/*
|
||||
* Apache Commons Lang StringUtils
|
||||
*/
|
||||
@Test
|
||||
public void givenSomeEmptyString_thenStringUtilsIsBlankReturnsTrue() {
|
||||
assertTrue(StringUtils.isBlank(emptyString));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSomeNonEmptyString_thenStringUtilsIsBlankReturnsFalse() {
|
||||
assertFalse(StringUtils.isBlank(nonEmptyString));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSomeBlankString_thenStringUtilsIsBlankReturnsTrue() {
|
||||
assertTrue(StringUtils.isBlank(blankString));
|
||||
}
|
||||
|
||||
/*
|
||||
* Google Guava Strings
|
||||
*/
|
||||
@Test
|
||||
public void givenSomeEmptyString_thenStringsIsNullOrEmptyStringReturnsTrue() {
|
||||
assertTrue(Strings.isNullOrEmpty(emptyString));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSomeNonEmptyString_thenStringsIsNullOrEmptyStringReturnsFalse() {
|
||||
assertFalse(Strings.isNullOrEmpty(nonEmptyString));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSomeBlankString_thenStringsIsNullOrEmptyStringReturnsFalse() {
|
||||
assertFalse(Strings.isNullOrEmpty(blankString));
|
||||
}
|
||||
|
||||
/*
|
||||
* Bean Validation
|
||||
*/
|
||||
private ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
private Validator validator = factory.getValidator();
|
||||
|
||||
@Test
|
||||
public void givenSomeEmptyString_thenBeanValidationReturnsViolations() {
|
||||
SomeClassWithValidations someClassWithValidations = new SomeClassWithValidations().setSomeString(emptyString);
|
||||
Set<ConstraintViolation<SomeClassWithValidations>> violations = validator.validate(someClassWithValidations);
|
||||
assertThat(violations, iterableWithSize(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSomeNonEmptyString_thenBeanValidationValidatesWithoutViolations() {
|
||||
SomeClassWithValidations someClassWithValidations = new SomeClassWithValidations().setSomeString(nonEmptyString);
|
||||
Set<ConstraintViolation<SomeClassWithValidations>> violations = validator.validate(someClassWithValidations);
|
||||
assertThat(violations, iterableWithSize(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSomeBlankString_thenBeanValidationReturnsViolations() {
|
||||
SomeClassWithValidations someClassWithValidations = new SomeClassWithValidations().setSomeString(blankString);
|
||||
Set<ConstraintViolation<SomeClassWithValidations>> violations = validator.validate(someClassWithValidations);
|
||||
assertThat(violations, iterableWithSize(1));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue