Skip to content

Commit

Permalink
Merge pull request #432 from terasolunaorg/issues/402_Support_CharSeq…
Browse files Browse the repository at this point in the history
…uence

Suppot CharSequence #402
  • Loading branch information
ikeyat committed Dec 4, 2015
2 parents d44f664 + d2c663a commit 4113947
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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"),
Expand Down

0 comments on commit 4113947

Please sign in to comment.