error handling for node type strategy called on non type nodes

This commit is contained in:
Michael Hunger
2011-03-05 04:32:52 +01:00
parent b074c66d21
commit b67ea1acf0
2 changed files with 20 additions and 1 deletions

View File

@@ -153,7 +153,10 @@ public class SubReferenceNodeTypeStrategy implements NodeTypeStrategy {
@Override
@SuppressWarnings("unchecked")
public <T extends NodeBacked> Class<T> getJavaType(Node node) {
Node subrefNode = node.getSingleRelationship(INSTANCE_OF_RELATIONSHIP_TYPE, Direction.OUTGOING).getEndNode();
if (node==null) throw new IllegalArgumentException("Node is null");
Relationship instanceOfRelationship = node.getSingleRelationship(INSTANCE_OF_RELATIONSHIP_TYPE, Direction.OUTGOING);
if (instanceOfRelationship==null) throw new IllegalArgumentException("The node "+node+" is not attached to a type hierarchy.");
Node subrefNode = instanceOfRelationship.getEndNode();
try {
Class<T> clazz = (Class<T>) Class.forName((String) subrefNode.getProperty(SUBREF_CLASS_KEY)).asSubclass(NodeBacked.class);
if (log.isDebugEnabled()) log.debug("Found class " + clazz.getSimpleName() + " for node: " + node);

View File

@@ -73,6 +73,22 @@ public class SubReferenceNodeTypeStrategyTest {
Assert.assertEquals("type node has property of type Thing.class", Thing.class.getName(), typeNode.getProperty(SubReferenceNodeTypeStrategy.SUBREF_CLASS_KEY));
Assert.assertEquals("one thing has been created", 1, typeNode.getProperty(SubReferenceNodeTypeStrategy.SUBREFERENCE_NODE_COUNTER_KEY));
}
@Test(expected = IllegalArgumentException.class)
public void gettingTypeFromNonTypeNodeShouldThrowAnDescriptiveException() throws Exception {
Node referenceNode = graphDatabaseContext.getReferenceNode();
nodeTypeStrategy.getJavaType(referenceNode);
}
@Test(expected = IllegalArgumentException.class)
public void confirmingTypeOfNonTypeNodeShouldThrowAnDescriptiveException() throws Exception {
Node referenceNode = graphDatabaseContext.getReferenceNode();
nodeTypeStrategy.confirmType(referenceNode,Thing.class);
}
@Test(expected = IllegalArgumentException.class)
public void gettingTypeFromNullShouldFail() throws Exception {
nodeTypeStrategy.getJavaType(null);
}
private Node createThing() {
Transaction tx = graphDatabaseContext.beginTx();