TreeChangeProcessor { public static StateNode processChange(StateTree tree, JsonObject change) { String type = change.getString(JsonConstants.CHANGE_TYPE); int nodeId = (int) change.getNumber(JsonConstants.CHANGE_NODE); StateNode node = tree.getNode(nodeId); assert node != null; switch (type) { case JsonConstants.CHANGE_TYPE_NOOP: populateFeature(change, node); break; case JsonConstants.CHANGE_TYPE_SPLICE: processSpliceChange(change, node); break; case JsonConstants.CHANGE_TYPE_PUT: processPutChange(change, node); break; case JsonConstants.CHANGE_TYPE_REMOVE: processRemoveChange(change, node); break; case JsonConstants.CHANGE_TYPE_DETACH: processDetachChange(node); break; case JsonConstants.CHANGE_TYPE_CLEAR: processClearChange(change, node); break; default: assert false : "Unsupported change type: " + type; } return node; } private  TreeChangeProcessor(); static JsSet<StateNode> processChanges(StateTree tree,
            JsonArray changes); static StateNode processChange(StateTree tree, JsonObject change);  }
@Test public void testPutChange() { JsonObject change = putChange(rootId, ns, myKey, Json.create(myValue)); StateNode node = TreeChangeProcessor.processChange(tree, change); Object value = tree.getRootNode().getMap(ns).getProperty(myKey) .getValue(); Assert.assertEquals(myValue, value); Assert.assertEquals(tree.getRootNode(), node); }
@Test public void testMapRemoveChange() { MapProperty property = tree.getRootNode().getMap(ns).getProperty(myKey); property.setValue(myValue); JsonObject change = removeChange(rootId, ns, myKey); StateNode node = TreeChangeProcessor.processChange(tree, change); Assert.assertFalse(property.hasValue()); Assert.assertEquals(tree.getRootNode(), node); }
@Test public void testMapReAdd() { MapProperty property = tree.getRootNode().getMap(ns).getProperty(myKey); property.setValue(myValue); JsonObject change = removeChange(rootId, ns, myKey); TreeChangeProcessor.processChange(tree, change); StateNode child = new StateNode(2, tree); tree.registerNode(child); change = putNodeChange(rootId, ns, myKey, child.getId()); StateNode node = TreeChangeProcessor.processChange(tree, change); Assert.assertSame(child, property.getValue()); Assert.assertEquals(tree.getRootNode(), node); }
@Test public void testPutNodeChange() { StateNode child = new StateNode(2, tree); tree.registerNode(child); JsonObject change = putNodeChange(rootId, ns, myKey, child.getId()); StateNode node = TreeChangeProcessor.processChange(tree, change); Object value = tree.getRootNode().getMap(ns).getProperty(myKey) .getValue(); Assert.assertSame(child, value); Assert.assertEquals(tree.getRootNode(), node); }
@Test public void testPrimitiveSpliceChange() { JsonObject change = spliceChange(rootId, ns, 0, 0, Json.create("foo"), Json.create("bar")); StateNode node = TreeChangeProcessor.processChange(tree, change); NodeList list = tree.getRootNode().getList(ns); Assert.assertEquals(2, list.length()); Assert.assertEquals("foo", list.get(0)); Assert.assertEquals("bar", list.get(1)); Assert.assertEquals(tree.getRootNode(), node); change = spliceChange(rootId, ns, 1, 0, Json.create("baz")); node = TreeChangeProcessor.processChange(tree, change); Assert.assertEquals(3, list.length()); Assert.assertEquals("foo", list.get(0)); Assert.assertEquals("baz", list.get(1)); Assert.assertEquals("bar", list.get(2)); change = spliceChange(rootId, ns, 1, 1); Assert.assertEquals(tree.getRootNode(), node); node = TreeChangeProcessor.processChange(tree, change); Assert.assertEquals(2, list.length()); Assert.assertEquals("foo", list.get(0)); Assert.assertEquals("bar", list.get(1)); Assert.assertEquals(tree.getRootNode(), node); }
@Test public void testNodeSpliceChange() { StateNode child = new StateNode(2, tree); tree.registerNode(child); JsonObject change = nodeSpliceChange(rootId, ns, 0, 0, child.getId()); StateNode node = TreeChangeProcessor.processChange(tree, change); NodeList list = tree.getRootNode().getList(ns); Assert.assertEquals(1, list.length()); Assert.assertSame(child, list.get(0)); Assert.assertEquals(tree.getRootNode(), node); }
@Test public void poulateMapFeature() { StateNode node = new StateNode(2, tree); tree.registerNode(node); int featureId = 11; JsonObject change = populateChange(node.getId(), false, featureId); StateNode updatedNode = TreeChangeProcessor.processChange(tree, change); Assert.assertTrue(node.hasFeature(featureId)); node.getMap(featureId); Assert.assertEquals(node, updatedNode); }
@Test public void poulateListFeature() { StateNode node = new StateNode(3, tree); tree.registerNode(node); int featureId = 12; JsonObject change = populateChange(node.getId(), true, featureId); StateNode updatedNode = TreeChangeProcessor.processChange(tree, change); Assert.assertTrue(node.hasFeature(featureId)); node.getList(featureId); Assert.assertEquals(node, updatedNode); }
@Test public void testPutNodeGetsParent() { StateNode child = new StateNode(2, tree); tree.registerNode(child); JsonObject change = putNodeChange(rootId, ns, myKey, child.getId()); StateNode node = TreeChangeProcessor.processChange(tree, change); Object value = tree.getRootNode().getMap(ns).getProperty(myKey) .getValue(); Assert.assertSame(child, value); Assert.assertEquals(tree.getRootNode(), node); Assert.assertEquals(node, child.getParent()); }
@Test public void testNodeChainGetsCorrectParents() { StateNode child = new StateNode(2, tree); tree.registerNode(child); StateNode subChild = new StateNode(3, tree); tree.registerNode(subChild); child.getList(NodeFeatures.ELEMENT_CHILDREN).add(0, child); JsonObject change = putNodeChange(rootId, ns, myKey, child.getId()); JsonObject subChange = putNodeChange(child.getId(), ns, myKey, subChild.getId()); StateNode node = TreeChangeProcessor.processChange(tree, change); Assert.assertEquals(tree.getRootNode(), node); Object value = tree.getRootNode().getMap(ns).getProperty(myKey) .getValue(); Assert.assertSame(child, value); Assert.assertEquals(node, child.getParent()); node = TreeChangeProcessor.processChange(tree, subChange); Assert.assertEquals(child, node); Assert.assertEquals(child, subChild.getParent()); }
@Test public void testNodeDetachRemovesParent() { StateNode child = new StateNode(2, tree); tree.registerNode(child); JsonObject change = putNodeChange(rootId, ns, myKey, child.getId()); StateNode node = TreeChangeProcessor.processChange(tree, change); Assert.assertEquals(tree.getRootNode(), node); Object value = tree.getRootNode().getMap(ns).getProperty(myKey) .getValue(); Assert.assertSame(child, value); Assert.assertEquals(node, child.getParent()); change = detachChange(child.getId()); TreeChangeProcessor.processChange(tree, change); Assert.assertNull(child.getParent()); }