/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.jackrabbit.test.api;

import org.apache.jackrabbit.test.AbstractJCRTest;

import javax.jcr.Session;
import javax.jcr.Node;

/**
 * Tests if {@link javax.jcr.Session#hasPermission(String, String)} yields the
 * correct permissions for a read-only, a read-write and an admin session.
 */
public class HasPermissionTest extends AbstractJCRTest {

    private static final String ACTION_ALL = Session.ACTION_READ + "," + Session.ACTION_ADD_NODE + "," + Session.ACTION_REMOVE + "," + Session.ACTION_SET_PROPERTY;

    /**
     * Tests if <code>Session.hasPermission(String, String)</code> returns
     * <ul>
     * <li><code>true</code> for READ.</li>
     * <li><code>false</code> for all other actions.</li>
     * </ul>
     */
    public void testReadOnlyPermission() throws Exception {
        Node n = testRootNode.addNode(nodeName2, testNodeType);
        superuser.save();

        Session readOnly = helper.getReadOnlySession();
        String path = n.getPath();

        assertTrue(readOnly.hasPermission(path, Session.ACTION_READ));
        assertFalse(readOnly.hasPermission(path + "newNode", Session.ACTION_ADD_NODE));
        assertFalse(readOnly.hasPermission(path, Session.ACTION_REMOVE));
        assertFalse(readOnly.hasPermission(path, Session.ACTION_SET_PROPERTY));
        assertFalse(readOnly.hasPermission(path, ACTION_ALL));
        assertFalse(readOnly.hasPermission(path, Session.ACTION_REMOVE + "," + Session.ACTION_SET_PROPERTY));
    }

    /**
     * Tests if <code>Session.hasPermission(String, String)</code> returns
     * <ul>
     * <li><code>true</code> for all actions.</li>
     * </ul>
     */
    public void testReadWritePermission() throws Exception {
        Node n = testRootNode.addNode(nodeName2, testNodeType);
        superuser.save();

        String path = n.getPath();
        Session rwSession = helper.getReadWriteSession();

        assertTrue(rwSession.hasPermission(path, Session.ACTION_READ));
        assertTrue(rwSession.hasPermission(path + "newNode", Session.ACTION_ADD_NODE));
        assertTrue(rwSession.hasPermission(path, Session.ACTION_REMOVE));
        assertTrue(rwSession.hasPermission(path, Session.ACTION_SET_PROPERTY));
        assertTrue(rwSession.hasPermission(path, ACTION_ALL));
    }


    /**
     * Tests if <code>Session.hasPermission(String, String)</code> returns
     * <ul>
     * <li><code>true</code> for all actions.</li>
     * </ul>
     */
    public void testAdminPermission() throws Exception {
        Node n = testRootNode.addNode(nodeName2, testNodeType);
        superuser.save();

        String path = n.getPath();

        assertTrue(superuser.hasPermission(path, Session.ACTION_READ));
        assertTrue(superuser.hasPermission(path + "newNode", Session.ACTION_ADD_NODE));
        assertTrue(superuser.hasPermission(path, Session.ACTION_REMOVE));
        assertTrue(superuser.hasPermission(path, Session.ACTION_SET_PROPERTY));
        assertTrue(superuser.hasPermission(path, ACTION_ALL));
    }
}
@@ -1745,24 +1745,42 @@
 ySession();%0A
+        try %7B%0A    
         Stri
@@ -1795,32 +1795,36 @@
 = n.getPath();%0A%0A
+    
         assertTr
@@ -1878,32 +1878,36 @@
 READ));%0A        
+   
+ 
 assertFalse(read
@@ -1970,32 +1970,36 @@
 NODE));%0A        
+    
 assertFalse(read
@@ -2048,32 +2048,36 @@
 MOVE));%0A        
+  
+  
 assertFalse(read
@@ -2124,32 +2124,36 @@
 SET_PROPERTY));%0A
+    
         assertFa
@@ -2199,32 +2199,36 @@
 _ALL));%0A        
+    
 assertFalse(read
@@ -2309,24 +2309,85 @@
 PROPERTY));%0A
+        %7D finally %7B%0A            readOnly.logout();%0A        %7D%0A
     %7D%0A%0A    /
@@ -2789,25 +2789,42 @@
 eSession();%0A
-%0A
+        try %7B%0A    
         asse
@@ -2875,32 +2875,36 @@
 .ACTION_READ));%0A
+    
         assertTr
@@ -2975,32 +2975,36 @@
 NODE));%0A        
+    
 assertTrue(rwSes
@@ -3053,32 +3053,36 @@
 MOVE));%0A        
+   
+ 
 assertTrue(rwSes
@@ -3137,32 +3137,36 @@
 ERTY));%0A        
+    
 assertTrue(rwSes
@@ -3200,24 +3200,86 @@
 TION_ALL));%0A
+        %7D finally %7B%0A            rwSession.logout();%0A        %7D%0A
     %7D%0A%0A%0A    

Add missing Session.logout to prevent ERROR in jcr.log