}
@Test
public void shouldCorrectlyManageIndexesOfSiblingsWithSameNames() {
Name name_a1 = nameFactory.create("a");
MapNode node_a1 = workspace.createNode(context, workspace.getRoot(), name_a1, null);
assertThat(node_a1, is(notNullValue()));
assertThat(node_a1.getParent(), is(workspace.getRoot()));
assertThat(node_a1.getName().getName(), is(name_a1));
assertThat(node_a1.getName().hasIndex(), is(false));
Name name_a2 = nameFactory.create("a");
MapNode node_a2 = workspace.createNode(context, workspace.getRoot(), name_a2, null);
assertThat(node_a2, is(notNullValue()));
assertThat(node_a2.getParent(), is(workspace.getRoot()));
assertThat(node_a2.getName().getName(), is(name_a2));
assertThat(node_a2.getName().hasIndex(), is(true));
assertThat(node_a2.getName().getIndex(), is(2));
// node 1 should now have an index ..
assertThat(node_a1.getName().getIndex(), is(1));
// Add another node without the same name ..
Name name_b = nameFactory.create("b");
MapNode node_b = workspace.createNode(context, workspace.getRoot(), name_b, null);
assertThat(node_b, is(notNullValue()));
assertThat(node_b.getParent(), is(workspace.getRoot()));
assertThat(node_b.getName().getName(), is(name_b));
assertThat(node_b.getName().hasIndex(), is(false));
// Add a third node with the same name ..
Name name_a3 = nameFactory.create("a");
MapNode node_a3 = workspace.createNode(context, workspace.getRoot(), name_a3, null);
assertThat(node_a3, is(notNullValue()));
assertThat(node_a3.getParent(), is(workspace.getRoot()));
assertThat(node_a3.getName().getName(), is(name_a3));
assertThat(node_a3.getName().hasIndex(), is(true));
assertThat(node_a3.getName().getIndex(), is(3));
// Check the number of children ..
assertThat(workspace.getRoot().getChildren().size(), is(4));
assertThat(workspace.getRoot().getChildren(), hasItems(node_a1, node_a2, node_b, node_a3));
assertThat(workspace.size(), is(5));
assertThat(workspace.getNode(pathFactory.create("/a[1]")), is(sameInstance(node_a1)));
assertThat(workspace.getNode(pathFactory.create("/a[2]")), is(sameInstance(node_a2)));
assertThat(workspace.getNode(pathFactory.create("/a[3]")), is(sameInstance(node_a3)));
assertThat(workspace.getNode(pathFactory.create("/b")), is(sameInstance(node_b)));
// Removing a node with the same name will reduce the index ..
workspace.removeNode(context, node_a2);
assertThat(workspace.getRoot().getChildren().size(), is(3));
assertThat(workspace.getRoot().getChildren(), hasItems(node_a1, node_b, node_a3));
assertThat(node_a1.getName().getIndex(), is(1));
assertThat(node_b.getName().hasIndex(), is(false));
assertThat(node_a3.getName().getIndex(), is(2));
// Removing a node with the same name will reduce the index ..
workspace.removeNode(context, node_a1);
assertThat(workspace.getRoot().getChildren().size(), is(2));
assertThat(workspace.getRoot().getChildren(), hasItems(node_b, node_a3));
assertThat(node_b.getName().hasIndex(), is(false));
assertThat(node_a3.getName().hasIndex(), is(false));
assertThat(workspace.size(), is(3));
}