-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
80ac048
commit 3e2d0a2
Showing
15 changed files
with
221 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/main/java/org/fuin/ddd4j/ddd/HasEntityTypeConstantValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package org.fuin.ddd4j.ddd; | ||
|
||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Modifier; | ||
|
||
/** | ||
* Determines if the annotated class has a public static constant with the given name and {@link EntityType} type. | ||
*/ | ||
public class HasEntityTypeConstantValidator implements ConstraintValidator<HasEntityTypeConstant, Object> { | ||
|
||
private String name; | ||
|
||
@Override | ||
public void initialize(HasEntityTypeConstant annotation) { | ||
this.name = annotation.value(); | ||
} | ||
|
||
@Override | ||
public boolean isValid(Object obj, ConstraintValidatorContext context) { | ||
try { | ||
final Field field = obj.getClass().getField(name); | ||
final int modifiers = field.getModifiers(); | ||
if (!Modifier.isStatic(modifiers)) { | ||
error(context, "Field '" + name + "' is not static (#1)"); | ||
return false; | ||
} | ||
if (field.getType() != EntityType.class) { | ||
error(context, "Expected constant '" + name + "' to be of type '" + EntityType.class.getName() + "', but was: " + field.getType().getName() + " (#3)"); | ||
return false; | ||
} | ||
final Object value = field.get(obj); | ||
if (value == null) { | ||
error(context, "Constant '" + name + "' is expected to be a non-null value (#4)"); | ||
return false; | ||
} | ||
if (!Modifier.isFinal(modifiers)) { | ||
error(context, "Constant '" + name + "' is not not final (#5)"); | ||
return false; | ||
} | ||
return true; | ||
} catch (final NoSuchFieldException ex) { | ||
error(context, "The field '" + name + "' is undefined or it is not public (#2)"); | ||
return false; | ||
} catch (final IllegalAccessException ex) { | ||
throw new IllegalStateException("Failed to execute method", ex); | ||
} | ||
|
||
} | ||
|
||
private void error(ConstraintValidatorContext context, String message) { | ||
context.disableDefaultConstraintViolation(); | ||
context.buildConstraintViolationWithTemplate(message).addConstraintViolation(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
src/test/java/org/fuin/ddd4j/ddd/HasEntityTypeConstantValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/** | ||
* Copyright (C) 2013 Future Invent Informationsmanagement GmbH. All rights | ||
* reserved. <http://www.fuin.org/> | ||
* <p> | ||
* This library is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU Lesser General Public License as published by the Free | ||
* Software Foundation; either version 3 of the License, or (at your option) any | ||
* later version. | ||
* <p> | ||
* This library is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more | ||
* details. | ||
* <p> | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.fuin.ddd4j.ddd; | ||
|
||
import jakarta.validation.ConstraintViolation; | ||
import jakarta.validation.Validation; | ||
import jakarta.validation.Validator; | ||
import jakarta.validation.ValidatorFactory; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Set; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public final class HasEntityTypeConstantValidatorTest { | ||
|
||
private static Validator validator; | ||
|
||
@BeforeAll | ||
static void beforeAll() { | ||
try (final ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory()) { | ||
validator = validatorFactory.getValidator(); | ||
} | ||
} | ||
|
||
@Test | ||
public final void testValid() { | ||
assertThat(validator.validate(new MyClassValid())).isEmpty(); | ||
} | ||
|
||
@Test | ||
public final void testNotStatic() { | ||
assertThat(first(validator.validate(new MyClassNotStatic()))).contains("#1"); | ||
} | ||
|
||
@Test | ||
public final void testNotPublic() { | ||
assertThat(first(validator.validate(new MyClassNotPublic()))).contains("#2"); | ||
} | ||
|
||
@Test | ||
public final void testWrongReturnType() { | ||
assertThat(first(validator.validate(new MyClassWrongType()))).contains("#3"); | ||
} | ||
|
||
@Test | ||
public final void testWrongReturn() { | ||
assertThat(first(validator.validate(new MyClassNullValue()))).contains("#4"); | ||
} | ||
|
||
@Test | ||
public final void testNoMethod() { | ||
assertThat(first(validator.validate(new MyClassNoField()))).contains("#2"); | ||
} | ||
|
||
@Test | ||
public final void testNotFinal() { | ||
assertThat(first(validator.validate(new MyClassNotFinal()))).contains("#5"); | ||
} | ||
|
||
private static String first(Set<?> violations) { | ||
return violations.stream().map(v -> ((ConstraintViolation<?>) v).getMessage()).findFirst().orElse(null); | ||
} | ||
|
||
@HasEntityTypeConstant | ||
public static final class MyClassValid { | ||
public static final EntityType TYPE = new StringBasedEntityType("XYZ"); | ||
} | ||
|
||
@HasEntityTypeConstant | ||
public static final class MyClassNotStatic { | ||
public final EntityType TYPE = new StringBasedEntityType("XYZ"); | ||
} | ||
|
||
@HasEntityTypeConstant | ||
public static final class MyClassNotPublic { | ||
protected static final EntityType TYPE = new StringBasedEntityType("XYZ"); | ||
} | ||
|
||
@HasEntityTypeConstant | ||
public static final class MyClassNoField { | ||
} | ||
|
||
@HasEntityTypeConstant | ||
public static final class MyClassWrongType { | ||
public static final Integer TYPE = 123; | ||
} | ||
|
||
@HasEntityTypeConstant | ||
public static final class MyClassNullValue { | ||
public static final EntityType TYPE = null; | ||
} | ||
|
||
@HasEntityTypeConstant | ||
public static final class MyClassNotFinal { | ||
public static EntityType TYPE = new StringBasedEntityType("XYZ"); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.