use of org.qi4j.api.constraint.ConstraintViolationException in project qi4j-sdk by Qi4j.
the class PropertyConstraintTest method givenConstraintOnPropertyWhenInvalidValueThenThrowException.
@org.junit.Test
public void givenConstraintOnPropertyWhenInvalidValueThenThrowException() throws Throwable {
TransientBuilder<Test> builder = module.newTransientBuilder(Test.class);
builder.prototype().test().set("XXXXXX");
Test test = builder.newInstance();
try {
test.test().set("YY");
fail("Should have thrown a ConstraintViolationException.");
} catch (ConstraintViolationException e) {
Collection<ConstraintViolation> violations = e.constraintViolations();
assertEquals(2, violations.size());
}
}
use of org.qi4j.api.constraint.ConstraintViolationException in project qi4j-sdk by Qi4j.
the class ConstraintTest method testNotEmptyFail.
@Test
public void testNotEmptyFail() {
TransientBuilder<TestCaseComposite> cb = module.newTransientBuilder(TestCaseComposite.class);
try {
cb.prototype().notEmptyString().set("");
fail("Should have thrown exception");
} catch (ConstraintViolationException e) {
}
try {
cb.prototype().notEmptyCollection().set(new ArrayList());
fail("Should have thrown exception");
} catch (ConstraintViolationException e) {
}
try {
cb.prototype().notEmptyList().set(new ArrayList());
fail("Should have thrown exception");
} catch (ConstraintViolationException e) {
}
}
use of org.qi4j.api.constraint.ConstraintViolationException in project qi4j-sdk by Qi4j.
the class HelloWorldCompositeTest method testComposite.
@Test
public void testComposite() {
SingletonAssembler assembler = new SingletonAssembler() {
@Override
public void assemble(ModuleAssembly module) throws AssemblyException {
module.transients(HelloWorldComposite.class, HelloWorldComposite2.class).withMixins(ScalaTraitMixin.class).withConcerns(ExclamationGenericConcern.class);
}
};
HelloWorldComposite composite = assembler.module().newTransient(HelloWorldComposite.class);
Assert.assertEquals("Do stuff!", composite.doStuff());
Assert.assertEquals("Hello there World!", composite.sayHello("World"));
try {
composite.sayHello("AReallyReallyLongName");
} catch (ConstraintViolationException e) {
// Ok!
}
HelloWorldComposite2 composite2 = assembler.module().newTransient(HelloWorldComposite2.class);
Assert.assertEquals("Do custom stuff!", composite2.doStuff());
}
use of org.qi4j.api.constraint.ConstraintViolationException in project qi4j-sdk by Qi4j.
the class ContextResource method handleException.
private void handleException(Response response, Throwable ex) {
while (ex instanceof InvocationTargetException) {
ex = ex.getCause();
}
try {
throw ex;
} catch (ResourceException e) {
// IAE (or subclasses) are considered client faults
response.setEntity(new StringRepresentation(e.getMessage()));
response.setStatus(e.getStatus());
} catch (ConstraintViolationException e) {
try {
ConstraintViolationMessages cvm = new ConstraintViolationMessages();
// CVE are considered client faults
String messages = "";
Locale locale = ObjectSelection.type(Locale.class);
for (ConstraintViolation constraintViolation : e.constraintViolations()) {
if (!messages.isEmpty()) {
messages += "\n";
}
messages += cvm.getMessage(constraintViolation, locale);
}
response.setEntity(new StringRepresentation(messages));
response.setStatus(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY);
} catch (Exception e1) {
response.setEntity(new StringRepresentation(e.getMessage()));
response.setStatus(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY);
}
} catch (IllegalArgumentException e) {
// IAE (or subclasses) are considered client faults
response.setEntity(new StringRepresentation(e.getMessage()));
response.setStatus(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY);
} catch (RuntimeException e) {
// RuntimeExceptions are considered server faults
LoggerFactory.getLogger(getClass()).warn("Exception thrown during processing", e);
response.setEntity(new StringRepresentation(e.getMessage()));
response.setStatus(Status.SERVER_ERROR_INTERNAL);
} catch (Exception e) {
// Checked exceptions are considered client faults
String s = e.getMessage();
if (s == null) {
s = e.getClass().getSimpleName();
}
response.setEntity(new StringRepresentation(s));
response.setStatus(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY);
} catch (Throwable e) {
// Anything else are considered server faults
LoggerFactory.getLogger(getClass()).error("Exception thrown during processing", e);
response.setEntity(new StringRepresentation(e.getMessage()));
response.setStatus(Status.SERVER_ERROR_INTERNAL);
}
}
Aggregations