/*
 * 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 java.util.Arrays;
import java.util.List;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.nodetype.NodeType;
import javax.jcr.nodetype.NodeTypeIterator;
import javax.jcr.nodetype.NodeTypeManager;

import org.apache.jackrabbit.test.AbstractJCRTest;

/**
 * Utility class to locate mixins in the NodeTyeManager.
 */
public class NodeMixinUtil {

    /**
     * @return the name of a mixin node type that can be added by the requested
     *         <code>node</code>
     */
    public static String getAddableMixinName(Session session, Node node)
            throws RepositoryException {

        NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
        NodeTypeIterator mixins = manager.getMixinNodeTypes();

        // Skip mix:shareable since not supported by removeMixin
        String mixShareable = session.getNamespacePrefix(AbstractJCRTest.NS_MIX_URI) + ":shareable";

        while (mixins.hasNext()) {
            String name = mixins.nextNodeType().getName();
            if (node.canAddMixin(name) && !mixShareable.equals(name)) {
                return name;
            }
        }
        return null;
    }

    public static String getNotAssignedMixinName(Session session, Node node) throws RepositoryException {
        NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
        NodeTypeIterator mixins = manager.getMixinNodeTypes();
        List<NodeType> existingMixins = Arrays.asList(node.getMixinNodeTypes());

        while (mixins.hasNext()) {
            NodeType nt = mixins.nextNodeType();
            if (!existingMixins.contains(nt)) {
                return nt.getName();
            }
        }
        return null;
    }

    /**
     * @return a string that is not the name of a mixin type
     */
    public static String getNonExistingMixinName(Session session)
            throws RepositoryException {

        NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
        NodeTypeIterator mixins = manager.getMixinNodeTypes();
        StringBuffer s = new StringBuffer("X");
        while (mixins.hasNext()) {
            s.append(mixins.nextNodeType().getName());
        }
        return s.toString().replaceAll(":", "");
    }


}

@@ -854,22 +854,23 @@
 va.util.
-Arrays
+HashSet
 ;%0Aimport
@@ -880,19 +880,18 @@
 va.util.
-Lis
+Se
 t;%0A%0Aimpo
@@ -2293,24 +2293,25 @@
 ypes();%0A
+%0A
         
 List%3CNod
@@ -2306,21 +2306,18 @@
     
-List%3CNodeType
+Set%3CString
 %3E ex
@@ -2335,48 +2335,141 @@
 s = 
-Arrays.asList(node.getMixinNodeTypes());
+new HashSet%3CString%3E();%0A        for (NodeType nt : node.getMixinNodeTypes()) %7B%0A            existingMixins.add(nt.getName());%0A        %7D
 %0A%0A  
@@ -2513,27 +2513,29 @@
         
-NodeType nt
+String ntName
  = mixin
@@ -2550,16 +2550,26 @@
 deType()
+.getName()
 ;%0A      
@@ -2605,16 +2605,20 @@
 tains(nt
+Name
 )) %7B%0A   
@@ -2639,26 +2639,20 @@
 eturn nt
-.get
 Name
-()
 ;%0A      

Test cases should not rely on equality of node types