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 2006/12/06 16:50:25 UTC

svn commit: r483124 - in /jackrabbit/trunk/contrib/spi/commons: ./ src/main/java/org/apache/jackrabbit/value/ src/test/ src/test/java/ src/test/java/org/ src/test/java/org/apache/ src/test/java/org/apache/jackrabbit/ src/test/java/org/apache/jackrabbit...

Author: angela
Date: Wed Dec  6 07:50:24 2006
New Revision: 483124

URL: http://svn.apache.org/viewvc?view=rev&rev=483124
Log:
work in progress

- QName.create: only partial check for 'null' value
- QName.equals: fixed for values of type DATE
- QName.equals: also compare type

TODO: check hashCode() again.

Added:
    jackrabbit/trunk/contrib/spi/commons/src/test/
    jackrabbit/trunk/contrib/spi/commons/src/test/java/
    jackrabbit/trunk/contrib/spi/commons/src/test/java/org/
    jackrabbit/trunk/contrib/spi/commons/src/test/java/org/apache/
    jackrabbit/trunk/contrib/spi/commons/src/test/java/org/apache/jackrabbit/
    jackrabbit/trunk/contrib/spi/commons/src/test/java/org/apache/jackrabbit/value/
    jackrabbit/trunk/contrib/spi/commons/src/test/java/org/apache/jackrabbit/value/QValueTest.java   (with props)
Modified:
    jackrabbit/trunk/contrib/spi/commons/project.xml
    jackrabbit/trunk/contrib/spi/commons/src/main/java/org/apache/jackrabbit/value/QValue.java

Modified: jackrabbit/trunk/contrib/spi/commons/project.xml
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/spi/commons/project.xml?view=diff&rev=483124&r1=483123&r2=483124
==============================================================================
--- jackrabbit/trunk/contrib/spi/commons/project.xml (original)
+++ jackrabbit/trunk/contrib/spi/commons/project.xml Wed Dec  6 07:50:24 2006
@@ -42,6 +42,12 @@
             <version>${jackrabbit.build.version.jcr}</version>
             <url>http://jcp.org/en/jsr/detail?id=170</url>
         </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>3.8.1</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <!-- ====================================================================== -->

Modified: jackrabbit/trunk/contrib/spi/commons/src/main/java/org/apache/jackrabbit/value/QValue.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/spi/commons/src/main/java/org/apache/jackrabbit/value/QValue.java?view=diff&rev=483124&r1=483123&r2=483124
==============================================================================
--- jackrabbit/trunk/contrib/spi/commons/src/main/java/org/apache/jackrabbit/value/QValue.java (original)
+++ jackrabbit/trunk/contrib/spi/commons/src/main/java/org/apache/jackrabbit/value/QValue.java Wed Dec  6 07:50:24 2006
@@ -79,7 +79,10 @@
      * @return
      */
     public static QValue create(String value) {
-        return new QValue(value);
+        if (value == null) {
+            throw new IllegalArgumentException("Cannot create QValue from null value.");
+        }
+        return new QValue(value, PropertyType.STRING);
     }
 
     /**
@@ -89,7 +92,7 @@
     public static QValue[] create(String[] values) {
         QValue[] ret = new QValue[values.length];
         for (int i = 0; i < values.length; i++) {
-            ret[i] = new QValue(values[i]);
+            ret[i] = create(values[i]);
         }
         return ret;
     }
@@ -113,13 +116,13 @@
                 return new QValue(Long.valueOf(value).longValue());
             case PropertyType.REFERENCE:
                 // NOTE: references are not forced to represent a UUID object
-                return new QValue(value, true);
+                return new QValue(value, PropertyType.REFERENCE);
             case PropertyType.PATH:
                 return new QValue(Path.valueOf(value));
             case PropertyType.NAME:
                 return new QValue(QName.valueOf(value));
             case PropertyType.STRING:
-                return new QValue(value);
+                return new QValue(value, PropertyType.STRING);
             case PropertyType.BINARY:
                 throw new IllegalArgumentException("this method does not support the type PropertyType.BINARY");
             default:
@@ -160,7 +163,7 @@
      * @return
      */
     public static QValue create(Calendar value) {
-        return new QValue(value);
+        return new QValue(ISO8601.format(value), PropertyType.DATE);
     }
 
     /**
@@ -176,6 +179,9 @@
      * @return
      */
     public static QValue create(byte[] value) {
+        if (value == null) {
+            throw new IllegalArgumentException("Cannot create QValue from null value.");
+        }
         return new QValue(new BLOBFileValue(value));
     }
 
@@ -185,6 +191,9 @@
      * @throws IOException
      */
     public static QValue create(InputStream value) throws IOException {
+        if (value == null) {
+            throw new IllegalArgumentException("Cannot create QValue from null value.");
+        }
         return new QValue(new BLOBFileValue(value));
     }
 
@@ -201,7 +210,7 @@
         switch (type) {
             case PropertyType.BINARY:
                 return new QValue(new BLOBFileValue(value));
-            
+
             case PropertyType.BOOLEAN:
             case PropertyType.DATE:
             case PropertyType.DOUBLE:
@@ -253,6 +262,9 @@
      * @throws IOException
      */
     public static QValue create(InputStream value, boolean temp) throws IOException {
+        if (value == null) {
+            throw new IllegalArgumentException("Cannot create QValue from null value.");
+        }
         return new QValue(new BLOBFileValue(value, temp));
     }
 
@@ -262,6 +274,9 @@
      * @throws IOException
      */
     public static QValue create(File value) throws IOException {
+        if (value == null) {
+            throw new IllegalArgumentException("Cannot create QValue from null value.");
+        }
         return new QValue(new BLOBFileValue(value));
     }
 
@@ -270,6 +285,9 @@
      * @return
      */
     public static QValue create(QName value) {
+        if (value == null) {
+            throw new IllegalArgumentException("Cannot create QValue from null value.");
+        }
         return new QValue(value);
     }
 
@@ -280,7 +298,7 @@
     public static QValue[] create(QName[] values) {
         QValue[] ret = new QValue[values.length];
         for (int i = 0; i < values.length; i++) {
-            ret[i] = new QValue(values[i]);
+            ret[i] = create(values[i]);
         }
         return ret;
     }
@@ -290,6 +308,9 @@
      * @return
      */
     public static QValue create(Path value) {
+        if (value == null) {
+            throw new IllegalArgumentException("Cannot create QValue from null value.");
+        }
         return new QValue(value);
     }
 
@@ -298,6 +319,9 @@
      * @return
      */
     public static QValue create(UUID value) {
+        if (value == null) {
+            throw new IllegalArgumentException("Cannot create QValue from null value.");
+        }
         return new QValue(value);
     }
 
@@ -314,9 +338,7 @@
      * @return
      */
     public String getString() throws RepositoryException {
-        if (type == PropertyType.DATE) {
-            return ISO8601.format((Calendar) val);
-        } else if (type == PropertyType.BINARY) {
+        if (type == PropertyType.BINARY) {
             return ((BLOBFileValue) val).getString();
         } else {
             return val.toString();
@@ -392,19 +414,19 @@
             case PropertyType.BOOLEAN:
                 return new QValue(((Boolean) val).booleanValue());
             case PropertyType.DATE:
-                return new QValue((Calendar) val);
+                return new QValue((String) val, PropertyType.DATE);
             case PropertyType.DOUBLE:
                 return new QValue(((Double) val).doubleValue());
             case PropertyType.LONG:
                 return new QValue(((Long) val).longValue());
             case PropertyType.REFERENCE:
-                return new QValue((String) val, true);
+                return new QValue((String) val, PropertyType.REFERENCE);
             case PropertyType.PATH:
                 return new QValue((Path) val);
             case PropertyType.NAME:
                 return new QValue((QName) val);
             case PropertyType.STRING:
-                return new QValue((String) val);
+                return new QValue((String) val, PropertyType.STRING);
             default:
                 throw new RepositoryException("Illegal internal value type");
         }
@@ -418,11 +440,7 @@
      * @return string representation of this internal value
      */
     public String toString() {
-        if (type == PropertyType.DATE) {
-            return ISO8601.format((Calendar) val);
-        } else {
-            return val.toString();
-        }
+        return val.toString();
     }
 
     /**
@@ -437,7 +455,7 @@
         }
         if (obj instanceof QValue) {
             QValue other = (QValue) obj;
-            return val.equals(other.val);
+            return val.equals(other.val) && type == other.type;
         }
         return false;
     }
@@ -450,15 +468,10 @@
         return val.hashCode();
     }
 
-    //-------------------------------------------------------< implementation >
-    private QValue(String value) {
+    //-----------------------------------------------------< implementation >---
+    private QValue(String value, int type) {
         val = value;
-        type = PropertyType.STRING;
-    }
-
-    private QValue(String value, boolean isReference) {
-        val = value;
-        type = (isReference) ? PropertyType.REFERENCE : PropertyType.STRING;
+        this.type = type;
     }
 
     private QValue(QName value) {
@@ -477,7 +490,7 @@
     }
 
     private QValue(Calendar value) {
-        val = value;
+        val = ISO8601.format(value);
         type = PropertyType.DATE;
     }
 
@@ -498,7 +511,7 @@
 
     private QValue(UUID value) {
         // NOTE: reference value must not represent a UUID object
-        val = (value == null) ? null : value.toString();
+        val = value.toString();
         type = PropertyType.REFERENCE;
     }
 
@@ -679,9 +692,6 @@
          * Frees temporarily allocated resources such as temporary file, buffer, etc.
          * If this <code>BLOBFileValue</code> is backed by a persistent resource
          * calling this method will have no effect.
-         *
-         * @see #delete()
-         * @see #delete(boolean)
          */
         private void discard() {
             if (!temp) {
@@ -699,29 +709,6 @@
         }
 
         /**
-         * Deletes the persistent resource backing this <code>BLOBFileValue</code>.
-         *
-         * @param pruneEmptyParentDirs if <code>true</code>, empty parent directories
-         *                             will automatically be deleted
-         */
-        private void delete(boolean pruneEmptyParentDirs) {
-            if (file != null) {
-                // this instance is backed by a 'real' file
-                file.delete();
-                if (pruneEmptyParentDirs) {
-                    // prune empty parent directories
-                    File parent = file.getParentFile();
-                    while (parent != null && parent.delete()) {
-                        parent = parent.getParentFile();
-                    }
-                }
-            } else {
-                // this instance is backed by an in-memory buffer
-                buffer = EMPTY_BYTE_ARRAY;
-            }
-        }
-
-        /**
          *
          * @return
          * @throws ValueFormatException
@@ -858,4 +845,4 @@
             return 0;
         }
     }
-}
+}
\ No newline at end of file

Added: jackrabbit/trunk/contrib/spi/commons/src/test/java/org/apache/jackrabbit/value/QValueTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/spi/commons/src/test/java/org/apache/jackrabbit/value/QValueTest.java?view=auto&rev=483124
==============================================================================
--- jackrabbit/trunk/contrib/spi/commons/src/test/java/org/apache/jackrabbit/value/QValueTest.java (added)
+++ jackrabbit/trunk/contrib/spi/commons/src/test/java/org/apache/jackrabbit/value/QValueTest.java Wed Dec  6 07:50:24 2006
@@ -0,0 +1,231 @@
+/*
+ * 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.value;
+
+import junit.framework.TestCase;
+
+import javax.jcr.PropertyType;
+import javax.jcr.RepositoryException;
+import java.util.Calendar;
+import java.io.InputStream;
+import java.io.IOException;
+import java.io.File;
+
+import org.apache.jackrabbit.util.ISO8601;
+import org.apache.jackrabbit.uuid.UUID;
+import org.apache.jackrabbit.name.QName;
+import org.apache.jackrabbit.name.Path;
+
+/**
+ * <code>QValueTest</code>...
+ */
+public class QValueTest extends TestCase {
+
+    private final Calendar CALENDAR = Calendar.getInstance();
+    private static final String REFERENCE = UUID.randomUUID().toString();
+
+    //---------------------------------------------------------------< DATE >---
+    public void testNullDateValue() throws IOException {
+        try {
+            QValue.create((Calendar) null);
+            fail();
+        } catch (IllegalArgumentException e) {
+            // ok
+        }
+        try {
+            QValue.create((String) null, PropertyType.DATE);
+            fail();
+        } catch (IllegalArgumentException e) {
+            // ok
+        }
+        try {
+            QValue.create(new String[] {null}, PropertyType.DATE);
+            fail();
+        } catch (IllegalArgumentException e) {
+            // ok
+        }
+        try {
+            QValue.create((InputStream) null, PropertyType.DATE);
+            fail();
+        } catch (IllegalArgumentException e) {
+            // ok
+        }
+        try {
+            QValue.create(new InputStream[] {null}, PropertyType.DATE);
+            fail();
+        } catch (IllegalArgumentException e) {
+            // ok
+        }
+    }
+
+    public void testDateValueType() {
+        QValue v = QValue.create(CALENDAR);
+        assertTrue("Type of a date value must be PropertyType.DATE", v.getType() == PropertyType.DATE);
+    }
+    public void testDateValueEquality() {
+        QValue v = QValue.create(CALENDAR);
+        QValue otherV = QValue.create(CALENDAR);
+        assertEquals("Equality of qualified date value must be calculated based on their String representation.", v, otherV);
+    }
+
+    public void testDateValueEquality2() throws RepositoryException {
+        QValue v = QValue.create(CALENDAR);
+        QValue otherV = QValue.create(v.getString(), PropertyType.DATE);
+        assertEquals("Equality of qualified date value must be calculated based on their String representation.", v, otherV);
+    }
+
+    public void testDateValueStringRepresentation() throws RepositoryException {
+        QValue v = QValue.create(CALENDAR);
+        String s = ISO8601.format(CALENDAR);
+        assertEquals("Expected String representation of qualified date value to be ISO8601 compliant.", s, v.getString());
+    }
+
+    public void testDateValueCopy() throws RepositoryException {
+        QValue v = QValue.create(CALENDAR);
+        QValue copy = v.createCopy();
+        assertTrue(copy.getType() == PropertyType.DATE);
+        assertNotSame(v, copy);
+        assertEquals(v, copy);
+    }
+
+    //----------------------------------------------------------< REFERENCE >---
+    public void testNullReferenceValue() throws IOException {
+        try {
+            QValue.create((UUID) null);
+            fail();
+        } catch (IllegalArgumentException e) {
+            // ok
+        }
+        try {
+            QValue.create((String) null, PropertyType.REFERENCE);
+            fail();
+        } catch (IllegalArgumentException e) {
+            // ok
+        }
+        try {
+            QValue.create(new String[] {null}, PropertyType.REFERENCE);
+            fail();
+        } catch (IllegalArgumentException e) {
+            // ok
+        }
+        try {
+            QValue.create((InputStream) null, PropertyType.REFERENCE);
+            fail();
+        } catch (IllegalArgumentException e) {
+            // ok
+        }
+        try {
+            QValue.create(new InputStream[] {null}, PropertyType.REFERENCE);
+            fail();
+        } catch (IllegalArgumentException e) {
+            // ok
+        }
+    }
+
+    public void testReferenceValueType() {
+        QValue v = QValue.create(REFERENCE, PropertyType.REFERENCE);
+        assertTrue("Type of a date value must be PropertyType.REFERENCE.", v.getType() == PropertyType.REFERENCE);
+    }
+
+    public void testReferenceValueEquality() {
+        QValue v = QValue.create(REFERENCE, PropertyType.REFERENCE);
+        QValue otherV = QValue.create(REFERENCE, PropertyType.REFERENCE);
+        assertEquals("Qualified ref values created from the same string must be equal.", v, otherV);
+    }
+
+    public void testReferenceValueCopy() throws RepositoryException {
+        QValue v = QValue.create(REFERENCE, PropertyType.REFERENCE);
+        QValue copy = v.createCopy();
+        assertTrue(copy.getType() == PropertyType.REFERENCE);
+        assertNotSame(v, copy);
+        assertEquals(v, copy);
+    }
+
+    public void testEqualityDifferentTypes() {
+        QValue v = QValue.create(REFERENCE, PropertyType.REFERENCE);
+        QValue v2 = QValue.create(REFERENCE, PropertyType.STRING);
+        assertFalse(v.equals(v2));
+    }
+
+
+    //--------------------------------------------------------------< QName >---
+    public void testNullQNameValue() throws IOException {
+        try {
+            QValue.create((QName) null);
+            fail();
+        } catch (IllegalArgumentException e) {
+            // ok
+        }
+        try {
+            QValue.create(new QName[] {null});
+            fail();
+        } catch (IllegalArgumentException e) {
+            // ok
+        }
+    }
+
+    public void testQNameValueType() throws IOException {
+        QValue v = QValue.create(QName.JCR_DATA);
+        assertTrue(v.getType() == PropertyType.NAME);
+        v = QValue.create(QName.JCR_DATA.toString(), PropertyType.NAME);
+        assertTrue(v.getType() == PropertyType.NAME);
+    }
+
+    //--------------------------------------------------------------< QPath >---
+    public void testNullPathValue() throws IOException {
+        try {
+            QValue.create((Path) null);
+            fail();
+        } catch (IllegalArgumentException e) {
+            // ok
+        }
+    }
+
+    public void testPathValueType() throws IOException {
+        QValue v = QValue.create(Path.ROOT);
+        assertTrue(v.getType() == PropertyType.PATH);
+        v = QValue.create(Path.ROOT.toString(), PropertyType.PATH);
+        assertTrue(v.getType() == PropertyType.PATH);
+    }
+
+    //-------------------------------------------------------------< BINARY >---
+    public void testNullBinaryValue() throws IOException {
+        try {
+            QValue.create((byte[]) null);
+            fail();
+        } catch (IllegalArgumentException e) {
+            // ok
+        }
+        try {
+            QValue.create((InputStream) null);
+            fail();
+        } catch (IllegalArgumentException e) {
+            // ok
+        }
+        try {
+            QValue.create((File) null);
+            fail();
+        } catch (IllegalArgumentException e) {
+            // ok
+        }
+    }
+
+    public void testBinaryValueType() throws IOException {
+        QValue v = QValue.create(new byte[] {'a', 'b', 'c'});
+        assertTrue(v.getType() == PropertyType.BINARY);
+    }
+}
\ No newline at end of file

Propchange: jackrabbit/trunk/contrib/spi/commons/src/test/java/org/apache/jackrabbit/value/QValueTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jackrabbit/trunk/contrib/spi/commons/src/test/java/org/apache/jackrabbit/value/QValueTest.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision url