@Ignore
 @Test(expected = IllegalArgumentException.class)
 public void shouldNotAllowPropertyWithInvalidName() throws Exception {
   JcrPropertyDefinitionTemplate prop = new JcrPropertyDefinitionTemplate(this.context);
   prop.setName(":foo[2]");
   System.out.println(prop.getName());
 }
  @Test(expected = InvalidNodeTypeDefinitionException.class)
  public void shouldNotAllowOverridingProtectedProperty() throws Exception {
    ntTemplate.setName(TEST_TYPE_NAME);
    ntTemplate.setDeclaredSuperTypeNames(new String[] {"nt:base", "mix:referenceable"});

    JcrPropertyDefinitionTemplate prop = new JcrPropertyDefinitionTemplate(this.context);
    prop.setName(JcrLexicon.PRIMARY_TYPE.getString(registry));
    prop.setRequiredType(PropertyType.NAME);
    ntTemplate.getPropertyDefinitionTemplates().add(prop);

    List<NodeTypeDefinition> templates = Arrays.asList(new NodeTypeDefinition[] {ntTemplate});
    compareTemplatesToNodeTypes(templates, repoTypeManager.registerNodeTypes(templates));
  }
  @Test
  public void shouldAllowDefinitionWithAProperty() throws Exception {
    ntTemplate.setName(TEST_TYPE_NAME);
    ntTemplate.setDeclaredSuperTypeNames(new String[] {"nt:base", "mix:referenceable"});

    JcrPropertyDefinitionTemplate prop = new JcrPropertyDefinitionTemplate(this.context);
    prop.setRequiredType(PropertyType.LONG);

    ntTemplate.getPropertyDefinitionTemplates().add(prop);

    List<NodeTypeDefinition> templates = Arrays.asList(new NodeTypeDefinition[] {ntTemplate});
    compareTemplatesToNodeTypes(templates, repoTypeManager.registerNodeTypes(templates));
  }
  @Test(expected = InvalidNodeTypeDefinitionException.class)
  public void shouldNotAllowMandatoryResidualProperty() throws Exception {
    ntTemplate.setName(TEST_TYPE_NAME);
    ntTemplate.setDeclaredSuperTypeNames(new String[] {"nt:base", "mix:referenceable"});

    JcrPropertyDefinitionTemplate prop = new JcrPropertyDefinitionTemplate(this.context);
    prop.setName(JcrNodeType.RESIDUAL_ITEM_NAME);
    prop.setRequiredType(PropertyType.UNDEFINED);
    prop.setMandatory(true);
    ntTemplate.getPropertyDefinitionTemplates().add(prop);

    List<NodeTypeDefinition> templates = Arrays.asList(new NodeTypeDefinition[] {ntTemplate});
    compareTemplatesToNodeTypes(templates, repoTypeManager.registerNodeTypes(templates));
  }
  @Test(expected = InvalidNodeTypeDefinitionException.class)
  public void shouldNotAllowAutocreatedPropertyWithNoDefault() throws Exception {
    ntTemplate.setName(TEST_TYPE_NAME);
    ntTemplate.setDeclaredSuperTypeNames(new String[] {"nt:base", "mix:referenceable"});

    JcrPropertyDefinitionTemplate prop = new JcrPropertyDefinitionTemplate(this.context);
    prop.setName(TEST_PROPERTY_NAME);
    prop.setRequiredType(PropertyType.LONG);
    prop.setAutoCreated(true);
    ntTemplate.getPropertyDefinitionTemplates().add(prop);

    List<NodeTypeDefinition> templates = Arrays.asList(new NodeTypeDefinition[] {ntTemplate});
    compareTemplatesToNodeTypes(templates, repoTypeManager.registerNodeTypes(templates));
  }
  @Test(expected = InvalidNodeTypeDefinitionException.class)
  public void shouldNotAllowOverridingPropertyIfTypeDoesNotNarrow() throws Exception {
    /*
     * testNode declares SV property testProperty of type BOOLEAN
     * testNodeB extends testNode with SV property testProperty of type DOUBLE -> ILLEGAL
     */
    ntTemplate.setName(TEST_TYPE_NAME);
    ntTemplate.setDeclaredSuperTypeNames(
        new String[] {
          "nt:base",
        });

    JcrPropertyDefinitionTemplate prop = new JcrPropertyDefinitionTemplate(this.context);
    prop.setName(TEST_PROPERTY_NAME);
    prop.setRequiredType(PropertyType.BOOLEAN);
    prop.setMultiple(false);
    ntTemplate.getPropertyDefinitionTemplates().add(prop);

    JcrNodeTypeTemplate nodeBTemplate = new JcrNodeTypeTemplate(this.context);
    nodeBTemplate.setName(TEST_TYPE_NAME + "B");
    nodeBTemplate.setDeclaredSuperTypeNames(new String[] {TEST_TYPE_NAME});

    prop = new JcrPropertyDefinitionTemplate(this.context);
    prop.setName(TEST_PROPERTY_NAME);
    prop.setRequiredType(PropertyType.DOUBLE);
    prop.setMultiple(false);
    nodeBTemplate.getPropertyDefinitionTemplates().add(prop);

    List<NodeTypeDefinition> templates =
        Arrays.asList(new NodeTypeDefinition[] {ntTemplate, nodeBTemplate});
    compareTemplatesToNodeTypes(templates, repoTypeManager.registerNodeTypes(templates));
  }
  @Test
  public void shouldAllowOverridingPropertyIfTypeNarrows() throws Exception {
    /*
     * testNode declares SV property testProperty of type UNDEFINED
     * testNodeB extends testNode with SV property testProperty of type STRING -> LEGAL
     */
    ntTemplate.setName(TEST_TYPE_NAME);
    ntTemplate.setDeclaredSuperTypeNames(
        new String[] {
          "nt:base",
        });

    JcrPropertyDefinitionTemplate prop = new JcrPropertyDefinitionTemplate(this.context);
    prop.setName(TEST_PROPERTY_NAME);
    prop.setRequiredType(PropertyType.UNDEFINED);
    prop.setMultiple(false);
    ntTemplate.getPropertyDefinitionTemplates().add(prop);

    JcrNodeTypeTemplate nodeBTemplate = new JcrNodeTypeTemplate(this.context);
    nodeBTemplate.setName(TEST_TYPE_NAME + "B");
    nodeBTemplate.setDeclaredSuperTypeNames(new String[] {TEST_TYPE_NAME});

    prop = new JcrPropertyDefinitionTemplate(this.context);
    prop.setName(TEST_PROPERTY_NAME);
    prop.setRequiredType(PropertyType.STRING);
    prop.setMultiple(false);
    nodeBTemplate.getPropertyDefinitionTemplates().add(prop);

    List<NodeTypeDefinition> templates =
        Arrays.asList(new NodeTypeDefinition[] {ntTemplate, nodeBTemplate});
    compareTemplatesToNodeTypes(templates, repoTypeManager.registerNodeTypes(templates));
  }
  @FixFor("MODE-826")
  @Test
  public void
      shouldAllowRegisteringNodeTypeWithPrimaryItemNameAndOnlyNonResidualPropertyNodeDefinition()
          throws Exception {
    ntTemplate.setName(TEST_TYPE_NAME);
    // Set the primary item name to be a name that DOES match the property definition ...
    ntTemplate.setPrimaryItemName(TEST_PROPERTY_NAME);

    // Create the residual property definition ...
    JcrPropertyDefinitionTemplate propertyDefn = new JcrPropertyDefinitionTemplate(this.context);
    propertyDefn.setRequiredType(PropertyType.STRING);
    propertyDefn.setName(TEST_PROPERTY_NAME);
    ntTemplate.getPropertyDefinitionTemplates().add(propertyDefn);

    // And register it ...
    repoTypeManager.registerNodeTypes(Arrays.asList(new NodeTypeDefinition[] {ntTemplate}));
  }
  @Test(expected = InvalidNodeTypeDefinitionException.class)
  public void shouldNotAllowOverridingPropertyFromDifferentAncestors() throws Exception {
    /*
     * testNode
     * testNodeB extends testNode and declares prop testProperty
     * testNodeC extends testNode and declares prop testProperty
     * testNodeD extends testNodeB and testNodeC and overrides testProperty --> ILLEGAL
     */
    ntTemplate.setName(TEST_TYPE_NAME);
    ntTemplate.setDeclaredSuperTypeNames(
        new String[] {
          "nt:base",
        });

    JcrNodeTypeTemplate nodeBTemplate = new JcrNodeTypeTemplate(this.context);
    nodeBTemplate.setName(TEST_TYPE_NAME + "B");
    nodeBTemplate.setDeclaredSuperTypeNames(new String[] {TEST_TYPE_NAME});

    JcrPropertyDefinitionTemplate prop = new JcrPropertyDefinitionTemplate(this.context);
    prop.setName(TEST_PROPERTY_NAME);
    prop.setRequiredType(PropertyType.UNDEFINED);
    nodeBTemplate.getPropertyDefinitionTemplates().add(prop);

    JcrNodeTypeTemplate nodeCTemplate = new JcrNodeTypeTemplate(this.context);
    nodeCTemplate.setName(TEST_TYPE_NAME + "C");
    nodeCTemplate.setDeclaredSuperTypeNames(new String[] {TEST_TYPE_NAME});

    prop = new JcrPropertyDefinitionTemplate(this.context);
    prop.setName(TEST_PROPERTY_NAME);
    prop.setRequiredType(PropertyType.UNDEFINED);
    nodeCTemplate.getPropertyDefinitionTemplates().add(prop);

    JcrNodeTypeTemplate nodeDTemplate = new JcrNodeTypeTemplate(this.context);
    nodeDTemplate.setName(TEST_TYPE_NAME + "D");
    nodeDTemplate.setDeclaredSuperTypeNames(
        new String[] {nodeBTemplate.getName(), nodeCTemplate.getName()});

    List<NodeTypeDefinition> templates =
        Arrays.asList(
            new NodeTypeDefinition[] {ntTemplate, nodeBTemplate, nodeCTemplate, nodeDTemplate});
    compareTemplatesToNodeTypes(templates, repoTypeManager.registerNodeTypes(templates));
  }
  @Test
  public void shouldAllowExtendingPropertyIfMultipleChanges() throws Exception {
    /*
     * testNode declares SV property testProperty
     * testNodeB extends testNode with MV property testProperty with incompatible type -> LEGAL
     * testNodeC extends testNode, testNodeB -> LEGAL
     */
    ntTemplate.setName(TEST_TYPE_NAME);
    ntTemplate.setDeclaredSuperTypeNames(
        new String[] {
          "nt:base",
        });

    JcrPropertyDefinitionTemplate prop = new JcrPropertyDefinitionTemplate(this.context);
    prop.setName(TEST_PROPERTY_NAME);
    prop.setRequiredType(PropertyType.DOUBLE);
    prop.setMultiple(false);
    ntTemplate.getPropertyDefinitionTemplates().add(prop);

    JcrNodeTypeTemplate nodeBTemplate = new JcrNodeTypeTemplate(this.context);
    nodeBTemplate.setName(TEST_TYPE_NAME + "B");
    nodeBTemplate.setDeclaredSuperTypeNames(new String[] {TEST_TYPE_NAME});

    prop = new JcrPropertyDefinitionTemplate(this.context);
    prop.setName(TEST_PROPERTY_NAME);
    prop.setRequiredType(PropertyType.BOOLEAN);
    prop.setMultiple(true);
    nodeBTemplate.getPropertyDefinitionTemplates().add(prop);

    JcrNodeTypeTemplate nodeCTemplate = new JcrNodeTypeTemplate(this.context);
    nodeCTemplate.setName(TEST_TYPE_NAME + "C");
    nodeCTemplate.setDeclaredSuperTypeNames(new String[] {TEST_TYPE_NAME, nodeBTemplate.getName()});

    List<NodeTypeDefinition> templates =
        Arrays.asList(new NodeTypeDefinition[] {ntTemplate, nodeBTemplate, nodeCTemplate});
    compareTemplatesToNodeTypes(templates, repoTypeManager.registerNodeTypes(templates));
  }
  /**
   * Returns a {@code NodeTypeTemplate} based on the definition given in {@code ntd}. This template
   * can then be used to define a node type and passed to {@link
   * JcrNodeTypeManager#registerNodeType(NodeTypeDefinition, boolean)}
   *
   * @param ntd an existing node type definition; null values will be ignored
   * @return an empty {@code NodeTypeTemplate} which can then be used to define a node type and
   *     passed to {@link JcrNodeTypeManager#registerNodeType(NodeTypeDefinition, boolean)}.
   * @throws RepositoryException if another error occurs
   */
  @Override
  @SuppressWarnings("unchecked")
  public NodeTypeTemplate createNodeTypeTemplate(NodeTypeDefinition ntd)
      throws RepositoryException {
    NodeTypeTemplate ntt = new JcrNodeTypeTemplate(context(), true);

    if (ntd != null) {
      ntt.setName(ntd.getName());
      ntt.setAbstract(ntd.isAbstract());
      ntt.setDeclaredSuperTypeNames(ntd.getDeclaredSupertypeNames());
      ntt.setMixin(ntd.isMixin());
      ntt.setOrderableChildNodes(ntd.hasOrderableChildNodes());
      ntt.setPrimaryItemName(ntd.getPrimaryItemName());
      ntt.setQueryable(ntd.isQueryable());

      // copy child nodes and props
      for (NodeDefinition nodeDefinition : ntd.getDeclaredChildNodeDefinitions()) {
        JcrNodeDefinitionTemplate ndt = new JcrNodeDefinitionTemplate(context());

        ndt.setAutoCreated(nodeDefinition.isAutoCreated());
        ndt.setDefaultPrimaryTypeName(nodeDefinition.getDefaultPrimaryTypeName());
        ndt.setMandatory(nodeDefinition.isMandatory());
        if (nodeDefinition.getName() != null) {
          ndt.setName(nodeDefinition.getName());
        }
        ndt.setOnParentVersion(nodeDefinition.getOnParentVersion());
        ndt.setProtected(nodeDefinition.isProtected());
        ndt.setRequiredPrimaryTypeNames(nodeDefinition.getRequiredPrimaryTypeNames());
        ndt.setSameNameSiblings(nodeDefinition.allowsSameNameSiblings());

        ntt.getNodeDefinitionTemplates().add(ndt);
      }

      for (PropertyDefinition propertyDefinition : ntd.getDeclaredPropertyDefinitions()) {
        JcrPropertyDefinitionTemplate pdt = new JcrPropertyDefinitionTemplate(context());

        pdt.setAutoCreated(propertyDefinition.isAutoCreated());
        pdt.setAvailableQueryOperators(propertyDefinition.getAvailableQueryOperators());
        pdt.setDefaultValues(propertyDefinition.getDefaultValues());
        pdt.setFullTextSearchable(propertyDefinition.isFullTextSearchable());
        pdt.setMandatory(propertyDefinition.isMandatory());
        pdt.setMultiple(propertyDefinition.isMultiple());
        if (propertyDefinition.getName() != null) {
          pdt.setName(propertyDefinition.getName());
        }
        pdt.setOnParentVersion(propertyDefinition.getOnParentVersion());
        pdt.setProtected(propertyDefinition.isProtected());
        pdt.setQueryOrderable(propertyDefinition.isQueryOrderable());
        pdt.setRequiredType(propertyDefinition.getRequiredType());
        pdt.setValueConstraints(propertyDefinition.getValueConstraints());

        ntt.getPropertyDefinitionTemplates().add(pdt);
      }
    }

    return ntt;
  }
  @SuppressWarnings("unchecked")
  protected NodeTypeDefinition[] getTestTypes()
      throws ConstraintViolationException, RepositoryException {
    ExecutionContext context = session.context();
    session
        .getWorkspace()
        .getNamespaceRegistry()
        .registerNamespace("modetest", "http://www.modeshape.org/test/1.0");
    NodeTypeTemplate nodeA = new JcrNodeTypeTemplate(context);
    nodeA.setName("modetest:nodeA");

    JcrPropertyDefinitionTemplate nodeASingleProp1 = new JcrPropertyDefinitionTemplate(context);
    nodeASingleProp1.setName("modetest:singleProp1");
    nodeASingleProp1.setRequiredType(PropertyType.STRING);
    nodeA.getPropertyDefinitionTemplates().add(nodeASingleProp1);

    NodeTypeTemplate nodeB = new JcrNodeTypeTemplate(context);
    nodeB.setName("modetest:nodeB");

    JcrPropertyDefinitionTemplate nodeBSingleProp1 = new JcrPropertyDefinitionTemplate(context);
    nodeBSingleProp1.setName("modetest:singleProp1");
    nodeBSingleProp1.setRequiredType(PropertyType.DOUBLE);
    nodeB.getPropertyDefinitionTemplates().add(nodeBSingleProp1);

    JcrPropertyDefinitionTemplate nodeBSingleProp2 = new JcrPropertyDefinitionTemplate(context);
    nodeBSingleProp2.setName("modetest:singleProp2");
    nodeBSingleProp2.setRequiredType(PropertyType.UNDEFINED);
    nodeB.getPropertyDefinitionTemplates().add(nodeBSingleProp2);

    NodeTypeTemplate nodeC = new JcrNodeTypeTemplate(context);
    nodeC.setName("modetest:nodeC");
    nodeC.setDeclaredSuperTypeNames(new String[] {"modetest:nodeB"});

    JcrPropertyDefinitionTemplate nodeCSingleProp1 = new JcrPropertyDefinitionTemplate(context);
    nodeCSingleProp1.setName("modetest:singleProp1");
    nodeCSingleProp1.setRequiredType(PropertyType.LONG);
    nodeC.getPropertyDefinitionTemplates().add(nodeCSingleProp1);

    JcrPropertyDefinitionTemplate nodeCSingleProp2Double =
        new JcrPropertyDefinitionTemplate(context);
    nodeCSingleProp2Double.setName("modetest:singleProp2");
    nodeCSingleProp2Double.setRequiredType(PropertyType.DOUBLE);
    nodeC.getPropertyDefinitionTemplates().add(nodeCSingleProp2Double);

    JcrPropertyDefinitionTemplate nodeCSingleProp2Long = new JcrPropertyDefinitionTemplate(context);
    nodeCSingleProp2Long.setName("modetest:singleProp2");
    nodeCSingleProp2Long.setRequiredType(PropertyType.LONG);
    nodeC.getPropertyDefinitionTemplates().add(nodeCSingleProp2Long);

    return new NodeTypeDefinition[] {nodeA, nodeB, nodeC};
  }