Search in sources :

Example 1 with ValidationResult

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

the class DbGenerator method safeExecute.

/**
 * Builds and executes a SQL statement, catching and storing SQL exceptions
 * resulting from invalid SQL. Only non-recoverable exceptions are rethrown.
 *
 * @since 1.1
 */
protected boolean safeExecute(Connection connection, String sql) throws SQLException {
    try (Statement statement = connection.createStatement()) {
        jdbcEventLogger.log(sql);
        statement.execute(sql);
        return true;
    } catch (SQLException ex) {
        if (this.failures == null) {
            this.failures = new ValidationResult();
        }
        failures.addFailure(new SimpleValidationFailure(sql, ex.getMessage()));
        jdbcEventLogger.logQueryError(ex);
        return false;
    }
}
Also used : SQLException(java.sql.SQLException) SimpleValidationFailure(org.apache.cayenne.validation.SimpleValidationFailure) Statement(java.sql.Statement) ValidationResult(org.apache.cayenne.validation.ValidationResult)

Example 2 with ValidationResult

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

the class ValidateAction method performAction.

/**
 * Validates project for possible conflicts and incomplete mappings.
 */
public void performAction(ActionEvent e) {
    ProjectValidator projectValidator = getApplication().getInjector().getInstance(ProjectValidator.class);
    ValidationResult validationResult = projectValidator.validate(getCurrentProject().getRootNode());
    // If there were errors or warnings at validation, display them
    if (validationResult.getFailures().size() > 0) {
        ValidatorDialog.showDialog(Application.getFrame(), validationResult.getFailures());
    } else {
        ValidatorDialog.showValidationSuccess(Application.getFrame());
    }
}
Also used : ProjectValidator(org.apache.cayenne.project.validation.ProjectValidator) ValidationResult(org.apache.cayenne.validation.ValidationResult)

Example 3 with ValidationResult

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

the class ValidationResultBrowser method startupAction.

public void startupAction(String title, String message, Collection<ValidationResult> failures) {
    this.view.setTitle(title);
    this.view.getMessageLabel().setText(message);
    for (ValidationResult failure : failures) {
        if (failure != null) {
            this.view.getErrorsDisplay().append(buildValidationText(failure) + " ");
        }
    }
    view.pack();
    view.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    view.setModal(true);
    makeCloseableOnEscape();
    centerView();
    view.setVisible(true);
}
Also used : ValidationResult(org.apache.cayenne.validation.ValidationResult)

Example 4 with ValidationResult

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

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

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