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