}
@Test
public void shouldCloneWorkspaceAndCopyContentsIfWorkspaceWithSpecifiedNameExists() {
String workspaceName = "Original Workspace";
MapWorkspace workspace = repository.createWorkspace(context, workspaceName, CreateConflictBehavior.DO_NOT_CREATE);
assertThat(workspace, is(notNullValue()));
assertThat(repository.getWorkspaceNames(), hasItems(workspaceName));
// Populate the workspace with a few nodes ...
MapNode root = workspace.getRoot();
MapNode node_a = workspace.createNode(context, root, nameFactory.create("a"), null);
MapNode node_b = workspace.createNode(context, node_a, nameFactory.create("b"), null);
MapNode node_c = workspace.createNode(context, node_b, nameFactory.create("c"), null);
MapNode node_d = workspace.createNode(context, root, nameFactory.create("d"), null);
MapNode node_e = workspace.createNode(context, node_d, nameFactory.create("e"), null);
MapNode node_b2 = workspace.createNode(context, node_d, nameFactory.create("b"), null);
ValueFactory<String> stringFactory = context.getValueFactories().getStringFactory();
Name propertyName = nameFactory.create("something");
Property property = propertyFactory.create(propertyName, stringFactory.create("Worth the wait"));
node_b.setProperty(property);
assertThat(((InMemoryRepository.Workspace) workspace).size(), is(7));
assertThat(workspace.getNode(pathFactory.create("/")), is(sameInstance(workspace.getRoot())));
assertThat(workspace.getNode(pathFactory.create("/a")), is(sameInstance(node_a)));
assertThat(workspace.getNode(pathFactory.create("/a/b")), is(sameInstance(node_b)));
assertThat(workspace.getNode(pathFactory.create("/a/b/c")), is(sameInstance(node_c)));
assertThat(workspace.getNode(pathFactory.create("/d")), is(sameInstance(node_d)));
assertThat(workspace.getNode(pathFactory.create("/d/e")), is(sameInstance(node_e)));
assertThat(workspace.getNode(pathFactory.create("/d/b")), is(sameInstance(node_b2)));
assertThat(workspace.getNode(pathFactory.create("/a/b")).getProperty(propertyName), is(property));
// Now clone the workspace ...
String newWorkspaceName = "New Workspace";
MapWorkspace new_workspace = repository.createWorkspace(context,
newWorkspaceName,
CreateConflictBehavior.DO_NOT_CREATE,
workspaceName);
assertThat(new_workspace, is(notNullValue()));
assertThat(repository.getWorkspaceNames(), hasItems(workspaceName, newWorkspaceName));
// Now check that the original workspace still has its content ...
assertThat(((InMemoryRepository.Workspace) workspace).size(), is(7));
assertThat(workspace.getNode(pathFactory.create("/")), is(sameInstance(workspace.getRoot())));
assertThat(workspace.getNode(pathFactory.create("/a")), is(sameInstance(node_a)));
assertThat(workspace.getNode(pathFactory.create("/a/b")), is(sameInstance(node_b)));
assertThat(workspace.getNode(pathFactory.create("/a/b/c")), is(sameInstance(node_c)));
assertThat(workspace.getNode(pathFactory.create("/d")), is(sameInstance(node_d)));
assertThat(workspace.getNode(pathFactory.create("/d/e")), is(sameInstance(node_e)));
assertThat(workspace.getNode(pathFactory.create("/d/b")), is(sameInstance(node_b2)));
assertThat(workspace.getNode(pathFactory.create("/a/b")).getProperty(propertyName), is(property));
// Now check that the new workspace has its content ...
assertThat(((InMemoryRepository.Workspace) new_workspace).size(), is(7));
// Since we cloned workspaces, the UUIDs should be the same in each workspace ...
assertThat(workspace.getNode(pathFactory.create("/")).getUuid(),
is(new_workspace.getNode(pathFactory.create("/")).getUuid()));
assertThat(workspace.getNode(pathFactory.create("/a")).getUuid(),
is(new_workspace.getNode(pathFactory.create("/a")).getUuid()));
assertThat(workspace.getNode(pathFactory.create("/a/b")).getUuid(),
is(new_workspace.getNode(pathFactory.create("/a/b")).getUuid()));
assertThat(workspace.getNode(pathFactory.create("/a/b/c")).getUuid(),
is(new_workspace.getNode(pathFactory.create("/a/b/c")).getUuid()));
assertThat(workspace.getNode(pathFactory.create("/d")).getUuid(),
is(new_workspace.getNode(pathFactory.create("/d")).getUuid()));
assertThat(workspace.getNode(pathFactory.create("/d/e")).getUuid(),
is(new_workspace.getNode(pathFactory.create("/d/e")).getUuid()));
assertThat(workspace.getNode(pathFactory.create("/d/b")).getUuid(),
is(new_workspace.getNode(pathFactory.create("/d/b")).getUuid()));
}