### Question:
ParentReferenceFactoryImpl implements ParentReferenceFactory<E, MutableBinaryTreeNode<E>> { @Override public ParentReference<MutableBinaryTreeNode<E>> wrap(MutableBinaryTreeNode<E> node) { return (oldRoot, newRoot) -> { if (node.getLeft() == oldRoot) { final MutableBinaryTreeNodeImpl<E> concreteNode = (MutableBinaryTreeNodeImpl<E>)node; concreteNode.setInternalLeft((MutableBinaryTreeNodeImpl<E>)newRoot); } else if (node.getRight() == oldRoot) { final MutableBinaryTreeNodeImpl<E> concreteNode = (MutableBinaryTreeNodeImpl<E>)node; concreteNode.setInternalRight((MutableBinaryTreeNodeImpl<E>) newRoot); } else { throw new IllegalArgumentException("The current node is not a child of parent"); } }; }  @Override ParentReference<MutableBinaryTreeNode<E>> wrap(MutableBinaryTreeNode<E> node); @Override ParentReference<MutableBinaryTreeNode<E>> wrap(Tree<E, MutableBinaryTreeNode<E>> tree); @Override Class<?> forClass();  }### Answer:
@Test(expected = IllegalArgumentException.class) public void unableToReplaceUnknown() { final ParentReferenceFactoryImpl<String> factory = new ParentReferenceFactoryImpl<>(); final MutableBinaryTreeNodeImpl<String> a = new MutableBinaryTreeNodeImpl<>("a"); final MutableBinaryTreeNodeImpl<String> b = new MutableBinaryTreeNodeImpl<>("b"); final MutableBinaryTreeNodeImpl<String> c = new MutableBinaryTreeNodeImpl<>("c"); final ParentReference<MutableBinaryTreeNode<String>> reference = factory.wrap(a); reference.replace(b, c); }
@Test(expected = IllegalArgumentException.class) public void unableToReplaceUnknownRoot() { final ParentReferenceFactoryImpl<String> factory = new ParentReferenceFactoryImpl<>(); final MutableBinaryTreeNodeImpl<String> a = new MutableBinaryTreeNodeImpl<>("a"); final MutableBinaryTreeNodeImpl<String> b = new MutableBinaryTreeNodeImpl<>("b"); final MutableBinaryTreeNodeImpl<String> c = new MutableBinaryTreeNodeImpl<>("c"); final MutableBinaryTreeImpl<String> tree = new MutableBinaryTreeImpl<>(a); final ParentReference<MutableBinaryTreeNode<String>> reference = factory.wrap(tree); reference.replace(b, c); }
@Test public void replaceRoot() { final ParentReferenceFactoryImpl<String> factory = new ParentReferenceFactoryImpl<>(); final MutableBinaryTreeNodeImpl<String> a = new MutableBinaryTreeNodeImpl<>("a"); final MutableBinaryTreeNodeImpl<String> b = new MutableBinaryTreeNodeImpl<>("b"); final MutableBinaryTreeImpl<String> tree = new MutableBinaryTreeImpl<>(a); final ParentReference<MutableBinaryTreeNode<String>> reference = factory.wrap(tree); reference.replace(a, b); assertEquals(b, tree.getRoot()); }
@Test public void replaceLeft() { final ParentReferenceFactoryImpl<String> factory = new ParentReferenceFactoryImpl<>(); final MutableBinaryTreeNodeImpl<String> a = new MutableBinaryTreeNodeImpl<>("a"); final MutableBinaryTreeNodeImpl<String> b = new MutableBinaryTreeNodeImpl<>("b", a, null); final MutableBinaryTreeNodeImpl<String> c = new MutableBinaryTreeNodeImpl<>("c"); final ParentReference<MutableBinaryTreeNode<String>> reference = factory.wrap(b); reference.replace(a, c); assertEquals(c, b.childIterator().next()); }
@Test public void replaceRight() { final ParentReferenceFactoryImpl<String> factory = new ParentReferenceFactoryImpl<>(); final MutableBinaryTreeNodeImpl<String> a = new MutableBinaryTreeNodeImpl<>("a"); final MutableBinaryTreeNodeImpl<String> b = new MutableBinaryTreeNodeImpl<>("b", null, a); final MutableBinaryTreeNodeImpl<String> c = new MutableBinaryTreeNodeImpl<>("c"); final ParentReference<MutableBinaryTreeNode<String>> reference = factory.wrap(b); reference.replace(a, c); assertEquals(c, b.childIterator().next()); }