use of javax.jcr.nodetype.PropertyDefinition in project jackrabbit-oak by apache.
the class PropertyTest method testRequiredTypeLongChangeBoolean.
public void testRequiredTypeLongChangeBoolean() throws Exception {
Property p = node.setProperty(LONG_PROP_NAME, 12345);
assertEquals(PropertyType.LONG, p.getType());
assertEquals(PropertyType.LONG, p.getValue().getType());
PropertyDefinition def = p.getDefinition();
assertEquals(PropertyType.LONG, def.getRequiredType());
assertEquals(NT_NAME, def.getDeclaringNodeType().getName());
superuser.save();
try {
p.setValue(true);
fail("Conversion from BOOLEAN to LONG must throw ValueFormatException");
} catch (ValueFormatException e) {
// success
}
try {
node.setProperty(LONG_PROP_NAME, true);
fail("Conversion from BOOLEAN to LONG must throw ValueFormatException");
} catch (ValueFormatException e) {
// success
}
}
use of javax.jcr.nodetype.PropertyDefinition in project jackrabbit-oak by apache.
the class PropertyTest method testMixTitleOnUnstructured2.
public void testMixTitleOnUnstructured2() throws Exception {
Node n = testRootNode.addNode("unstructured", JcrConstants.NT_UNSTRUCTURED);
n.addMixin("mix:title");
superuser.save();
// create jcr:title as STRING => declaring NT is mix:title
Property title = n.setProperty("jcr:title", "str");
assertEquals(PropertyType.STRING, title.getType());
PropertyDefinition def = title.getDefinition();
assertEquals("mix:title", def.getDeclaringNodeType().getName());
assertEquals(PropertyType.STRING, def.getRequiredType());
// changing value to BOOLEAN => value is converted
title.setValue(true);
assertEquals(PropertyType.STRING, title.getType());
def = title.getDefinition();
assertEquals("mix:title", def.getDeclaringNodeType().getName());
assertEquals(PropertyType.STRING, def.getRequiredType());
// re-setting property to type BOOLEAN => declaring NT is nt:unstructured
title = n.setProperty("jcr:title", true);
def = title.getDefinition();
assertEquals(JcrConstants.NT_UNSTRUCTURED, def.getDeclaringNodeType().getName());
assertEquals(PropertyType.UNDEFINED, def.getRequiredType());
// same if property is set to type DOUBLE
title = n.setProperty("jcr:title", superuser.getValueFactory().createValue(2.3));
assertEquals(PropertyType.DOUBLE, title.getType());
def = title.getDefinition();
assertEquals(JcrConstants.NT_UNSTRUCTURED, def.getDeclaringNodeType().getName());
assertEquals(PropertyType.UNDEFINED, def.getRequiredType());
// setting property to STRING => declaring NT is back to mix:title
title = n.setProperty("jcr:title", "str");
assertEquals(PropertyType.STRING, title.getType());
def = title.getDefinition();
assertEquals("mix:title", def.getDeclaringNodeType().getName());
assertEquals(PropertyType.STRING, def.getRequiredType());
}
use of javax.jcr.nodetype.PropertyDefinition in project jackrabbit-oak by apache.
the class UpgradeOldSegmentTest method upgradeFrom10.
@Test
public void upgradeFrom10() throws Exception {
File testFolder = new File(new File("target"), UpgradeOldSegmentTest.class.getSimpleName());
FileUtils.deleteDirectory(testFolder);
File oldRepo = new File(testFolder, "test-repo-1.0");
oldRepo.mkdirs();
try (InputStream in = UpgradeOldSegmentTest.class.getResourceAsStream("/test-repo-1.0.zip")) {
Util.unzip(in, oldRepo);
}
SegmentTarNodeStoreContainer newRepoContainer = new SegmentTarNodeStoreContainer();
OakUpgrade.main("segment-old:" + oldRepo.getPath(), newRepoContainer.getDescription());
Repository repo = new Jcr(newRepoContainer.open()).createRepository();
Session s = repo.login(new SimpleCredentials("admin", "admin".toCharArray()));
Node myType = s.getNode("/jcr:system/jcr:nodeTypes/test:MyType");
assertEquals(2, Iterators.size(myType.getNodes("jcr:propertyDefinition")));
NodeTypeManager ntMgr = s.getWorkspace().getNodeTypeManager();
assertTrue(ntMgr.hasNodeType("test:MyType"));
NodeType nt = ntMgr.getNodeType("test:MyType");
PropertyDefinition[] pDefs = nt.getDeclaredPropertyDefinitions();
assertEquals(2, pDefs.length);
for (PropertyDefinition pd : pDefs) {
String name = pd.getName();
if (name.equals("test:mandatory")) {
assertTrue(pd.isMandatory());
} else if (name.equals("test:optional")) {
assertFalse(pd.isMandatory());
} else {
fail("Unexpected property definition: " + name);
}
}
// flip mandatory flag for test:mandatory
String cnd = "<'test'='http://www.apache.org/jackrabbit/test'>\n" + "[test:MyType] > nt:unstructured\n" + " - test:mandatory (string)\n" + " - test:optional (string)";
CndImporter.registerNodeTypes(new StringReader(cnd), s, true);
myType = s.getNode("/jcr:system/jcr:nodeTypes/test:MyType");
assertEquals(2, Iterators.size(myType.getNodes("jcr:propertyDefinition")));
nt = ntMgr.getNodeType("test:MyType");
pDefs = nt.getDeclaredPropertyDefinitions();
assertEquals(2, pDefs.length);
for (PropertyDefinition pd : pDefs) {
String name = pd.getName();
if (name.equals("test:mandatory")) {
assertFalse(pd.isMandatory());
} else if (name.equals("test:optional")) {
assertFalse(pd.isMandatory());
} else {
fail("Unexpected property definition: " + name);
}
}
s.logout();
if (repo instanceof JackrabbitRepository) {
((JackrabbitRepository) repo).shutdown();
}
newRepoContainer.close();
newRepoContainer.clean();
deleteRecursive(testFolder);
}
use of javax.jcr.nodetype.PropertyDefinition in project jackrabbit-oak by apache.
the class RepositoryUpgradeTest method verifyCustomNodeTypes.
@Test
public void verifyCustomNodeTypes() throws Exception {
Session session = createAdminSession();
try {
NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
assertTrue(manager.hasNodeType("test:unstructured"));
NodeType type = manager.getNodeType("test:unstructured");
assertFalse(type.isMixin());
assertTrue(type.isNodeType("nt:unstructured"));
boolean foundDefaultString = false;
boolean foundDefaultPath = false;
for (PropertyDefinition pDef : type.getPropertyDefinitions()) {
if ("defaultString".equals(pDef.getName())) {
assertEquals(PropertyType.STRING, pDef.getRequiredType());
assertNotNull(pDef.getDefaultValues());
assertEquals(1, pDef.getDefaultValues().length);
assertEquals("stringValue", pDef.getDefaultValues()[0].getString());
foundDefaultString = true;
} else if ("defaultPath".equals(pDef.getName())) {
assertEquals(PropertyType.PATH, pDef.getRequiredType());
assertNotNull(pDef.getDefaultValues());
assertEquals(1, pDef.getDefaultValues().length);
assertEquals("/jcr:path/nt:value", pDef.getDefaultValues()[0].getString());
foundDefaultPath = true;
}
}
assertTrue("Expected property definition with name \"defaultString\"", foundDefaultString);
assertTrue("Expected property definition with name \"defaultPath\"", foundDefaultPath);
} finally {
session.logout();
}
}
use of javax.jcr.nodetype.PropertyDefinition in project jackrabbit by apache.
the class NodeImpl method createChildNode.
protected synchronized NodeImpl createChildNode(Name name, NodeTypeImpl nodeType, NodeId id) throws RepositoryException {
// create a new node state
NodeState nodeState = stateMgr.createTransientNodeState(id, nodeType.getQName(), getNodeId(), ItemState.STATUS_NEW);
// create Node instance wrapping new node state
NodeImpl node;
try {
// NOTE: since the node is not yet connected to its parent, avoid
// calling ItemManager#getItem(ItemId) which may include a permission
// check (with subsequent usage of the hierarachy-mgr -> error).
// just let the mgr create the new node that is known to exist and
// which has not been accessed before.
node = (NodeImpl) itemMgr.createItemInstance(nodeState);
} catch (RepositoryException re) {
// something went wrong
stateMgr.disposeTransientItemState(nodeState);
// re-throw
throw re;
}
// modify the state of 'this', i.e. the parent node
NodeState thisState = (NodeState) getOrCreateTransientItemState();
// add new child node entry
thisState.addChildNodeEntry(name, nodeState.getNodeId());
// add 'auto-create' properties defined in node type
for (PropertyDefinition aPda : nodeType.getAutoCreatedPropertyDefinitions()) {
PropertyDefinitionImpl pd = (PropertyDefinitionImpl) aPda;
node.createChildProperty(pd.unwrap().getName(), pd.getRequiredType(), pd);
}
// recursively add 'auto-create' child nodes defined in node type
for (NodeDefinition aNda : nodeType.getAutoCreatedNodeDefinitions()) {
NodeDefinitionImpl nd = (NodeDefinitionImpl) aNda;
node.createChildNode(nd.unwrap().getName(), (NodeTypeImpl) nd.getDefaultPrimaryType(), null);
}
return node;
}
Aggregations