diff --git a/terasoluna-gfw-common/src/main/java/org/terasoluna/gfw/common/codelist/ExistInCodeList.java b/terasoluna-gfw-common/src/main/java/org/terasoluna/gfw/common/codelist/ExistInCodeList.java index d2e2cc5aa..96df6fd47 100644 --- a/terasoluna-gfw-common/src/main/java/org/terasoluna/gfw/common/codelist/ExistInCodeList.java +++ b/terasoluna-gfw-common/src/main/java/org/terasoluna/gfw/common/codelist/ExistInCodeList.java @@ -29,7 +29,7 @@ import javax.validation.Payload; import org.terasoluna.gfw.common.codelist.validator.ExistInCodeListValidatorForCharacter; -import org.terasoluna.gfw.common.codelist.validator.ExistInCodeListValidatorForString; +import org.terasoluna.gfw.common.codelist.validator.ExistInCodeListValidatorForCharSequence; /** * Custom annotation that provides the functionality to check the existence of a code in the specified codelist. @@ -54,7 +54,7 @@ @Documented @Target({ METHOD, FIELD, ANNOTATION_TYPE, PARAMETER }) @Retention(RUNTIME) -@Constraint(validatedBy = { ExistInCodeListValidatorForString.class, +@Constraint(validatedBy = { ExistInCodeListValidatorForCharSequence.class, ExistInCodeListValidatorForCharacter.class }) public @interface ExistInCodeList { diff --git a/terasoluna-gfw-common/src/main/java/org/terasoluna/gfw/common/codelist/validator/ExistInCodeListValidatorForString.java b/terasoluna-gfw-common/src/main/java/org/terasoluna/gfw/common/codelist/validator/ExistInCodeListValidatorForCharSequence.java similarity index 86% rename from terasoluna-gfw-common/src/main/java/org/terasoluna/gfw/common/codelist/validator/ExistInCodeListValidatorForString.java rename to terasoluna-gfw-common/src/main/java/org/terasoluna/gfw/common/codelist/validator/ExistInCodeListValidatorForCharSequence.java index 58866f5cb..b41a668ad 100644 --- a/terasoluna-gfw-common/src/main/java/org/terasoluna/gfw/common/codelist/validator/ExistInCodeListValidatorForString.java +++ b/terasoluna-gfw-common/src/main/java/org/terasoluna/gfw/common/codelist/validator/ExistInCodeListValidatorForCharSequence.java @@ -26,16 +26,16 @@ * </p> */ -public class ExistInCodeListValidatorForString +public class ExistInCodeListValidatorForCharSequence extends - AbstractExistInCodeListValidator<String> { + AbstractExistInCodeListValidator<CharSequence> { /** * Fetches the code value which is the target of validation * @see org.terasoluna.gfw.common.codelist.validator.AbstractExistInCodeListValidator#getCode(Object) */ @Override - protected String getCode(String value) { - return value; + protected String getCode(CharSequence value) { + return value == null ? null : value.toString(); } } diff --git a/terasoluna-gfw-common/src/test/java/org/terasoluna/gfw/common/codelist/ExistInCodeListTest.java b/terasoluna-gfw-common/src/test/java/org/terasoluna/gfw/common/codelist/ExistInCodeListTest.java index a7bc03ed2..1683ae1c6 100644 --- a/terasoluna-gfw-common/src/test/java/org/terasoluna/gfw/common/codelist/ExistInCodeListTest.java +++ b/terasoluna-gfw-common/src/test/java/org/terasoluna/gfw/common/codelist/ExistInCodeListTest.java @@ -60,6 +60,15 @@ public void test_allValidStringCode() { assertThat(result.isEmpty(), is(true)); } + @Test + public void test_allValidStringBuilderCode() { + Employee e = new Employee(); + e.gender = new StringBuilder("F"); + e.lang = new StringBuilder("JP"); + Set<ConstraintViolation<Employee>> result = validator.validate(e); + assertThat(result.isEmpty(), is(true)); + } + @Test public void test_allValidStringCodeWithNull() { Person p = new Person(); @@ -69,6 +78,15 @@ public void test_allValidStringCodeWithNull() { assertThat(result.isEmpty(), is(true)); } + @Test + public void test_allValidStringBuilderCodeWithNull() { + Employee e = new Employee(); + e.gender = null; + e.lang = new StringBuilder("JP"); + Set<ConstraintViolation<Employee>> result = validator.validate(e); + assertThat(result.isEmpty(), is(true)); + } + @Test public void test_allValidStringCodeWithEmpty() { Person p = new Person(); @@ -78,6 +96,15 @@ public void test_allValidStringCodeWithEmpty() { assertThat(result.isEmpty(), is(true)); } + @Test + public void test_allValidStringBuilderCodeWithEmpty() { + Employee e = new Employee(); + e.gender = new StringBuilder(""); + e.lang = new StringBuilder("JP"); + Set<ConstraintViolation<Employee>> result = validator.validate(e); + assertThat(result.isEmpty(), is(true)); + } + @Test public void test_hasInValidStringCode() { Person p = new Person(); @@ -87,6 +114,15 @@ public void test_hasInValidStringCode() { assertThat(result.size(), is(1)); } + @Test + public void test_hasInValidStringBuilderCode() { + Employee e = new Employee(); + e.gender = new StringBuilder("G"); + e.lang = new StringBuilder("JP"); + Set<ConstraintViolation<Employee>> result = validator.validate(e); + assertThat(result.size(), is(1)); + } + @Test public void test_allInValidStringCode() { Person p = new Person(); @@ -96,6 +132,15 @@ public void test_allInValidStringCode() { assertThat(result.size(), is(2)); } + @Test + public void test_allInValidStringBuilderCode() { + Employee e = new Employee(); + e.gender = new StringBuilder("G"); + e.lang = new StringBuilder("FR"); + Set<ConstraintViolation<Employee>> result = validator.validate(e); + assertThat(result.size(), is(2)); + } + @Test public void test_validCharacterCode() { Customer c = new Customer(); @@ -342,6 +387,14 @@ class Customer { public String lang; } +class Employee { + @ExistInCodeList(codeListId = "CD_GENDER") + public StringBuilder gender; + + @ExistInCodeList(codeListId = "CD_LANG") + public StringBuilder lang; +} + class Order { @ExistInCodeList.List(value = { @ExistInCodeList(codeListId = "CD_MULTIPLES_OF_3", message = "number must be multiples of 3"),