Search in sources :

Example 1 with ConstraintViolationException

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);
    }
}
Also used : Type(java.lang.reflect.Type) GenericAssociationInfo(org.qi4j.api.association.GenericAssociationInfo) TypeVariable(java.lang.reflect.TypeVariable) QualifiedName(org.qi4j.api.common.QualifiedName) ConstraintViolationException(org.qi4j.api.constraint.ConstraintViolationException) AccessibleObject(java.lang.reflect.AccessibleObject) Member(java.lang.reflect.Member)

Example 2 with ConstraintViolationException

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());
}
Also used : ConstraintViolationException(org.qi4j.api.constraint.ConstraintViolationException) List(java.util.List) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 3 with ConstraintViolationException

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);
}
Also used : NamedAssociation(org.qi4j.api.association.NamedAssociation) OgnlContext(ognl.OgnlContext) OgnlException(ognl.OgnlException) ManyAssociation(org.qi4j.api.association.ManyAssociation) Association(org.qi4j.api.association.Association) NamedAssociation(org.qi4j.api.association.NamedAssociation) ConstraintViolation(org.qi4j.api.constraint.ConstraintViolation) ConstraintViolationException(org.qi4j.api.constraint.ConstraintViolationException) Collection(java.util.Collection) Property(org.qi4j.api.property.Property)

Example 4 with ConstraintViolationException

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);
    }
}
Also used : Type(java.lang.reflect.Type) GenericAssociationInfo(org.qi4j.api.association.GenericAssociationInfo) TypeVariable(java.lang.reflect.TypeVariable) QualifiedName(org.qi4j.api.common.QualifiedName) ConstraintViolationException(org.qi4j.api.constraint.ConstraintViolationException) AccessibleObject(java.lang.reflect.AccessibleObject) Member(java.lang.reflect.Member)

Example 5 with ConstraintViolationException

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);
    }
}
Also used : Type(java.lang.reflect.Type) GenericAssociationInfo(org.qi4j.api.association.GenericAssociationInfo) TypeVariable(java.lang.reflect.TypeVariable) QualifiedName(org.qi4j.api.common.QualifiedName) ConstraintViolationException(org.qi4j.api.constraint.ConstraintViolationException) AccessibleObject(java.lang.reflect.AccessibleObject) Member(java.lang.reflect.Member)

Aggregations

ConstraintViolationException (org.qi4j.api.constraint.ConstraintViolationException)9 AccessibleObject (java.lang.reflect.AccessibleObject)3 Member (java.lang.reflect.Member)3 Type (java.lang.reflect.Type)3 TypeVariable (java.lang.reflect.TypeVariable)3 Test (org.junit.Test)3 GenericAssociationInfo (org.qi4j.api.association.GenericAssociationInfo)3 QualifiedName (org.qi4j.api.common.QualifiedName)3 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)3 Collection (java.util.Collection)2 ConstraintViolation (org.qi4j.api.constraint.ConstraintViolation)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Locale (java.util.Locale)1 OgnlContext (ognl.OgnlContext)1 OgnlException (ognl.OgnlException)1 Association (org.qi4j.api.association.Association)1 ManyAssociation (org.qi4j.api.association.ManyAssociation)1