You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by an...@apache.org on 2011/11/30 13:42:39 UTC

svn commit: r1208406 - in /jackrabbit/trunk/jackrabbit-jcr-commons/src: main/java/org/apache/jackrabbit/commons/JcrUtils.java test/java/org/apache/jackrabbit/util/JcrUtilsTest.java

Author: angela
Date: Wed Nov 30 12:42:38 2011
New Revision: 1208406

URL: http://svn.apache.org/viewvc?rev=1208406&view=rev
Log:
JCR-3161 - Add JcrUtils.getPropertyTypeNames

Added:
    jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/util/JcrUtilsTest.java
Modified:
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java?rev=1208406&r1=1208405&r2=1208406&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java (original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/JcrUtils.java Wed Nov 30 12:42:38 2011
@@ -27,8 +27,10 @@ import java.net.URI;
 import java.net.URISyntaxException;
 import java.util.Calendar;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Map;
+import java.util.Set;
 import java.util.StringTokenizer;
 
 import javax.imageio.spi.ServiceRegistry;
@@ -830,11 +832,13 @@ public class JcrUtils {
         }
     }
 
+    private static final Set<String> PROPERTY_TYPES_NAMES = new HashSet<String>();
     private static final Map<String, Integer> PROPERTY_TYPES = new HashMap<String, Integer>();
-
     static {
         for (int i = PropertyType.UNDEFINED; i <= PropertyType.DECIMAL; i++) {
-            PROPERTY_TYPES.put(PropertyType.nameFromValue(i).toLowerCase(), i);
+            String typeName = PropertyType.nameFromValue(i);
+            PROPERTY_TYPES_NAMES.add(typeName);
+            PROPERTY_TYPES.put(typeName.toLowerCase(), i);
         }
     }
 
@@ -861,6 +865,29 @@ public class JcrUtils {
     }
 
     /**
+     * Return the property type names including or excluding 'undefined' depending
+     * on the specified flag.
+     *
+     * @param includeUndefined If true the returned array will contain the name
+     * of the 'undefined' property type.
+     * @return array of property type names.
+     */
+    public static String[] getPropertyTypeNames(boolean includeUndefined) {
+        if (includeUndefined) {
+            return PROPERTY_TYPES_NAMES.toArray(new String[PROPERTY_TYPES_NAMES.size()]);
+        } else {
+            String[] typeNames = new String[PROPERTY_TYPES_NAMES.size()-1];
+            int i = 0;
+            for (String name : PROPERTY_TYPES_NAMES) {
+                if (!PropertyType.TYPENAME_UNDEFINED.equals(name)) {
+                    typeNames[i++] = name;
+                }
+            }
+            return typeNames;
+        }
+    }
+
+    /**
      * Creates or gets the {@link javax.jcr.Node Node} at the given Path.
      * In case it has to create the Node all non-existent intermediate path-elements
      * will be created with the given NodeType.

Added: jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/util/JcrUtilsTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/util/JcrUtilsTest.java?rev=1208406&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/util/JcrUtilsTest.java (added)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/util/JcrUtilsTest.java Wed Nov 30 12:42:38 2011
@@ -0,0 +1,47 @@
+/*
+ * 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.util;
+
+import junit.framework.TestCase;
+import org.apache.jackrabbit.commons.JcrUtils;
+
+import javax.jcr.PropertyType;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * <code>JcrUtilsTest</code>...
+ */
+public class JcrUtilsTest extends TestCase {
+
+    public void testGetPropertyTypeNames() {
+        String[] names = JcrUtils.getPropertyTypeNames(true);
+        assertEquals(13, names.length);
+        Set<String> nameSet = new HashSet<String>(Arrays.asList(names));
+        assertEquals(13, nameSet.size());
+        assertTrue(nameSet.contains(PropertyType.TYPENAME_UNDEFINED));
+
+        names = JcrUtils.getPropertyTypeNames(false);
+        assertEquals(12, names.length);
+        nameSet = new HashSet<String>(Arrays.asList(names));
+        assertEquals(12, nameSet.size());
+        assertFalse(nameSet.contains(PropertyType.TYPENAME_UNDEFINED));
+    }
+
+}
\ No newline at end of file