/** Test if errors with expressions also set the error flag on the node. */ public void testExpressionError() { Node n = numberNode.newInstance(testLibrary, "number1"); n.setExpression("value", "***"); try { n.update(); fail("Should have caused an exception."); } catch (ProcessingError e) { assertTrue(e.getCause().toString().toLowerCase().contains("cannot compile expression")); assertTrue(n.hasError()); // As stated in Node#update(ProcessingContext), even if an error occurred the node is still // marked as clean // and events are fired. It is important to mark the node as clean so that subsequent changes // to the node // mark it as dirty, triggering an event. This allows you to fix the cause of the error in the // node. assertFalse(n.isDirty()); assertNull(n.getOutputValue()); } n.setExpression("value", "10 + 1"); assertTrue(n.isDirty()); n.update(); assertFalse(n.hasError()); assertFalse(n.isDirty()); assertEquals(11, n.getOutputValue()); }
/** * Test the behaviour of {@link Node#stampDirty()}. * * @throws ExpressionError if the expression causes an error. This indicates a regression. */ public void testMarkStampedDirty() throws ExpressionError { // Setup a graph where a <- b <- c. Node a = Node.ROOT_NODE.newInstance(testLibrary, "a", Integer.class); Node b = Node.ROOT_NODE.newInstance(testLibrary, "b", Integer.class); Node c = Node.ROOT_NODE.newInstance(testLibrary, "c", Integer.class); a.addParameter("a", Parameter.Type.INT); b.addParameter("b", Parameter.Type.INT); Port bIn = b.addPort("in"); Port cIn = c.addPort("in"); bIn.connect(a); cIn.connect(b); // Update the graph. This will make a, b and c clean. c.update(); assertFalse(a.isDirty()); assertFalse(b.isDirty()); assertFalse(c.isDirty()); // Set b to a stamped expression. This will make node b, and all of its dependencies, dirty. b.setExpression("b", "stamp(\"my_b\", 55)"); assertTrue(b.hasStampExpression()); assertFalse(a.isDirty()); assertTrue(b.isDirty()); assertTrue(c.isDirty()); // Update the graph, cleaning all of the nodes. c.update(); assertFalse(a.isDirty()); assertFalse(b.isDirty()); assertFalse(c.isDirty()); // Mark only stamped upstream nodes as dirty. This will make b dirty, and all of its // dependencies. c.stampDirty(); assertFalse(a.isDirty()); assertTrue(b.isDirty()); assertTrue(c.isDirty()); // Remove the expression and update. This will make all nodes clean again. b.clearExpression("b"); c.update(); // Node b will not be dirty, since everything was updated. assertFalse(b.isDirty()); // Since there are no nodes with stamp expressions, marking the stamped upstream nodes will have // no effect. c.stampDirty(); assertFalse(a.isDirty()); assertFalse(b.isDirty()); assertFalse(c.isDirty()); }
/** This is the same test as ParameterTest#testExpressionDependencies, but at the Node level. */ public void testRemoveExpressionDependency() { Node net = testNetworkNode.newInstance(testLibrary, "net"); Node number1 = net.create(numberNode); number1.addParameter("bob", Parameter.Type.INT, 10); number1.setExpression("value", "bob"); number1.setRendered(); net.update(); assertEquals(10, net.getOutputValue()); number1.removeParameter("bob"); try { net.update(); fail(); } catch (ProcessingError e) { assertTrue(e.getCause().getMessage().toLowerCase().contains("cannot evaluate expression")); } assertTrue(net.hasError()); assertTrue(number1.hasError()); assertNull(net.getOutputValue()); }