Search in sources :

Example 16 with ConstraintViolationException

use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.

the class AddNodeTest method testMixinNodeType.

/**
     * Tests if addNode() throws a ConstraintViolationException in case
     * of an mixin node type.
     */
public void testMixinNodeType() throws RepositoryException, NotExecutableException {
    NodeTypeManager ntMgr = superuser.getWorkspace().getNodeTypeManager();
    NodeTypeIterator nts = ntMgr.getMixinNodeTypes();
    if (nts.hasNext()) {
        try {
            testRootNode.addNode(nodeName1, nts.nextNodeType().getName());
            superuser.save();
            fail("Expected ConstraintViolationException.");
        } catch (ConstraintViolationException e) {
        // correct.
        }
    } else {
        throw new NotExecutableException("no mixins.");
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException)

Example 17 with ConstraintViolationException

use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.

the class NodeTypeCreationTest method testInvalidJCRNames.

public void testInvalidJCRNames() throws Exception {
    String invalidName = ":ab[2]";
    // invalid name(s) passed to NT-template methods
    NodeTypeTemplate ntt = ntm.createNodeTypeTemplate();
    try {
        ntt.setName(invalidName);
        fail("ConstraintViolationException expected. Nt-name is invalid");
    } catch (ConstraintViolationException e) {
    // success
    }
    try {
        ntt.setDeclaredSuperTypeNames(new String[] { "{" + NS_MIX_URI + "}" + "littlemixin", invalidName });
        fail("ConstraintViolationException expected. One of the super type names is invalid");
    } catch (ConstraintViolationException e) {
    // success
    }
    try {
        ntt.setPrimaryItemName(invalidName);
        fail("ConstraintViolationException expected. Primary item name is invalid");
    } catch (ConstraintViolationException e) {
    // success
    }
    // invalid name(s) passed to NodeDefinitionTemplate
    NodeDefinitionTemplate ndt = ntm.createNodeDefinitionTemplate();
    try {
        ndt.setName(invalidName);
        fail("ConstraintViolationException expected. Name is invalid");
    } catch (ConstraintViolationException e) {
    // success
    }
    try {
        ndt.setRequiredPrimaryTypeNames(new String[] { "{" + NS_MIX_URI + "}" + "littlemixin", invalidName });
        fail("ConstraintViolationException expected. One of the required primary type names is invalid");
    } catch (ConstraintViolationException e) {
    // success
    }
    try {
        ndt.setDefaultPrimaryTypeName(invalidName);
        fail("ConstraintViolationException expected. Default primary type name is invalid");
    } catch (ConstraintViolationException e) {
    // success
    }
    // invalid name(s) passed to PropertyDefinitionTemplate
    PropertyDefinitionTemplate pdt = ntm.createPropertyDefinitionTemplate();
    try {
        pdt.setName(invalidName);
        fail("ConstraintViolationException expected. Name is invalid");
    } catch (ConstraintViolationException e) {
    // success
    }
}
Also used : NodeDefinitionTemplate(javax.jcr.nodetype.NodeDefinitionTemplate) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) PropertyDefinitionTemplate(javax.jcr.nodetype.PropertyDefinitionTemplate) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException)

Example 18 with ConstraintViolationException

use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.

the class NodeTypeCreationTest method testPropertyDefinitionTemplate.

public void testPropertyDefinitionTemplate() throws Exception {
    PropertyDefinitionTemplate pdt = createBooleanPropTemplate();
    assertEquals(jcrPropName, pdt.getName());
    try {
        pdt.setName(null);
        fail("null isn't a valid JCR name");
    } catch (ConstraintViolationException e) {
    // success
    }
    assertEquals(false, pdt.isAutoCreated());
    assertEquals(false, pdt.isMandatory());
    assertEquals(OnParentVersionAction.IGNORE, pdt.getOnParentVersion());
    assertEquals(false, pdt.isProtected());
    assertEquals(PropertyType.BOOLEAN, pdt.getRequiredType());
    assertEquals(null, pdt.getValueConstraints());
    assertEquals(null, pdt.getDefaultValues());
    assertEquals(false, pdt.isMultiple());
    String[] qo = pdt.getAvailableQueryOperators();
    assertEquals(1, qo.length);
    assertEquals(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, qo[0]);
    assertEquals(false, pdt.isFullTextSearchable());
    assertEquals(false, pdt.isQueryOrderable());
}
Also used : PropertyDefinitionTemplate(javax.jcr.nodetype.PropertyDefinitionTemplate) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException)

Example 19 with ConstraintViolationException

use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.

the class NodeTypeRegistryImpl method getEffectiveNodeType.

/**
     * @see EffectiveNodeTypeProvider#getEffectiveNodeType(QNodeTypeDefinition, Map)
     */
public EffectiveNodeType getEffectiveNodeType(QNodeTypeDefinition ntd, Map<Name, QNodeTypeDefinition> ntdMap) throws ConstraintViolationException, NoSuchNodeTypeException {
    TreeSet<Name> mergedNodeTypes = new TreeSet<Name>();
    TreeSet<Name> inheritedNodeTypes = new TreeSet<Name>();
    TreeSet<Name> allNodeTypes = new TreeSet<Name>();
    Map<Name, List<QItemDefinition>> namedItemDefs = new HashMap<Name, List<QItemDefinition>>();
    List<QItemDefinition> unnamedItemDefs = new ArrayList<QItemDefinition>();
    Set<Name> supportedMixins = null;
    Name ntName = ntd.getName();
    // prepare new instance
    mergedNodeTypes.add(ntName);
    allNodeTypes.add(ntName);
    Name[] smixins = ntd.getSupportedMixinTypes();
    if (smixins != null) {
        supportedMixins = new HashSet<Name>();
        supportedMixins.addAll(Arrays.asList(smixins));
    }
    // map of all item definitions (maps id to definition)
    // used to effectively detect ambiguous child definitions where
    // ambiguity is defined in terms of definition identity
    Set<QItemDefinition> itemDefIds = new HashSet<QItemDefinition>();
    for (QNodeDefinition nd : ntd.getChildNodeDefs()) {
        // this node type definition
        if (itemDefIds.contains(nd)) {
            // conflict
            String msg;
            if (nd.definesResidual()) {
                msg = ntName + " contains ambiguous residual child node definitions";
            } else {
                msg = ntName + " contains ambiguous definitions for child node named " + nd.getName();
            }
            log.debug(msg);
            throw new ConstraintViolationException(msg);
        } else {
            itemDefIds.add(nd);
        }
        if (nd.definesResidual()) {
            // residual node definition
            unnamedItemDefs.add(nd);
        } else {
            // named node definition
            Name name = nd.getName();
            List<QItemDefinition> defs = namedItemDefs.get(name);
            if (defs == null) {
                defs = new ArrayList<QItemDefinition>();
                namedItemDefs.put(name, defs);
            }
            if (defs.size() > 0) {
                /**
                     * there already exists at least one definition with that
                     * name; make sure none of them is auto-create
                     */
                for (QItemDefinition qDef : defs) {
                    if (nd.isAutoCreated() || qDef.isAutoCreated()) {
                        // conflict
                        String msg = "There are more than one 'auto-create' item definitions for '" + name + "' in node type '" + ntName + "'";
                        log.debug(msg);
                        throw new ConstraintViolationException(msg);
                    }
                }
            }
            defs.add(nd);
        }
    }
    for (QPropertyDefinition pd : ntd.getPropertyDefs()) {
        // this node type definition
        if (itemDefIds.contains(pd)) {
            // conflict
            String msg;
            if (pd.definesResidual()) {
                msg = ntName + " contains ambiguous residual property definitions";
            } else {
                msg = ntName + " contains ambiguous definitions for property named " + pd.getName();
            }
            log.debug(msg);
            throw new ConstraintViolationException(msg);
        } else {
            itemDefIds.add(pd);
        }
        if (pd.definesResidual()) {
            // residual property definition
            unnamedItemDefs.add(pd);
        } else {
            // named property definition
            Name name = pd.getName();
            List<QItemDefinition> defs = namedItemDefs.get(name);
            if (defs == null) {
                defs = new ArrayList<QItemDefinition>();
                namedItemDefs.put(name, defs);
            }
            if (defs.size() > 0) {
                /**
                     * there already exists at least one definition with that
                     * name; make sure none of them is auto-create
                     */
                for (QItemDefinition qDef : defs) {
                    if (pd.isAutoCreated() || qDef.isAutoCreated()) {
                        // conflict
                        String msg = "There are more than one 'auto-create' item definitions for '" + name + "' in node type '" + ntName + "'";
                        log.debug(msg);
                        throw new ConstraintViolationException(msg);
                    }
                }
            }
            defs.add(pd);
        }
    }
    // create empty effective node type instance
    EffectiveNodeTypeImpl ent = new EffectiveNodeTypeImpl(mergedNodeTypes, inheritedNodeTypes, allNodeTypes, namedItemDefs, unnamedItemDefs, supportedMixins);
    // resolve supertypes recursively
    Name[] supertypes = ntd.getSupertypes();
    if (supertypes.length > 0) {
        EffectiveNodeTypeImpl effSuperType = (EffectiveNodeTypeImpl) getEffectiveNodeType(supertypes, ntdMap);
        ent.internalMerge(effSuperType, true);
    }
    return ent;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) QItemDefinition(org.apache.jackrabbit.spi.QItemDefinition) QNodeDefinition(org.apache.jackrabbit.spi.QNodeDefinition) Name(org.apache.jackrabbit.spi.Name) TreeSet(java.util.TreeSet) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 20 with ConstraintViolationException

use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.

the class ItemDefinitionProviderImpl method getMatchingPropDef.

private static QPropertyDefinition getMatchingPropDef(QPropertyDefinition[] defs, int type, boolean multiValued, boolean throwWhenAmbiguous) throws ConstraintViolationException {
    QPropertyDefinition match = null;
    for (int i = 0; i < defs.length; i++) {
        QItemDefinition qDef = defs[i];
        if (!qDef.definesNode()) {
            QPropertyDefinition pd = (QPropertyDefinition) qDef;
            int reqType = pd.getRequiredType();
            // match type
            if (reqType == PropertyType.UNDEFINED || type == PropertyType.UNDEFINED || reqType == type) {
                // match multiValued flag
                if (multiValued == pd.isMultiple()) {
                    // found match
                    if (pd.getRequiredType() != PropertyType.UNDEFINED) {
                        if (match != null && throwWhenAmbiguous) {
                            throw new ConstraintViolationException("ambiguous property definitions found: " + match + " vs " + pd);
                        }
                        // thus obviously specified a String.
                        if (match == null || match.getRequiredType() != PropertyType.STRING) {
                            // found best possible match
                            match = pd;
                        }
                    } else {
                        if (match == null) {
                            match = pd;
                        }
                    }
                }
            }
        }
    }
    return match;
}
Also used : QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) QItemDefinition(org.apache.jackrabbit.spi.QItemDefinition)

Aggregations

ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)177 Node (javax.jcr.Node)73 RepositoryException (javax.jcr.RepositoryException)39 Name (org.apache.jackrabbit.spi.Name)32 Value (javax.jcr.Value)30 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)28 Test (org.junit.Test)26 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)22 QPropertyDefinition (org.apache.jackrabbit.spi.QPropertyDefinition)22 Session (javax.jcr.Session)17 ItemExistsException (javax.jcr.ItemExistsException)16 NodeState (org.apache.jackrabbit.core.state.NodeState)16 QNodeDefinition (org.apache.jackrabbit.spi.QNodeDefinition)16 Property (javax.jcr.Property)14 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)14 ArrayList (java.util.ArrayList)13 EffectiveNodeType (org.apache.jackrabbit.core.nodetype.EffectiveNodeType)13 Authorizable (org.apache.jackrabbit.api.security.user.Authorizable)12 NodeId (org.apache.jackrabbit.core.id.NodeId)12 Path (org.apache.jackrabbit.spi.Path)12