jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1375557 - in /jackrabbit/oak/trunk/oak-core/src: main/java/org/apache/jackrabbit/oak/kernel/CoreValueMapper.java main/java/org/apache/jackrabbit/oak/kernel/JsopDiff.java test/java/org/apache/jackrabbit/oak/kernel/CoreValueFactoryTest.java
Date Tue, 21 Aug 2012 14:12:35 GMT
Author: thomasm
Date: Tue Aug 21 14:12:35 2012
New Revision: 1375557

URL: http://svn.apache.org/viewvc?rev=1375557&view=rev
Log:
OAK-33 : Values in oak-core - avoid a public static final map, and use a getter instead; added
test case

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/CoreValueMapper.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/JsopDiff.java
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/kernel/CoreValueFactoryTest.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/CoreValueMapper.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/CoreValueMapper.java?rev=1375557&r1=1375556&r2=1375557&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/CoreValueMapper.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/CoreValueMapper.java
Tue Aug 21 14:12:35 2012
@@ -35,7 +35,7 @@ import org.apache.jackrabbit.oak.api.Cor
  */
 public class CoreValueMapper {
 
-    public static final Map<Integer, String> TYPE2HINT = new HashMap<Integer, String>();
+    private static final Map<Integer, String> TYPE2HINT = new HashMap<Integer, String>();
     private static final Map<String, Integer> HINT2TYPE = new HashMap<String, Integer>();
 
     static {
@@ -111,10 +111,19 @@ public class CoreValueMapper {
      * @param jsonString The JSON String representation of a {@code CoreValue}
      * @return {@code true} if the {@code jsonString} starts with a type
      * hint; {@code false} otherwise.
-     * @see #buildJsonStringWithHint(org.apache.jackrabbit.oak.api.CoreValue)
      */
     public static boolean startsWithHint(String jsonString) {
         return jsonString.length() >= 4 && jsonString.charAt(3) == ':';
     }
 
+    /**
+     * Get the type hint (JSON prefix) for the given property type.
+     *
+     * @param propertyType the property type
+     * @return the hint
+     */
+    public static String getHintForType(int propertyType) {
+        return TYPE2HINT.get(propertyType);
+    }
+
 }
\ No newline at end of file

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/JsopDiff.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/JsopDiff.java?rev=1375557&r1=1375556&r2=1375557&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/JsopDiff.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/JsopDiff.java
Tue Aug 21 14:12:35 2012
@@ -26,8 +26,6 @@ import org.apache.jackrabbit.oak.spi.sta
 import org.apache.jackrabbit.oak.spi.state.NodeState;
 import org.apache.jackrabbit.oak.spi.state.NodeStateDiff;
 
-import static org.apache.jackrabbit.oak.kernel.CoreValueMapper.TYPE2HINT;
-
 class JsopDiff implements NodeStateDiff {
 
     protected final JsopBuilder jsop;
@@ -91,6 +89,7 @@ class JsopDiff implements NodeStateDiff 
 
     //------------------------------------------------------------< Object >--
 
+    @Override
     public String toString() {
         return jsop.toString();
     }
@@ -136,7 +135,7 @@ class JsopDiff implements NodeStateDiff 
             String string = value.getString();
             if (type != PropertyType.STRING
                     || CoreValueMapper.startsWithHint(string)) {
-                string = TYPE2HINT.get(type) + ':' + string;
+                string = CoreValueMapper.getHintForType(type) + ':' + string;
             }
             jsop.value(string);
         }

Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/kernel/CoreValueFactoryTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/kernel/CoreValueFactoryTest.java?rev=1375557&r1=1375556&r2=1375557&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/kernel/CoreValueFactoryTest.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/kernel/CoreValueFactoryTest.java
Tue Aug 21 14:12:35 2012
@@ -22,6 +22,7 @@ import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import javax.jcr.PropertyType;
@@ -33,10 +34,12 @@ import org.apache.jackrabbit.mk.json.Jso
 import org.apache.jackrabbit.oak.api.CoreValue;
 import org.apache.jackrabbit.oak.api.CoreValueFactory;
 import org.apache.jackrabbit.oak.plugins.identifier.IdentifierManager;
+import org.apache.jackrabbit.oak.plugins.memory.MemoryValueFactory;
 import org.junit.Before;
 import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 
 /**
  * CoreValueUtilTest...
@@ -161,4 +164,59 @@ public class CoreValueFactoryTest {
         }
     }
 
+    @Test
+    public void testHints() {
+        HashSet<String> hints = new HashSet<String>();
+        for (int i = PropertyType.UNDEFINED; i <= PropertyType.DECIMAL; i++) {
+            String hint = CoreValueMapper.getHintForType(i);
+            assertTrue(hints.add(hint));
+            assertEquals(3, hint.length());
+            assertTrue(CoreValueMapper.startsWithHint(hint + ":"));
+            String def = getDefaultValue(i).getString();
+            JsopTokenizer t = new JsopTokenizer("\"" + hint + ":" + def + "\"");
+            CoreValue cv = CoreValueMapper.fromJsopReader(t,
+                    MemoryValueFactory.INSTANCE);
+            assertEquals(i, cv.getType());
+            assertEquals(def, cv.getString());
+        }
+    }
+
+    private static CoreValue getDefaultValue(int propertyType) {
+        CoreValueFactory cv = MemoryValueFactory.INSTANCE;
+        switch (propertyType) {
+        case PropertyType.STRING:
+            return cv.createValue("");
+        case PropertyType.BINARY:
+            try {
+                return cv.createValue(new ByteArrayInputStream(new byte[0]));
+            } catch (IOException e) {
+                throw new RuntimeException(e);
+            }
+        case PropertyType.DATE:
+            return cv.createValue("1970-01-01T00:00:00.0", PropertyType.DATE);
+        case PropertyType.LONG:
+            return cv.createValue(0);
+        case PropertyType.DOUBLE:
+            return cv.createValue(0.0);
+        case PropertyType.DECIMAL:
+            return cv.createValue(new BigDecimal("0"));
+        case PropertyType.BOOLEAN:
+            return cv.createValue(false);
+        case PropertyType.NAME:
+            return cv.createValue("", PropertyType.NAME);
+        case PropertyType.PATH:
+            return cv.createValue("", PropertyType.PATH);
+        case PropertyType.REFERENCE:
+            return cv.createValue("", PropertyType.REFERENCE);
+        case PropertyType.WEAKREFERENCE:
+            return cv.createValue("", PropertyType.WEAKREFERENCE);
+        case PropertyType.URI:
+            return cv.createValue("", PropertyType.URI);
+        case PropertyType.UNDEFINED:
+            return cv.createValue("", PropertyType.UNDEFINED);
+        default:
+            throw new IllegalArgumentException("type: " + propertyType);
+        }
+    }
+
 }
\ No newline at end of file



Mime
View raw message