Search in sources :

Example 1 with RegexConstraint

use of org.alfresco.repo.dictionary.constraint.RegexConstraint in project alfresco-repository by Alfresco.

the class RepoDictionaryDAOTest method testConstraintsOverrideInheritance.

public void testConstraintsOverrideInheritance() {
    QName baseQName = QName.createQName(TEST_URL, "base");
    QName fileQName = QName.createQName(TEST_URL, "file");
    QName folderQName = QName.createQName(TEST_URL, "folder");
    QName prop1QName = QName.createQName(TEST_URL, "prop1");
    // get the base property
    PropertyDefinition prop1Def = service.getProperty(baseQName, prop1QName);
    assertNotNull(prop1Def);
    List<ConstraintDefinition> prop1Constraints = prop1Def.getConstraints();
    assertEquals("Incorrect number of constraints", 3, prop1Constraints.size());
    assertTrue("Constraint instance incorrect", prop1Constraints.get(0).getConstraint() instanceof RegexConstraint);
    assertTrue("Constraint instance incorrect", prop1Constraints.get(1).getConstraint() instanceof StringLengthConstraint);
    assertTrue("Constraint instance incorrect", prop1Constraints.get(2).getConstraint() instanceof RegisteredConstraint);
    // check the inherited property on folder (must be same as above)
    prop1Def = service.getProperty(folderQName, prop1QName);
    assertNotNull(prop1Def);
    prop1Constraints = prop1Def.getConstraints();
    assertEquals("Incorrect number of constraints", 3, prop1Constraints.size());
    assertTrue("Constraint instance incorrect", prop1Constraints.get(0).getConstraint() instanceof RegexConstraint);
    assertTrue("Constraint instance incorrect", prop1Constraints.get(1).getConstraint() instanceof StringLengthConstraint);
    assertTrue("Constraint instance incorrect", prop1Constraints.get(2).getConstraint() instanceof RegisteredConstraint);
    // check the overridden property on file (must be reverse of above)
    prop1Def = service.getProperty(fileQName, prop1QName);
    assertNotNull(prop1Def);
    prop1Constraints = prop1Def.getConstraints();
    assertEquals("Incorrect number of constraints", 3, prop1Constraints.size());
    assertTrue("Constraint instance incorrect", prop1Constraints.get(0).getConstraint() instanceof StringLengthConstraint);
    assertTrue("Constraint instance incorrect", prop1Constraints.get(1).getConstraint() instanceof RegexConstraint);
    assertTrue("Constraint instance incorrect", prop1Constraints.get(2).getConstraint() instanceof RegisteredConstraint);
}
Also used : RegisteredConstraint(org.alfresco.repo.dictionary.constraint.RegisteredConstraint) QName(org.alfresco.service.namespace.QName) RegexConstraint(org.alfresco.repo.dictionary.constraint.RegexConstraint) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition) StringLengthConstraint(org.alfresco.repo.dictionary.constraint.StringLengthConstraint) ConstraintDefinition(org.alfresco.service.cmr.dictionary.ConstraintDefinition)

Example 2 with RegexConstraint

use of org.alfresco.repo.dictionary.constraint.RegexConstraint in project alfresco-repository by Alfresco.

the class RepoDictionaryDAOTest method testConstraints.

public void testConstraints() {
    // Check that the registered constraints are correct
    assertNotNull("Constraint reg1 not registered", ConstraintRegistry.getInstance().getConstraint("cm:reg1"));
    assertNotNull("Constraint reg2 not registered", ConstraintRegistry.getInstance().getConstraint("cm:reg2"));
    QName model = QName.createQName(TEST_URL, "dictionarydaotest");
    Collection<ConstraintDefinition> modelConstraints = service.getConstraints(model);
    // 10 + 7 + 6
    assertEquals(23, modelConstraints.size());
    QName conRegExp1QName = QName.createQName(TEST_URL, "regex1");
    boolean found1 = false;
    QName conStrLen1QName = QName.createQName(TEST_URL, "stringLength1");
    boolean found2 = false;
    for (ConstraintDefinition constraintDef : modelConstraints) {
        if (constraintDef.getName().equals(conRegExp1QName)) {
            assertEquals("Regex1 title", constraintDef.getTitle(service));
            assertEquals("Regex1 description", constraintDef.getDescription(service));
            found1 = true;
        }
        if (constraintDef.getName().equals(conStrLen1QName)) {
            assertNull(constraintDef.getTitle(service));
            assertNull(constraintDef.getDescription(service));
            found2 = true;
        }
    }
    assertTrue(found1);
    assertTrue(found2);
    // get the constraints for a property without constraints
    QName propNoConstraintsQName = QName.createQName(TEST_URL, "fileprop");
    PropertyDefinition propNoConstraintsDef = service.getProperty(propNoConstraintsQName);
    assertNotNull("Property without constraints returned null list", propNoConstraintsDef.getConstraints());
    // get the constraints defined for the property
    QName prop1QName = QName.createQName(TEST_URL, "prop1");
    PropertyDefinition propDef = service.getProperty(prop1QName);
    List<ConstraintDefinition> constraints = propDef.getConstraints();
    assertNotNull("Null constraints list", constraints);
    assertEquals("Incorrect number of constraints", 3, constraints.size());
    assertTrue("Constraint instance incorrect", constraints.get(0).getConstraint() instanceof RegexConstraint);
    assertTrue("Constraint instance incorrect", constraints.get(1).getConstraint() instanceof StringLengthConstraint);
    assertTrue("Constraint instance incorrect", constraints.get(2).getConstraint() instanceof RegisteredConstraint);
    // check the individual constraints
    ConstraintDefinition constraintDef = constraints.get(0);
    assertTrue("Constraint anonymous name incorrect", constraintDef.getName().getLocalName().equals("dictionarydaotest_base_prop1_anon_0"));
    // inherit title / description for reference constraint
    assertTrue("Constraint title incorrect", constraintDef.getTitle(service).equals("Regex1 title"));
    assertTrue("Constraint description incorrect", constraintDef.getDescription(service).equals("Regex1 description"));
    constraintDef = constraints.get(1);
    assertTrue("Constraint anonymous name incorrect", constraintDef.getName().getLocalName().equals("dictionarydaotest_base_prop1_anon_1"));
    assertTrue("Constraint title incorrect", constraintDef.getTitle(service).equals("Prop1 Strlen1 title"));
    assertTrue("Constraint description incorrect", constraintDef.getDescription(service).equals("Prop1 Strlen1 description"));
    // check that the constraint implementation is valid (it used a reference)
    Constraint constraint = constraintDef.getConstraint();
    assertNotNull("Reference constraint has no implementation", constraint);
}
Also used : RegisteredConstraint(org.alfresco.repo.dictionary.constraint.RegisteredConstraint) RegisteredConstraint(org.alfresco.repo.dictionary.constraint.RegisteredConstraint) Constraint(org.alfresco.service.cmr.dictionary.Constraint) StringLengthConstraint(org.alfresco.repo.dictionary.constraint.StringLengthConstraint) AbstractConstraint(org.alfresco.repo.dictionary.constraint.AbstractConstraint) UserNameConstraint(org.alfresco.repo.dictionary.constraint.UserNameConstraint) RegexConstraint(org.alfresco.repo.dictionary.constraint.RegexConstraint) QName(org.alfresco.service.namespace.QName) RegexConstraint(org.alfresco.repo.dictionary.constraint.RegexConstraint) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition) StringLengthConstraint(org.alfresco.repo.dictionary.constraint.StringLengthConstraint) ConstraintDefinition(org.alfresco.service.cmr.dictionary.ConstraintDefinition)

Aggregations

RegexConstraint (org.alfresco.repo.dictionary.constraint.RegexConstraint)2 RegisteredConstraint (org.alfresco.repo.dictionary.constraint.RegisteredConstraint)2 StringLengthConstraint (org.alfresco.repo.dictionary.constraint.StringLengthConstraint)2 ConstraintDefinition (org.alfresco.service.cmr.dictionary.ConstraintDefinition)2 PropertyDefinition (org.alfresco.service.cmr.dictionary.PropertyDefinition)2 QName (org.alfresco.service.namespace.QName)2 AbstractConstraint (org.alfresco.repo.dictionary.constraint.AbstractConstraint)1 UserNameConstraint (org.alfresco.repo.dictionary.constraint.UserNameConstraint)1 Constraint (org.alfresco.service.cmr.dictionary.Constraint)1