Author: rhauch
Date: 2009-03-24 18:33:26 -0400 (Tue, 24 Mar 2009)
New Revision: 795
Added:
trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/PropertyTypeUtil.java
trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/Vehicles.java
Modified:
trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/AbstractJcrProperty.java
trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrMultiValueProperty.java
trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrSession.java
trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrSingleValueProperty.java
trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/SessionCache.java
trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/AbstractJcrPropertyTest.java
trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrSessionTest.java
trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/SessionCacheTest.java
Log:
DNA-194 Implement update JCR capability
Added (and tested) functionality to set properties and remove properties. However, none
of the TCK unit tests could be run, however, since adding nodes appears to be a
prerequisite of all of the Level 2 TCK unit tests.
Modified: trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/AbstractJcrProperty.java
===================================================================
--- trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/AbstractJcrProperty.java 2009-03-24
18:16:53 UTC (rev 794)
+++ trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/AbstractJcrProperty.java 2009-03-24
22:33:26 UTC (rev 795)
@@ -23,15 +23,12 @@
*/
package org.jboss.dna.jcr;
-import java.io.InputStream;
-import java.util.Calendar;
import javax.jcr.Item;
import javax.jcr.ItemVisitor;
import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
-import javax.jcr.Value;
import javax.jcr.nodetype.PropertyDefinition;
import net.jcip.annotations.NotThreadSafe;
import org.jboss.dna.common.util.CheckArg;
@@ -45,7 +42,7 @@
@NotThreadSafe
abstract class AbstractJcrProperty extends AbstractJcrItem implements Property {
- private final PropertyId propertyId;
+ protected final PropertyId propertyId;
AbstractJcrProperty( SessionCache cache,
PropertyId propertyId ) {
@@ -81,6 +78,11 @@
return new JcrValue(context().getValueFactories(), this.cache,
propertyInfo().getPropertyType(), value);
}
+ final JcrValue createValue( Object value,
+ int propertyType ) {
+ return new JcrValue(context().getValueFactories(), this.cache, propertyType,
value);
+ }
+
@Override
Path path() throws RepositoryException {
return cache.getPathFor(propertyInfo());
@@ -168,106 +170,6 @@
* {@inheritDoc}
*
* @throws UnsupportedOperationException always
- * @see javax.jcr.Property#setValue(javax.jcr.Value)
- */
- public final void setValue( Value value ) {
- throw new UnsupportedOperationException();
- }
-
- /**
- * {@inheritDoc}
- *
- * @throws UnsupportedOperationException always
- * @see javax.jcr.Property#setValue(javax.jcr.Value[])
- */
- public final void setValue( Value[] values ) {
- throw new UnsupportedOperationException();
- }
-
- /**
- * {@inheritDoc}
- *
- * @throws UnsupportedOperationException always
- * @see javax.jcr.Property#setValue(java.lang.String)
- */
- public final void setValue( String value ) {
- throw new UnsupportedOperationException();
- }
-
- /**
- * {@inheritDoc}
- *
- * @throws UnsupportedOperationException always
- * @see javax.jcr.Property#setValue(java.lang.String[])
- */
- public final void setValue( String[] values ) {
- throw new UnsupportedOperationException();
- }
-
- /**
- * {@inheritDoc}
- *
- * @throws UnsupportedOperationException always
- * @see javax.jcr.Property#setValue(java.io.InputStream)
- */
- public final void setValue( InputStream value ) {
- throw new UnsupportedOperationException();
- }
-
- /**
- * {@inheritDoc}
- *
- * @throws UnsupportedOperationException always
- * @see javax.jcr.Property#setValue(long)
- */
- public final void setValue( long value ) {
- throw new UnsupportedOperationException();
- }
-
- /**
- * {@inheritDoc}
- *
- * @throws UnsupportedOperationException always
- * @see javax.jcr.Property#setValue(double)
- */
- public final void setValue( double value ) {
- throw new UnsupportedOperationException();
- }
-
- /**
- * {@inheritDoc}
- *
- * @throws UnsupportedOperationException always
- * @see javax.jcr.Property#setValue(java.util.Calendar)
- */
- public final void setValue( Calendar value ) {
- throw new UnsupportedOperationException();
- }
-
- /**
- * {@inheritDoc}
- *
- * @throws UnsupportedOperationException always
- * @see javax.jcr.Property#setValue(boolean)
- */
- public final void setValue( boolean value ) {
- throw new UnsupportedOperationException();
- }
-
- /**
- * {@inheritDoc}
- *
- * @throws UnsupportedOperationException always
- * @see javax.jcr.Property#setValue(javax.jcr.Node)
- */
- public final void setValue( Node value ) {
- throw new UnsupportedOperationException();
- }
-
- /**
- * {@inheritDoc}
- *
- * @throws UnsupportedOperationException always
* @see javax.jcr.Item#refresh(boolean)
*/
public void refresh( boolean keepChanges ) {
Modified: trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrMultiValueProperty.java
===================================================================
--- trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrMultiValueProperty.java 2009-03-24
18:16:53 UTC (rev 794)
+++ trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrMultiValueProperty.java 2009-03-24
22:33:26 UTC (rev 795)
@@ -27,11 +27,16 @@
import java.util.Calendar;
import java.util.Iterator;
import javax.jcr.Node;
+import javax.jcr.PropertyType;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import javax.jcr.ValueFormatException;
+import javax.jcr.lock.LockException;
+import javax.jcr.nodetype.ConstraintViolationException;
+import javax.jcr.version.VersionException;
import net.jcip.annotations.NotThreadSafe;
import org.jboss.dna.graph.property.Property;
+import org.jboss.dna.graph.property.ValueFactory;
/**
* @author jverhaeg
@@ -140,16 +145,6 @@
/**
* {@inheritDoc}
- *
- * @throws ValueFormatException always
- * @see javax.jcr.Property#getValue()
- */
- public Value getValue() throws ValueFormatException {
- throw new ValueFormatException();
- }
-
- /**
- * {@inheritDoc}
* <p>
* Per the JCR specification, these values need to be created each time this method
is called, since the Value cannot be used
* after {@link Value#getStream()} is called/processed. The spec says that the client
simply needs to obtain a new Value (or
@@ -167,4 +162,176 @@
}
return values;
}
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see javax.jcr.Property#setValue(javax.jcr.Value[])
+ */
+ public final void setValue( Value[] values )
+ throws ValueFormatException, VersionException, LockException,
ConstraintViolationException, RepositoryException {
+ JcrValue[] jcrValues = null;
+ if (values != null && values.length != 0) {
+ int numValues = values.length;
+ jcrValues = new JcrValue[numValues];
+ ValueFactory<?> factory = null;
+ for (int i = 0; i != numValues; ++i) {
+ Value value = values[i];
+ if (value instanceof JcrValue) {
+ // just use the value ...
+ jcrValues[i] = (JcrValue)value;
+ } else {
+ // This isn't our implementation, so create one for use
+ if (factory == null) {
+ int currentType = this.getType();
+ factory =
context().getValueFactories().getValueFactory(PropertyTypeUtil.dnaPropertyTypeFor(currentType));
+ }
+ int type = value.getType();
+ Object data = null;
+ switch (value.getType()) {
+ case PropertyType.STRING:
+ data = value.getString();
+ break;
+ case PropertyType.BINARY:
+ data = value.getStream();
+ break;
+ case PropertyType.BOOLEAN:
+ data = value.getBoolean();
+ break;
+ case PropertyType.DATE:
+ data = value.getDate();
+ break;
+ case PropertyType.DOUBLE:
+ data = value.getDouble();
+ break;
+ case PropertyType.LONG:
+ data = value.getLong();
+ break;
+ case PropertyType.NAME:
+ data = value.getString();
+ break;
+ case PropertyType.PATH:
+ data = value.getString();
+ break;
+ case PropertyType.REFERENCE:
+ data = value.getString();
+ break;
+ default:
+ throw new RepositoryException();
+ }
+ jcrValues[i] = createValue(factory.create(data), type);
+ }
+ }
+ }
+
cache.getEditorFor(propertyId.getNodeId()).setProperty(propertyId.getPropertyName(),
jcrValues);
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see javax.jcr.Property#setValue(java.lang.String[])
+ */
+ public final void setValue( String[] values )
+ throws ValueFormatException, VersionException, LockException,
ConstraintViolationException, RepositoryException {
+ JcrValue[] jcrValues = null;
+ if (values != null && values.length != 0) {
+ int numValues = values.length;
+ jcrValues = new JcrValue[numValues];
+ for (int i = 0; i != numValues; ++i) {
+ jcrValues[i] = createValue(values[i], PropertyType.STRING);
+ }
+ }
+
cache.getEditorFor(propertyId.getNodeId()).setProperty(propertyId.getPropertyName(),
jcrValues);
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @throws ValueFormatException always
+ * @see javax.jcr.Property#getValue()
+ */
+ public Value getValue() throws ValueFormatException {
+ throw new ValueFormatException();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @throws ValueFormatException always
+ * @see javax.jcr.Property#setValue(javax.jcr.Value)
+ */
+ public final void setValue( Value value ) throws ValueFormatException {
+ throw new ValueFormatException();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @throws ValueFormatException always
+ * @see javax.jcr.Property#setValue(java.lang.String)
+ */
+ public final void setValue( String value ) throws ValueFormatException {
+ throw new ValueFormatException();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @throws ValueFormatException always
+ * @see javax.jcr.Property#setValue(java.io.InputStream)
+ */
+ public final void setValue( InputStream value ) throws ValueFormatException {
+ throw new ValueFormatException();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @throws ValueFormatException always
+ * @see javax.jcr.Property#setValue(long)
+ */
+ public final void setValue( long value ) throws ValueFormatException {
+ throw new ValueFormatException();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @throws ValueFormatException always
+ * @see javax.jcr.Property#setValue(double)
+ */
+ public final void setValue( double value ) throws ValueFormatException {
+ throw new ValueFormatException();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @throws ValueFormatException always
+ * @see javax.jcr.Property#setValue(java.util.Calendar)
+ */
+ public final void setValue( Calendar value ) throws ValueFormatException {
+ throw new ValueFormatException();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @throws ValueFormatException always
+ * @see javax.jcr.Property#setValue(boolean)
+ */
+ public final void setValue( boolean value ) throws ValueFormatException {
+ throw new ValueFormatException();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @throws ValueFormatException always
+ * @see javax.jcr.Property#setValue(javax.jcr.Node)
+ */
+ public final void setValue( Node value ) throws ValueFormatException {
+ throw new ValueFormatException();
+ }
+
}
Modified: trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrSession.java
===================================================================
--- trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrSession.java 2009-03-24 18:16:53 UTC
(rev 794)
+++ trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrSession.java 2009-03-24 22:33:26 UTC
(rev 795)
@@ -573,53 +573,6 @@
}
/**
- * Compute the JCR {@link PropertyType} for the given DNA {@link
org.jboss.dna.graph.property.PropertyType}.
- * <p>
- * See DNA-293 for complete discussion on why this method works the way it does. The
best option appears to be basing the
- * PropertyType on the first value, since that should be compatible with the
PropertyType that was used when the values were
- * set on the property in the first place.
- * </p>
- *
- * @param property the DNA property for which the {@link PropertyType} is to be
determined; never null
- * @return the JCR property type; always a valid value and never {@link
PropertyType#UNDEFINED}.
- */
- static final int jcrPropertyTypeFor( org.jboss.dna.graph.property.Property property )
{
- Object value = property.getFirstValue();
-
- // Get the DNA property type for this ...
- switch (org.jboss.dna.graph.property.PropertyType.discoverType(value)) {
- case STRING:
- return PropertyType.STRING;
- case NAME:
- return PropertyType.NAME;
- case LONG:
- return PropertyType.LONG;
- case UUID:
- return PropertyType.STRING; // JCR treats UUID properties as strings
- case URI:
- return PropertyType.STRING;
- case PATH:
- return PropertyType.PATH;
- case BOOLEAN:
- return PropertyType.BOOLEAN;
- case DATE:
- return PropertyType.DATE;
- case DECIMAL:
- return PropertyType.STRING; // better than losing information
- case DOUBLE:
- return PropertyType.DOUBLE;
- case BINARY:
- return PropertyType.BINARY;
- case OBJECT:
- return PropertyType.UNDEFINED;
- case REFERENCE:
- return PropertyType.REFERENCE;
- }
- assert false;
- return PropertyType.UNDEFINED;
- }
-
- /**
* {@inheritDoc}
*
* @see javax.jcr.Session#refresh(boolean)
Modified: trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrSingleValueProperty.java
===================================================================
--- trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrSingleValueProperty.java 2009-03-24
18:16:53 UTC (rev 794)
+++ trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrSingleValueProperty.java 2009-03-24
22:33:26 UTC (rev 795)
@@ -27,9 +27,13 @@
import java.util.Calendar;
import java.util.UUID;
import javax.jcr.Node;
+import javax.jcr.PropertyType;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import javax.jcr.ValueFormatException;
+import javax.jcr.lock.LockException;
+import javax.jcr.nodetype.ConstraintViolationException;
+import javax.jcr.version.VersionException;
import org.jboss.dna.graph.property.Binary;
import org.jboss.dna.graph.property.Reference;
import org.jboss.dna.graph.property.ValueFactories;
@@ -170,6 +174,129 @@
/**
* {@inheritDoc}
*
+ * @see javax.jcr.Property#setValue(javax.jcr.Value)
+ */
+ public void setValue( Value value )
+ throws ValueFormatException, VersionException, LockException,
ConstraintViolationException, RepositoryException {
+ JcrValue jcrValue = null;
+ if (value instanceof JcrValue) {
+ jcrValue = (JcrValue)value;
+
cache.getEditorFor(propertyId.getNodeId()).setProperty(propertyId.getPropertyName(),
jcrValue);
+ return;
+ }
+ // We have to convert from one Value implementation to ours ...
+ switch (value.getType()) {
+ case PropertyType.STRING:
+ setValue(value.getString());
+ break;
+ case PropertyType.BINARY:
+ setValue(value.getStream());
+ break;
+ case PropertyType.BOOLEAN:
+ setValue(value.getBoolean());
+ break;
+ case PropertyType.DATE:
+ setValue(value.getDate());
+ break;
+ case PropertyType.DOUBLE:
+ setValue(value.getDouble());
+ break;
+ case PropertyType.LONG:
+ setValue(value.getLong());
+ break;
+ case PropertyType.NAME:
+ setValue(value.getString());
+ break;
+ case PropertyType.PATH:
+ setValue(value.getString());
+ break;
+ case PropertyType.REFERENCE:
+ setValue(value.getString());
+ break;
+ default:
+ throw new RepositoryException();
+ }
+ }
+
+ protected void setValue( JcrValue jcrValue )
+ throws ValueFormatException, VersionException, LockException,
ConstraintViolationException, RepositoryException {
+
cache.getEditorFor(propertyId.getNodeId()).setProperty(propertyId.getPropertyName(),
jcrValue);
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see javax.jcr.Property#setValue(java.lang.String)
+ */
+ public void setValue( String value )
+ throws ValueFormatException, VersionException, LockException,
ConstraintViolationException, RepositoryException {
+ setValue(createValue(value, PropertyType.STRING));
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see javax.jcr.Property#setValue(java.io.InputStream)
+ */
+ public void setValue( InputStream value )
+ throws ValueFormatException, VersionException, LockException,
ConstraintViolationException, RepositoryException {
+
setValue(createValue(context().getValueFactories().getBinaryFactory().create(value),
PropertyType.DATE));
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see javax.jcr.Property#setValue(long)
+ */
+ public void setValue( long value )
+ throws ValueFormatException, VersionException, LockException,
ConstraintViolationException, RepositoryException {
+ setValue(createValue(new Long(value), PropertyType.LONG));
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see javax.jcr.Property#setValue(double)
+ */
+ public void setValue( double value )
+ throws ValueFormatException, VersionException, LockException,
ConstraintViolationException, RepositoryException {
+ setValue(createValue(new Double(value), PropertyType.DOUBLE));
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see javax.jcr.Property#setValue(java.util.Calendar)
+ */
+ public void setValue( Calendar value )
+ throws ValueFormatException, VersionException, LockException,
ConstraintViolationException, RepositoryException {
+
setValue(createValue(context().getValueFactories().getDateFactory().create(value),
PropertyType.DATE));
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see javax.jcr.Property#setValue(boolean)
+ */
+ public void setValue( boolean value )
+ throws ValueFormatException, VersionException, LockException,
ConstraintViolationException, RepositoryException {
+ setValue(createValue(new Boolean(value), PropertyType.BOOLEAN));
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see javax.jcr.Property#setValue(javax.jcr.Node)
+ */
+ public void setValue( Node value )
+ throws ValueFormatException, VersionException, LockException,
ConstraintViolationException, RepositoryException {
+ String uuid = value.getUUID();
+ setValue(createValue(uuid, PropertyType.REFERENCE));
+ }
+
+ /**
+ * {@inheritDoc}
+ *
* @throws ValueFormatException always
* @see javax.jcr.Property#getValues()
*/
@@ -177,60 +304,21 @@
throw new ValueFormatException();
}
- /*
+ /**
* {@inheritDoc}
*
- * @throws IllegalArgumentException if <code>value</code> is
<code>null</code>.
- * @see javax.jcr.Property#setValue(javax.jcr.Value)
- *
- @SuppressWarnings( "fallthrough" )
- public void setValue( Value value ) throws RepositoryException {
- CheckArg.isNotNull(value, "value");
- // TODOx: Check node type constraint
- try {
- jcrValue = JcrValue.class.cast(value);
- } catch (ClassCastException error) {
- // TODOx: not sure if this is even possible
- ValueFactories valueFactories = getExecutionContext().getValueFactories();
- int type = value.getType();
- switch (type) {
- case PropertyType.BINARY: {
- jcrValue = new JcrValue<InputStream>(valueFactories, type,
value.getStream());
- break;
- }
- case PropertyType.BOOLEAN: {
- jcrValue = new JcrValue<Boolean>(valueFactories, type,
value.getBoolean());
- break;
- }
- case PropertyType.DATE: {
- jcrValue = new JcrValue<Calendar>(valueFactories, type,
value.getDate());
- break;
- }
- case PropertyType.DOUBLE: {
- jcrValue = new JcrValue<Double>(valueFactories, type,
value.getDouble());
- break;
- }
- case PropertyType.LONG: {
- jcrValue = new JcrValue<Long>(valueFactories, type,
value.getLong());
- break;
- }
- case PropertyType.REFERENCE: {
- try {
- jcrValue = new JcrValue<UUID>(valueFactories, type,
UUID.fromString(value.getString()));
- } catch (IllegalArgumentException fallsThroughToString) {
- }
- }
- case PropertyType.NAME:
- case PropertyType.PATH:
- case PropertyType.STRING: {
- jcrValue = new JcrValue<String>(valueFactories, type,
value.getString());
- break;
- }
- default: {
- throw new AssertionError("Unsupported PropertyType: " +
value.getType());
- }
- }
- }
+ * @see javax.jcr.Property#setValue(javax.jcr.Value[])
+ */
+ public void setValue( Value[] values ) throws ValueFormatException {
+ throw new ValueFormatException();
}
- */
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see javax.jcr.Property#setValue(java.lang.String[])
+ */
+ public void setValue( String[] values ) throws ValueFormatException {
+ throw new ValueFormatException();
+ }
}
Added: trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/PropertyTypeUtil.java
===================================================================
--- trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/PropertyTypeUtil.java
(rev 0)
+++ trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/PropertyTypeUtil.java 2009-03-24
22:33:26 UTC (rev 795)
@@ -0,0 +1,112 @@
+/*
+ * JBoss DNA (
http://www.jboss.org/dna)
+ * See the COPYRIGHT.txt file distributed with this work for information
+ * regarding copyright ownership. Some portions may be licensed
+ * to Red Hat, Inc. under one or more contributor license agreements.
+ * See the AUTHORS.txt file in the distribution for a full listing of
+ * individual contributors.
+ *
+ * Unless otherwise indicated, all code in JBoss DNA is licensed
+ * to you under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * JBoss DNA is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.dna.jcr;
+
+import javax.jcr.PropertyType;
+
+/**
+ *
+ */
+class PropertyTypeUtil {
+
+ /**
+ * Compute the JCR {@link PropertyType} for the given DNA {@link
org.jboss.dna.graph.property.PropertyType}.
+ * <p>
+ * See DNA-293 for complete discussion on why this method works the way it does. The
best option appears to be basing the
+ * PropertyType on the first value, since that should be compatible with the
PropertyType that was used when the values were
+ * set on the property in the first place.
+ * </p>
+ *
+ * @param property the DNA property for which the {@link PropertyType} is to be
determined; never null
+ * @return the JCR property type; always a valid value and never {@link
PropertyType#UNDEFINED}.
+ */
+ static final int jcrPropertyTypeFor( org.jboss.dna.graph.property.Property property )
{
+ Object value = property.getFirstValue();
+
+ // Get the DNA property type for this ...
+ switch (org.jboss.dna.graph.property.PropertyType.discoverType(value)) {
+ case STRING:
+ return PropertyType.STRING;
+ case NAME:
+ return PropertyType.NAME;
+ case LONG:
+ return PropertyType.LONG;
+ case UUID:
+ return PropertyType.STRING; // JCR treats UUID properties as strings
+ case URI:
+ return PropertyType.STRING;
+ case PATH:
+ return PropertyType.PATH;
+ case BOOLEAN:
+ return PropertyType.BOOLEAN;
+ case DATE:
+ return PropertyType.DATE;
+ case DECIMAL:
+ return PropertyType.STRING; // better than losing information
+ case DOUBLE:
+ return PropertyType.DOUBLE;
+ case BINARY:
+ return PropertyType.BINARY;
+ case OBJECT:
+ return PropertyType.UNDEFINED;
+ case REFERENCE:
+ return PropertyType.REFERENCE;
+ }
+ assert false;
+ return PropertyType.UNDEFINED;
+ }
+
+ /**
+ * Compute the DNA {@link org.jboss.dna.graph.property.PropertyType} for the given
JCR {@link PropertyType} value.
+ *
+ * @param jcrPropertyType the DNA property for which the {@link PropertyType} is to
be determined; never null
+ * @return the JCR property type; always a valid value and never {@link
PropertyType#UNDEFINED}.
+ */
+ static final org.jboss.dna.graph.property.PropertyType dnaPropertyTypeFor( int
jcrPropertyType ) {
+ // Make sure the value is the correct type ...
+ switch (jcrPropertyType) {
+ case PropertyType.STRING:
+ return org.jboss.dna.graph.property.PropertyType.STRING;
+ case PropertyType.BINARY:
+ return org.jboss.dna.graph.property.PropertyType.BINARY;
+ case PropertyType.BOOLEAN:
+ return org.jboss.dna.graph.property.PropertyType.BOOLEAN;
+ case PropertyType.DOUBLE:
+ return org.jboss.dna.graph.property.PropertyType.DOUBLE;
+ case PropertyType.LONG:
+ return org.jboss.dna.graph.property.PropertyType.LONG;
+ case PropertyType.DATE:
+ return org.jboss.dna.graph.property.PropertyType.DATE;
+ case PropertyType.PATH:
+ return org.jboss.dna.graph.property.PropertyType.PATH;
+ case PropertyType.NAME:
+ return org.jboss.dna.graph.property.PropertyType.NAME;
+ case PropertyType.REFERENCE:
+ return org.jboss.dna.graph.property.PropertyType.REFERENCE;
+ }
+ assert false;
+ return org.jboss.dna.graph.property.PropertyType.STRING;
+ }
+
+}
Property changes on: trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/PropertyTypeUtil.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Modified: trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/SessionCache.java
===================================================================
--- trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/SessionCache.java 2009-03-24 18:16:53
UTC (rev 794)
+++ trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/SessionCache.java 2009-03-24 22:33:26
UTC (rev 795)
@@ -431,16 +431,74 @@
* existing values will be replaced with the supplied value.
*
* @param name the property name; may not be null
- * @param propertyType the property type; must be a valid {@link PropertyType}
value
- * @param value the new property values, which may be converted to the
appropriate {@link PropertyType type}
+ * @param value the new property values; may not be null
* @throws ConstraintViolationException if the property could not be set because
of a node type constraint or property
* definition constraint
*/
public void setProperty( Name name,
- int propertyType,
- Object value ) throws ConstraintViolationException {
- Property dnaProp = propertyFactory.create(name, value);
- setProperty(name, dnaProp, propertyType);
+ JcrValue value ) throws ConstraintViolationException {
+ assert name != null;
+ assert value != null;
+ JcrPropertyDefinition definition = null;
+ PropertyId id = null;
+
+ // Look for an existing property ...
+ PropertyInfo existing = node.getProperty(name);
+ if (existing != null) {
+ // Reuse the existing ID ...
+ id = existing.getPropertyId();
+
+ // We're replacing an existing property, but we still need to check
that the property definition
+ // (still) defines a type. So, find the property definition for the
existing property ...
+ definition =
nodeTypes().getPropertyDefinition(existing.getDefinitionId());
+
+ if (definition != null) {
+ // The definition's require type must match the value's ...
+ if (definition.getRequiredType() != PropertyType.UNDEFINED &&
definition.getRequiredType() != value.getType()) {
+ // The property type is not right, so we have to check if we can
cast.
+ // It's easier and could save more work if we just find a new
property definition that works ...
+ definition = null;
+ } else {
+ // The types match, so see if the value satisfies the constraints
...
+ if (!definition.satisfiesConstraints(value)) definition = null;
+ }
+ }
+ } else {
+ // This is a new property, so create a new ID ...
+ id = new PropertyId(node.getUuid(), name);
+ }
+ if (definition == null) {
+ // Look for a definition ...
+ definition =
nodeTypes().findPropertyDefinition(node.getPrimaryTypeName(),
+
node.getMixinTypeNames(),
+ name,
+ value,
+ true,
+ true);
+ if (definition == null) {
+ throw new ConstraintViolationException();
+ }
+ }
+ // Create the DNA property ...
+ Object objValue = value.value();
+ int propertyType = definition.getRequiredType();
+ if (propertyType == PropertyType.UNDEFINED || propertyType ==
value.getType()) {
+ // Can use the values as is ...
+ propertyType = value.getType();
+ } else {
+ // A cast is required ...
+ org.jboss.dna.graph.property.PropertyType dnaPropertyType =
PropertyTypeUtil.dnaPropertyTypeFor(propertyType);
+ ValueFactory<?> factory =
context.getValueFactories().getValueFactory(dnaPropertyType);
+ objValue = factory.create(value);
+ }
+ Property dnaProp = propertyFactory.create(name, objValue);
+
+ // Create the property info ...
+ PropertyInfo newProperty = new PropertyInfo(id, definition.getId(),
propertyType, dnaProp, definition.isMultiple());
+
+ // Finally update the cached information and record the change ...
+ node.setProperty(newProperty, context().getValueFactories());
+ operations().set(dnaProp).on(currentLocation);
}
/**
@@ -448,63 +506,86 @@
* existing values will be replaced with those that are supplied.
*
* @param name the property name; may not be null
- * @param propertyType the property type; must be a valid {@link PropertyType}
value
- * @param value the new property values, which may be converted to the
appropriate {@link PropertyType type}
+ * @param values new property values, all of which must have the same {@link
Value#getType() property type}; may not be
+ * null or empty
* @throws ConstraintViolationException if the property could not be set because
of a node type constraint or property
* definition constraint
*/
public void setProperty( Name name,
- int propertyType,
- Object[] value ) throws ConstraintViolationException {
- Property dnaProp = propertyFactory.create(name, value);
- setProperty(name, dnaProp, propertyType);
- }
+ JcrValue[] values ) throws ConstraintViolationException
{
+ assert name != null;
+ assert values != null;
+ int numValues = values.length;
+ assert numValues != 0;
+ JcrPropertyDefinition definition = null;
+ PropertyId id = null;
- protected final void setProperty( Name name,
- Property dnaProp,
- int propertyType ) throws
ConstraintViolationException {
- assert propertyType != PropertyType.UNDEFINED;
+ // Look for an existing property ...
PropertyInfo existing = node.getProperty(name);
- PropertyInfo newProperty = null;
if (existing != null) {
+ // Reuse the existing ID ...
+ id = existing.getPropertyId();
+
// We're replacing an existing property, but we still need to check
that the property definition
- // defines a type. So, find the property definition for the existing
property ...
- JcrPropertyDefinition definition =
nodeTypes().getPropertyDefinition(existing.getDefinitionId());
- if (definition == null &&
existing.getDefinitionId().allowsMultiple() && dnaProp.isSingle()) {
- // Look for a single-valued definition ...
- PropertyDefinitionId id =
existing.getDefinitionId().asSingleValued();
- definition = nodeTypes().getPropertyDefinition(id);
+ // (still) defines a type. So, find the property definition for the
existing property ...
+ definition =
nodeTypes().getPropertyDefinition(existing.getDefinitionId());
+
+ if (definition != null) {
+ // The definition's require type must match the value's ...
+ if (numValues == 0) {
+ // Just use the definition as is ...
+ } else {
+ int type = values[0].getType();
+ if (definition.getRequiredType() != PropertyType.UNDEFINED
&& definition.getRequiredType() != type) {
+ // The property type is not right, so we have to check if we
can cast.
+ // It's easier and could save more work if we just find a
new property definition that works ...
+ definition = null;
+ } else {
+ // The types match, so see if the value satisfies the
constraints ...
+ if (!definition.satisfiesConstraints(values)) definition =
null;
+ }
+ }
}
+ } else {
+ // This is a new property, so create a new ID ...
+ id = new PropertyId(node.getUuid(), name);
+ }
+ if (definition == null) {
+ // Look for a definition ...
+ definition =
nodeTypes().findPropertyDefinition(node.getPrimaryTypeName(),
+
node.getMixinTypeNames(),
+ name,
+ values,
+ true);
if (definition == null) {
- // Try to find a different (but existing) property definition ...
- definition = findBestPropertyDefintion(node.getPrimaryTypeName(),
- node.getMixinTypeNames(),
- dnaProp,
- propertyType,
- true);
- }
- if (definition == null) {
throw new ConstraintViolationException();
}
-
- // Create the property info ...
- newProperty = new PropertyInfo(existing.getPropertyId(),
existing.getDefinitionId(), propertyType, dnaProp,
- existing.isMultiValued());
+ }
+ // Create the DNA property ...
+ int type = values.length != 0 ? values[0].getType() :
definition.getRequiredType();
+ Object[] objValues = new Object[values.length];
+ int propertyType = definition.getRequiredType();
+ if (propertyType == PropertyType.UNDEFINED || propertyType == type) {
+ // Can use the values as is ...
+ propertyType = type;
+ for (int i = 0; i != numValues; ++i) {
+ objValues[i] = values[i].value();
+ }
} else {
- // It's a new property ...
- PropertyId id = new PropertyId(node.getUuid(), name);
- // Look find the property definition to use ...
- JcrPropertyDefinition definition =
findBestPropertyDefintion(node.getPrimaryTypeName(),
-
node.getMixinTypeNames(),
- dnaProp,
-
propertyType,
- true);
- if (definition == null) {
- throw new ConstraintViolationException();
+ // A cast is required ...
+ assert propertyType != type;
+ org.jboss.dna.graph.property.PropertyType dnaPropertyType =
PropertyTypeUtil.dnaPropertyTypeFor(propertyType);
+ ValueFactory<?> factory =
context.getValueFactories().getValueFactory(dnaPropertyType);
+ for (int i = 0; i != numValues; ++i) {
+ objValues[i] = factory.create(values[i].value());
}
- // Create the property info ...
- newProperty = new PropertyInfo(id, definition.getId(), propertyType,
dnaProp, definition.isMultiple());
}
+ Property dnaProp = propertyFactory.create(name, objValues);
+
+ // Create the property info ...
+ PropertyInfo newProperty = new PropertyInfo(id, definition.getId(),
propertyType, dnaProp, definition.isMultiple());
+
+ // Finally update the cached information and record the change ...
node.setProperty(newProperty, context().getValueFactories());
operations().set(dnaProp).on(currentLocation);
}
@@ -571,7 +652,9 @@
if (!definition.getId().equals(node.getDefinitionId())) {
// The node definition changed, so try to set the property ...
try {
- setProperty(DnaLexicon.NODE_DEFINITON, PropertyType.STRING,
definition.getId().getString());
+ JcrValue value = new JcrValue(context().getValueFactories(),
SessionCache.this, PropertyType.STRING,
+ definition.getId().getString());
+ setProperty(DnaLexicon.NODE_DEFINITON, value);
} catch (ConstraintViolationException e) {
// We can't set this property on the node (according to the node
definition).
// But we still want the node info to have the correct node
definition.
@@ -1297,7 +1380,7 @@
Name name = dnaProp.getName();
// Figure out the JCR property type for this property ...
- int propertyType = JcrSession.jcrPropertyTypeFor(dnaProp);
+ int propertyType = PropertyTypeUtil.jcrPropertyTypeFor(dnaProp);
PropertyDefinition propertyDefinition =
findBestPropertyDefintion(primaryTypeName,
mixinTypeNames,
dnaProp,
Modified: trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/AbstractJcrPropertyTest.java
===================================================================
--- trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/AbstractJcrPropertyTest.java 2009-03-24
18:16:53 UTC (rev 794)
+++ trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/AbstractJcrPropertyTest.java 2009-03-24
22:33:26 UTC (rev 795)
@@ -39,7 +39,6 @@
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.Workspace;
-import org.jboss.dna.common.util.StringUtil;
import org.jboss.dna.graph.ExecutionContext;
import org.jboss.dna.graph.property.Name;
import org.jboss.dna.graph.property.Path;
@@ -277,56 +276,6 @@
assertThat(prop.isSame(otherProp), is(false));
}
- @Test( expected = UnsupportedOperationException.class )
- public void shouldNotAllowSetBooleanValue() {
- prop.setValue(false);
- }
-
- @Test( expected = UnsupportedOperationException.class )
- public void shouldNotAllowSetCalendarValue() {
- prop.setValue(Calendar.getInstance());
- }
-
- @Test( expected = UnsupportedOperationException.class )
- public void shouldNotAllowSetDoubleValue() {
- prop.setValue(0.0);
- }
-
- @Test( expected = UnsupportedOperationException.class )
- public void shouldNotAllowSetInputStreamValue() {
- prop.setValue(Mockito.mock(InputStream.class));
- }
-
- @Test( expected = UnsupportedOperationException.class )
- public void shouldNotAllowSetLongValue() {
- prop.setValue(0L);
- }
-
- @Test( expected = UnsupportedOperationException.class )
- public void shouldNotAllowSetNodeValue() {
- prop.setValue(Mockito.mock(Node.class));
- }
-
- @Test( expected = UnsupportedOperationException.class )
- public void shouldNotAllowSetStringValue() {
- prop.setValue("");
- }
-
- @Test( expected = UnsupportedOperationException.class )
- public void shouldNotAllowSetStringArrayValue() {
- prop.setValue(StringUtil.EMPTY_STRING_ARRAY);
- }
-
- @Test( expected = UnsupportedOperationException.class )
- public void shouldNotAllowSetValueValue() {
- prop.setValue(Mockito.mock(Value.class));
- }
-
- @Test( expected = UnsupportedOperationException.class )
- public void shouldNotAllowSetValueArrayValue() {
- prop.setValue(new Value[0]);
- }
-
private class MockAbstractJcrProperty extends AbstractJcrProperty {
MockAbstractJcrProperty( SessionCache cache,
@@ -433,5 +382,95 @@
public Value[] getValues() {
return null;
}
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see javax.jcr.Property#setValue(javax.jcr.Value)
+ */
+ public void setValue( Value value ) {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see javax.jcr.Property#setValue(javax.jcr.Value[])
+ */
+ public void setValue( Value[] values ) {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see javax.jcr.Property#setValue(java.lang.String)
+ */
+ public void setValue( String value ) {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see javax.jcr.Property#setValue(java.lang.String[])
+ */
+ public void setValue( String[] values ) {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see javax.jcr.Property#setValue(java.io.InputStream)
+ */
+ public void setValue( InputStream value ) {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see javax.jcr.Property#setValue(long)
+ */
+ public void setValue( long value ) {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see javax.jcr.Property#setValue(double)
+ */
+ public void setValue( double value ) {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see javax.jcr.Property#setValue(java.util.Calendar)
+ */
+ public void setValue( Calendar value ) {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see javax.jcr.Property#setValue(boolean)
+ */
+ public void setValue( boolean value ) {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see javax.jcr.Property#setValue(javax.jcr.Node)
+ */
+ public void setValue( Node value ) {
+ throw new UnsupportedOperationException();
+ }
}
}
Modified: trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrSessionTest.java
===================================================================
--- trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrSessionTest.java 2009-03-24 18:16:53
UTC (rev 794)
+++ trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrSessionTest.java 2009-03-24 22:33:26
UTC (rev 795)
@@ -32,6 +32,7 @@
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.stub;
+import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
@@ -325,12 +326,13 @@
@Test
public void shouldProvideValueFactory() throws Exception {
+ InputStream stream = new ByteArrayInputStream("something".getBytes());
ValueFactory factory = session.getValueFactory();
assertThat(factory, notNullValue());
assertThat(factory.createValue(false), notNullValue());
assertThat(factory.createValue(Calendar.getInstance()), notNullValue());
assertThat(factory.createValue(0.0), notNullValue());
- assertThat(factory.createValue(Mockito.mock(InputStream.class)),
notNullValue());
+ assertThat(factory.createValue(stream), notNullValue());
assertThat(factory.createValue(0L), notNullValue());
Node node = Mockito.mock(Node.class);
stub(node.getUUID()).toReturn(UUID.randomUUID().toString());
Modified: trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/SessionCacheTest.java
===================================================================
--- trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/SessionCacheTest.java 2009-03-24
18:16:53 UTC (rev 794)
+++ trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/SessionCacheTest.java 2009-03-24
22:33:26 UTC (rev 795)
@@ -23,6 +23,10 @@
*/
package org.jboss.dna.jcr;
+import static javax.jcr.PropertyType.DOUBLE;
+import static javax.jcr.PropertyType.LONG;
+import static javax.jcr.PropertyType.STRING;
+import static javax.jcr.PropertyType.UNDEFINED;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.hamcrest.core.IsNull.nullValue;
@@ -34,15 +38,13 @@
import static org.mockito.Mockito.stub;
import java.io.File;
import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import javax.jcr.InvalidItemStateException;
+import javax.jcr.nodetype.ConstraintViolationException;
import org.jboss.dna.common.statistic.Stopwatch;
import org.jboss.dna.common.util.StringUtil;
import org.jboss.dna.graph.ExecutionContext;
@@ -54,7 +56,9 @@
import org.jboss.dna.graph.property.Path;
import org.jboss.dna.graph.property.PathNotFoundException;
import org.jboss.dna.graph.property.Property;
+import org.jboss.dna.graph.property.ValueFactory;
import org.jboss.dna.jcr.SessionCache.NodeEditor;
+import org.jboss.dna.jcr.Vehicles.Lexicon;
import org.jboss.dna.jcr.cache.Children;
import org.jboss.dna.jcr.cache.NodeInfo;
import org.jboss.dna.jcr.cache.PropertyInfo;
@@ -89,7 +93,7 @@
cachesByName = new HashMap<String, SessionCache>();
context = new ExecutionContext();
- context.getNamespaceRegistry().register("vehix",
"http://example.com/vehicles");
+ context.getNamespaceRegistry().register(Vehicles.Lexicon.Namespace.PREFIX,
Vehicles.Lexicon.Namespace.URI);
stub(session.getExecutionContext()).toReturn(context);
stub(session.namespaces()).toReturn(context.getNamespaceRegistry());
@@ -98,7 +102,7 @@
JcrNodeTypeSource nodeTypeSource = null;
nodeTypeSource = new JcrBuiltinNodeTypeSource(this.context, nodeTypeSource);
nodeTypeSource = new DnaBuiltinNodeTypeSource(this.context, nodeTypeSource);
- nodeTypeSource = new VehixNodeTypeSource(context, nodeTypeSource);
+ nodeTypeSource = new Vehicles.NodeTypeSource(context, nodeTypeSource);
repoTypes = new RepositoryNodeTypeManager(context, nodeTypeSource);
nodeTypes = new JcrNodeTypeManager(this.context, repoTypes);
stub(session.nodeTypeManager()).toReturn(nodeTypes);
@@ -108,46 +112,6 @@
cache = getCache("vehicles");
}
- /**
- * Define the node types for the "vehix" namespace.
- */
- public static class VehixNodeTypeSource extends AbstractJcrNodeTypeSource {
- private final List<JcrNodeType> nodeTypes;
-
- public VehixNodeTypeSource( ExecutionContext context,
- JcrNodeTypeSource predecessor ) {
- super(predecessor);
- this.nodeTypes = new ArrayList<JcrNodeType>();
-
- Name carName =
context.getValueFactories().getNameFactory().create("vehix:car");
- Name aircraftName =
context.getValueFactories().getNameFactory().create("vehix:aircraft");
- JcrNodeType unstructured = findType(JcrNtLexicon.UNSTRUCTURED);
-
- // Add in the "vehix:car" node type (which extends
"nt:unstructured") ...
- JcrNodeType car = new JcrNodeType(context, (RepositoryNodeTypeManager)null,
carName,
- Arrays.asList(new JcrNodeType[]
{unstructured}), NO_PRIMARY_ITEM_NAME,
- NO_CHILD_NODES, NO_PROPERTIES, NOT_MIXIN,
ORDERABLE_CHILD_NODES);
-
- // Add in the "vehix:aircraft" node type (which extends
"nt:unstructured") ...
- JcrNodeType aircraft = new JcrNodeType(context,
(RepositoryNodeTypeManager)null, aircraftName,
- Arrays.asList(new JcrNodeType[]
{unstructured}), NO_PRIMARY_ITEM_NAME,
- NO_CHILD_NODES, NO_PROPERTIES,
NOT_MIXIN, ORDERABLE_CHILD_NODES);
-
- nodeTypes.addAll(Arrays.asList(new JcrNodeType[] {car, aircraft,}));
- }
-
- /**
- * {@inheritDoc}
- *
- * @see org.jboss.dna.jcr.AbstractJcrNodeTypeSource#getDeclaredNodeTypes()
- */
- @Override
- public Collection<JcrNodeType> getDeclaredNodeTypes() {
- return nodeTypes;
- }
-
- }
-
protected Graph createFrom( String repositoryName,
String workspaceName,
File file ) throws IOException, SAXException {
@@ -231,6 +195,11 @@
}
}
+ protected JcrValue value( int propertyType,
+ Object value ) {
+ return new JcrValue(context.getValueFactories(), cache, propertyType, value);
+ }
+
protected void assertDoesExist( Graph graph,
Location location ) {
Node node = store.getNodeAt(location);
@@ -296,6 +265,52 @@
assertThat(info.getUuid(), is(uuid));
}
+ protected void assertProperty( NodeInfo info,
+ Name propertyName,
+ int type,
+ Name nodeTypeName,
+ Name propertyDefinitionName,
+ int definitionType,
+ Object value ) {
+ PropertyDefinitionId defnId = new PropertyDefinitionId(nodeTypeName,
propertyDefinitionName, definitionType, false);
+ PropertyInfo propertyInfo = info.getProperty(propertyName);
+ assertThat(propertyInfo, is(notNullValue()));
+ assertThat(propertyInfo.getPropertyType(), is(type));
+ assertThat(propertyInfo.getDefinitionId(), is(defnId));
+ org.jboss.dna.graph.property.PropertyType dnaPropertyType =
PropertyTypeUtil.dnaPropertyTypeFor(type);
+ // Check the value ...
+ ValueFactory<?> factory =
context.getValueFactories().getValueFactory(dnaPropertyType);
+ Object actual = factory.create(propertyInfo.getProperty().getFirstValue());
+ assertThat(actual, is(value));
+ }
+
+ protected void assertProperty( NodeInfo info,
+ Name propertyName,
+ int type,
+ Name nodeTypeName,
+ Name propertyDefinitionName,
+ int definitionType,
+ Object... values ) {
+ PropertyDefinitionId defnId = new PropertyDefinitionId(nodeTypeName,
propertyDefinitionName, definitionType, true);
+ PropertyInfo propertyInfo = info.getProperty(propertyName);
+ assertThat(propertyInfo, is(notNullValue()));
+ assertThat(propertyInfo.getPropertyType(), is(type));
+ assertThat(propertyInfo.getDefinitionId(), is(defnId));
+ org.jboss.dna.graph.property.PropertyType dnaPropertyType =
PropertyTypeUtil.dnaPropertyTypeFor(type);
+ // Check the values ...
+ ValueFactory<?> factory =
context.getValueFactories().getValueFactory(dnaPropertyType);
+ int i = 0;
+ for (Object value : propertyInfo.getProperty()) {
+ assertThat(factory.create(value), is(values[i++]));
+ }
+ }
+
+ protected void assertNoProperty( NodeInfo info,
+ Name propertyName ) {
+ PropertyInfo propertyInfo = info.getProperty(propertyName);
+ assertThat(propertyInfo, is(nullValue()));
+ }
+
@Test
public void shouldCreateWithValidParameters() {
assertThat(cache, is(notNullValue()));
@@ -631,4 +646,91 @@
assertDoesExist(store, sports.getLocation());
}
+ @Test
+ public void shouldGetExistingPropertyOnExistingNode() throws Exception {
+ NodeInfo root = cache.findNodeInfoForRoot();
+ NodeInfo lr3 = cache.findNodeInfo(root.getUuid(),
path("/vehix:Vehicles/vehix:Cars/vehix:Utility/vehix:Land Rover LR3"));
+ assertProperty(lr3, Lexicon.MODEL, STRING, Lexicon.CAR, Lexicon.MODEL, STRING,
"LR3");
+ assertProperty(lr3, Lexicon.MAKER, STRING, Lexicon.CAR, Lexicon.MAKER, STRING,
"Land Rover");
+ assertProperty(lr3, Lexicon.YEAR, LONG, Lexicon.CAR, Lexicon.YEAR, LONG, 2008L);
+ assertProperty(lr3, Lexicon.MSRP, STRING, Lexicon.CAR, Lexicon.MSRP, STRING,
"$48,525");
+
+ NodeInfo db9 = cache.findNodeInfo(root.getUuid(),
path("/vehix:Vehicles/vehix:Cars/vehix:Sports/vehix:Aston Martin DB9"));
+ assertProperty(db9, Lexicon.MODEL, STRING, Lexicon.CAR, Lexicon.MODEL, STRING,
"DB9");
+ assertProperty(db9, Lexicon.MAKER, STRING, Lexicon.CAR, Lexicon.MAKER, STRING,
"Aston Martin");
+ assertProperty(db9, Lexicon.LENGTH_IN_INCHES, DOUBLE, Lexicon.CAR,
Lexicon.LENGTH_IN_INCHES, DOUBLE, 185.5D);
+
+ NodeInfo b878 = cache.findNodeInfo(root.getUuid(),
+
path("/vehix:Vehicles/vehix:Aircraft/vehix:Commercial/vehix:Boeing 787"));
+ assertProperty(b878, Lexicon.MODEL, STRING, Lexicon.AIRCRAFT, Lexicon.MODEL,
STRING, "787-3");
+ assertProperty(b878, Lexicon.MAKER, STRING, Lexicon.AIRCRAFT, Lexicon.MAKER,
STRING, "Boeing");
+ assertProperty(b878, Lexicon.INTRODUCED, LONG, Lexicon.AIRCRAFT,
Lexicon.INTRODUCED, LONG, 2009L);
+ assertProperty(b878, Lexicon.EMPTY_WEIGHT, STRING, JcrNtLexicon.UNSTRUCTURED,
name("*"), UNDEFINED, "223000lb");
+
+ }
+
+ @Test
+ public void shouldNotFindNonExistantPropertyOnExistingNode() throws Exception {
+ NodeInfo root = cache.findNodeInfoForRoot();
+ NodeInfo lr3 = cache.findNodeInfo(root.getUuid(),
path("/vehix:Vehicles/vehix:Cars/vehix:Utility/vehix:Land Rover LR3"));
+ assertProperty(lr3, Lexicon.MODEL, STRING, Lexicon.CAR, Lexicon.MODEL, STRING,
"LR3");
+ assertProperty(lr3, Lexicon.MAKER, STRING, Lexicon.CAR, Lexicon.MAKER, STRING,
"Land Rover");
+ assertNoProperty(lr3, Lexicon.LENGTH_IN_INCHES);
+ assertNoProperty(lr3, Lexicon.INTRODUCED);
+ }
+
+ @Test
+ public void shouldSetNewValueOnExistingPropertyOnExistingNode() throws Exception {
+ NodeInfo root = cache.findNodeInfoForRoot();
+ NodeInfo lr3 = cache.findNodeInfo(root.getUuid(),
path("/vehix:Vehicles/vehix:Cars/vehix:Utility/vehix:Land Rover LR3"));
+ assertProperty(lr3, Lexicon.MODEL, STRING, Lexicon.CAR, Lexicon.MODEL, STRING,
"LR3");
+ assertProperty(lr3, Lexicon.MAKER, STRING, Lexicon.CAR, Lexicon.MAKER, STRING,
"Land Rover");
+ assertNoProperty(lr3, Lexicon.LENGTH_IN_INCHES);
+
+ SessionCache.NodeEditor editor = cache.getEditorFor(lr3.getUuid());
+ editor.setProperty(Lexicon.LENGTH_IN_INCHES, value(DOUBLE, 100.0D));
+ }
+
+ @Test
+ public void shouldSetNewPropertyOnExistingNode() throws Exception {
+ NodeInfo root = cache.findNodeInfoForRoot();
+ NodeInfo db9 = cache.findNodeInfo(root.getUuid(),
path("/vehix:Vehicles/vehix:Cars/vehix:Sports/vehix:Aston Martin DB9"));
+ assertProperty(db9, Lexicon.MODEL, STRING, Lexicon.CAR, Lexicon.MODEL, STRING,
"DB9");
+ assertProperty(db9, Lexicon.MAKER, STRING, Lexicon.CAR, Lexicon.MAKER, STRING,
"Aston Martin");
+ assertProperty(db9, Lexicon.LENGTH_IN_INCHES, DOUBLE, Lexicon.CAR,
Lexicon.LENGTH_IN_INCHES, DOUBLE, 185.5D);
+
+ SessionCache.NodeEditor editor = cache.getEditorFor(db9.getUuid());
+ editor.setProperty(Lexicon.LENGTH_IN_INCHES, value(DOUBLE, 100.0D));
+ }
+
+ @Test( expected = ConstraintViolationException.class )
+ public void shouldFailToSetPropertyToInvalidValuesOnExistingNode() throws Exception
{
+ NodeInfo root = cache.findNodeInfoForRoot();
+ NodeInfo db9 = cache.findNodeInfo(root.getUuid(),
path("/vehix:Vehicles/vehix:Cars/vehix:Sports/vehix:Aston Martin DB9"));
+ assertProperty(db9, Lexicon.MODEL, STRING, Lexicon.CAR, Lexicon.MODEL, STRING,
"DB9");
+ assertProperty(db9, Lexicon.MAKER, STRING, Lexicon.CAR, Lexicon.MAKER, STRING,
"Aston Martin");
+ assertProperty(db9, Lexicon.LENGTH_IN_INCHES, DOUBLE, Lexicon.CAR,
Lexicon.LENGTH_IN_INCHES, DOUBLE, 185.5D);
+
+ SessionCache.NodeEditor editor = cache.getEditorFor(db9.getUuid());
+ editor.setProperty(Lexicon.LENGTH_IN_INCHES, value(STRING, "This is not a
valid double"));
+ }
+
+ @Test
+ public void shouldRemoveExistingPropertyOnExistingNode() throws Exception {
+ NodeInfo root = cache.findNodeInfoForRoot();
+ NodeInfo db9 = cache.findNodeInfo(root.getUuid(),
path("/vehix:Vehicles/vehix:Cars/vehix:Sports/vehix:Aston Martin DB9"));
+ assertProperty(db9, Lexicon.MODEL, STRING, Lexicon.CAR, Lexicon.MODEL, STRING,
"DB9");
+ assertProperty(db9, Lexicon.MAKER, STRING, Lexicon.CAR, Lexicon.MAKER, STRING,
"Aston Martin");
+ assertProperty(db9, Lexicon.LENGTH_IN_INCHES, DOUBLE, Lexicon.CAR,
Lexicon.LENGTH_IN_INCHES, DOUBLE, 185.5D);
+
+ UUID db9uuid = db9.getUuid();
+ SessionCache.NodeEditor editor = cache.getEditorFor(db9uuid);
+ editor.removeProperty(Lexicon.LENGTH_IN_INCHES);
+
+ db9 = cache.findNodeInfo(db9uuid);
+ assertNoProperty(db9, Lexicon.LENGTH_IN_INCHES);
+
+ db9 = cache.findNodeInfo(root.getUuid(),
path("/vehix:Vehicles/vehix:Cars/vehix:Sports/vehix:Aston Martin DB9"));
+ assertNoProperty(db9, Lexicon.LENGTH_IN_INCHES);
+ }
}
Added: trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/Vehicles.java
===================================================================
--- trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/Vehicles.java
(rev 0)
+++ trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/Vehicles.java 2009-03-24 22:33:26 UTC
(rev 795)
@@ -0,0 +1,170 @@
+/*
+ * JBoss DNA (
http://www.jboss.org/dna)
+ * See the COPYRIGHT.txt file distributed with this work for information
+ * regarding copyright ownership. Some portions may be licensed
+ * to Red Hat, Inc. under one or more contributor license agreements.
+ * See the AUTHORS.txt file in the distribution for a full listing of
+ * individual contributors.
+ *
+ * Unless otherwise indicated, all code in JBoss DNA is licensed
+ * to you under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * JBoss DNA is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.dna.jcr;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import javax.jcr.PropertyType;
+import org.jboss.dna.graph.ExecutionContext;
+import org.jboss.dna.graph.property.Name;
+import org.jboss.dna.graph.property.basic.BasicName;
+
+/**
+ * Define the node types for the "vehix" namespace.
+ */
+public class Vehicles {
+
+ public static class Lexicon {
+ public static class Namespace {
+ public static final String URI = "http://example.com/vehicles";
+ public static final String PREFIX = "vehix";
+ }
+
+ public static final Name CAR = new BasicName(Namespace.URI, "car");
+ public static final Name AIRCRAFT = new BasicName(Namespace.URI,
"aircraft");
+
+ public static final Name MAKER = new BasicName(Namespace.URI,
"maker");
+ public static final Name MODEL = new BasicName(Namespace.URI,
"model");
+ public static final Name INTRODUCED = new BasicName(Namespace.URI,
"introduced");
+ public static final Name YEAR = new BasicName(Namespace.URI, "year");
+ public static final Name MSRP = new BasicName(Namespace.URI, "msrp");
+ public static final Name USER_RATING = new BasicName(Namespace.URI,
"userRating");
+ public static final Name VALUE_RATING = new BasicName(Namespace.URI,
"valueRating");
+ public static final Name MPG_CITY = new BasicName(Namespace.URI,
"mpgCity");
+ public static final Name MPG_HIGHWAY = new BasicName(Namespace.URI,
"mpgHighway");
+ public static final Name LENGTH_IN_INCHES = new BasicName(Namespace.URI,
"lengthInInches");
+ public static final Name WHEELBASE_IN_INCHES = new BasicName(Namespace.URI,
"wheelbaseInInches");
+ public static final Name ENGINE = new BasicName(Namespace.URI,
"engine");
+
+ public static final Name EMPTY_WEIGHT = new BasicName(Namespace.URI,
"emptyWeight");
+
+ }
+
+ public static class NodeTypeSource extends AbstractJcrNodeTypeSource {
+
+ private final List<JcrNodeType> nodeTypes;
+
+ public NodeTypeSource( ExecutionContext context,
+ JcrNodeTypeSource predecessor ) {
+ super(predecessor);
+ this.nodeTypes = new ArrayList<JcrNodeType>();
+
+ JcrNodeType base = findType(JcrNtLexicon.BASE);
+ JcrNodeType unstructured = findType(JcrNtLexicon.UNSTRUCTURED);
+
+ // Add in the "vehix:car" node type (which extends
"nt:unstructured") ...
+ JcrNodeType car = new JcrNodeType(context, NO_NODE_TYPE_MANAGER,
Lexicon.CAR,
+ Arrays.asList(new JcrNodeType[] {base}),
NO_PRIMARY_ITEM_NAME, NO_CHILD_NODES,
+ Arrays.asList(new JcrPropertyDefinition[]
{
+ new JcrPropertyDefinition(context,
null, Lexicon.MAKER,
+
OnParentVersionBehavior.COMPUTE.getJcrValue(), false,
+ false, false,
NO_DEFAULT_VALUES, PropertyType.STRING,
+
NO_CONSTRAINTS, false),
+ new JcrPropertyDefinition(context,
null, Lexicon.MODEL,
+
OnParentVersionBehavior.COMPUTE.getJcrValue(), false,
+ false, false,
NO_DEFAULT_VALUES, PropertyType.STRING,
+
NO_CONSTRAINTS, false),
+ new JcrPropertyDefinition(context,
null, Lexicon.INTRODUCED,
+
OnParentVersionBehavior.COMPUTE.getJcrValue(), false,
+ false, false,
NO_DEFAULT_VALUES, PropertyType.LONG,
+
NO_CONSTRAINTS, false),
+ /* Year IS mandatory for car */
+ new JcrPropertyDefinition(context,
null, Lexicon.YEAR,
+
OnParentVersionBehavior.COMPUTE.getJcrValue(), false,
+ true, false,
NO_DEFAULT_VALUES, PropertyType.LONG,
+
NO_CONSTRAINTS, false),
+ new JcrPropertyDefinition(context,
null, Lexicon.MSRP,
+
OnParentVersionBehavior.COMPUTE.getJcrValue(), false,
+ false, false,
NO_DEFAULT_VALUES, PropertyType.STRING,
+
NO_CONSTRAINTS, false),
+ new JcrPropertyDefinition(context,
null, Lexicon.USER_RATING,
+
OnParentVersionBehavior.COMPUTE.getJcrValue(), false,
+ false, false,
NO_DEFAULT_VALUES, PropertyType.DOUBLE,
+
NO_CONSTRAINTS, false),
+ new JcrPropertyDefinition(context,
null, Lexicon.VALUE_RATING,
+
OnParentVersionBehavior.COMPUTE.getJcrValue(), false,
+ false, false,
NO_DEFAULT_VALUES, PropertyType.DOUBLE,
+
NO_CONSTRAINTS, false),
+ new JcrPropertyDefinition(context,
null, Lexicon.MPG_CITY,
+
OnParentVersionBehavior.COMPUTE.getJcrValue(), false,
+ false, false,
NO_DEFAULT_VALUES, PropertyType.LONG,
+
NO_CONSTRAINTS, false),
+ new JcrPropertyDefinition(context,
null, Lexicon.MPG_HIGHWAY,
+
OnParentVersionBehavior.COMPUTE.getJcrValue(), false,
+ false, false,
NO_DEFAULT_VALUES, PropertyType.LONG,
+
NO_CONSTRAINTS, false),
+ new JcrPropertyDefinition(context,
null, Lexicon.LENGTH_IN_INCHES,
+
OnParentVersionBehavior.COMPUTE.getJcrValue(), false,
+ false, false,
NO_DEFAULT_VALUES, PropertyType.DOUBLE,
+
NO_CONSTRAINTS, false),
+ new JcrPropertyDefinition(context,
null, Lexicon.WHEELBASE_IN_INCHES,
+
OnParentVersionBehavior.COMPUTE.getJcrValue(), false,
+ false, false,
NO_DEFAULT_VALUES, PropertyType.DOUBLE,
+
NO_CONSTRAINTS, false),
+ new JcrPropertyDefinition(context,
null, Lexicon.ENGINE,
+
OnParentVersionBehavior.COMPUTE.getJcrValue(), false,
+ false, false,
NO_DEFAULT_VALUES, PropertyType.STRING,
+
NO_CONSTRAINTS, false),}), NOT_MIXIN,
+ ORDERABLE_CHILD_NODES);
+
+ // Add in the "vehix:aircraft" node type (which extends
"nt:unstructured") ...
+ JcrNodeType aircraft = new JcrNodeType(context, NO_NODE_TYPE_MANAGER,
Lexicon.AIRCRAFT,
+ Arrays.asList(new JcrNodeType[]
{unstructured}), NO_PRIMARY_ITEM_NAME,
+ NO_CHILD_NODES, Arrays.asList(new
JcrPropertyDefinition[] {
+ new JcrPropertyDefinition(context,
null, Lexicon.MAKER,
+
OnParentVersionBehavior.COMPUTE.getJcrValue(),
+ false,
false, false, NO_DEFAULT_VALUES,
+
PropertyType.STRING, NO_CONSTRAINTS, false),
+ new JcrPropertyDefinition(context,
null, Lexicon.MODEL,
+
OnParentVersionBehavior.COMPUTE.getJcrValue(),
+ false,
false, false, NO_DEFAULT_VALUES,
+
PropertyType.STRING, NO_CONSTRAINTS, false),
+ /* Year is NOT mandatory for
aircraft */
+ new JcrPropertyDefinition(context,
null, Lexicon.YEAR,
+
OnParentVersionBehavior.COMPUTE.getJcrValue(),
+ false,
false, false, NO_DEFAULT_VALUES,
+
PropertyType.LONG, NO_CONSTRAINTS, false),
+ new JcrPropertyDefinition(context,
null, Lexicon.INTRODUCED,
+
OnParentVersionBehavior.COMPUTE.getJcrValue(),
+ false,
false, false, NO_DEFAULT_VALUES,
+
PropertyType.LONG, NO_CONSTRAINTS, false),}),
+ NOT_MIXIN, ORDERABLE_CHILD_NODES);
+
+ nodeTypes.addAll(Arrays.asList(new JcrNodeType[] {car, aircraft,}));
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.jboss.dna.jcr.AbstractJcrNodeTypeSource#getDeclaredNodeTypes()
+ */
+ @Override
+ public Collection<JcrNodeType> getDeclaredNodeTypes() {
+ return nodeTypes;
+ }
+
+ }
+}
Property changes on: trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/Vehicles.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain