DefaultXMLDiff implements XMLDiff { @Override public Map<Node, Patch<?>> diff(Node left, Node right, XMLDiffConfiguration config) throws DiffException { Map<Node, Patch<?>> patches = new LinkedHashMap<>(); Patch<Node> rootPatch = new DefaultPatch<>(); if (areSimilar(left, right)) { if (left != null) { patches.putAll(diffSimilarNodes(left, right, config)); } } else if (left == null) { rootPatch.add(new InsertDelta<>(new DefaultChunk<>(-1, Collections.<Node>emptyList()), new DefaultChunk<>(XMLDiffUtils.getNodeIndex(right), Collections.singletonList(right)))); } else if (right == null) { rootPatch.add( new DeleteDelta<>(new DefaultChunk<>(XMLDiffUtils.getNodeIndex(left), Collections.singletonList(left)), new DefaultChunk<>(-1, Collections.<Node>emptyList()))); } else { rootPatch.add( new ChangeDelta<>(new DefaultChunk<>(XMLDiffUtils.getNodeIndex(left), Collections.singletonList(left)), new DefaultChunk<>(XMLDiffUtils.getNodeIndex(right), Collections.singletonList(right)))); } if (!rootPatch.isEmpty()) { patches.put(null, rootPatch); } return patches; }  @Override void xxx(); @Override Map<Node, Patch<?>> diff(Node left, Node right, XMLDiffConfiguration config); @Override Map<Node, Patch<?>> diff(NodeList left, NodeList right, XMLDiffConfiguration config); @Override Map<Node, Patch<?>> diff(NamedNodeMap left, NamedNodeMap right, XMLDiffConfiguration config);  }
@Test void nonSimilarNodeInsert() throws Exception { Node text = this.document.getDocumentElement().getLastChild(); Map<Node, Patch<?>> patches = this.defaultXMLDiff.diff(null, text, this.config); assertEquals(1, patches.size()); Patch<?> patch = patches.get(null); assertEquals(1, patch.size()); Delta<?> delta = patch.get(0); assertEquals(Delta.Type.INSERT, delta.getType()); assertEquals(Collections.emptyList(), delta.getPrevious().getElements()); assertEquals(Collections.singletonList(text), delta.getNext().getElements()); }
@Test void nonSimilarNodeDelete() throws Exception { Node text = this.document.getDocumentElement().getLastChild(); Map<Node, Patch<?>> patches = this.defaultXMLDiff.diff(text, null, this.config); assertEquals(1, patches.size()); Patch<?> patch = patches.get(null); assertEquals(1, patch.size()); Delta<?> delta = patch.get(0); assertEquals(Delta.Type.DELETE, delta.getType()); assertEquals(Collections.singletonList(text), delta.getPrevious().getElements()); assertEquals(Collections.emptyList(), delta.getNext().getElements()); }
@Test void nonSimilarNodeChange() throws Exception { Node child = this.document.getDocumentElement().getFirstChild(); Node text = this.document.getDocumentElement().getLastChild(); Map<Node, Patch<?>> patches = this.defaultXMLDiff.diff(child, text, this.config); assertEquals(1, patches.size()); Patch<?> patch = patches.get(null); assertEquals(1, patch.size()); Delta<?> delta = patch.get(0); assertEquals(Delta.Type.CHANGE, delta.getType()); assertEquals(Collections.singletonList(child), delta.getPrevious().getElements()); assertEquals(Collections.singletonList(text), delta.getNext().getElements()); }