@Test
  public void shouldBackupRepositoryWithMultipleWorkspaces() throws Exception {
    loadContent();
    Problems problems =
        session().getWorkspace().getRepositoryManager().backupRepository(backupDirectory);
    assertNoProblems(problems);

    // Make some changes that will not be in the backup ...
    session().getRootNode().addNode("node-not-in-backup");
    session().save();

    assertContentInWorkspace(repository(), "default", "/node-not-in-backup");
    assertContentInWorkspace(repository(), "ws2");
    assertContentInWorkspace(repository(), "ws3");

    // Start up a new repository
    ((LocalEnvironment) environment).setShared(true);
    RepositoryConfiguration config =
        RepositoryConfiguration.read("config/restore-repo-config.json").with(environment);
    JcrRepository newRepository = new JcrRepository(config);
    try {
      newRepository.start();

      // And restore it from the contents ...
      JcrSession newSession = newRepository.login();
      try {
        Problems restoreProblems =
            newSession.getWorkspace().getRepositoryManager().restoreRepository(backupDirectory);
        assertNoProblems(restoreProblems);
      } finally {
        newSession.logout();
      }

      // Check that the node that was added *after* the backup is not there ...
      assertContentNotInWorkspace(newRepository, "default", "/node-not-in-backup");

      // Before we assert the content, create a backup of it (for comparison purposes when
      // debugging) ...
      newSession = newRepository.login();
      try {
        Problems backupProblems =
            newSession.getWorkspace().getRepositoryManager().backupRepository(backupDirectory2);
        assertNoProblems(backupProblems);
      } finally {
        newSession.logout();
      }

      assertWorkspaces(newRepository, "default", "ws2", "ws3");

      assertContentInWorkspace(newRepository, null);
      assertContentInWorkspace(newRepository, "ws2");
      assertContentInWorkspace(newRepository, "ws3");
      queryContentInWorkspace(newRepository, null);
    } finally {
      newRepository.shutdown().get(10, TimeUnit.SECONDS);
    }
  }
  @Test
  public void shouldReturnFalseFromIsSameIfTheRepositoryInstanceIsDifferent() throws Exception {
    // Set up the store ...
    InMemoryRepositorySource source2 = new InMemoryRepositorySource();
    source2.setName("store");
    Graph store2 = Graph.create(source2, context);
    store2
        .importXmlFrom(AbstractJcrTest.class.getClassLoader().getResourceAsStream("cars.xml"))
        .into("/");
    JcrSession jcrSession2 = mock(JcrSession.class);
    when(jcrSession2.nodeTypeManager()).thenReturn(nodeTypes);
    when(jcrSession2.isLive()).thenReturn(true);
    SessionCache cache2 =
        new SessionCache(jcrSession2, store2.getCurrentWorkspaceName(), context, nodeTypes, store2);

    Workspace workspace2 = mock(Workspace.class);
    Repository repository2 = mock(JcrRepository.class);

    when(jcrSession2.getWorkspace()).thenReturn(workspace2);
    when(jcrSession2.getRepository()).thenReturn(repository2);
    when(workspace2.getName()).thenReturn("workspace1");

    // Use the same id and location ...
    javax.jcr.Node prius2 = cache2.findJcrNode(null, path("/Cars/Hybrid/Toyota Prius"));
    assertThat(prius2.isSame(prius), is(false));

    // Check the properties ...
    javax.jcr.Property model = prius.getProperty("vehix:model");
    javax.jcr.Property model2 = prius2.getProperty("vehix:model");
    assertThat(model.isSame(model2), is(false));
  }
  @Test
  @FixFor("MODE-2109 ")
  public void shouldOnlyAllowCloningInSomeCases() throws Exception {
    session.getWorkspace().createWorkspace("other");

    try {
      session.getRootNode().addNode("col1", "test:smallCollection");
      Node regular = session.getRootNode().addNode("regular");
      regular.addNode("regular1");
      session.save();

      // cloning a large collection is not allowed
      JcrWorkspace workspace = session.getWorkspace();
      try {
        workspace.clone(workspace.getName(), "/col1", "/regular", false);
        fail("Should not allow cloning");
      } catch (ConstraintViolationException e) {
        // expected
      }

      // clone a regular node into a large collection
      JcrSession otherSession = repository.login("other");
      Node col2 = otherSession.getRootNode().addNode("col2", "test:smallCollection");
      col2.addNode("child1");
      otherSession.save();

      otherSession.getWorkspace().clone(workspace.getName(), "/regular", "/col2/regular", false);
      NodeIterator nodes = otherSession.getNode("/col2").getNodes();
      assertEquals(2, nodes.getSize());
    } finally {
      session.getWorkspace().deleteWorkspace("other");
    }
  }
 private void queryContentInWorkspace(JcrRepository newRepository, String workspaceName)
     throws RepositoryException {
   JcrSession session = newRepository.login();
   try {
     String statement = "SELECT [car:model], [car:year], [car:msrp] FROM [car:Car] AS car";
     Query query = session.getWorkspace().getQueryManager().createQuery(statement, Query.JCR_SQL2);
     QueryResult results = query.execute();
     assertThat(results.getRows().getSize(), is(13L));
   } finally {
     session.logout();
   }
 }
  private void assertWorkspaces(JcrRepository newRepository, String... workspaceNames)
      throws RepositoryException {
    Set<String> expectedNames = new HashSet<String>();
    for (String expectedName : workspaceNames) {
      expectedNames.add(expectedName);
    }

    Set<String> actualNames = new HashSet<String>();
    JcrSession session = newRepository.login();
    try {
      for (String actualName : session.getWorkspace().getAccessibleWorkspaceNames()) {
        actualNames.add(actualName);
      }
    } finally {
      session.logout();
    }

    assertThat(actualNames, is(expectedNames));
  }
  @Test
  public void shouldReturnTrueFromIsSameIfTheNodeUuidAndWorkspaceNameAndRepositoryInstanceAreSame()
      throws Exception {
    // Set up the store ...
    InMemoryRepositorySource source2 = new InMemoryRepositorySource();
    source2.setName("store");
    Graph store2 = Graph.create(source2, context);
    store2
        .importXmlFrom(AbstractJcrTest.class.getClassLoader().getResourceAsStream("cars.xml"))
        .into("/");
    JcrSession jcrSession2 = mock(JcrSession.class);
    when(jcrSession2.nodeTypeManager()).thenReturn(nodeTypes);
    when(jcrSession2.isLive()).thenReturn(true);
    SessionCache cache2 =
        new SessionCache(jcrSession2, store2.getCurrentWorkspaceName(), context, nodeTypes, store2);

    Workspace workspace2 = mock(Workspace.class);
    when(jcrSession2.getWorkspace()).thenReturn(workspace2);
    when(jcrSession2.getRepository()).thenReturn(repository);
    when(workspace2.getName()).thenReturn("workspace1");

    WorkspaceLockManager lockManager =
        new WorkspaceLockManager(context, repoLockManager, "workspace2", null);
    JcrLockManager jcrLockManager = new JcrLockManager(jcrSession2, lockManager);
    when(jcrSession2.lockManager()).thenReturn(jcrLockManager);

    // Use the same id and location ...
    javax.jcr.Node prius2 = cache2.findJcrNode(null, path("/Cars/Hybrid/Toyota Prius"));
    prius2.addMixin("mix:referenceable");
    prius.addMixin("mix:referenceable");
    String priusUuid = prius.getIdentifier();
    String priusUuid2 = prius2.getIdentifier();
    assertThat(priusUuid, is(priusUuid2));
    assertThat(prius2.isSame(prius), is(true));

    // Check the properties ...
    javax.jcr.Property model = prius.getProperty("vehix:model");
    javax.jcr.Property model2 = prius2.getProperty("vehix:model");
    javax.jcr.Property year2 = prius2.getProperty("vehix:year");
    assertThat(model.isSame(model2), is(true));
    assertThat(model.isSame(year2), is(false));
  }
  @Test
  public void shouldReturnFalseFromIsSameIfTheNodeUuidIsDifferent() throws Exception {
    // Set up the store ...
    InMemoryRepositorySource source2 = new InMemoryRepositorySource();
    source2.setName("store");
    Graph store2 = Graph.create(source2, context);
    store2
        .importXmlFrom(AbstractJcrTest.class.getClassLoader().getResourceAsStream("cars.xml"))
        .into("/");
    JcrSession jcrSession2 = mock(JcrSession.class);
    when(jcrSession2.nodeTypeManager()).thenReturn(nodeTypes);
    when(jcrSession2.isLive()).thenReturn(true);
    SessionCache cache2 =
        new SessionCache(jcrSession2, store2.getCurrentWorkspaceName(), context, nodeTypes, store2);

    Workspace workspace2 = mock(Workspace.class);
    JcrRepository repository2 = mock(JcrRepository.class);
    RepositoryLockManager repoLockManager2 = mock(RepositoryLockManager.class);
    when(jcrSession2.getWorkspace()).thenReturn(workspace2);
    when(jcrSession2.getRepository()).thenReturn(repository2);
    when(workspace2.getName()).thenReturn("workspace1");

    WorkspaceLockManager lockManager =
        new WorkspaceLockManager(context, repoLockManager2, "workspace2", null);
    JcrLockManager jcrLockManager = new JcrLockManager(jcrSession2, lockManager);
    when(jcrSession2.lockManager()).thenReturn(jcrLockManager);

    // Use the same id and location; use 'Nissan Altima'
    // since the UUIDs will be different (cars.xml doesn't define on this node) ...
    javax.jcr.Node altima2 = cache2.findJcrNode(null, path("/Cars/Hybrid/Nissan Altima"));
    altima2.addMixin("mix:referenceable");
    altima.addMixin("mix:referenceable");
    String altimaUuid = altima.getIdentifier();
    String altimaUuid2 = altima2.getIdentifier();
    assertThat(altimaUuid, is(not(altimaUuid2)));
    assertThat(altima2.isSame(altima), is(false));

    // Check the properties ...
    javax.jcr.Property model = altima.getProperty("vehix:model");
    javax.jcr.Property model2 = altima2.getProperty("vehix:model");
    assertThat(model.isSame(model2), is(false));
  }
  @Test
  public void shouldBackupAndRestoreRepositoryWithMultipleWorkspaces() throws Exception {
    // Load the content and verify it's there ...
    loadContent();
    assertContentInWorkspace(repository(), "default");
    assertContentInWorkspace(repository(), "ws2");
    assertContentInWorkspace(repository(), "ws3");

    // Make the backup, and check that there are no problems ...
    Problems problems =
        session().getWorkspace().getRepositoryManager().backupRepository(backupDirectory);
    assertNoProblems(problems);

    // Make some changes that will not be in the backup ...
    session().getRootNode().addNode("node-not-in-backup");
    session().save();

    // Check the content again ...
    assertContentInWorkspace(repository(), "default", "/node-not-in-backup");
    assertContentInWorkspace(repository(), "ws2");
    assertContentInWorkspace(repository(), "ws3");

    // Restore the content from the backup into our current repository ...
    JcrSession newSession = repository().login();
    try {
      Problems restoreProblems =
          newSession.getWorkspace().getRepositoryManager().restoreRepository(backupDirectory);
      assertNoProblems(restoreProblems);
    } finally {
      newSession.logout();
    }

    assertWorkspaces(repository(), "default", "ws2", "ws3");

    // Check the content again ...
    assertContentInWorkspace(repository(), "default");
    assertContentInWorkspace(repository(), "ws2");
    assertContentInWorkspace(repository(), "ws3");
    assertContentNotInWorkspace(repository(), "default", "/node-not-in-backup");
    queryContentInWorkspace(repository(), null);
  }