use of javax.jcr.Property in project jackrabbit by apache.
the class PathPropertyTest method testGetProperty.
/**
* Since JCR 2.0 a path property can be dereferenced if it points to a
* Property.
* TODO: create several tests out of this one
*/
public void testGetProperty() throws RepositoryException {
if (!multiple) {
String propPath = prop.getPath();
String propName = prop.getName();
// absolute property path
prop.getParent().setProperty(propName, propPath, PropertyType.PATH);
String path = prop.getString();
Property p = prop.getProperty();
assertEquals("The path of the dereferenced property must be equal to the value", path, p.getPath());
assertTrue("The property value must be resolved to the correct property.", prop.isSame(p));
// relative property path
prop.getParent().setProperty(propName, propName, PropertyType.PATH);
path = prop.getString();
p = prop.getProperty();
assertEquals("The path of the dereferenced property must be equal to the value", path, p.getName());
assertTrue("The property value must be resolved to the correct property.", prop.getParent().getProperty(path).isSame(p));
// non-existing property path
while (session.propertyExists(propPath)) {
propPath += "x";
}
prop.getParent().setProperty(propName, propPath, PropertyType.PATH);
try {
prop.getProperty();
fail("Calling Property.getProperty() for a PATH value that doesn't have a corresponding Property, ItemNotFoundException is expected");
} catch (ItemNotFoundException e) {
//ok
}
} else {
try {
prop.getProperty();
fail("Property.getNode() called on a multivalue property " + "should throw a ValueFormatException.");
} catch (ValueFormatException vfe) {
// ok
}
}
}
use of javax.jcr.Property in project jackrabbit by apache.
the class NodeReadMethodsTest method locateNodeWithReference.
/**
* Returns the first descendant of <code>node</code> which has a property of
* type {@link javax.jcr.PropertyType#REFERENCE} set and is <b>not</b>
* multivalued.
*
* @param node <code>Node</code> to start traversal.
* @return first node with a property of PropertType.REFERENCE
*/
private Node locateNodeWithReference(Node node) throws RepositoryException {
PropertyIterator properties = node.getProperties();
while (properties.hasNext()) {
Property p = properties.nextProperty();
if (p.getType() == PropertyType.REFERENCE && !p.getDefinition().isMultiple()) {
return node;
}
}
NodeIterator nodes = node.getNodes();
while (nodes.hasNext()) {
Node returnedNode = locateNodeWithReference(nodes.nextNode());
if (returnedNode != null) {
return returnedNode;
}
}
return null;
}
use of javax.jcr.Property in project jackrabbit by apache.
the class NodeReadMethodsTest method testGetReferences.
public void testGetReferences() throws NotExecutableException, RepositoryException {
Node node = locateNodeWithReference(testRootNode);
if (node == null) {
throw new NotExecutableException("Workspace does not contain a node with a reference property set");
}
PropertyIterator properties = node.getProperties();
while (properties.hasNext()) {
Property p = properties.nextProperty();
if (p.getType() == PropertyType.REFERENCE && !p.getDefinition().isMultiple()) {
Node referencedNode = p.getNode();
PropertyIterator refs = referencedNode.getReferences();
boolean referenceFound = false;
while (refs.hasNext()) {
Property ref = refs.nextProperty();
if (ref.isSame(p)) {
referenceFound = true;
}
}
assertTrue("Correct reference not found", referenceFound);
}
}
}
use of javax.jcr.Property in project jackrabbit by apache.
the class NodeReadMethodsTest method testHasProperty.
/**
* Test if hasProperty(String relPath) returns true if a required property
* exists and false if it doesn't. Tested node: root
*/
public void testHasProperty() throws NotExecutableException, RepositoryException {
Node node = testRootNode;
PropertyIterator properties = node.getProperties();
StringBuffer notExistingPropertyName = new StringBuffer();
while (properties.hasNext()) {
Property p = properties.nextProperty();
assertTrue("node.hasProperty(\"relPath\") returns false " + "although property at relPath is existing", node.hasProperty(p.getName()));
notExistingPropertyName.append(p.getName() + "X");
}
if (notExistingPropertyName.toString().equals("")) {
fail("Root node must at least have one property: jcr:primaryType");
}
assertFalse("node.hasProperty(\"relPath\") returns true " + "although property at relPath is not existing", node.hasProperty(notExistingPropertyName.toString().replaceAll(":", "")));
}
use of javax.jcr.Property in project jackrabbit by apache.
the class NodeReadMethodsTest method testGetProperty.
/**
* Test if getProperty(String relPath) returns the correct node and if a
* PathNotFoundException is thrown when property at relPath does not exist
*/
public void testGetProperty() throws NotExecutableException, RepositoryException {
StringBuffer notExistingPath = new StringBuffer("X");
PropertyIterator properties = testRootNode.getProperties();
while (properties.hasNext()) {
// build a path that for sure is not existing
// (":" of namespace prefix will be replaced later on)
notExistingPath.append(properties.nextProperty().getName());
}
try {
testRootNode.getProperty(notExistingPath.toString().replaceAll(":", ""));
fail("getProperty(String relPath) must throw a " + "PathNotFoundException if no node exists at relPath");
} catch (PathNotFoundException e) {
// success
}
try {
PropertyIterator properties2 = testRootNode.getProperties();
Property property = properties2.nextProperty();
assertTrue("Property returned by getProperties() is not the same as returned by getProperty(String).", testRootNode.getProperty(property.getName()).isSame(property));
} catch (NoSuchElementException e) {
fail("Root node must always have at least one property: jcr:primaryType");
}
}
Aggregations