Search in sources :

Example 6 with ValidationResult

use of org.apache.cayenne.validation.ValidationResult in project cayenne by apache.

the class FlattenedRelationshipsIT method testNullFlattenedComplexToOneRelationship.

/**
 * Should be able to save/insert an object with null flattened (complex) toOne relationship
 * @throws Exception
 */
@Test
public void testNullFlattenedComplexToOneRelationship() throws Exception {
    FlattenedTest5 ft5 = context.newObject(FlattenedTest5.class);
    ft5.setName("FT5");
    // should be valid for save
    ValidationResult validationResult = new ValidationResult();
    ft5.validateForSave(validationResult);
    assertTrue(validationResult.toString(), validationResult.getFailures().isEmpty());
    context.commitChanges();
    assertEquals(1, ObjectSelect.query(FlattenedTest5.class).selectCount(context));
}
Also used : ValidationResult(org.apache.cayenne.validation.ValidationResult) FlattenedTest5(org.apache.cayenne.testdo.relationships_flattened.FlattenedTest5) Test(org.junit.Test)

Example 7 with ValidationResult

use of org.apache.cayenne.validation.ValidationResult in project cayenne by apache.

the class MeaningfulFKIT method testValidateForSave1.

@Test
public void testValidateForSave1() throws Exception {
    MeaningfulFK testObject = context.newObject(MeaningfulFK.class);
    ValidationResult validation = new ValidationResult();
    testObject.validateForSave(validation);
    assertTrue("Must fail validation due to missing required relationship", validation.hasFailures());
    assertEquals("Must fail validation due to missing required relationship", 1, validation.getFailures().size());
}
Also used : MeaningfulFK(org.apache.cayenne.testdo.relationships.MeaningfulFK) ValidationResult(org.apache.cayenne.validation.ValidationResult) Test(org.junit.Test)

Example 8 with ValidationResult

use of org.apache.cayenne.validation.ValidationResult 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)

Example 9 with ValidationResult

use of org.apache.cayenne.validation.ValidationResult in project cayenne by apache.

the class CayenneDataObjectValidationIT method testValidateForSaveMandatoryToOneMissing.

@Test
public void testValidateForSaveMandatoryToOneMissing() throws Exception {
    Exhibit exhibit = context.newObject(Exhibit.class);
    exhibit.setOpeningDate(new Date());
    exhibit.setClosingDate(new Date());
    ValidationResult result = new ValidationResult();
    exhibit.validateForSave(result);
    assertTrue("Validation of 'toGallery' should've failed.", result.hasFailures());
    assertTrue(result.hasFailures(exhibit));
    List<ValidationFailure> failures = result.getFailures();
    assertEquals(1, failures.size());
    BeanValidationFailure failure = (BeanValidationFailure) failures.get(0);
    assertEquals(Exhibit.TO_GALLERY.getName(), failure.getProperty());
    // fix the problem and see if it goes away
    Gallery gallery = context.newObject(Gallery.class);
    exhibit.setToGallery(gallery);
    result = new ValidationResult();
    exhibit.validateForSave(result);
    assertFalse("No failures expected: " + result, result.hasFailures());
}
Also used : Exhibit(org.apache.cayenne.testdo.testmap.Exhibit) BeanValidationFailure(org.apache.cayenne.validation.BeanValidationFailure) Gallery(org.apache.cayenne.testdo.testmap.Gallery) ValidationResult(org.apache.cayenne.validation.ValidationResult) Date(java.util.Date) ValidationFailure(org.apache.cayenne.validation.ValidationFailure) BeanValidationFailure(org.apache.cayenne.validation.BeanValidationFailure) Test(org.junit.Test)

Example 10 with ValidationResult

use of org.apache.cayenne.validation.ValidationResult in project cayenne by apache.

the class MeaningfulFKIT method testValidateForSave2.

@Test
public void testValidateForSave2() throws Exception {
    MeaningfulFK testObject = context.newObject(MeaningfulFK.class);
    RelationshipHelper related = testObject.getObjectContext().newObject(RelationshipHelper.class);
    testObject.setToRelationshipHelper(related);
    ValidationResult validation = new ValidationResult();
    testObject.validateForSave(validation);
    assertFalse(validation.hasFailures());
}
Also used : MeaningfulFK(org.apache.cayenne.testdo.relationships.MeaningfulFK) RelationshipHelper(org.apache.cayenne.testdo.relationships.RelationshipHelper) ValidationResult(org.apache.cayenne.validation.ValidationResult) Test(org.junit.Test)

Aggregations

ValidationResult (org.apache.cayenne.validation.ValidationResult)18 Test (org.junit.Test)7 ValidationFailure (org.apache.cayenne.validation.ValidationFailure)6 ProjectValidator (org.apache.cayenne.project.validation.ProjectValidator)4 Artist (org.apache.cayenne.testdo.testmap.Artist)3 BeanValidationFailure (org.apache.cayenne.validation.BeanValidationFailure)3 ArrayList (java.util.ArrayList)2 DbEntity (org.apache.cayenne.map.DbEntity)2 ObjEntity (org.apache.cayenne.map.ObjEntity)2 MeaningfulFK (org.apache.cayenne.testdo.relationships.MeaningfulFK)2 SimpleValidationFailure (org.apache.cayenne.validation.SimpleValidationFailure)2 ValidationException (org.apache.cayenne.validation.ValidationException)2 File (java.io.File)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 Date (java.util.Date)1 LinkedList (java.util.LinkedList)1 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)1 Persistent (org.apache.cayenne.Persistent)1 Validating (org.apache.cayenne.Validating)1