use of org.qi4j.api.constraint.ConstraintViolationException in project qi4j-sdk by Qi4j.
the class NamedAssociationModel method bind.
@Override
public void bind(Resolution resolution) throws BindingException {
builderInfo = new AssociationInfo() {
@Override
public boolean isImmutable() {
return false;
}
@Override
public QualifiedName qualifiedName() {
return qualifiedName;
}
@Override
public Type type() {
return type;
}
@Override
public void checkConstraints(Object value) throws ConstraintViolationException {
NamedAssociationModel.this.checkConstraints(value);
}
};
if (type instanceof TypeVariable) {
Class mainType = first(resolution.model().types());
type = Classes.resolveTypeVariable((TypeVariable) type, ((Member) accessor).getDeclaringClass(), mainType);
}
}
use of org.qi4j.api.constraint.ConstraintViolationException in project qi4j-sdk by Qi4j.
the class AlarmServiceTest method testListeners.
@Test
public void testListeners() throws Exception {
AlarmSystem alarmService = (AlarmSystem) module.findService(AlarmSystem.class).get();
AlarmPoint alarm = alarmService.createAlarm("TestAlarm", createCategory("AlarmServiceTest"));
CountingListener listener1 = new CountingListener();
ExceptionThrowingListener listener2 = new ExceptionThrowingListener();
ErrorThrowingListener listener3 = new ErrorThrowingListener();
alarmService.addAlarmListener(listener1);
alarmService.addAlarmListener(listener3);
alarmService.addAlarmListener(listener1);
try {
alarm.activate();
fail("InternalError was expected to be thrown.");
} catch (InternalError e) {
// Expected. If an Error is thrown it should not be captured anywhere.
}
// One time, because the second listener would not be called.
assertEquals(1, listener1.getCounter());
alarmService.removeAlarmListener(listener3);
alarmService.removeAlarmListener(listener1);
alarmService.addAlarmListener(listener2);
// We should now have, 'counting', 'exception', 'counting' in the list.
alarmService.addAlarmListener(listener1);
// No Exception should be thrown. The fireAlarm() should swallow it and ensure that
alarm.deactivate();
// all listeners are called and then return.
assertEquals(3, listener1.getCounter());
List listeners = alarmService.alarmListeners();
assertEquals("Listeners registered.", 3, listeners.size());
alarmService.removeAlarmListener(listener1);
listeners = alarmService.alarmListeners();
assertEquals("Listeners registered.", 2, listeners.size());
alarmService.removeAlarmListener(listener1);
listeners = alarmService.alarmListeners();
assertEquals("Listeners registered.", 1, listeners.size());
alarmService.removeAlarmListener(listener1);
listeners = alarmService.alarmListeners();
assertEquals("Listeners registered.", 1, listeners.size());
alarmService.removeAlarmListener(listener2);
listeners = alarmService.alarmListeners();
System.out.println(listeners);
assertEquals("Listeners registered.", 0, listeners.size());
alarmService.removeAlarmListener(listener2);
listeners = alarmService.alarmListeners();
assertEquals("Listeners registered.", 0, listeners.size());
try {
alarmService.removeAlarmListener(null);
fail("Should not be able to call removeAlarmListener with null argument.");
} catch (ConstraintViolationException e) {
// expected
}
listeners = alarmService.alarmListeners();
assertEquals("Listeners registered.", 0, listeners.size());
}
use of org.qi4j.api.constraint.ConstraintViolationException in project qi4j-sdk by Qi4j.
the class Qi4jPropertyAccessor method setProperty.
@Override
@SuppressWarnings("unchecked")
public final void setProperty(Map aContext, Object aTarget, Object aPropertyName, Object aPropertyValue) throws OgnlException {
String fieldName = aPropertyName.toString();
Object qi4jField = getQi4jField(aContext, aTarget, fieldName);
if (qi4jField != null) {
Class<?> memberClass = qi4jField.getClass();
if (Property.class.isAssignableFrom(memberClass)) {
Property property = (Property) qi4jField;
OgnlContext ognlContext = (OgnlContext) aContext;
Class<?> propertyType = (Class) api.propertyDescriptorFor(property).type();
Object convertedValue = getConvertedType(ognlContext, aTarget, null, fieldName, aPropertyValue, propertyType);
try {
property.set(convertedValue);
} catch (ConstraintViolationException e) {
Collection<ConstraintViolation> violations = e.constraintViolations();
handleConstraintViolation(aContext, aTarget, fieldName, convertedValue, violations);
}
return;
} else if (Association.class.isAssignableFrom(memberClass)) {
Association association = (Association) qi4jField;
OgnlContext ognlContext = (OgnlContext) aContext;
Class<?> associationType = (Class) api.associationDescriptorFor(association).type();
Object convertedValue = getConvertedType(ognlContext, aTarget, null, fieldName, aPropertyValue, associationType);
if (convertedValue == OgnlRuntime.NoConversionPossible) {
throw new OgnlException("Could not convert value to association type");
}
try {
association.set(convertedValue);
} catch (ConstraintViolationException e) {
Collection<ConstraintViolation> violations = e.constraintViolations();
handleConstraintViolation(aContext, aTarget, fieldName, aPropertyValue, violations);
}
return;
} else if (ManyAssociation.class.isAssignableFrom(memberClass)) {
throw new OgnlException("Setting many association [" + fieldName + "] is impossible.");
} else if (NamedAssociation.class.isAssignableFrom(memberClass)) {
throw new OgnlException("Setting named association [" + fieldName + "] is impossible.");
}
}
super.setProperty(aContext, aTarget, aPropertyName, aPropertyValue);
}
use of org.qi4j.api.constraint.ConstraintViolationException in project qi4j-sdk by Qi4j.
the class AssociationModel method bind.
@Override
public void bind(Resolution resolution) throws BindingException {
builderInfo = new AssociationInfo() {
@Override
public boolean isImmutable() {
return false;
}
@Override
public QualifiedName qualifiedName() {
return qualifiedName;
}
@Override
public Type type() {
return type;
}
@Override
public void checkConstraints(Object value) throws ConstraintViolationException {
AssociationModel.this.checkConstraints(value);
}
};
if (type instanceof TypeVariable) {
Class mainType = first(resolution.model().types());
type = Classes.resolveTypeVariable((TypeVariable) type, ((Member) accessor).getDeclaringClass(), mainType);
}
}
use of org.qi4j.api.constraint.ConstraintViolationException in project qi4j-sdk by Qi4j.
the class ManyAssociationModel method bind.
@Override
public void bind(Resolution resolution) throws BindingException {
builderInfo = new AssociationInfo() {
@Override
public boolean isImmutable() {
return false;
}
@Override
public QualifiedName qualifiedName() {
return qualifiedName;
}
@Override
public Type type() {
return type;
}
@Override
public void checkConstraints(Object value) throws ConstraintViolationException {
ManyAssociationModel.this.checkConstraints(value);
}
};
if (type instanceof TypeVariable) {
Class mainType = first(resolution.model().types());
type = Classes.resolveTypeVariable((TypeVariable) type, ((Member) accessor).getDeclaringClass(), mainType);
}
}
Aggregations