Search in sources :

Example 6 with ValidationFailure

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

the class CayenneDataObjectValidationIT method testValidateForSaveAttributeTooLong.

@Test
public void testValidateForSaveAttributeTooLong() throws Exception {
    Artist artist = context.newObject(Artist.class);
    DbEntity entity = context.getEntityResolver().getObjEntity(artist).getDbEntity();
    int len = entity.getAttribute("ARTIST_NAME").getMaxLength();
    StringBuffer buf = new StringBuffer(len);
    for (int i = 0; i < len + 1; i++) {
        buf.append("c");
    }
    artist.setArtistName(buf.toString());
    ValidationResult result = new ValidationResult();
    artist.validateForSave(result);
    assertTrue(result.hasFailures());
    assertTrue(result.hasFailures(artist));
    List<ValidationFailure> failures = result.getFailures();
    assertEquals(1, failures.size());
    BeanValidationFailure failure = (BeanValidationFailure) failures.get(0);
    assertEquals(Artist.ARTIST_NAME.getName(), failure.getProperty());
    // fix the problem and see if it goes away
    artist.setArtistName("aa");
    result = new ValidationResult();
    artist.validateForSave(result);
    assertFalse(result.hasFailures());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) DbEntity(org.apache.cayenne.map.DbEntity) BeanValidationFailure(org.apache.cayenne.validation.BeanValidationFailure) ValidationResult(org.apache.cayenne.validation.ValidationResult) ValidationFailure(org.apache.cayenne.validation.ValidationFailure) BeanValidationFailure(org.apache.cayenne.validation.BeanValidationFailure) Test(org.junit.Test)

Example 7 with ValidationFailure

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

the class CayenneDataObjectValidationIT method testValidateForSaveMandatoryAttributeMissing.

@Test
public void testValidateForSaveMandatoryAttributeMissing() throws Exception {
    Artist artist = context.newObject(Artist.class);
    ValidationResult result = new ValidationResult();
    artist.validateForSave(result);
    assertTrue("Validation of 'artistName' should've failed.", result.hasFailures());
    assertTrue(result.hasFailures(artist));
    List<ValidationFailure> failures = result.getFailures();
    assertEquals(1, failures.size());
    BeanValidationFailure failure = (BeanValidationFailure) failures.get(0);
    assertEquals(Artist.ARTIST_NAME.getName(), failure.getProperty());
    // fix the problem and see if it goes away
    artist.setArtistName("aa");
    result = new ValidationResult();
    artist.validateForSave(result);
    assertFalse(result.hasFailures());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) BeanValidationFailure(org.apache.cayenne.validation.BeanValidationFailure) ValidationResult(org.apache.cayenne.validation.ValidationResult) ValidationFailure(org.apache.cayenne.validation.ValidationFailure) BeanValidationFailure(org.apache.cayenne.validation.BeanValidationFailure) Test(org.junit.Test)

Example 8 with ValidationFailure

use of org.apache.cayenne.validation.ValidationFailure 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 9 with ValidationFailure

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

the class DefaultDbImportAction method applyTokens.

private boolean applyTokens(DataMap targetDataMap, Collection<MergerToken> tokens, DbImportConfiguration config) {
    if (tokens.isEmpty()) {
        logger.info("");
        logger.info("Detected changes: No changes to import.");
        return false;
    }
    final Collection<ObjEntity> loadedObjEntities = new LinkedList<>();
    ModelMergeDelegate mergeDelegate = new ProxyModelMergeDelegate(config.createMergeDelegate()) {

        @Override
        public void objEntityAdded(ObjEntity ent) {
            loadedObjEntities.add(ent);
            super.objEntityAdded(ent);
        }
    };
    ObjectNameGenerator nameGenerator = config.createNameGenerator();
    MergerContext mergerContext = MergerContext.builder(targetDataMap).delegate(mergeDelegate).nameGenerator(nameGenerator).usingPrimitives(config.isUsePrimitives()).usingJava7Types(config.isUseJava7Types()).meaningfulPKFilter(config.createMeaningfulPKFilter()).build();
    for (MergerToken token : tokens) {
        try {
            token.execute(mergerContext);
        } catch (Throwable th) {
            String message = "Migration Error. Can't apply changes from token: " + token.getTokenName() + " (" + token.getTokenValue() + ")";
            logger.error(message, th);
            mergerContext.getValidationResult().addFailure(new SimpleValidationFailure(th, message));
        }
    }
    ValidationResult failures = mergerContext.getValidationResult();
    if (failures.hasFailures()) {
        logger.info("Migration Complete.");
        logger.warn("Migration finished. The following problem(s) were encountered and ignored.");
        for (ValidationFailure failure : failures.getFailures()) {
            logger.warn(failure.toString());
        }
    } else {
        logger.info("Migration Complete Successfully.");
    }
    flattenManyToManyRelationships(targetDataMap, loadedObjEntities, nameGenerator);
    relationshipsSanity(targetDataMap);
    return true;
}
Also used : ObjectNameGenerator(org.apache.cayenne.dbsync.naming.ObjectNameGenerator) ModelMergeDelegate(org.apache.cayenne.dbsync.reverse.dbload.ModelMergeDelegate) ProxyModelMergeDelegate(org.apache.cayenne.dbsync.reverse.dbload.ProxyModelMergeDelegate) ValidationResult(org.apache.cayenne.validation.ValidationResult) MergerContext(org.apache.cayenne.dbsync.merge.context.MergerContext) LinkedList(java.util.LinkedList) SimpleValidationFailure(org.apache.cayenne.validation.SimpleValidationFailure) ValidationFailure(org.apache.cayenne.validation.ValidationFailure) ObjEntity(org.apache.cayenne.map.ObjEntity) SimpleValidationFailure(org.apache.cayenne.validation.SimpleValidationFailure) MergerToken(org.apache.cayenne.dbsync.merge.token.MergerToken) ProxyModelMergeDelegate(org.apache.cayenne.dbsync.reverse.dbload.ProxyModelMergeDelegate)

Example 10 with ValidationFailure

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

the class CodeGeneratorControllerBase method getProblem.

/**
 * Returns the first encountered validation problem for an antity matching the name or
 * null if the entity is valid or the entity is not present.
 */
public String getProblem(Object obj) {
    String name = null;
    if (obj instanceof ObjEntity) {
        name = ((ObjEntity) obj).getName();
    } else if (obj instanceof Embeddable) {
        name = ((Embeddable) obj).getClassName();
    }
    if (validation == null) {
        return null;
    }
    List failures = validation.getFailures(name);
    if (failures.isEmpty()) {
        return null;
    }
    return ((ValidationFailure) failures.get(0)).getDescription();
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ArrayList(java.util.ArrayList) List(java.util.List) Embeddable(org.apache.cayenne.map.Embeddable) ValidationFailure(org.apache.cayenne.validation.ValidationFailure)

Aggregations

ValidationFailure (org.apache.cayenne.validation.ValidationFailure)17 BeanValidationFailure (org.apache.cayenne.validation.BeanValidationFailure)11 SimpleValidationFailure (org.apache.cayenne.validation.SimpleValidationFailure)7 ValidationResult (org.apache.cayenne.validation.ValidationResult)6 ObjEntity (org.apache.cayenne.map.ObjEntity)4 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 DbAttribute (org.apache.cayenne.map.DbAttribute)2 DbEntity (org.apache.cayenne.map.DbEntity)2 DbRelationship (org.apache.cayenne.map.DbRelationship)2 ProjectValidator (org.apache.cayenne.project.validation.ProjectValidator)2 Artist (org.apache.cayenne.testdo.testmap.Artist)2 File (java.io.File)1 Date (java.util.Date)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 MergerContext (org.apache.cayenne.dbsync.merge.context.MergerContext)1 MergerToken (org.apache.cayenne.dbsync.merge.token.MergerToken)1 ObjectNameGenerator (org.apache.cayenne.dbsync.naming.ObjectNameGenerator)1 ModelMergeDelegate (org.apache.cayenne.dbsync.reverse.dbload.ModelMergeDelegate)1