jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ang...@apache.org
Subject svn commit: r656637 - in /jackrabbit/trunk: jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/ jackrabbit-spi-commons/src/test/java/org/apache/jackrabbit/spi/commons/nodetype/ jackrabbit-spi-commons/src/test/java/org/apache/j...
Date Thu, 15 May 2008 12:52:07 GMT
Author: angela
Date: Thu May 15 05:52:06 2008
New Revision: 656637

URL: http://svn.apache.org/viewvc?rev=656637&view=rev
Log:
JCR-1555: Add support for boolean values to QValue

Modified:
    jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/QValueFactoryImpl.java
    jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/ValueFactoryQImpl.java
    jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/ValueFormat.java
    jackrabbit/trunk/jackrabbit-spi-commons/src/test/java/org/apache/jackrabbit/spi/commons/nodetype/BooleanConstraintTest.java
    jackrabbit/trunk/jackrabbit-spi-commons/src/test/java/org/apache/jackrabbit/spi/commons/value/QValueTest.java
    jackrabbit/trunk/jackrabbit-spi/src/main/java/org/apache/jackrabbit/spi/QValue.java
    jackrabbit/trunk/jackrabbit-spi/src/main/java/org/apache/jackrabbit/spi/QValueFactory.java

Modified: jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/QValueFactoryImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/QValueFactoryImpl.java?rev=656637&r1=656636&r2=656637&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/QValueFactoryImpl.java
(original)
+++ jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/QValueFactoryImpl.java
Thu May 15 05:52:06 2008
@@ -16,40 +16,41 @@
  */
 package org.apache.jackrabbit.spi.commons.value;
 
-import org.apache.jackrabbit.spi.commons.name.NameConstants;
-import org.apache.jackrabbit.spi.commons.name.PathFactoryImpl;
-import org.apache.jackrabbit.spi.commons.name.NameFactoryImpl;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.OutputStream;
+import java.io.Serializable;
+import java.io.UnsupportedEncodingException;
+import java.util.Arrays;
+import java.util.Calendar;
+import java.util.TimeZone;
+
+import javax.jcr.PropertyType;
+import javax.jcr.RepositoryException;
+import javax.jcr.ValueFormatException;
+
 import org.apache.jackrabbit.spi.Name;
+import org.apache.jackrabbit.spi.NameFactory;
 import org.apache.jackrabbit.spi.Path;
+import org.apache.jackrabbit.spi.PathFactory;
 import org.apache.jackrabbit.spi.QPropertyDefinition;
 import org.apache.jackrabbit.spi.QValue;
 import org.apache.jackrabbit.spi.QValueFactory;
-import org.apache.jackrabbit.spi.PathFactory;
-import org.apache.jackrabbit.spi.NameFactory;
-import org.apache.jackrabbit.util.TransientFileFactory;
+import org.apache.jackrabbit.spi.commons.name.NameConstants;
+import org.apache.jackrabbit.spi.commons.name.NameFactoryImpl;
+import org.apache.jackrabbit.spi.commons.name.PathFactoryImpl;
 import org.apache.jackrabbit.util.ISO8601;
+import org.apache.jackrabbit.util.TransientFileFactory;
 import org.apache.jackrabbit.uuid.UUID;
 
-import javax.jcr.RepositoryException;
-import javax.jcr.ValueFormatException;
-import javax.jcr.PropertyType;
-import java.util.Calendar;
-import java.util.Arrays;
-import java.util.TimeZone;
-import java.io.InputStream;
-import java.io.IOException;
-import java.io.File;
-import java.io.ByteArrayInputStream;
-import java.io.UnsupportedEncodingException;
-import java.io.OutputStream;
-import java.io.FileOutputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.Serializable;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
-
 /**
  * <code>QValueFactoryImpl</code>...
  */
@@ -84,7 +85,9 @@
         try {
             switch (type) {
                 case PropertyType.BOOLEAN:
-                    return new QValueImpl(Boolean.valueOf(value));
+                    return (Boolean.valueOf(value).booleanValue()) ?
+                            QValueImpl.TRUE :
+                            QValueImpl.FALSE;
                 case PropertyType.DATE: {
                         Calendar cal = ISO8601.parse(value);
                         if (cal == null) {
@@ -144,6 +147,17 @@
     }
 
     /**
+     * @see QValueFactory#create(boolean)
+     */
+    public QValue create(boolean value) {
+        if (value) {
+            return QValueImpl.TRUE;
+        } else {
+            return QValueImpl.FALSE;
+        }
+    }
+
+    /**
      * @see QValueFactory#create(Name)
      */
     public QValue create(Name value) {
@@ -219,6 +233,10 @@
      */
     private static class QValueImpl implements QValue, Serializable {
 
+        private static final QValue TRUE = new QValueImpl(Boolean.TRUE);
+
+        private static final QValue FALSE = new QValueImpl(Boolean.FALSE);
+
         private final Object val;
         private final int type;
 
@@ -371,6 +389,18 @@
         }
 
         /**
+         * @throws RepositoryException
+         * @see QValue#getBoolean()
+         */
+        public boolean getBoolean() throws RepositoryException {
+            if (type == PropertyType.BOOLEAN) {
+                return ((Boolean) val).booleanValue();
+            } else {
+                return Boolean.valueOf(getString()).booleanValue();
+            }
+        }
+
+        /**
          * @see QValue#getPath()
          */
         public Path getPath() throws RepositoryException {
@@ -425,6 +455,7 @@
         public int hashCode() {
             return val.hashCode();
         }
+
     }
 
     //--------------------------------------------------------< Inner Class >---
@@ -744,6 +775,13 @@
         }
 
         /**
+         * @see QValue#getBoolean()
+         */
+        public boolean getBoolean() throws RepositoryException {
+            return Boolean.valueOf(getString()).booleanValue();
+        }
+
+        /**
          * @see QValue#getPath()
          */
         public Path getPath() throws RepositoryException {
@@ -903,5 +941,7 @@
             // deserialized value is always temp
             temp = true;
         }
+
     }
-}
\ No newline at end of file
+
+}

Modified: jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/ValueFactoryQImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/ValueFactoryQImpl.java?rev=656637&r1=656636&r2=656637&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/ValueFactoryQImpl.java
(original)
+++ jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/ValueFactoryQImpl.java
Thu May 15 05:52:06 2008
@@ -114,7 +114,7 @@
      */
     public Value createValue(boolean value) {
         try {
-            QValue qvalue = qfactory.create(Boolean.toString(value), PropertyType.BOOLEAN);
+            QValue qvalue = qfactory.create(value);
             return new QValueValue(qvalue, resolver);
         } catch (RepositoryException ex) {
             throw new RuntimeException(ex);

Modified: jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/ValueFormat.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/ValueFormat.java?rev=656637&r1=656636&r2=656637&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/ValueFormat.java
(original)
+++ jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/ValueFormat.java
Thu May 15 05:52:06 2008
@@ -148,13 +148,11 @@
                                     ValueFactory factory) throws RepositoryException {
         if (factory instanceof ValueFactoryQImpl) {
             return ((ValueFactoryQImpl)factory).createValue(qualifiedValue);
-        }
-        else {
+        } else {
             Value jcrValue;
             int propertyType = qualifiedValue.getType();
             switch (propertyType) {
                 case PropertyType.STRING:
-                case PropertyType.BOOLEAN:
                 case PropertyType.REFERENCE:
                     jcrValue = factory.createValue(qualifiedValue.getString(), propertyType);
                     break;
@@ -166,6 +164,9 @@
                     Name qName = qualifiedValue.getName();
                     jcrValue = factory.createValue(resolver.getJCRName(qName), propertyType);
                     break;
+                case PropertyType.BOOLEAN:
+                    jcrValue = factory.createValue(qualifiedValue.getBoolean());
+                    break;
                 case PropertyType.BINARY:
                     jcrValue = factory.createValue(qualifiedValue.getStream());
                     break;
@@ -173,8 +174,8 @@
                     jcrValue = factory.createValue(qualifiedValue.getCalendar());
                     break;
                 case PropertyType.DOUBLE:
-                  jcrValue = factory.createValue(qualifiedValue.getDouble());
-                  break;
+                    jcrValue = factory.createValue(qualifiedValue.getDouble());
+                    break;
                 case PropertyType.LONG:
                     jcrValue = factory.createValue(qualifiedValue.getLong());
                     break;

Modified: jackrabbit/trunk/jackrabbit-spi-commons/src/test/java/org/apache/jackrabbit/spi/commons/nodetype/BooleanConstraintTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-spi-commons/src/test/java/org/apache/jackrabbit/spi/commons/nodetype/BooleanConstraintTest.java?rev=656637&r1=656636&r2=656637&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-spi-commons/src/test/java/org/apache/jackrabbit/spi/commons/nodetype/BooleanConstraintTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-spi-commons/src/test/java/org/apache/jackrabbit/spi/commons/nodetype/BooleanConstraintTest.java
Thu May 15 05:52:06 2008
@@ -57,8 +57,8 @@
 
     protected QValue[] createNonMatchingValues() throws RepositoryException {
         return new QValue[] {
-                valueFactory.create(Boolean.FALSE.toString(), PropertyType.BOOLEAN),
-                valueFactory.create(Boolean.TRUE.toString(), PropertyType.BOOLEAN)
+                valueFactory.create(Boolean.FALSE.booleanValue()),
+                valueFactory.create(Boolean.TRUE.booleanValue())
         };
     }
 
@@ -68,9 +68,9 @@
 
     public void testTrueConstraint() throws RepositoryException, ConstraintViolationException
{
         ValueConstraint vc = new BooleanConstraint(Boolean.TRUE.toString());
-        vc.check(valueFactory.create(Boolean.TRUE.toString(), PropertyType.BOOLEAN));
+        vc.check(valueFactory.create(true));
         try {
-           vc.check(valueFactory.create(Boolean.FALSE.toString(), PropertyType.BOOLEAN));
+            vc.check(valueFactory.create(false));
             fail();
         } catch (ConstraintViolationException e) {
             // ok

Modified: jackrabbit/trunk/jackrabbit-spi-commons/src/test/java/org/apache/jackrabbit/spi/commons/value/QValueTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-spi-commons/src/test/java/org/apache/jackrabbit/spi/commons/value/QValueTest.java?rev=656637&r1=656636&r2=656637&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-spi-commons/src/test/java/org/apache/jackrabbit/spi/commons/value/QValueTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-spi-commons/src/test/java/org/apache/jackrabbit/spi/commons/value/QValueTest.java
Thu May 15 05:52:06 2008
@@ -16,34 +16,34 @@
  */
 package org.apache.jackrabbit.spi.commons.value;
 
-import junit.framework.TestCase;
+import java.io.BufferedInputStream;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.OutputStream;
+import java.util.Arrays;
+import java.util.Calendar;
 
 import javax.jcr.PropertyType;
 import javax.jcr.RepositoryException;
 import javax.jcr.ValueFormatException;
 
-import java.util.Calendar;
-import java.util.Arrays;
-import java.io.InputStream;
-import java.io.IOException;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.OutputStream;
-import java.io.FileInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.ObjectOutputStream;
-import java.io.ByteArrayInputStream;
-import java.io.ObjectInputStream;
-import java.io.BufferedInputStream;
+import junit.framework.TestCase;
 
-import org.apache.jackrabbit.util.ISO8601;
-import org.apache.jackrabbit.uuid.UUID;
 import org.apache.jackrabbit.spi.Name;
 import org.apache.jackrabbit.spi.Path;
 import org.apache.jackrabbit.spi.QValue;
 import org.apache.jackrabbit.spi.QValueFactory;
 import org.apache.jackrabbit.spi.commons.name.NameConstants;
 import org.apache.jackrabbit.spi.commons.name.PathFactoryImpl;
+import org.apache.jackrabbit.util.ISO8601;
+import org.apache.jackrabbit.uuid.UUID;
 
 /**
  * <code>QValueTest</code>...
@@ -66,6 +66,7 @@
     }
 
     //-------------------------------------------------------------< DOUBLE >---
+
     public void testCreateInvalidDoubleValue() throws RepositoryException {
         try {
             factory.create("any", PropertyType.DOUBLE);
@@ -77,7 +78,7 @@
 
     public void testGetDoubleOnBooleanValue() throws RepositoryException {
         try {
-            QValue v = factory.create(Boolean.TRUE.toString(), PropertyType.BOOLEAN);
+            QValue v = factory.create(true);
             v.getDouble();
             fail("'true' cannot be converted to a valid double value.");
         } catch (ValueFormatException e) {
@@ -98,7 +99,7 @@
 
     public void testGetLongOnBooleanValue() throws RepositoryException {
         try {
-            QValue v = factory.create(Boolean.TRUE.toString(), PropertyType.BOOLEAN);
+            QValue v = factory.create(true);
             v.getLong();
             fail("'true' cannot be converted to a valid long value.");
         } catch (ValueFormatException e) {
@@ -106,6 +107,71 @@
         }
     }
 
+    //------------------------------------------------------------< BOOLEAN >---
+    /**
+     * QValueImpl has a final static constant for the TRUE and the FALSE boolean
+     * values. Test if the various create methods use the constants (thus always
+     * return the 'same' object.
+     *
+     * @throws RepositoryException
+     */
+    public void testFinalBooleanValue() throws RepositoryException {
+        assertSame(factory.create(true), factory.create(Boolean.TRUE.toString(), PropertyType.BOOLEAN));
+        assertSame(factory.create(true), factory.create(true));
+
+        assertSame(factory.create(false), factory.create(Boolean.FALSE.toString(), PropertyType.BOOLEAN));
+        assertSame(factory.create(false), factory.create(false));
+    }
+
+    /**
+     * Test if creating Boolean QValue from boolean and from String with boolean
+     * type return equal objects.
+     * 
+     * @throws RepositoryException
+     */
+    public void testCreateBooleanValueFromString() throws RepositoryException {
+        QValue v = factory.create(Boolean.TRUE.toString(), PropertyType.BOOLEAN);
+        assertEquals("Creating boolean type QValue from boolean or String must be equal.",
+                factory.create(true), v);
+
+        v = factory.create(Boolean.FALSE.toString(), PropertyType.BOOLEAN);
+        assertEquals("Creating boolean type QValue from boolean or String must be equal.",
+                factory.create(false), v);
+    }
+
+    public void testCreateTrueBooleanValue() throws RepositoryException {
+        QValue v = factory.create(true);
+        assertEquals("Boolean value must be true", Boolean.TRUE.toString(), v.getString());
+        assertEquals("Boolean value must be true", true, v.getBoolean());
+    }
+
+    public void testCreateFalseBooleanValue() throws RepositoryException {
+        QValue v = factory.create(false);
+        assertEquals("Boolean value must be false", Boolean.FALSE.toString(), v.getString());
+        assertEquals("Boolean value must be false", false, v.getBoolean());
+    }
+
+    public void testCreateTrueFromString() throws ValueFormatException, RepositoryException
{
+        QValue v = factory.create(Boolean.TRUE.toString(), PropertyType.STRING);
+        assertEquals("Boolean value must be true", true, v.getBoolean());
+    }
+
+    public void testCreateFalseFromString() throws ValueFormatException, RepositoryException
{
+        QValue v = factory.create("any", PropertyType.STRING);
+        assertEquals("Boolean value must be false", false, v.getBoolean());
+    }
+
+    public void testReadBooleanAsLong() throws RepositoryException {
+        try {
+            QValue v = factory.create(true);
+            v.getLong();
+        }
+        catch (ValueFormatException e) {
+            return; // ok
+        }
+        assertTrue("Cannot convert value to long", false);
+    }
+
     //---------------------------------------------------------------< DATE >---
     public void testNullDateValue() throws IOException, RepositoryException {
         try {
@@ -145,6 +211,7 @@
     }
 
     //----------------------------------------------------------< REFERENCE >---
+
     public void testNullReferenceValue() throws IOException, RepositoryException {
         try {
             factory.create(null, PropertyType.REFERENCE);
@@ -173,6 +240,7 @@
 
 
     //---------------------------------------------------------------< Name >---
+
     public void testNullNameValue() throws IOException, RepositoryException {
         try {
             factory.create((Name) null);
@@ -224,6 +292,7 @@
     }
 
     //---------------------------------------------------------------< Path >---
+
     public void testNullPathValue() throws IOException, RepositoryException {
         try {
             factory.create((Path) null);
@@ -274,7 +343,9 @@
             // ok
         }
     }
+
     //-------------------------------------------------------------< BINARY >---
+
     public void testNullBinaryValue() throws IOException, RepositoryException {
         try {
             factory.create((byte[]) null);
@@ -347,4 +418,4 @@
             serValue.discard();
         }
     }
-}
\ No newline at end of file
+}

Modified: jackrabbit/trunk/jackrabbit-spi/src/main/java/org/apache/jackrabbit/spi/QValue.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-spi/src/main/java/org/apache/jackrabbit/spi/QValue.java?rev=656637&r1=656636&r2=656637&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-spi/src/main/java/org/apache/jackrabbit/spi/QValue.java (original)
+++ jackrabbit/trunk/jackrabbit-spi/src/main/java/org/apache/jackrabbit/spi/QValue.java Thu
May 15 05:52:06 2008
@@ -16,12 +16,13 @@
  */
 package org.apache.jackrabbit.spi;
 
-import javax.jcr.RepositoryException;
-import javax.jcr.PropertyType;
-import javax.jcr.Property;
 import java.io.InputStream;
 import java.util.Calendar;
 
+import javax.jcr.Property;
+import javax.jcr.PropertyType;
+import javax.jcr.RepositoryException;
+
 /**
  * <code>QValue</code> is the qualified SPI representation of a
  * {@link javax.jcr.Value jcr value}. It therefore refers to <code>Name</code>s
@@ -107,6 +108,15 @@
     public long getLong() throws RepositoryException;
 
     /**
+     * Returns a <code>boolean</code> representation of this value.
+     *
+     * @return A <code>boolean</code> representation of this value.
+     * @throws RepositoryException if an error occurs.
+     */
+    public boolean getBoolean() throws RepositoryException;
+
+
+    /**
      * Returns a <code>Name</code> representation of this value.
      *
      * @return A <code>Name</code> representation of this value.

Modified: jackrabbit/trunk/jackrabbit-spi/src/main/java/org/apache/jackrabbit/spi/QValueFactory.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-spi/src/main/java/org/apache/jackrabbit/spi/QValueFactory.java?rev=656637&r1=656636&r2=656637&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-spi/src/main/java/org/apache/jackrabbit/spi/QValueFactory.java
(original)
+++ jackrabbit/trunk/jackrabbit-spi/src/main/java/org/apache/jackrabbit/spi/QValueFactory.java
Thu May 15 05:52:06 2008
@@ -16,8 +16,8 @@
  */
 package org.apache.jackrabbit.spi;
 
-import java.io.IOException;
 import java.io.File;
+import java.io.IOException;
 import java.io.InputStream;
 import java.util.Calendar;
 
@@ -73,6 +73,15 @@
     public QValue create(long value) throws RepositoryException;
 
     /**
+     * Create a new <code>QValue</code> with type {@link javax.jcr.PropertyType#BOOLEAN}.
+     *
+     * @param value A <code>boolean</code> containing the value
+     * of the new <code>QValue</code>.
+     * @return a new <code>QValue</code>.
+     */
+    public QValue create(boolean value) throws RepositoryException;
+
+    /**
      * Create a new <code>QValue</code> with type {@link javax.jcr.PropertyType#NAME}.
      *
      * @param value A non-null <code>Name</code>.



Mime
View raw message