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));
}
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());
}
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());
}
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());
}
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());
}
Aggregations