Search in sources :

Example 71 with ValueFormatException

use of javax.jcr.ValueFormatException in project jackrabbit by apache.

the class NamePropertyTest method testGetProperty.

/**
 * Since JCR 2.0 a path property can be dereferenced if it points to a
 * Property.
 * TODO: create several tests out of this one
 */
public void testGetProperty() throws RepositoryException {
    if (!multiple) {
        String path = prop.getString();
        if (prop.getParent().hasProperty(path)) {
            Property p = prop.getProperty();
            assertEquals("The name of the dereferenced property must be equal to the value", path, p.getName());
        } else {
            try {
                prop.getProperty();
                fail("Calling Property.getProperty() for a NAME value that doesn't have a corresponding Node, ItemNotFoundException is expected");
            } catch (ItemNotFoundException e) {
            // success.
            }
        }
    } else {
        try {
            prop.getProperty();
            fail("Property.getNode() called on a multivalue property " + "should throw a ValueFormatException.");
        } catch (ValueFormatException vfe) {
        // ok
        }
    }
}
Also used : ValueFormatException(javax.jcr.ValueFormatException) Property(javax.jcr.Property) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 72 with ValueFormatException

use of javax.jcr.ValueFormatException in project jackrabbit by apache.

the class NamePropertyTest method testGetDouble.

/**
 * Tests failure from Name type to Double type.
 *
 * @throws RepositoryException
 */
public void testGetDouble() throws RepositoryException {
    try {
        Value val = PropertyUtil.getValue(prop);
        val.getDouble();
        fail("Conversion from a Name value to a Double value " + "should throw a ValueFormatException.");
    } catch (ValueFormatException vfe) {
    // ok
    }
}
Also used : Value(javax.jcr.Value) ValueFormatException(javax.jcr.ValueFormatException)

Example 73 with ValueFormatException

use of javax.jcr.ValueFormatException in project jackrabbit by apache.

the class LongPropertyTest method testGetBoolean.

/**
 * Tests failure of conversion from Long type to Boolean type.
 */
public void testGetBoolean() throws RepositoryException {
    try {
        Value val = PropertyUtil.getValue(prop);
        val.getBoolean();
        fail("Conversion from a Long value to a Boolean value " + "should throw a ValueFormatException.");
    } catch (ValueFormatException vfe) {
    // ok
    }
}
Also used : Value(javax.jcr.Value) ValueFormatException(javax.jcr.ValueFormatException)

Example 74 with ValueFormatException

use of javax.jcr.ValueFormatException in project jackrabbit by apache.

the class ValueFactoryTest method testValueFormatException.

/**
 * Tests if a ValueFormatexception is thrown in case the passed string
 * cannot be converted to the required value type.
 * value creation.
 * @throws RepositoryException
 */
public void testValueFormatException() throws RepositoryException {
    Value value = null;
    for (int i = 0; i < types.length; i++) {
        switch(types[i]) {
            case PropertyType.DATE:
                try {
                    value = valueFactory.createValue(dateValueFail, PropertyType.DATE);
                    fail("Conversion from String " + dateValueFail + " to a " + PropertyType.nameFromValue(types[i]) + " value should throw ValueFormatException.");
                } catch (ValueFormatException vfe) {
                // ok
                }
                break;
            case PropertyType.DOUBLE:
                try {
                    value = valueFactory.createValue(doubleValueFail, PropertyType.DOUBLE);
                    fail("Conversion from String " + doubleValueFail + " to a " + PropertyType.nameFromValue(types[i]) + " value should throw ValueFormatException.");
                } catch (ValueFormatException vfe) {
                // ok
                }
                break;
            case PropertyType.LONG:
                try {
                    value = valueFactory.createValue(longValueFail, PropertyType.LONG);
                    fail("Conversion from String " + longValueFail + " to a " + PropertyType.nameFromValue(types[i]) + " value should throw ValueFormatException.");
                } catch (ValueFormatException vfe) {
                // ok
                }
                break;
            case PropertyType.NAME:
                try {
                    value = valueFactory.createValue(nameValueFail, PropertyType.NAME);
                    fail("Conversion from String " + nameValueFail + " to a " + PropertyType.nameFromValue(types[i]) + " value should throw ValueFormatException.");
                } catch (ValueFormatException vfe) {
                // ok
                }
                break;
            case PropertyType.PATH:
                try {
                    value = valueFactory.createValue(pathValueFail, PropertyType.PATH);
                    fail("Conversion from String " + pathValueFail + " to a " + PropertyType.nameFromValue(types[i]) + " value should throw ValueFormatException.");
                } catch (ValueFormatException vfe) {
                // ok
                }
                break;
            default:
                break;
        }
    }
}
Also used : Value(javax.jcr.Value) ValueFormatException(javax.jcr.ValueFormatException)

Example 75 with ValueFormatException

use of javax.jcr.ValueFormatException in project jackrabbit by apache.

the class SysViewContentHandler method checkAllProps.

/**
 * Checks the values of all exported properties of a given node.
 *
 * @param nodeElem The nodeElem of the given node.
 * @param skipBinary Boolean if the binary properties should be exported.
 * @throws RepositoryException
 * @throws SAXException
 * @throws IOException
 */
private void checkAllProps(NodeElemData nodeElem, boolean skipBinary) throws RepositoryException, SAXException, IOException {
    boolean allFound = false;
    boolean correctVal = false;
    Node node = nodeElem.node;
    List<PropElemData> propElems = nodeElem.propElems;
    // no props exported
    if (propElems.size() == 0) {
        // if node has properties they should be of Binary type and skipBinary should be true
        if (node.hasProperties()) {
            if (skipBinary) {
                PropertyIterator iter = node.getProperties();
                while (iter.hasNext()) {
                    Property prop = iter.nextProperty();
                    checkCondition("Property " + prop.getName() + " of node " + node.getPath() + " is not exported.", prop.getType() == PropertyType.BINARY);
                }
            } else {
                checkCondition("One or more properties of node " + node.getPath() + " are not exported.", false);
            }
        } else {
        // ok
        }
    } else {
        // compare the propElems with the properties of the given node
        for (int i = 0; i < propElems.size(); i++) {
            correctVal = false;
            PropElemData propElem = propElems.get(i);
            int propType = propElem.type;
            if (node.hasProperty(propElem.name)) {
                Property prop = node.getProperty(propElem.name);
                // compare the propTypes
                correctVal = (propType == prop.getType());
                checkCondition("Property type of property " + propElem.name + " of node " + nodeElem.path + " is not exported correctly." + "expected: " + prop.getType() + " received: " + propType, correctVal);
                // property which should be exported
                if (propType == PropertyType.BINARY && !skipBinary || propType != PropertyType.BINARY) {
                    try {
                        int size = propElem.values.size();
                        // multivalue property with empty value array
                        if (size == 0) {
                            if (prop.getDefinition().isMultiple()) {
                                long length = prop.getValues().length;
                                checkCondition("Value of property " + prop.getPath() + " is not exported.", length == 0);
                            } else {
                                checkCondition("Singler value property " + prop.getPath() + " with no value is exported.", false);
                            }
                        }
                        // single value property or multivalue property with one value
                        if (size == 1) {
                            String str = "";
                            if (prop.getDefinition().isMultiple()) {
                                str = (prop.getValues()[0]).getString();
                            } else {
                                str = prop.getString();
                            }
                            String val = propElem.values.get(0);
                            if (prop.getType() == PropertyType.BINARY) {
                                // decode value
                                val = decodeBase64(val);
                            }
                            correctVal = (str.equals(val));
                            checkCondition("Property value of property " + propElem.name + " of node " + nodeElem.path + " is not exported correctly:" + " expected value: " + str + " found value: " + val, correctVal);
                        } else // multivalue property with several values
                        {
                            Value[] vals = prop.getValues();
                            checkCondition("Number of exported values of property " + prop.getPath() + " does not match the number " + "its values", vals.length == size);
                            for (int j = 0; j < size; j++) {
                                // we know that the order of the values
                                // of a mulitval prop is preserved during export
                                String val = propElem.values.get(j);
                                if (prop.getType() == PropertyType.BINARY) {
                                    // decode value
                                    val = decodeBase64(val);
                                }
                                String str = vals[j].getString();
                                correctVal = (val.equals(str));
                                checkCondition("Property value of property " + propElem.name + " of node " + nodeElem.path + " is not exported correctly.", correctVal);
                            }
                        }
                    } catch (ValueFormatException vfe) {
                        checkCondition("Error during retreiviing the value(s)" + " of property " + prop.getPath() + vfe.toString() + " .", false);
                    }
                } else // skipBinary true and propType is Binary, should be skipped
                {
                    checkCondition("Value of binary property " + prop.getPath() + " exported although skipBinary flag is true.", propElem.values.isEmpty());
                }
            } else // given node has no property with the name given by the prop element
            {
                checkCondition("Property element " + propElem.name + " found but node " + nodeElem.node.getPath() + " does not have a property with that name", false);
            }
        }
        // compare the sizes here
        long otherSize = getSize(node.getProperties());
        allFound = propElems.size() == otherSize;
        checkCondition("Not all properties of node " + nodeElem.path + " are exported.", allFound);
    }
}
Also used : Node(javax.jcr.Node) PropertyIterator(javax.jcr.PropertyIterator) Value(javax.jcr.Value) ValueFormatException(javax.jcr.ValueFormatException) Property(javax.jcr.Property)

Aggregations

ValueFormatException (javax.jcr.ValueFormatException)106 Value (javax.jcr.Value)53 Property (javax.jcr.Property)34 RepositoryException (javax.jcr.RepositoryException)16 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)12 Calendar (java.util.Calendar)10 ItemNotFoundException (javax.jcr.ItemNotFoundException)10 Node (javax.jcr.Node)10 QValue (org.apache.jackrabbit.spi.QValue)10 PathNotFoundException (javax.jcr.PathNotFoundException)6 Name (org.apache.jackrabbit.spi.Name)6 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)5 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)5 InternalValue (org.apache.jackrabbit.core.value.InternalValue)5 InputStream (java.io.InputStream)4 BigDecimal (java.math.BigDecimal)4 Date (java.util.Date)4 Path (org.apache.jackrabbit.spi.Path)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3