/*
 * 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.api;

import org.apache.jackrabbit.commons.cnd.CndImporter;
import org.apache.jackrabbit.test.AbstractJCRTest;

import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
import java.io.InputStreamReader;
import java.io.Reader;

/**
 * <code>JackrabbitNodeTest</code>...
 */
public class JackrabbitNodeTest  extends AbstractJCRTest {

    static final String SEQ_BEFORE = "jackrabbit";
    static final String SEQ_AFTER =  "jackraBbit";
    static final int RELPOS = 6;

    static final String TEST_NODETYPES = "org/apache/jackrabbit/api/test_mixin_nodetypes.cnd";

    protected void setUp() throws Exception {
        super.setUp();
        assertTrue(testRootNode.getPrimaryNodeType().hasOrderableChildNodes());
        for (char c : SEQ_BEFORE.toCharArray()) {
            testRootNode.addNode(new String(new char[]{c}));
        }

        Reader cnd = new InputStreamReader(getClass().getClassLoader().getResourceAsStream(TEST_NODETYPES));
        CndImporter.registerNodeTypes(cnd, superuser);
        cnd.close();
    }

    public void testRename() throws RepositoryException {
        Node renamedNode = null;
        NodeIterator it = testRootNode.getNodes();
        int pos = 0;
        while (it.hasNext()) {
            Node n = it.nextNode();
            String name = n.getName();
            assertEquals(name, new String(new char[]{SEQ_BEFORE.charAt(pos)}));
            if (pos == RELPOS) {
                JackrabbitNode node = (JackrabbitNode) n;
                node.rename(name.toUpperCase());
                renamedNode = n;
            }
            pos++;
        }

        it = testRootNode.getNodes();
        pos = 0;
        while (it.hasNext()) {
            Node n = it.nextNode();
            String name = n.getName();
            assertEquals(name, new String(new char[]{SEQ_AFTER.charAt(pos)}));
            if (pos == RELPOS) {
                assertTrue(n.isSame(renamedNode));
            }
            pos++;
        }
    }

    public void testSetMixins() throws RepositoryException {
        // create node with mixin test:AA
        Node n = testRootNode.addNode("foo", "nt:folder");
        n.addMixin("test:AA");
        n.setProperty("test:propAA", "AA");
        n.setProperty("test:propA", "A");
        superuser.save();

        // 'downgrade' from test:AA to test:A
        ((JackrabbitNode) n).setMixins(new String[]{"test:A"});
        superuser.save();

        assertTrue(n.hasProperty("test:propA"));
        assertFalse(n.hasProperty("test:propAA"));

        // 'upgrade' from test:A to test:AA
        ((JackrabbitNode) n).setMixins(new String[]{"test:AA"});
        n.setProperty("test:propAA", "AA");
        superuser.save();

        assertTrue(n.hasProperty("test:propA"));
        assertTrue(n.hasProperty("test:propAA"));

        // replace test:AA with mix:title
        ((JackrabbitNode) n).setMixins(new String[]{"mix:title"});
        n.setProperty("jcr:title", "...");
        n.setProperty("jcr:description", "blah blah");
        superuser.save();

        assertTrue(n.hasProperty("jcr:title"));
        assertTrue(n.hasProperty("jcr:description"));
        assertFalse(n.hasProperty("test:propA"));
        assertFalse(n.hasProperty("test:propAA"));

        // clean up
        n.remove();
        superuser.save();
    }
}

@@ -936,16 +936,79 @@
 JCRTest;
+%0Aimport org.apache.jackrabbit.test.api.observation.EventResult;
 %0A%0Aimport
@@ -1093,16 +1093,127 @@
 eption;%0A
+import javax.jcr.Session;%0Aimport javax.jcr.observation.Event;%0Aimport javax.jcr.observation.ObservationManager;%0A
 import j
@@ -1340,17 +1340,16 @@
 odeTest 
- 
 extends 
@@ -1599,16 +1599,30 @@
 .cnd%22;%0A%0A
+    @Override%0A
     prot
@@ -1874,32 +1874,58 @@
 %7Bc%7D));%0A        %7D
+%0A        superuser.save();
 %0A%0A        Reader
@@ -2998,32 +2998,32 @@
 ;%0A            %7D%0A
-
             pos+
@@ -3034,32 +3034,1197 @@
        %7D%0A    %7D%0A%0A
+    public void testRenameEventHandling() throws RepositoryException %7B%0A        Session s = getHelper().getSuperuserSession();%0A        ObservationManager mgr = s.getWorkspace().getObservationManager();%0A        EventResult result = new EventResult(log);%0A%0A        try %7B%0A            mgr.addEventListener(result, Event.PERSIST%7CEvent.NODE_ADDED%7CEvent.NODE_MOVED%7CEvent.NODE_REMOVED, testRootNode.getPath(), true, null, null, false);%0A%0A            NodeIterator it = testRootNode.getNodes();%0A%0A            Node n = it.nextNode();%0A            String name = n.getName();%0A%0A            JackrabbitNode node = (JackrabbitNode) n;%0A            node.rename(name.toUpperCase());%0A            superuser.save();%0A%0A            boolean foundMove = false;%0A            for (Event event : result.getEvents(5000)) %7B%0A                if (Event.NODE_MOVED == event.getType()) %7B%0A                    foundMove = true;%0A                    break;%0A                %7D%0A            %7D%0A%0A            if (!foundMove) %7B%0A                fail(%22Expected NODE_MOVED event upon renaming a node.%22);%0A            %7D%0A        %7D finally %7B%0A            mgr.removeEventListener(result);%0A            s.logout();%0A        %7D%0A    %7D%0A%0A
     public void 

test if JackrabbitNode.rename triggers NODE_MOVED event