jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mreut...@apache.org
Subject svn commit: r156984 [3/3] - in incubator/jackrabbit/trunk: applications/test/repository/nodetypes/ applications/test/workspaces/default/ applications/test/workspaces/test/ src/test/org/apache/jackrabbit/test/api/ src/test/org/apache/jackrabbit/test/api/nodetype/
Date Thu, 10 Mar 2005 19:38:05 GMT
Added: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/nodetype/CanSetPropertyStringTest.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/nodetype/CanSetPropertyStringTest.java?view=auto&rev=156984
==============================================================================
--- incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/nodetype/CanSetPropertyStringTest.java
(added)
+++ incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/nodetype/CanSetPropertyStringTest.java
Thu Mar 10 11:37:51 2005
@@ -0,0 +1,255 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ *                     as applicable.
+ *
+ * Licensed 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.test.api.nodetype;
+
+import org.apache.jackrabbit.test.AbstractJCRTest;
+import org.apache.jackrabbit.test.NotExecutableException;
+
+import javax.jcr.nodetype.PropertyDef;
+import javax.jcr.nodetype.NodeType;
+import javax.jcr.Session;
+import javax.jcr.PropertyType;
+import javax.jcr.RepositoryException;
+import javax.jcr.Value;
+import java.text.ParseException;
+
+/**
+ * Test of <code>NodeType.canSetProperty(String propertyName, Value
+ * value)</code> and <code>NodeType.canSetProperty(String propertyName, Value[]
+ * values)</code> where property is of type String.
+ *
+ * @test
+ * @sources CanSetPropertyStringTest.java
+ * @executeClass org.apache.jackrabbit.test.api.nodetype.CanSetPropertyStringTest
+ * @keywords level1
+ */
+public class CanSetPropertyStringTest extends AbstractJCRTest {
+    /**
+     * The session we use for the tests
+     */
+    private Session session;
+
+    /**
+     * Sets up the fixture for the test cases.
+     */
+    protected void setUp() throws Exception {
+        isReadOnly = true;
+        super.setUp();
+
+        session = helper.getReadOnlySession();
+    }
+
+    /**
+     * Releases the session aquired in {@link #setUp()}.
+     */
+    protected void tearDown() throws Exception {
+        if (session != null) {
+            session.logout();
+        }
+        super.tearDown();
+    }
+
+
+    /**
+     * Tests if NodeType.canSetProperty(String propertyName, Value value)
+     * returns true if value and its type are convertable to StringValue.
+     */
+    public void testConversions()
+            throws NotExecutableException, RepositoryException {
+
+        PropertyDef propDef =
+                NodeTypeUtil.locatePropertyDef(session, PropertyType.STRING, false, false,
false, false);
+
+        if (propDef == null) {
+            throw new NotExecutableException("No string property def that meets the " +
+                    "requirements of the test has been found");
+        }
+
+        NodeType nodeType = propDef.getDeclaringNodeType();
+
+
+        Value stringValue = NodeTypeUtil.getValueOfType(PropertyType.STRING);
+
+        assertTrue("canSetProperty(String propertyName, Value value) must return " +
+                "true if the property is of type String and value is a StringValue",
+                nodeType.canSetProperty(propDef.getName(), stringValue));
+
+        Value binaryValue = NodeTypeUtil.getValueOfType(PropertyType.BINARY);
+        assertTrue("canSetProperty(String propertyName, Value value) must return " +
+                "true if the property is of type String and value is a BinaryValue " +
+                "and is UTF-8",
+                nodeType.canSetProperty(propDef.getName(), binaryValue));
+
+        Value dateValue = NodeTypeUtil.getValueOfType(PropertyType.DATE);
+        assertTrue("canSetProperty(String propertyName, Value value) must return " +
+                "true if the property is of type String and value is a DateValue",
+                nodeType.canSetProperty(propDef.getName(), dateValue));
+
+        Value doubleValue = NodeTypeUtil.getValueOfType(PropertyType.DOUBLE);
+        assertTrue("canSetProperty(String propertyName, Value value) must return " +
+                "true if the property is of type String and value is a DoubleValue",
+                nodeType.canSetProperty(propDef.getName(), doubleValue));
+
+        Value longValue = NodeTypeUtil.getValueOfType(PropertyType.LONG);
+        assertTrue("canSetProperty(String propertyName, Value value) must return " +
+                "true if the property is of type String and value is a LongValue",
+                nodeType.canSetProperty(propDef.getName(), longValue));
+
+        Value booleanValue = NodeTypeUtil.getValueOfType(PropertyType.BOOLEAN);
+        assertTrue("canSetProperty(String propertyName, Value value) must return " +
+                "true if the property is of type String and value is a BooleanValue",
+                nodeType.canSetProperty(propDef.getName(), booleanValue));
+
+        Value nameValue = NodeTypeUtil.getValueOfType(PropertyType.NAME);
+        assertTrue("canSetProperty(String propertyName, Value value) must return " +
+                "true if the property is of type String and value is a NameValue",
+                nodeType.canSetProperty(propDef.getName(), nameValue));
+
+        Value pathValue = NodeTypeUtil.getValueOfType(PropertyType.PATH);
+        assertTrue("canSetProperty(String propertyName, Value value) must return " +
+                "true if the property is of type String and value is a PathValue",
+                nodeType.canSetProperty(propDef.getName(), pathValue));
+    }
+
+    /**
+     * Tests if NodeType.canSetProperty(String propertyName, Value[] values)
+     * returns true if all values and its types are convertible to StringValue.
+     */
+    public void testConversionsMultiple()
+            throws NotExecutableException, RepositoryException {
+
+        PropertyDef propDef =
+                NodeTypeUtil.locatePropertyDef(session, PropertyType.STRING, true, false,
false, false);
+
+        if (propDef == null) {
+            throw new NotExecutableException("No multiple string property def that meets
the " +
+                    "requirements of the test has been found");
+        }
+
+        NodeType nodeType = propDef.getDeclaringNodeType();
+
+
+        Value stringValue = NodeTypeUtil.getValueOfType(PropertyType.STRING);
+        Value stringValues[] = {stringValue};
+        assertTrue("canSetProperty(String propertyName, Value[] values) must return " +
+                "true if the property is of type String and values are of type StringValue",
+                nodeType.canSetProperty(propDef.getName(), stringValues));
+
+        Value binaryValue = NodeTypeUtil.getValueOfType(PropertyType.BINARY);
+        Value binaryValues[] = {binaryValue};
+        assertTrue("canSetProperty(String propertyName, Value[] values) must return " +
+                "true if the property is of type String and values are of type BinaryValue
" +
+                "and is UTF-8",
+                nodeType.canSetProperty(propDef.getName(), binaryValues));
+
+        Value dateValue = NodeTypeUtil.getValueOfType(PropertyType.DATE);
+        Value dateValues[] = {dateValue};
+        assertTrue("canSetProperty(String propertyName, Value[] values) must return " +
+                "true if the property is of type String and values are of type DateValue",
+                nodeType.canSetProperty(propDef.getName(), dateValues));
+
+        Value doubleValue = NodeTypeUtil.getValueOfType(PropertyType.DOUBLE);
+        Value doubleValues[] = {doubleValue};
+        assertTrue("canSetProperty(String propertyName, Value[] values) must return " +
+                "true if the property is of type String and values are of type DoubleValue",
+                nodeType.canSetProperty(propDef.getName(), doubleValues));
+
+        Value longValue = NodeTypeUtil.getValueOfType(PropertyType.LONG);
+        Value longValues[] = {longValue};
+        assertTrue("canSetProperty(String propertyName, Value[] values) must return " +
+                "true if the property is of type String and values are of type LongValue",
+                nodeType.canSetProperty(propDef.getName(), longValues));
+
+        Value booleanValue = NodeTypeUtil.getValueOfType(PropertyType.BOOLEAN);
+        Value booleanValues[] = {booleanValue};
+        assertTrue("canSetProperty(String propertyName, Value[] values) must return " +
+                "true if the property is of type String and values are of type BooleanValue",
+                nodeType.canSetProperty(propDef.getName(), booleanValues));
+
+        Value nameValue = NodeTypeUtil.getValueOfType(PropertyType.NAME);
+        Value nameValues[] = {nameValue};
+        assertTrue("canSetProperty(String propertyName, Value[] values) must return " +
+                "true if the property is of type String and values are of type NameValue",
+                nodeType.canSetProperty(propDef.getName(), nameValues));
+
+        Value pathValue = NodeTypeUtil.getValueOfType(PropertyType.PATH);
+        Value pathValues[] = {pathValue};
+        assertTrue("canSetProperty(String propertyName, Value[] values) must return " +
+                "true if the property is of type String and values are of type PathValue",
+                nodeType.canSetProperty(propDef.getName(), pathValues));
+    }
+
+    /**
+     * Tests if canSetProperty(String propertyName, Value value) returns false
+     * if value does not match the value constraints of the property def
+     */
+    public void testOutOfValueConstraint()
+            throws NotExecutableException, ParseException, RepositoryException {
+
+        PropertyDef propDef =
+                NodeTypeUtil.locatePropertyDef(session, PropertyType.STRING, false, false,
true, false);
+
+        if (propDef == null) {
+            throw new NotExecutableException("No string property def with " +
+                    "testable value constraints has been found");
+        }
+
+        Value value = NodeTypeUtil.getValueOutOfContstraint(propDef);
+        if (value == null) {
+            // value should never be null since this is catched already in locatePropertyDef
+            throw new NotExecutableException("No string property def with " +
+                    "testable value constraints has been found");
+        }
+
+        NodeType nodeType = propDef.getDeclaringNodeType();
+
+        assertFalse("canSetProperty(String propertyName, Value value) must " +
+                "return false if value does not match the value constraints.",
+                nodeType.canSetProperty(propDef.getName(), value));
+    }
+
+    /**
+     * Tests if canSetProperty(String propertyName, Value[] values) returns
+     * false if values do not match the value constraints of the property def
+     */
+    public void testOutOfValueConstraintMultiple()
+            throws NotExecutableException, ParseException, RepositoryException {
+
+        PropertyDef propDef =
+                NodeTypeUtil.locatePropertyDef(session, PropertyType.STRING, true, false,
true, false);
+
+        if (propDef == null) {
+            throw new NotExecutableException("No multiple string property def with " +
+                    "testable value constraints has been found");
+        }
+
+        Value value = NodeTypeUtil.getValueOutOfContstraint(propDef);
+        if (value == null) {
+            // value should never be null since this is catched already in locatePropertyDef
+            throw new NotExecutableException("No multiple string property def with " +
+                    "testable value constraints has been found");
+        }
+
+        NodeType nodeType = propDef.getDeclaringNodeType();
+        Value values[] = {value};
+
+        assertFalse("canSetProperty(String propertyName, Value[] values) must " +
+                "return false if values do not match the value constraints.",
+                nodeType.canSetProperty(propDef.getName(), values));
+    }
+
+}
\ No newline at end of file

Propchange: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/nodetype/CanSetPropertyStringTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/nodetype/CanSetPropertyTest.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/nodetype/CanSetPropertyTest.java?view=auto&rev=156984
==============================================================================
--- incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/nodetype/CanSetPropertyTest.java
(added)
+++ incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/nodetype/CanSetPropertyTest.java
Thu Mar 10 11:37:51 2005
@@ -0,0 +1,134 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ *                     as applicable.
+ *
+ * Licensed 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.test.api.nodetype;
+
+import org.apache.jackrabbit.test.AbstractJCRTest;
+import org.apache.jackrabbit.test.NotExecutableException;
+
+import javax.jcr.nodetype.NodeType;
+import javax.jcr.nodetype.PropertyDef;
+import javax.jcr.Session;
+import javax.jcr.RepositoryException;
+import javax.jcr.PropertyType;
+import javax.jcr.Value;
+
+/**
+ * Test of <code>NodeType.canSetProperty(String propertyName, Value
+ * value)</code>
+ *
+ * @test
+ * @sources CanSetPropertyTest.java
+ * @executeClass org.apache.jackrabbit.test.api.nodetype.CanSetPropertyTest
+ * @keywords level1
+ */
+public class CanSetPropertyTest extends AbstractJCRTest {
+    /**
+     * The session we use for the tests
+     */
+    private Session session;
+
+
+    /**
+     * Sets up the fixture for the test cases.
+     */
+    protected void setUp() throws Exception {
+        isReadOnly = true;
+        super.setUp();
+
+        session = helper.getReadOnlySession();
+    }
+
+    /**
+     * Releases the session aquired in {@link #setUp()}.
+     */
+    protected void tearDown() throws Exception {
+        if (session != null) {
+            session.logout();
+        }
+        super.tearDown();
+    }
+
+    /**
+     * Tests if NodeType.canSetProperty(String propertyName, Value value)
+     * returns false if the property is protected.
+     */
+    public void testReturnFalseBecauseIsProtected()
+            throws NotExecutableException, RepositoryException {
+
+        PropertyDef propDef =
+                NodeTypeUtil.locatePropertyDef(session, PropertyType.UNDEFINED, false, true,
false, false);
+
+        // will never happen since at least jcr:primaryType of nt:base accomplish the request
+        if (propDef == null) {
+            throw new NotExecutableException("No protected property def found.");
+        }
+
+        NodeType nodeType = propDef.getDeclaringNodeType();
+        Value value = NodeTypeUtil.getValueOfType(propDef.getRequiredType());
+
+        assertFalse("canSetProperty(String propertyName, Value value) must " +
+                "return false if the property is protected.",
+                nodeType.canSetProperty(propDef.getName(), value));
+    }
+
+
+    /**
+     * Tests if NodeType.canSetProperty(String propertyName, Value value)
+     * returns false if the property is multiple
+     */
+    public void testReturnFalseBecauseIsMultiple()
+            throws NotExecutableException, RepositoryException {
+
+        PropertyDef propDef =
+                NodeTypeUtil.locatePropertyDef(session, PropertyType.UNDEFINED, true, false,
false, false);
+
+        if (propDef == null) {
+            throw new NotExecutableException("No multiple, not protected property def found.");
+        }
+
+        NodeType nodeType = propDef.getDeclaringNodeType();
+        Value value = NodeTypeUtil.getValueOfType(propDef.getRequiredType());
+
+        assertFalse("canSetProperty(String propertyName, Value value) must " +
+                "return false if the property is multiple.",
+                nodeType.canSetProperty(propDef.getName(), value));
+    }
+
+    /**
+     * Tests if canSetProperty(String propertyName, Value value) where value is
+     * null returns the same as canRemoveItem
+     */
+    public void testValueNull()
+            throws NotExecutableException, RepositoryException {
+
+        PropertyDef propDef =
+                NodeTypeUtil.locatePropertyDef(session, PropertyType.UNDEFINED, false, false,
false, false);
+
+        if (propDef == null) {
+            throw new NotExecutableException("No not protected property def found.");
+        }
+
+        NodeType nodeType = propDef.getDeclaringNodeType();
+
+        assertEquals("nodeType.canSetProperty(String propertyName, Value value) " +
+                "where value is null must return the same result as " +
+                "nodeType.canRemoveItem(String propertyName).",
+                nodeType.canRemoveItem(propDef.getName()),
+                nodeType.canSetProperty(propDef.getName(), (Value) null));
+    }
+
+}
\ No newline at end of file

Propchange: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/nodetype/CanSetPropertyTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/nodetype/NodeTypeUtil.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/nodetype/NodeTypeUtil.java?view=auto&rev=156984
==============================================================================
--- incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/nodetype/NodeTypeUtil.java
(added)
+++ incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/nodetype/NodeTypeUtil.java
Thu Mar 10 11:37:51 2005
@@ -0,0 +1,461 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ *                     as applicable.
+ *
+ * Licensed 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.test.api.nodetype;
+
+import javax.jcr.nodetype.PropertyDef;
+import javax.jcr.nodetype.NodeTypeManager;
+import javax.jcr.nodetype.NodeTypeIterator;
+import javax.jcr.nodetype.NodeType;
+import javax.jcr.Session;
+import javax.jcr.RepositoryException;
+import javax.jcr.PropertyType;
+import javax.jcr.Value;
+import javax.jcr.ValueFormatException;
+import javax.jcr.BinaryValue;
+import javax.jcr.BooleanValue;
+import javax.jcr.DateValue;
+import javax.jcr.DoubleValue;
+import javax.jcr.LongValue;
+import javax.jcr.NameValue;
+import javax.jcr.PathValue;
+import javax.jcr.StringValue;
+import javax.jcr.util.ISO8601;
+import java.util.Calendar;
+import java.util.regex.Pattern;
+import java.util.regex.Matcher;
+import java.text.ParseException;
+
+/**
+ * Utility class to locate item definitions in the NodeTyeManager.
+ */
+class NodeTypeUtil {
+
+    /**
+     * Locate a property def parsing all node types
+     *
+     * @param session:      the session to access the node types
+     * @param propertyType: the type of the returned property. <cod>PropertyType.UNDEFINED</code>
+     *                      returns a property of any type
+     * @param multiple:     if true, the returned <code>PropertyDef</code> is
+     *                      multiple, else not
+     * @param isProtected:  if true, the returned <code>PropertyDef</code> is
+     *                      protected, else not
+     * @param residual:     if true, the returned <code>PropertyDef</code> is
of
+     *                      the residual name "*", else not
+     * @return the first <code>PropertyDef</code> found fitting the
+     *         requirements
+     */
+    public static PropertyDef locatePropertyDef(Session session,
+                                                int propertyType,
+                                                boolean multiple,
+                                                boolean isProtected,
+                                                boolean constraints,
+                                                boolean residual)
+            throws RepositoryException {
+
+        NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
+        NodeTypeIterator types = manager.getAllNodeTypes();
+
+        while (types.hasNext()) {
+            NodeType type = types.nextNodeType();
+            PropertyDef propDefs[] = type.getDeclaredPropertyDefs();
+            for (int i = 0; i < propDefs.length; i++) {
+                PropertyDef propDef = propDefs[i];
+
+                // PropertyType.UNDEFINED is in use to get a property of any type
+                if (propertyType != PropertyType.UNDEFINED &&
+                        propDef.getRequiredType() != propertyType) {
+                    continue;
+                }
+
+                if (multiple && !propDef.isMultiple()) {
+                    continue;
+                }
+                if (!multiple && propDef.isMultiple()) {
+                    continue;
+                }
+
+                if (isProtected && !propDef.isProtected()) {
+                    continue;
+                }
+                if (!isProtected && propDef.isProtected()) {
+                    continue;
+                }
+
+                String vc[] = propDef.getValueConstraints();
+                if (!constraints && vc != null && vc.length > 0) {
+                    continue;
+                }
+                if (constraints) {
+                    // property def with constraints requested
+                    if (vc == null || vc.length == 0) {
+                        // property def has no constraints
+                        continue;
+                    }
+                    try {
+                        // check if a value out of constraint is buildable
+                        Value v = getValueOutOfContstraint(propDef);
+                        if (v == null) {
+                            // no value out of the constraint range available
+                            continue;
+                        }
+                    } catch (ParseException e) {
+                        // an error occured
+                        continue;
+                    }
+                }
+
+                if (!residual && propDef.getName().equals("*")) {
+                    continue;
+                }
+
+                return propDef;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Returns any value of the requested type
+     */
+    public static Value getValueOfType(int type) throws ValueFormatException {
+        switch (type) {
+            case (PropertyType.BINARY):
+                {
+                    // note: If binary is not UTF-8 behavior is implementation-specific
+                    return new BinaryValue("abc");
+                }
+            case (PropertyType.BOOLEAN):
+                {
+                    return new BooleanValue(true);
+                }
+            case (PropertyType.DATE):
+                {
+                    return new DateValue(Calendar.getInstance());
+                }
+            case (PropertyType.DOUBLE):
+                {
+                    return new DoubleValue(1.0);
+                }
+            case (PropertyType.LONG):
+                {
+                    return new LongValue(1);
+                }
+            case (PropertyType.NAME):
+                {
+                    return NameValue.valueOf("abc");
+                }
+            case (PropertyType.PATH):
+                {
+                    return PathValue.valueOf("/abc");
+                }
+            default:
+                {
+                    // STRING and UNDEFINED
+                    // note: REFERENCE is not testable since its format is implementation-specific
+                    return new StringValue("abc");
+                }
+        }
+    }
+
+    public static Value getValueOutOfContstraint(PropertyDef propDef)
+            throws ValueFormatException, RepositoryException, ParseException {
+
+        int type = propDef.getRequiredType();
+        String constraints[] = propDef.getValueConstraints();
+
+        if (constraints == null || constraints.length == 0) {
+            return null;
+        }
+
+        switch (type) {
+            case (PropertyType.BINARY):
+                {
+                    long absMin = 0;
+                    long absMax = 0;
+                    // boundless vars indicate min/max without bounds,
+                    // if constraint is e.g.(min,) or [,max]
+                    boolean maxBoundless = false;
+                    boolean minBoundless = false;
+
+                    // find smallest min and largest max value
+                    for (int i = 0; i < constraints.length; i++) {
+                        if (!minBoundless) {
+                            String minStr = getConstraintMin(constraints[i]);
+                            if (minStr == null) {
+                                minBoundless = true;
+                            } else {
+                                long min = Long.valueOf(minStr).longValue();
+                                if (min < absMin) {
+                                    absMin = min;
+                                }
+                            }
+                        }
+                        if (!maxBoundless) {
+                            String maxStr = getConstraintMax(constraints[i]);
+                            if (maxStr == null) {
+                                maxBoundless = true;
+                            } else {
+                                long max = Long.valueOf(maxStr).longValue();
+                                if (max > absMax) {
+                                    absMax = max;
+                                }
+                            }
+                        }
+                    }
+                    if (!minBoundless && absMin > 1) {
+                        return new BinaryValue("0");
+                    } else if (!maxBoundless) {
+                        // build a binary value of size > absMax
+                        StringBuffer content = new StringBuffer();
+                        for (int i = 0; i <= absMax; i = i + 10) {
+                            content.append("0123456789");
+                        }
+                        return new BinaryValue(content.toString());
+                    } else {
+                        return null;
+                    }
+                }
+
+            case (PropertyType.BOOLEAN):
+                {
+                    if (constraints.length > 1) {
+                        return null; // silly constraint
+                    }
+                    boolean value = !Boolean.valueOf(constraints[0]).booleanValue();
+                    return new BooleanValue(value);
+                }
+
+            case (PropertyType.DATE):
+                {
+                    Calendar absMin = null;
+                    Calendar absMax = null;
+
+                    // boundless vars indicate min/max without bounds,
+                    // if constraint is e.g.(min,) or [,max]
+                    boolean maxBoundless = false;
+                    boolean minBoundless = false;
+
+                    // find smallest min and largest max value
+                    for (int i = 0; i < constraints.length; i++) {
+                        if (!minBoundless) {
+                            String minStr = getConstraintMin(constraints[i]);
+                            if (minStr == null) {
+                                minBoundless = true;
+                            } else {
+                                Calendar min = ISO8601.parse(minStr);
+                                if (absMin == null || min.before(absMin)) {
+                                    absMin = min;
+                                }
+                            }
+                        }
+                        if (!maxBoundless) {
+                            String maxStr = getConstraintMax(constraints[i]);
+                            if (maxStr == null) {
+                                maxBoundless = true;
+                            } else {
+                                Calendar max = ISO8601.parse(maxStr);
+                                if (absMax == null || max.after(absMax)) {
+                                    absMax = max;
+                                }
+                            }
+                        }
+                    }
+                    if (!minBoundless) {
+                        absMin.setTimeInMillis(absMin.getTimeInMillis() - 1);
+                        return new DateValue(absMin);
+                    } else if (!maxBoundless) {
+                        absMax.setTimeInMillis(absMax.getTimeInMillis() + 1);
+                        return new DateValue(absMax);
+                    } else {
+                        return null;
+                    }
+                }
+
+            case (PropertyType.DOUBLE):
+                {
+                    double absMin = 0;
+                    double absMax = 0;
+
+                    // boundless vars indicate min/max without bounds,
+                    // if constraint is e.g.(min,) or [,max]
+                    boolean maxBoundless = false;
+                    boolean minBoundless = false;
+
+                    // find smallest min and largest max value
+                    for (int i = 0; i < constraints.length; i++) {
+                        if (!minBoundless) {
+                            String minStr = getConstraintMin(constraints[i]);
+                            if (minStr == null) {
+                                minBoundless = true;
+                            } else {
+                                double min = Double.valueOf(minStr).doubleValue();
+                                if (min < absMin) {
+                                    absMin = min;
+                                }
+                            }
+                        }
+                        if (!maxBoundless) {
+                            String maxStr = getConstraintMax(constraints[i]);
+                            if (maxStr == null) {
+                                maxBoundless = true;
+                            } else {
+                                double max = Double.valueOf(maxStr).doubleValue();
+                                if (max > absMax) {
+                                    absMax = max;
+                                }
+                            }
+                        }
+                    }
+                    if (!minBoundless) {
+                        return new DoubleValue(absMin - 1);
+                    } else if (!maxBoundless) {
+                        return new DoubleValue(absMax + 1);
+                    } else {
+                        return null;
+                    }
+                }
+
+            case (PropertyType.LONG):
+                {
+                    long absMin = 0;
+                    long absMax = 0;
+
+                    // boundless vars indicate min/max without bounds,
+                    // if constraint is e.g.(min,) or [,max]
+                    boolean maxBoundless = false;
+                    boolean minBoundless = false;
+
+                    // find smallest min and largest max value
+                    for (int i = 0; i < constraints.length; i++) {
+                        if (!minBoundless) {
+                            String minStr = getConstraintMin(constraints[i]);
+                            if (minStr == null) {
+                                minBoundless = true;
+                            } else {
+                                long min = Long.valueOf(minStr).longValue();
+                                if (min < absMin) {
+                                    absMin = min;
+                                }
+                            }
+                        }
+                        if (!maxBoundless) {
+                            String maxStr = getConstraintMax(constraints[i]);
+                            if (maxStr == null) {
+                                maxBoundless = true;
+                            } else {
+                                long max = Long.valueOf(maxStr).longValue();
+                                if (max > absMax) {
+                                    absMax = max;
+                                }
+                            }
+                        }
+                    }
+                    if (!minBoundless) {
+                        return new LongValue(absMin - 1);
+                    } else if (!maxBoundless) {
+                        return new LongValue(absMax + 1);
+                    } else {
+                        return null;
+                    }
+                }
+
+            case (PropertyType.NAME):
+                {
+                    // build a name that is for sure not part of the constraints
+                    StringBuffer name = new StringBuffer("X");
+                    for (int i = 0; i < constraints.length; i++) {
+                        name.append(constraints[i].replaceAll(":", ""));
+                    }
+                    return NameValue.valueOf(name.toString());
+                }
+
+            case (PropertyType.PATH):
+                {
+                    // build a path that is for sure not part of the constraints
+                    StringBuffer path = new StringBuffer("X");
+                    for (int i = 0; i < constraints.length; i++) {
+                        path.append(constraints[i]);
+                    }
+                    String pathStr = path.toString();
+
+                    // replace colon to avoid /a/x:b + y:c => /a/x:b:y:c
+                    // where x:b:y:c is not a legal path element
+                    pathStr = pathStr.replaceAll(":", "");
+                    pathStr = pathStr.replaceAll("\\*", "");
+                    pathStr = pathStr.replaceAll("//", "/");
+
+                    return PathValue.valueOf(pathStr);
+                }
+
+            case (PropertyType.UNDEFINED):
+                {
+                    return null;
+                }
+
+            default:
+                {
+                    // build a string that will probably not be part of the constraints
+                    StringBuffer value = new StringBuffer("X");
+                    for (int i = 0; i < constraints.length; i++) {
+                        value.append(constraints[i]);
+                    }
+
+                    // test if value does not match any of the constraints
+                    for (int i = 0; i < constraints.length; i++) {
+                        Pattern pattern = Pattern.compile(constraints[i]);
+                        Matcher matcher = pattern.matcher(value);
+                        if (matcher.matches()) {
+                            return null;
+                        }
+                    }
+                    return new StringValue(value.toString());
+                }
+        }
+    }
+
+    // ------------------------< internal >-------------------------------------
+
+    /**
+     * Get the min value (as string) of a numeric/date constraint string
+     */
+    private static String getConstraintMin(String constraint) {
+        String min = constraint.substring(0, constraint.indexOf(","));
+        min = min.replaceAll("\\(", "");
+        min = min.replaceAll("\\[", "");
+        min = min.replaceAll(" ", "");
+        if (min.equals("")) {
+            min = null;
+        }
+        return min;
+    }
+
+    /**
+     * Get the max value (as string) of a numeric/date constraint string
+     */
+    private static String getConstraintMax(String constraint) {
+        String max = constraint.substring(constraint.indexOf(",") + 1);
+        max = max.replaceAll("\\)", "");
+        max = max.replaceAll("\\]", "");
+        max = max.replaceAll(" ", "");
+        if (max.equals("")) {
+            max = null;
+        }
+        return max;
+    }
+
+}

Propchange: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/nodetype/NodeTypeUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/nodetype/TestAll.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/nodetype/TestAll.java?view=diff&r1=156983&r2=156984
==============================================================================
--- incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/nodetype/TestAll.java
(original)
+++ incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/nodetype/TestAll.java
Thu Mar 10 11:37:51 2005
@@ -44,6 +44,17 @@
 
         suite.addTestSuite(PredefinedNodeTypeTest.class);
 
+        suite.addTestSuite(CanSetPropertyBinaryTest.class);
+        suite.addTestSuite(CanSetPropertyBooleanTest.class);
+        suite.addTestSuite(CanSetPropertyDateTest.class);
+        suite.addTestSuite(CanSetPropertyDoubleTest.class);
+        suite.addTestSuite(CanSetPropertyLongTest.class);
+        suite.addTestSuite(CanSetPropertyMultipleTest.class);
+        suite.addTestSuite(CanSetPropertyNameTest.class);
+        suite.addTestSuite(CanSetPropertyPathTest.class);
+        suite.addTestSuite(CanSetPropertyStringTest.class);
+        suite.addTestSuite(CanSetPropertyTest.class);
+
         return suite;
     }
 }



Mime
View raw message