use of com.alibaba.dubbo.examples.validation.api.ValidationService in project dubbo by alibaba.
the class ValidationTest method testValidation.
@Test
public void testValidation() {
ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(ValidationTest.class.getPackage().getName().replace('.', '/') + "/validation-provider.xml");
providerContext.start();
try {
ClassPathXmlApplicationContext consumerContext = new ClassPathXmlApplicationContext(ValidationTest.class.getPackage().getName().replace('.', '/') + "/validation-consumer.xml");
consumerContext.start();
try {
ValidationService validationService = (ValidationService) consumerContext.getBean("validationService");
// Save OK
ValidationParameter parameter = new ValidationParameter();
parameter.setName("liangfei");
parameter.setEmail("liangfei@liang.fei");
parameter.setAge(50);
parameter.setLoginDate(new Date(System.currentTimeMillis() - 1000000));
parameter.setExpiryDate(new Date(System.currentTimeMillis() + 1000000));
validationService.save(parameter);
try {
parameter = new ValidationParameter();
parameter.setName("l");
parameter.setEmail("liangfei@liang.fei");
parameter.setAge(50);
parameter.setLoginDate(new Date(System.currentTimeMillis() - 1000000));
parameter.setExpiryDate(new Date(System.currentTimeMillis() + 1000000));
validationService.save(parameter);
Assert.fail();
} catch (RpcException e) {
ConstraintViolationException ve = (ConstraintViolationException) e.getCause();
Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
Assert.assertNotNull(violations);
}
// Save Error
try {
parameter = new ValidationParameter();
validationService.save(parameter);
Assert.fail();
} catch (RpcException e) {
ConstraintViolationException ve = (ConstraintViolationException) e.getCause();
Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
Assert.assertNotNull(violations);
}
// Delete OK
validationService.delete(2, "abc");
// Delete Error
try {
validationService.delete(2, "a");
Assert.fail();
} catch (RpcException e) {
ConstraintViolationException ve = (ConstraintViolationException) e.getCause();
Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
Assert.assertNotNull(violations);
Assert.assertEquals(1, violations.size());
}
// Delete Error
try {
validationService.delete(0, "abc");
Assert.fail();
} catch (RpcException e) {
ConstraintViolationException ve = (ConstraintViolationException) e.getCause();
Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
Assert.assertNotNull(violations);
Assert.assertEquals(1, violations.size());
}
try {
validationService.delete(2, null);
Assert.fail();
} catch (RpcException e) {
ConstraintViolationException ve = (ConstraintViolationException) e.getCause();
Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
Assert.assertNotNull(violations);
Assert.assertEquals(1, violations.size());
}
try {
validationService.delete(0, null);
Assert.fail();
} catch (RpcException e) {
ConstraintViolationException ve = (ConstraintViolationException) e.getCause();
Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
Assert.assertNotNull(violations);
Assert.assertEquals(2, violations.size());
}
} finally {
consumerContext.stop();
consumerContext.close();
}
} finally {
providerContext.stop();
providerContext.close();
}
}
use of com.alibaba.dubbo.examples.validation.api.ValidationService in project dubbo by alibaba.
the class ValidationConsumer method main.
public static void main(String[] args) throws Exception {
String config = ValidationConsumer.class.getPackage().getName().replace('.', '/') + "/validation-consumer.xml";
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(config);
context.start();
ValidationService validationService = (ValidationService) context.getBean("validationService");
// Save OK
ValidationParameter parameter = new ValidationParameter();
parameter.setName("liangfei");
parameter.setEmail("liangfei@liang.fei");
parameter.setAge(50);
parameter.setLoginDate(new Date(System.currentTimeMillis() - 1000000));
parameter.setExpiryDate(new Date(System.currentTimeMillis() + 1000000));
validationService.save(parameter);
System.out.println("Validation Save OK");
// Save Error
try {
parameter = new ValidationParameter();
validationService.save(parameter);
System.err.println("Validation Save ERROR");
} catch (RpcException e) {
ConstraintViolationException ve = (ConstraintViolationException) e.getCause();
Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
System.out.println(violations);
}
// Delete OK
validationService.delete(2, "abc");
System.out.println("Validation Delete OK");
// Delete Error
try {
validationService.delete(0, "abc");
System.err.println("Validation Delete ERROR");
} catch (RpcException e) {
ConstraintViolationException ve = (ConstraintViolationException) e.getCause();
Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
System.out.println(violations);
}
}
Aggregations