Search in sources :

Example 1 with ValidationDelegate

use of org.apache.cayenne.unit.util.ValidationDelegate in project cayenne by apache.

the class DataContextValidationIT method testValidationModifyingContext.

@Test
public void testValidationModifyingContext() throws Exception {
    ValidationDelegate delegate = new ValidationDelegate() {

        public void validateForSave(Object object, ValidationResult validationResult) {
            Artist a = (Artist) object;
            Painting p = a.getObjectContext().newObject(Painting.class);
            p.setPaintingTitle("XXX");
            p.setToArtist(a);
        }
    };
    context.setValidatingObjectsOnCommit(true);
    Artist a1 = context.newObject(Artist.class);
    a1.setValidationDelegate(delegate);
    a1.setArtistName("a1");
    // add another artist to ensure that modifying context works when more than one
    // object is committed
    Artist a2 = context.newObject(Artist.class);
    a2.setValidationDelegate(delegate);
    a2.setArtistName("a2");
    context.commitChanges();
    assertEquals(2, context.performQuery(new SelectQuery(Painting.class)).size());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SelectQuery(org.apache.cayenne.query.SelectQuery) ValidationDelegate(org.apache.cayenne.unit.util.ValidationDelegate) ValidationResult(org.apache.cayenne.validation.ValidationResult) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Aggregations

SelectQuery (org.apache.cayenne.query.SelectQuery)1 Artist (org.apache.cayenne.testdo.testmap.Artist)1 Painting (org.apache.cayenne.testdo.testmap.Painting)1 ValidationDelegate (org.apache.cayenne.unit.util.ValidationDelegate)1 ValidationResult (org.apache.cayenne.validation.ValidationResult)1 Test (org.junit.Test)1