Introduction
The Validator component brings Bean Validation to OSGi.
How to use
Java classes can be annotated with Bean Validation rules. Using Amdatu Validator you can use the standard Bean Validation annotations, Hibernate Validator annotations, and custom annotations. Take the following Java class as an example.
package org.amdatu.validator.test;
import javax.validation.constraints.NotNull;
public class ObjectToValidate {
@NotNull
private String m_name;
public ObjectToValidate(String name) {
m_name = name;
}
public String getName() {
return m_name;
}
public void setName(String name) {
m_name = name;
}
}
To validate instances of this class we use the Amdatu Validator API. The following integration test shows this.
package org.amdatu.validator.test;
import static org.amdatu.testing.configurator.TestConfigurator.cleanUp;
import static org.amdatu.testing.configurator.TestConfigurator.configure;
import static org.amdatu.testing.configurator.TestConfigurator.createServiceDependency;
import static org.junit.Assert.assertEquals;
import javax.validation.ConstraintViolationException;
import org.amdatu.validator.ValidatorService;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class ValidatorServiceTest {
private volatile ValidatorService m_validator;
@Before
public void setup() {
configure(this).add(createServiceDependency().setService(ValidatorService.class).setRequired(true)).apply();
}
@Test
public void testDefaultErrorMessage() {
try {
m_validator.validate(new ObjectToValidate(null));
} catch(ConstraintViolationException ex) {
String message = ex.getConstraintViolations().iterator().next().getMessage();
assertEquals("May not be null.", message);
}
}
@After
public void cleanup() {
cleanUp(this);
}
}
Getting access to the Bean Validation API
Sometimes you need more control over validation logic. For this it’s useful to have access to the Bean Validation API directly.
@Test
public void testUseUnderlyingValidator() {
Validator validator = m_validator.getValidator();
Set<ConstraintViolation<ObjectToValidate>> result =
validator.validate(new ObjectToValidate(null, null));
assertFalse(result.isEmpty());
Set<ConstraintViolation<ObjectToValidate>> result2 =
validator.validate(new ObjectToValidate("test", "abc"));
assertTrue(result2.isEmpty());
}
Message interpolation
You can add your own internationalized validation messages as property files. You have to package these property files in a fragment bundle with fragment-host 'org.amdatu.validator'. The property files must be in the root of the fragment bundle and contain key/value pairs as shown below.
javax.validation.constraints.AssertFalse.message=The value must be false.
javax.validation.constraints.AssertTrue.message=The value must be true.
javax.validation.constraints.DecimalMax.message=Must be less than or equal to {value}.
Components
Amdatu Validator provides the following components:
Bundle |
Required |
Description |
org.amdatu.validator |
yes |
The implementation bundle based on Hibernate Validator |
Dependencies
The following table provides an overview of the required and optional dependencies for Amdatu Validator:
Bundle |
Required |
Description |
org.apache.felix.dependencymanager |
yes |
Apache Felix Dependency Manager is used internally by all Amdatu components |
javax.validation.api |
yes |
The Bean Validation API |
javax.el-api |
yes |
Expression Language API, used by Bean Validation for message interpolation |
org.glassfish.web.javax.el |
yes |
Expression Language Implementation, used by Bean Validation for message interpolation |
Resources
Tools |
Location |
Source Code |
|
Issue Tracking |
|
Continuous Build |
https://bitbucket.org/amdatu/amdatu-validator/addon/pipelines/home |