Search in sources :

Example 51 with Session

use of javax.jcr.Session in project jackrabbit by apache.

the class UtilsGetPathTest method testGetOrCreateByPathNoRoot.

@Test
public void testGetOrCreateByPathNoRoot() throws RepositoryException {
    String base = testRoot + "/foo";
    Node inter = JcrUtils.getOrCreateByPath(base, "nt:unstructured", superuser);
    assertEquals(base, inter.getPath());
    superuser.save();
    // test what happens if getRootNode() throws
    Session mockedSession = Mockito.spy(superuser);
    Mockito.when(mockedSession.getRootNode()).thenThrow(new AccessDeniedException("access denied"));
    Mockito.when(mockedSession.getNode("/")).thenThrow(new AccessDeniedException("access denied"));
    Mockito.when(mockedSession.getItem("/")).thenThrow(new AccessDeniedException("access denied"));
    Mockito.when(mockedSession.nodeExists("/")).thenReturn(false);
    Node result = JcrUtils.getOrCreateByPath(base + "/bar", false, null, null, mockedSession, false);
    mockedSession.save();
    assertEquals(base + "/bar", result.getPath());
    // already exists -> nop
    Node result2 = JcrUtils.getOrCreateByPath(base + "/bar", false, null, null, mockedSession, false);
    mockedSession.save();
    assertEquals(base + "/bar", result2.getPath());
    // create unique
    Node result3 = JcrUtils.getOrCreateByPath(base + "/bar", true, null, null, mockedSession, false);
    mockedSession.save();
    assertEquals(base + "/bar0", result3.getPath());
    // already exists with createUnique == false should pass even when parent isn't readable
    Mockito.when(mockedSession.getNode(base)).thenThrow(new AccessDeniedException("access denied"));
    Mockito.when(mockedSession.getItem(base)).thenThrow(new AccessDeniedException("access denied"));
    Mockito.when(mockedSession.nodeExists(base)).thenReturn(false);
    Node result4 = JcrUtils.getOrCreateByPath(base + "/bar", false, null, null, mockedSession, false);
    mockedSession.save();
    assertEquals(base + "/bar", result4.getPath());
}
Also used : AccessDeniedException(javax.jcr.AccessDeniedException) Node(javax.jcr.Node) Session(javax.jcr.Session) AbstractJCRTest(org.apache.jackrabbit.test.AbstractJCRTest) Test(org.junit.Test)

Example 52 with Session

use of javax.jcr.Session in project jackrabbit by apache.

the class VersioningTest method tearDown.

protected void tearDown() throws Exception {
    super.tearDown();
    Session s1 = n1.getSession();
    n1.remove();
    s1.save();
    s1.logout();
    Session s2 = n2.getSession();
    n2.remove();
    s2.save();
    s2.logout();
}
Also used : Session(javax.jcr.Session)

Example 53 with Session

use of javax.jcr.Session in project jackrabbit by apache.

the class SessionImplTest method testSessionAttributes.

/**
     * JCR-1932: Session.getAttributes( ) call always returns an empty array
     *
     * @see <a href="https://issues.apache.org/jira/browse/JCR-1932">JCR-1932</a>
     */
public void testSessionAttributes() throws RepositoryException {
    SimpleCredentials credentials = new SimpleCredentials("admin", "admin".toCharArray());
    credentials.setAttribute("test", "attribute");
    Session session = getHelper().getRepository().login(credentials);
    try {
        String[] names = session.getAttributeNames();
        assertEquals(1, names.length);
        assertEquals("test", names[0]);
        assertEquals("attribute", session.getAttribute("test"));
    } finally {
        session.logout();
    }
}
Also used : SimpleCredentials(javax.jcr.SimpleCredentials) Session(javax.jcr.Session) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession)

Example 54 with Session

use of javax.jcr.Session in project jackrabbit by apache.

the class SimpleBench method test.

void test(int run) throws Exception {
    this.run = run;
    new File("target/jcr.log").delete();
    FileUtils.deleteQuietly(new File("repository"));
    start();
    repository = new TransientRepository();
    Session session = repository.login(new SimpleCredentials("", "".toCharArray()));
    if (session.getRootNode().hasNode("test")) {
        session.getRootNode().getNode("test").remove();
        session.save();
    }
    session.getRootNode().addNode("test");
    session.save();
    end("init");
    Node node = session.getRootNode().getNode("test");
    Node n = null;
    int len = run == 0 ? 100 : 1000;
    start();
    for (int i = 0; i < len; i++) {
        if (i % 100 == 0) {
            n = node.addNode("sub" + i);
        }
        Node x = n.addNode("x" + (i % 100));
        x.setProperty("name", "John");
        x.setProperty("firstName", "Doe");
        session.save();
    }
    end("addNodes");
    session.logout();
}
Also used : SimpleCredentials(javax.jcr.SimpleCredentials) TransientRepository(org.apache.jackrabbit.core.TransientRepository) Node(javax.jcr.Node) File(java.io.File) Session(javax.jcr.Session)

Example 55 with Session

use of javax.jcr.Session in project jackrabbit by apache.

the class ItemStateHierarchyManagerDeadlockTest method createNodesUnderInvRootNode.

public void createNodesUnderInvRootNode() {
    System.out.println("Start createNodesUnderInvRootNode ");
    Session session = null;
    try {
        session = login();
        Node inventoryRoot = getInvRootNode(session);
        for (int num = 0; num < 3; num++) {
            Node current = inventoryRoot.addNode("Test" + num + "_" + System.currentTimeMillis());
            current.setProperty("profondeur", 123);
            current.setProperty("tree", "1");
            current.setProperty("clientid", 1);
            current.setProperty("propId", System.currentTimeMillis());
            current.setProperty("name", "Node " + System.currentTimeMillis());
            current.setProperty("address", "1.22.3.3");
        }
        session.save();
        System.out.println("End createNodesUnderInvRootNode ");
    } catch (Exception e) {
        System.err.println("Exception in createNodesUnderInvRootNode:" + e.getMessage());
    } finally {
        if (session != null)
            session.logout();
    }
}
Also used : Node(javax.jcr.Node) IOException(java.io.IOException) PathNotFoundException(javax.jcr.PathNotFoundException) RepositoryException(javax.jcr.RepositoryException) Session(javax.jcr.Session)

Aggregations

Session (javax.jcr.Session)1177 Node (javax.jcr.Node)645 Test (org.junit.Test)359 RepositoryException (javax.jcr.RepositoryException)206 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)158 SimpleCredentials (javax.jcr.SimpleCredentials)86 Property (javax.jcr.Property)78 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)77 Privilege (javax.jcr.security.Privilege)76 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)64 Value (javax.jcr.Value)63 Query (javax.jcr.query.Query)58 NodeIterator (javax.jcr.NodeIterator)55 QueryManager (javax.jcr.query.QueryManager)53 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)50 Authorizable (org.apache.jackrabbit.api.security.user.Authorizable)48 AccessControlManager (javax.jcr.security.AccessControlManager)47 HashMap (java.util.HashMap)44 UserManager (org.apache.jackrabbit.api.security.user.UserManager)43 ArrayList (java.util.ArrayList)41