You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by mr...@apache.org on 2005/02/16 16:20:49 UTC
svn commit: r154030 [1/2] - in incubator/jackrabbit/trunk:
applications/test/ src/test/org/apache/jackrabbit/init/
src/test/org/apache/jackrabbit/test/api/
Author: mreutegg
Date: Wed Feb 16 07:20:44 2005
New Revision: 154030
URL: http://svn.apache.org/viewcvs?view=rev&rev=154030
Log:
Adding level 1 property test cases
Added:
incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/init/PropertyTestData.java (with props)
incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/AbstractPropertyTest.java (with props)
incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/BinaryPropertyTest.java (with props)
incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/BooleanPropertyTest.java (with props)
incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/DatePropertyTest.java (with props)
incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/DoublePropertyTest.java (with props)
incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/LongPropertyTest.java (with props)
incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/NamePropertyTest.java (with props)
incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/PathPropertyTest.java (with props)
incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/PropertyUtil.java (with props)
incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/ReferencePropertyTest.java (with props)
incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/StringPropertyTest.java (with props)
incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/UndefinedPropertyTest.java (with props)
Modified:
incubator/jackrabbit/trunk/applications/test/repositoryStubImpl.properties
incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/init/NodeTestData.java
incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/init/TestAll.java
incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/TestAll.java
Modified: incubator/jackrabbit/trunk/applications/test/repositoryStubImpl.properties
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/applications/test/repositoryStubImpl.properties?view=diff&r1=154029&r2=154030
==============================================================================
--- incubator/jackrabbit/trunk/applications/test/repositoryStubImpl.properties (original)
+++ incubator/jackrabbit/trunk/applications/test/repositoryStubImpl.properties Wed Feb 16 07:20:44 2005
@@ -54,6 +54,36 @@
# Test class: PropertyTypeTest
javax.jcr.tck.PropertyTypeTest.testroot=/
+# Test class: BinaryPropertyTest
+javax.jcr.tck.BinaryPropertyTest.testRoot=/
+
+# Test class: BooleanPropertyTest
+javax.jcr.tck.BooleanPropertyTest.testRoot=/
+
+# Test class: DatePropertyTest
+javax.jcr.tck.DatePropertyTest.testRoot=/
+
+# Test class: DoublePropertyTest
+javax.jcr.tck.DoublePropertyTest.testRoot=/
+
+# Test class: LongPropertyTest
+javax.jcr.tck.LongPropertyTest.testRoot=/
+
+# Test class: NamePropertyTest
+javax.jcr.tck.NamePropertyTest.testRoot=/
+
+# Test class: PathPropertyTest
+javax.jcr.tck.PathPropertyTest.testRoot=/
+
+# Test class: ReferencePropertyTest
+javax.jcr.tck.ReferencePropertyTest.testRoot=/
+
+# Test class: StringPropertyTest
+javax.jcr.tck.StringPropertyTest.testRoot=/
+
+# Test class: UndefinedPropertyTest
+javax.jcr.tck.UndefinedPropertyTest.testRoot=/
+
# ==============================================================================
# JAVAX.JCR.QUERY CONFIGURATION
# ==============================================================================
Modified: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/init/NodeTestData.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/init/NodeTestData.java?view=diff&r1=154029&r2=154030
==============================================================================
--- incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/init/NodeTestData.java (original)
+++ incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/init/NodeTestData.java Wed Feb 16 07:20:44 2005
@@ -35,6 +35,9 @@
/** Path pointing to the test root */
private static final String TEST_DATA_PATH = "testdata/node";
+ /** The encoding for the test resource */
+ private static final String ENCODING = "ISO-8859-1";
+
/** Resolved QName for nt:resource */
private String ntResource;
@@ -66,7 +69,7 @@
* Creates two nodes under {@link #TEST_DATA_PATH}: one of type
* nt:resource and a second node referencing the first.
*/
- public void testFillInSearchData() throws RepositoryException, IOException {
+ public void testFillInTestData() throws RepositoryException, IOException {
if (superuser.getRootNode().hasNode(TEST_DATA_PATH)) {
// delete previous data
superuser.getRootNode().getNode(TEST_DATA_PATH).remove();
@@ -85,11 +88,11 @@
}
Node resource = dataRoot.addNode("myResource", ntResource);
- resource.setProperty(jcrEncoding, "ISO-8859-1");
+ resource.setProperty(jcrEncoding, ENCODING);
resource.setProperty(jcrMimeType, "text/plain");
ByteArrayOutputStream data = new ByteArrayOutputStream();
- OutputStreamWriter writer = new OutputStreamWriter(data, "ISO-8859-1");
- writer.write("Hello world.");
+ OutputStreamWriter writer = new OutputStreamWriter(data, ENCODING);
+ writer.write("Hello wörld.");
writer.close();
resource.setProperty(jcrData, new ByteArrayInputStream(data.toByteArray()));
resource.setProperty(jcrLastModified, Calendar.getInstance());
Added: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/init/PropertyTestData.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/init/PropertyTestData.java?view=auto&rev=154030
==============================================================================
--- incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/init/PropertyTestData.java (added)
+++ incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/init/PropertyTestData.java Wed Feb 16 07:20:44 2005
@@ -0,0 +1,67 @@
+/*
+ * 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.init;
+
+import org.apache.jackrabbit.test.AbstractJCRTest;
+
+import javax.jcr.RepositoryException;
+import javax.jcr.Node;
+import javax.jcr.PathValue;
+import java.io.IOException;
+import java.util.StringTokenizer;
+import java.util.Calendar;
+
+/**
+ * Sets up test data required for level 1 property test cases.
+ */
+public class PropertyTestData extends AbstractJCRTest {
+
+ /** Path pointing to the test root */
+ private static final String TEST_DATA_PATH = "testdata/property";
+
+ /**
+ * Creates a test node at {@link #TEST_DATA_PATH} with a boolean,
+ * double, long, calendar and a path property.
+ */
+ public void testFillInTestData() throws RepositoryException, IOException {
+ if (superuser.getRootNode().hasNode(TEST_DATA_PATH)) {
+ // delete previous data
+ superuser.getRootNode().getNode(TEST_DATA_PATH).remove();
+ superuser.save();
+ }
+ // create nodes to testPath
+ StringTokenizer names = new StringTokenizer(TEST_DATA_PATH, "/");
+ Node dataRoot = superuser.getRootNode();
+ while (names.hasMoreTokens()) {
+ String name = names.nextToken();
+ if (!dataRoot.hasNode(name)) {
+ dataRoot = dataRoot.addNode(name, testNodeType);
+ } else {
+ dataRoot = dataRoot.getNode(name);
+ }
+ }
+
+ dataRoot.setProperty("boolean", true);
+ dataRoot.setProperty("double", Math.PI);
+ dataRoot.setProperty("long", 90834953485278298l);
+ Calendar c = Calendar.getInstance();
+ c.set(2005, 6, 18, 17, 30);
+ dataRoot.setProperty("calendar", c);
+ dataRoot.setProperty("path", PathValue.valueOf("/"));
+ superuser.save();
+ }
+}
Propchange: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/init/PropertyTestData.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/init/TestAll.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/init/TestAll.java?view=diff&r1=154029&r2=154030
==============================================================================
--- incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/init/TestAll.java (original)
+++ incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/init/TestAll.java Wed Feb 16 07:20:44 2005
@@ -39,6 +39,7 @@
suite.addTestSuite(QueryTestData.class);
suite.addTestSuite(NodeTestData.class);
+ suite.addTestSuite(PropertyTestData.class);
return suite;
}
Added: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/AbstractPropertyTest.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/AbstractPropertyTest.java?view=auto&rev=154030
==============================================================================
--- incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/AbstractPropertyTest.java (added)
+++ incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/AbstractPropertyTest.java Wed Feb 16 07:20:44 2005
@@ -0,0 +1,74 @@
+/*
+ * 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;
+
+import org.apache.jackrabbit.test.AbstractJCRTest;
+import org.apache.jackrabbit.test.NotExecutableException;
+
+import javax.jcr.Session;
+import javax.jcr.Property;
+import javax.jcr.PropertyType;
+import javax.jcr.Value;
+
+/**
+ * Provides the common setup method for all level 1 property tests.
+ */
+abstract class AbstractPropertyTest extends AbstractJCRTest {
+
+ /** String encoding in a stream */
+ protected static String UTF8 = "UTF-8";
+
+ /** A read only session */
+ protected Session session;
+
+ /* The property under test */
+ protected Property prop;
+
+ /** <code>true</code> if the property is multi valued */
+ protected boolean multiple;
+
+ /**
+ * Concrete subclasses return the type of property they test. One of the
+ * values defined in {@link javax.jcr.PropertyType}.
+ */
+ protected abstract int getPropertyType();
+
+ /**
+ * Sets up the fixture for the tests.
+ */
+ protected void setUp() throws Exception {
+ isReadOnly = true;
+ super.setUp();
+ session = helper.getReadOnlySession();
+
+ prop = PropertyUtil.searchProp(session, session.getRootNode(), getPropertyType());
+ if (prop == null) {
+ String msg = "Workspace does not contain a node with a " +
+ PropertyType.nameFromValue(getPropertyType()) + " property.";
+ throw new NotExecutableException(msg);
+ }
+ multiple = prop.getDefinition().isMultiple();
+ Value val = PropertyUtil.getValue(prop);
+ if (val == null) {
+ String msg = PropertyType.nameFromValue(getPropertyType()) +
+ " property does not contain a value";
+ throw new NotExecutableException(msg);
+ }
+ }
+
+
+}
Propchange: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/AbstractPropertyTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/BinaryPropertyTest.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/BinaryPropertyTest.java?view=auto&rev=154030
==============================================================================
--- incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/BinaryPropertyTest.java (added)
+++ incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/BinaryPropertyTest.java Wed Feb 16 07:20:44 2005
@@ -0,0 +1,308 @@
+/*
+ * 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;
+
+import org.apache.jackrabbit.test.NotExecutableException;
+
+import javax.jcr.PropertyType;
+import javax.jcr.Value;
+import javax.jcr.RepositoryException;
+import javax.jcr.ValueFormatException;
+import java.io.InputStream;
+import java.io.BufferedInputStream;
+import java.io.IOException;
+
+/**
+ * Tests a binary property. If the workspace does not contain a node with a
+ * binary property a {@link org.apache.jackrabbit.test.NotExecutableException}
+ * is thrown.
+ *
+ * @test
+ * @sources BinaryPropertyTest.java
+ * @executeClass org.apache.jackrabbit.test.api.BinaryPropertyTest
+ * @keywords level1
+ */
+public class BinaryPropertyTest extends AbstractPropertyTest {
+
+ /**
+ * Returns {@link javax.jcr.PropertyType#BINARY}.
+ * @return {@link javax.jcr.PropertyType#BINARY}.
+ */
+ protected int getPropertyType() {
+ return PropertyType.BINARY;
+ }
+
+ /**
+ * Tests that when Value.getStream() is called a second time the same Stream
+ * object is returned. Also tests that when a new Value object is requested
+ * also a new Stream object is returned by calling getStream() on the new
+ * Value object.
+ */
+ public void testSameStream() throws RepositoryException, IOException {
+ Value val = PropertyUtil.getValue(prop);
+ InputStream in = val.getStream();
+ InputStream in2 = val.getStream();
+ Value otherVal = PropertyUtil.getValue(prop);
+ InputStream in3 = otherVal.getStream();
+ assertSame("Same InputStream object expected when " +
+ "Value.getStream is called twice.", in, in2);
+ assertNotSame("Value.getStream() called on a new value " +
+ "object should return a different Stream object.", in, in3);
+ }
+
+ /**
+ * Tests the failure of calling Property.getStream() on a multivalue
+ * property.
+ */
+ public void testMultiValue() throws RepositoryException, IOException {
+ if (multiple) {
+ InputStream in = null;
+ try {
+ in = prop.getStream();
+ fail("Calling getStream() on a multivalue property " +
+ "should throw a ValueFormatException.");
+ } catch (ValueFormatException vfe) {
+ // ok
+ } finally {
+ if (in != null) {
+ in.close();
+ }
+ }
+ }
+ }
+
+ /**
+ * Tests that Property.getStream() delivers the same as Value.getStream().
+ * We check this by reading each byte of the two streams and assuring that
+ * they are equal.
+ */
+ public void testValue() throws IOException, RepositoryException {
+ Value val = PropertyUtil.getValue(prop);
+ InputStream in = val.getStream();
+ InputStream in2 = prop.getStream();
+ int b = in.read();
+ while (b != -1) {
+ int b2 = in2.read();
+ assertEquals("Value.getStream() and Property.getStream() " +
+ "return different values.", b, b2);
+ b = in.read();
+ }
+ assertEquals("Value.getStream() and Property.getStream() " +
+ "return different values.", -1, in2.read());
+ }
+
+ /**
+ * Tests the conversion from a Binary value to a String value and tests
+ * that the received string is encoded in UTF-8.
+ */
+ public void testGetString() throws NotExecutableException, RepositoryException, IOException {
+ BufferedInputStream in = null;
+ try {
+ Value val = PropertyUtil.getValue(prop);
+ Value otherVal = prop.getValue();
+ in = new BufferedInputStream(val.getStream());
+ String str = otherVal.getString();
+ // read the stream into a byte array and compare the result
+ // of getString with that array.
+ // hopefully the binary property is not too long
+ byte[] utf8bytes = str.getBytes(UTF8);
+ byte b[] = new byte[1];
+ int i = 0;
+ while (in.read(b) != -1) {
+ assertEquals("The string received with Value.getString() " +
+ "on a Stream value is not utf-8 encoded.", b[0], utf8bytes[i]);
+ i++;
+ }
+ } catch (ValueFormatException e) {
+ // throwing a ValueFormatException is permitted when the stream
+ // cannot be turned into a proper UTF-8 string
+ throw new NotExecutableException("Binary property value cannot be converted to a String");
+ } finally {
+ if (in != null) {
+ in.close();
+ }
+ }
+ }
+
+ /**
+ * Tests conversion from Binary type to Boolean type. This is done via
+ * String conversion.
+ */
+ public void testGetBoolean() throws RepositoryException {
+ Value val = PropertyUtil.getValue(prop);
+ String str = val.getString();
+ boolean bool = val.getBoolean();
+ assertEquals("Wrong conversion from Binary to Boolean.",
+ new Boolean(bool), Boolean.valueOf(str));
+ }
+
+ /**
+ * Tests conversion from Binary type to Date type. This is done via String
+ * conversion.
+ */
+ public void testGetDate() throws RepositoryException {
+ Value val = PropertyUtil.getValue(prop);
+ if (PropertyUtil.isDateFormat(val.getString())) {
+ val.getDate();
+ } else {
+ try {
+ val.getDate();
+ fail("Conversion from a malformed String to a Date " +
+ "should throw a ValueFormatException.");
+ } catch (ValueFormatException vfe) {
+ // ok
+ }
+ }
+ }
+
+ /**
+ * Tests conversion from Binary type to Double type. This is done via String
+ * conversion.
+ */
+ public void testGetDouble() throws RepositoryException {
+ Value val = PropertyUtil.getValue(prop);
+ String str = val.getString();
+ // double
+ try {
+ Double.parseDouble(str);
+ double d = val.getDouble();
+ assertEquals("Wrong conversion from Binary to Double",
+ new Double(d), Double.valueOf(str));
+ } catch (NumberFormatException nfe) {
+ try {
+ val.getDouble();
+ fail("Conversion from malformed Binary to Double " +
+ "should throw ValueFormatException.");
+ } catch (ValueFormatException vfe) {
+ // ok
+ }
+ }
+ }
+
+ /**
+ * Tests conversion from Binary type to Long type. This is done via String
+ * conversion.
+ */
+ public void testGetLong() throws RepositoryException {
+ Value val = PropertyUtil.getValue(prop);
+ String str = val.getString();
+ try {
+ Long.parseLong(str);
+ long l = val.getLong();
+ assertEquals("Wrong conversion from Binary to Long",
+ new Long(l), Long.valueOf(str));
+ } catch (NumberFormatException nfe) {
+ try {
+ val.getLong();
+ fail("Conversion from malformed Binary to Long " +
+ "should throw ValueFormatException.");
+ } catch (ValueFormatException vfe) {
+ // ok
+ }
+ }
+ }
+
+ /**
+ * Tests if Value.getType() returns the same as Property.getType() and
+ * also tests that prop.getDefinition().getRequiredType() returns the same
+ * type in case it is not of Undefined type.
+ */
+ public void testGetType() throws RepositoryException {
+ assertTrue("Value.getType() returns wrong type.",
+ PropertyUtil.checkGetType(prop, PropertyType.BINARY));
+ }
+
+ /**
+ * Tests the conversion from Binary type to Reference type. This conversion
+ * passes through previous String conversion.
+ */
+ public void testAsReference() throws RepositoryException, NotExecutableException {
+ if (!multiple) {
+ if (!PropertyUtil.isUUID(prop.getString())) {
+ try {
+ prop.getNode();
+ fail("Conversion from a Path value to a Reference value " +
+ "should throw a ValueFormatException");
+ } catch (ValueFormatException vfe) {
+ //ok
+ }
+ } else {
+ prop.getNode();
+ }
+ } else {
+ try {
+ prop.getNode();
+ fail("Property.getNode() called on a multivalue property " +
+ "should throw a ValueFormatException.");
+ } catch (ValueFormatException vfe) {
+ // ok
+ }
+ }
+ }
+
+ /**
+ * Tests the Property.getLength() method.
+ */
+ public void testGetLength() throws RepositoryException {
+ if (multiple) {
+ try {
+ prop.getLength();
+ fail("Property.getLength() called on a multivalue property " +
+ "should throw a ValueFormatException.");
+
+ } catch (ValueFormatException vfe) {
+ // ok
+ }
+ } else {
+ long length = prop.getLength();
+ if (length > -1) {
+ long bytes = PropertyUtil.countBytes(prop.getValue());
+ if (bytes != -1) {
+ assertEquals("Property.getLength() returns wrong number of bytes.",
+ bytes, length);
+ }
+
+ }
+ }
+ }
+
+ /**
+ * Tests the Property.getLengths() method. The test is successful, if either
+ * -1 is returned
+ */
+ public void testGetLengths() throws RepositoryException {
+ if (multiple) {
+ Value[] values = prop.getValues();
+ long[] lengths = prop.getLengths();
+ for (int i = 0; i < lengths.length; i++) {
+ long length = PropertyUtil.countBytes(values[i]);
+ assertEquals("Property.getLengths() returns " +
+ "wrong array of the lengths of a multivalue property.",
+ length, lengths[i]);
+ }
+ } else {
+ try {
+ prop.getLengths();
+ fail("Property.getLengths() called on a sinlge value property " +
+ "should throw a ValueFormatException.");
+
+ } catch (ValueFormatException vfe) {
+ // ok
+ }
+ }
+ }
+}
\ No newline at end of file
Propchange: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/BinaryPropertyTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/BooleanPropertyTest.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/BooleanPropertyTest.java?view=auto&rev=154030
==============================================================================
--- incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/BooleanPropertyTest.java (added)
+++ incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/BooleanPropertyTest.java Wed Feb 16 07:20:44 2005
@@ -0,0 +1,244 @@
+/*
+ * 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;
+
+import javax.jcr.PropertyType;
+import javax.jcr.Value;
+import javax.jcr.RepositoryException;
+import javax.jcr.ValueFormatException;
+import java.io.IOException;
+import java.io.BufferedInputStream;
+import java.io.InputStream;
+
+/**
+ * Tests a boolean property. If the workspace does not contain a node with
+ * a boolean property a {@link org.apache.jackrabbit.test.NotExecutableException}
+ * is thrown.
+ *
+ * @test
+ * @sources BooleanPropertyTest.java
+ * @executeClass org.apache.jackrabbit.test.api.BooleanPropertyTest
+ * @keywords level1
+ */
+public class BooleanPropertyTest extends AbstractPropertyTest {
+
+ /**
+ * Returns {@link javax.jcr.PropertyType#BOOLEAN}.
+ * @return {@link javax.jcr.PropertyType#BOOLEAN}.
+ */
+ protected int getPropertyType() {
+ return PropertyType.BOOLEAN;
+ }
+
+ /**
+ * Tests that Property.getBoolean() delivers the same as Value.getBoolean()
+ * and that in case of a multivalue property Property.getBoolean()
+ * throws a ValueFormatException.
+ */
+ public void testValue() throws RepositoryException {
+ if (multiple) {
+ try {
+ prop.getBoolean();
+ fail("Property.getBoolean() called on a multivalue property " +
+ "should throw a ValueFormatException.");
+ } catch (ValueFormatException vfe) {
+ // ok
+ }
+ }
+ boolean bool = prop.getValue().getBoolean();
+ boolean otherBool = prop.getBoolean();
+ assertTrue("Value.getBoolean() and Property.getBoolean() " +
+ "return different values.", bool == otherBool) ;
+ }
+
+ /**
+ * Tests failure of conversion from Boolean type to Date type.
+ */
+ public void testGetDate() throws RepositoryException {
+ try {
+ Value val = PropertyUtil.getValue(prop);
+ val.getDate();
+ fail("Conversion from a Boolean value to a Date value " +
+ "should throw a ValueFormatException");
+ } catch (ValueFormatException vfe) {
+ //ok
+ }
+ }
+
+ /**
+ * Tests failure from Boolean type to Double type.
+ */
+ public void testGetDouble() throws RepositoryException {
+ try {
+ Value val = PropertyUtil.getValue(prop);
+ val.getDouble();
+ fail("Conversion from a Boolean value to a Double value " +
+ "should throw a ValueFormatException");
+ } catch (ValueFormatException vfe) {
+ //ok
+ }
+ }
+
+ /**
+ * Tests failure of conversion from Boolean type to Long type.
+ */
+ public void testGetLong() throws RepositoryException {
+ try {
+ Value val = PropertyUtil.getValue(prop);
+ val.getLong();
+ fail("Conversion from a Boolean value to a Long value " +
+ "should throw a ValueFormatException");
+ } catch (ValueFormatException vfe) {
+ //ok
+ }
+ }
+
+ /**
+ * Tests conversion from Boolean type to Binary type.
+ */
+ public void testGetStream() throws RepositoryException, IOException {
+ Value val = PropertyUtil.getValue(prop);
+ BufferedInputStream in = new BufferedInputStream(val.getStream());
+ Value otherVal = prop.getValue();
+ InputStream ins = null;
+ byte[] utf8bytes = otherVal.getString().getBytes(UTF8);
+ // if yet utf-8 encoded these bytes should be equal
+ // to the ones received from the stream
+ int i = 0;
+ byte[] b = new byte[1];
+ while (in.read(b) != -1) {
+ assertTrue("Boolean as a Stream is not utf-8 encoded",
+ b[0] == utf8bytes[i]);
+ i++;
+ }
+ try {
+ val.getBoolean();
+ fail("Non stream method call after stream method call " +
+ "should throw an IllegalStateException");
+ } catch (IllegalStateException ise) {
+ //ok
+ }
+ try {
+ ins = otherVal.getStream();
+ fail("Stream method call after a non stream method call " +
+ "should throw an IllegalStateException");
+ } catch (IllegalStateException ise) {
+ // ok
+ }
+ finally {
+ if (in != null) in.close();
+ if (ins != null) ins.close();
+ }
+ }
+
+ /**
+ * Tests the conversion from a Boolean to a String Value.
+ */
+ public void testGetString() throws RepositoryException {
+ Value val = PropertyUtil.getValue(prop);
+ String str = val.getString();
+ String otherStr = new Boolean(val.getBoolean()).toString();
+ assertEquals("Conversion from a Boolean value to a String value failed.",
+ str, otherStr);
+ }
+
+ /**
+ * Tests if Value.getType() returns the same as Property.getType()
+ * and also tests that prop.getDefinition().getRequiredType() returns
+ * the same type in case it is not of Undefined type.
+ */
+ public void testGetType() throws RepositoryException {
+ assertTrue("Value.getType() returns wrong type.",
+ PropertyUtil.checkGetType(prop, PropertyType.BOOLEAN));
+ }
+
+ /**
+ * Tests failure of conversion from Boolean type to Reference type.
+ */
+ public void testAsReference() throws RepositoryException {
+ if (!multiple) {
+ try {
+ prop.getNode();
+ fail("Conversion from a Boolean value to a Reference value " +
+ "should throw a ValueFormatException");
+ } catch (ValueFormatException vfe) {
+ //ok
+ }
+ }
+ else {
+ try {
+ prop.getNode();
+ fail("Property.getNode() called on a multivalue property " +
+ "should throw a ValueFormatException.");
+ } catch (ValueFormatException vfe) {
+ // ok
+ }
+ }
+ }
+
+ /**
+ * Tests the Property.getLength() method. The length returned is either -1
+ * or it is the length of the string received by conversion.
+ */
+ public void testGetLength() throws RepositoryException {
+ if (multiple) {
+ try {
+ prop.getLength();
+ fail("Property.getLength() called on a multivalue property " +
+ "should throw a ValueFormatException.");
+
+ } catch (ValueFormatException vfe) {
+ // ok
+ }
+ }
+ else {
+ long length = prop.getLength();
+ if (length > -1) {
+ assertEquals("Property.getLength() returns wrong number of bytes.",
+ length, prop.getString().length());
+ }
+ }
+ }
+
+ /**
+ * Tests the Property.getLengths() method. The returned values are either -1
+ * or the lengths of the according conversions to strings.
+ */
+ public void testGetLengths() throws RepositoryException {
+ if (multiple) {
+ Value[] values = prop.getValues();
+ long[] lengths = prop.getLengths();
+ for (int i = 0; i < lengths.length; i++) {
+ if (lengths[i] > -1) {
+ assertEquals("Property.getLengths() returns " +
+ "wrong array of the lengths of a multivalue property.",
+ values[i].getString().length(), lengths[i]);
+ }
+ }
+ }
+ else {
+ try {
+ prop.getLengths();
+ fail("Property.getLengths() called on a sinlge value property " +
+ "should throw a ValueFormatException.");
+
+ } catch (ValueFormatException vfe) {
+ // ok
+ }
+ }
+ }
+}
\ No newline at end of file
Propchange: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/BooleanPropertyTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/DatePropertyTest.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/DatePropertyTest.java?view=auto&rev=154030
==============================================================================
--- incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/DatePropertyTest.java (added)
+++ incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/DatePropertyTest.java Wed Feb 16 07:20:44 2005
@@ -0,0 +1,245 @@
+/*
+ * 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;
+
+import javax.jcr.PropertyType;
+import javax.jcr.Value;
+import javax.jcr.RepositoryException;
+import javax.jcr.ValueFormatException;
+import java.util.Calendar;
+import java.io.IOException;
+import java.io.BufferedInputStream;
+import java.io.InputStream;
+
+/**
+ * Tests a date property. If the workspace does not contain a node with a date
+ * property a {@link org.apache.jackrabbit.test.NotExecutableException} is
+ * thrown.
+ *
+ * @test
+ * @sources DatePropertyTest.java
+ * @executeClass org.apache.jackrabbit.test.api.DatePropertyTest
+ * @keywords level1
+ */
+public class DatePropertyTest extends AbstractPropertyTest {
+
+ /**
+ * Returns {@link javax.jcr.PropertyType#DATE}.
+ * @return {@link javax.jcr.PropertyType#DATE}.
+ */
+ protected int getPropertyType() {
+ return PropertyType.DATE;
+ }
+
+ /**
+ * Tests that Property.getDate() delivers the same as Value.getDate() and
+ * that in case of a multivalue property a ValueFormatException is thrown.
+ */
+ public void testValue() throws RepositoryException {
+ if (multiple) {
+ try {
+ prop.getDate();
+ fail("Property.getDate() called on a multivalue property " +
+ "should throw a ValueFormatException.");
+ } catch (ValueFormatException vfe) {
+ // ok
+ }
+ }
+ // Calendar returned?
+ Calendar calendar = prop.getValue().getDate();
+ Calendar calendar2 = prop.getDate();
+ assertTrue("Value.getDate() and Property.getDate() return different values.",
+ calendar.getTimeInMillis() == calendar2.getTimeInMillis());
+ }
+
+ /**
+ * Tests if a calendar is returned and if the conversion to a string has
+ * correct format.
+ */
+ public void testGetString() throws RepositoryException {
+ Value val = PropertyUtil.getValue(prop);
+ // correct format: YYYY-MM-DDThh:mm:ss.sssTZD
+ // month(01-12), day(01-31), hours(00-23), minutes(00-59), seconds(00-59),
+ // TZD(Z or +hh:mm or -hh:mm)
+ //String aDay="2005-01-19T15:34:15.917+01:00";
+ String date = val.getString();
+ System.out.println("date str = " + date);
+ boolean match = PropertyUtil.isDateFormat(prop.getString());
+ assertTrue("Date not in correct String format.", match);
+ }
+
+ /**
+ * Tests failure of conversion from Date type to Boolean type.
+ */
+ public void testGetBoolean() throws RepositoryException {
+ try {
+ Value val = PropertyUtil.getValue(prop);
+ val.getBoolean();
+ fail("Conversion from a Date value to a Boolean value " +
+ "should throw a ValueFormatException.");
+ } catch (ValueFormatException vfe) {
+ //ok
+ }
+ }
+
+ /**
+ * Tests conversion from Date type to Double type.
+ */
+ public void testGetDouble() throws RepositoryException {
+ Value val = PropertyUtil.getValue(prop);
+ double d = val.getDouble();
+ long mili = val.getDate().getTimeInMillis();
+ assertEquals("Conversion from a Date value to a Double value " +
+ "returns a different number of miliseconds.", mili, (long) d);
+ }
+
+ /**
+ * Tests conversion from Date type to Long type.
+ */
+ public void testGetLong() throws RepositoryException {
+ Value val = PropertyUtil.getValue(prop);
+ long l = val.getLong();
+ long mili = val.getDate().getTimeInMillis();
+ assertEquals("Conversion from a Date value to a Long value " +
+ "returns a different number of miliseconds.", mili, l);
+ }
+
+ /**
+ * Tests conversion from Date type to Binary type.
+ */
+ public void testGetStream() throws RepositoryException, IOException {
+ Value val = PropertyUtil.getValue(prop);
+ BufferedInputStream in = new BufferedInputStream(val.getStream());
+ Value otherVal = prop.getValue();
+ InputStream ins = null;
+ byte[] utf8bytes = otherVal.getString().getBytes(UTF8);
+ // if yet utf-8 encoded these bytes should be equal
+ // to the ones received from the stream
+ int i = 0;
+ byte b[] = new byte[1];
+ while (in.read(b) != -1) {
+ assertTrue("Date as a Stream is not utf-8 encoded.",
+ b[0] == utf8bytes[i]);
+ i++;
+ }
+ try {
+ val.getDate();
+ fail("Non stream method call after stream method call " +
+ "should throw an IllegalStateException.");
+ } catch (IllegalStateException ise) {
+ //ok
+ }
+ try {
+ ins = otherVal.getStream();
+ fail("Stream method call after a non stream method call " +
+ "should throw an IllegalStateException.");
+ } catch (IllegalStateException ise) {
+ // ok
+ } finally {
+ if (in != null) {
+ in.close();
+ }
+ if (ins != null) {
+ ins.close();
+ }
+ }
+ }
+
+ /**
+ * Tests if Value.getType() returns the same as Property.getType() and
+ * also tests that prop.getDefinition().getRequiredType() returns the same
+ * type in case it is not of Undefined type.
+ */
+ public void testGetType() throws RepositoryException {
+ assertTrue("Value.getType() returns wrong type.",
+ PropertyUtil.checkGetType(prop, PropertyType.DATE));
+ }
+
+
+ /**
+ * Tests failure of conversion from Date type to Reference type.
+ */
+ public void testAsReference() throws RepositoryException {
+ if (!multiple) {
+ try {
+ prop.getNode();
+ fail("Conversion from a Date value to a Reference value " +
+ "should throw a ValueFormatException.");
+ } catch (ValueFormatException vfe) {
+ //ok
+ }
+ } else {
+ try {
+ prop.getNode();
+ fail("Property.getNode() called on a multivalue property " +
+ "should throw a ValueFormatException.");
+ } catch (ValueFormatException vfe) {
+ // ok
+ }
+ }
+ }
+
+ /**
+ * Tests the Property.getLength() method. The length returned is either -1
+ * or it is the length of the string received by conversion.
+ */
+ public void testGetLength() throws RepositoryException {
+ if (multiple) {
+ try {
+ prop.getLength();
+ fail("Property.getLength() called on a multivalue property " +
+ "should throw a ValueFormatException.");
+
+ } catch (ValueFormatException vfe) {
+ // ok
+ }
+ } else {
+ long length = prop.getLength();
+ if (length > -1) {
+ assertEquals("Property.getLength() returns wrong number of bytes.",
+ length, prop.getString().length());
+ }
+ }
+ }
+
+ /**
+ * Tests the Property.getLengths() method. The returned values are either -1
+ * or the lengths of the according conversions to strings.
+ */
+ public void testGetLengths() throws RepositoryException {
+ if (multiple) {
+ Value[] values = prop.getValues();
+ long[] lengths = prop.getLengths();
+ for (int i = 0; i < lengths.length; i++) {
+ if (lengths[i] > -1) {
+ assertEquals("Property.getLengths() returns " +
+ "wrong array of the lengths of a multivalue property.",
+ values[i].getString().length(), lengths[i]);
+ }
+ }
+ } else {
+ try {
+ prop.getLengths();
+ fail("Property.getLengths() called on a sinlge value property " +
+ "should throw a ValueFormatException.");
+
+ } catch (ValueFormatException vfe) {
+ // ok
+ }
+ }
+ }
+}
\ No newline at end of file
Propchange: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/DatePropertyTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/DoublePropertyTest.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/DoublePropertyTest.java?view=auto&rev=154030
==============================================================================
--- incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/DoublePropertyTest.java (added)
+++ incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/DoublePropertyTest.java Wed Feb 16 07:20:44 2005
@@ -0,0 +1,234 @@
+/*
+ * 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;
+
+import javax.jcr.PropertyType;
+import javax.jcr.RepositoryException;
+import javax.jcr.ValueFormatException;
+import javax.jcr.Value;
+import java.util.Calendar;
+import java.io.BufferedInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * Tests a double property. If the workspace does not contain a node with a double
+ * property a {@link org.apache.jackrabbit.test.NotExecutableException} is
+ * thrown.
+ *
+ * @test
+ * @sources DoublePropertyTest.java
+ * @executeClass org.apache.jackrabbit.test.api.DoublePropertyTest
+ * @keywords level1
+ */
+public class DoublePropertyTest extends AbstractPropertyTest {
+
+ /**
+ * Returns {@link javax.jcr.PropertyType#DOUBLE}.
+ * @return {@link javax.jcr.PropertyType#DOUBLE}.
+ */
+ protected int getPropertyType() {
+ return PropertyType.DOUBLE;
+ }
+
+ /**
+ * tests that Property.getDouble() delivers the same as Value.getDouble()
+ * and if in case of a multivalue property a ValueFormatException is thrown.
+ */
+ public void testValue() throws RepositoryException {
+ if (multiple) {
+ try {
+ prop.getDouble();
+ fail("Property.getDouble() called on a multivalue property " +
+ "should throw a ValueFormatException.");
+ } catch (ValueFormatException vfe) {
+ // ok
+ }
+ }
+ double d = prop.getValue().getDouble();
+ double dd = prop.getDouble();
+ assertTrue("Value.getDouble() and Property.getDouble() return different values.", d == dd) ;
+ }
+
+ /**
+ * tests failure of conversion from Double type to Boolean type
+ */
+ public void testGetBoolean() throws RepositoryException {
+ try {
+ Value val = PropertyUtil.getValue(prop);
+ val.getBoolean();
+ fail("Conversion from a Double value to a Boolean value " +
+ "should throw a ValueFormatException.");
+ } catch (ValueFormatException vfe) {
+ //ok
+ }
+ }
+
+ /**
+ * tests conversion from Double type to Date type
+ */
+ public void testGetDate() throws RepositoryException {
+ Value val = PropertyUtil.getValue(prop);
+ Calendar calendar = val.getDate();
+ assertEquals("Conversion from Double value to Date value is not correct.",
+ calendar.getTimeInMillis(), new Double(val.getDouble()).longValue());
+ }
+ /**
+ * tests the conversion from a Double to a Long Value
+ */
+ public void testGetLong() throws RepositoryException {
+ Value val = PropertyUtil.getValue(prop);
+ long l = val.getLong();
+ long ll = new Double(val.getDouble()).longValue();
+ assertTrue("Conversion from Double value to Long value is not correct.", l == ll);
+ }
+
+ /**
+ * tests conversion from Double type to Binary type
+ */
+ public void testGetStream() throws RepositoryException, IOException {
+ Value val = PropertyUtil.getValue(prop);
+ BufferedInputStream in = new BufferedInputStream(val.getStream());
+ Value otherVal = prop.getValue();
+ InputStream ins = null;
+ byte[] utf8bytes = otherVal.getString().getBytes();
+ // if yet utf-8 encoded these bytes should be equal
+ // to the ones received from the stream
+ int i = 0;
+ byte b = b = (byte) in.read();
+ while (b != -1) {
+ assertTrue("Double as a Stream is not utf-8 encoded.",
+ b == utf8bytes[i]);
+ b = (byte) in.read();
+ i++;
+ }
+ try {
+ val.getDouble();
+ fail("Non stream method call after stream method call " +
+ "should throw an IllegalStateException.");
+ } catch (IllegalStateException ise) {
+ //ok
+ }
+ try {
+ ins = otherVal.getStream();
+ fail("Stream method call after a non stream method call " +
+ "should throw an IllegalStateException.");
+ } catch (IllegalStateException ise) {
+ // ok
+ }
+ finally {
+ if (in != null) in.close();
+ if (ins != null) ins.close();
+ }
+ }
+
+ /**
+ * tests the conversion from a Double to a String Value
+ */
+ public void testGetString() throws RepositoryException {
+ Value val = PropertyUtil.getValue(prop);
+ String str = val.getString();
+ String otherStr = Double.toString(val.getDouble());
+ assertEquals("Conversion from Double value to String value is not correct.", str, otherStr);
+ }
+
+ /**
+ * Tests if Value.getType() returns the same as Property.getType()
+ * and also tests that prop.getDefinition().getRequiredType() returns
+ * the same type in case it is not of Undefined type.
+ */
+ public void testGetType() throws RepositoryException {
+ assertTrue("Value.getType() returns wrong type.",
+ PropertyUtil.checkGetType(prop, PropertyType.DOUBLE));
+ }
+
+ /**
+ * tests failure of conversion from Double type to Reference type
+ */
+ public void testAsReference() throws RepositoryException {
+ if (!multiple) {
+ try {
+ prop.getNode();
+ fail("Conversion from a Double value to a Reference value " +
+ "should throw a ValueFormatException.");
+ } catch (ValueFormatException vfe) {
+ //ok
+ }
+ }
+ else {
+ try {
+ prop.getNode();
+ fail("Property.getNode() called on a multivalue property " +
+ "should throw a ValueFormatException.");
+ } catch (ValueFormatException vfe) {
+ // ok
+ }
+ }
+ }
+
+ /**
+ * Tests the Property.getLength() method. The length returned is either -1
+ * or it is the length of the string received by conversion.
+ */
+ public void testGetLength() throws RepositoryException {
+ if (multiple) {
+ try {
+ prop.getLength();
+ fail("Property.getLength() called on a multivalue property " +
+ "should throw a ValueFormatException.");
+
+ } catch (ValueFormatException vfe) {
+ // ok
+ }
+ }
+ else {
+ long length = prop.getLength();
+ if (length > -1) {
+ assertEquals("Property.getLength() returns wrong number of bytes.",
+ length, prop.getString().length());
+ }
+ }
+ }
+
+ /**
+ * Tests the Property.getLengths() method. The returned values are either -1
+ * or the lengths of the according conversions to strings.
+ */
+ public void testGetLengths() throws RepositoryException {
+ if (multiple) {
+ Value[] values = prop.getValues();
+ long[] lengths = prop.getLengths();
+ for (int i = 0; i < lengths.length; i++) {
+ if (lengths[i] > -1) {
+ assertEquals("Property.getLengths() returns " +
+ "wrong array of the lengths of a multivalue property.",
+ values[i].getString().length(), lengths[i]);
+ }
+ }
+ }
+ else {
+ try {
+ prop.getLengths();
+ fail("Property.getLengths() called on a sinlge value property " +
+ "should throw a ValueFormatException.");
+
+ } catch (ValueFormatException vfe) {
+ // ok
+ }
+ }
+ }
+}
\ No newline at end of file
Propchange: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/DoublePropertyTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/LongPropertyTest.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/LongPropertyTest.java?view=auto&rev=154030
==============================================================================
--- incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/LongPropertyTest.java (added)
+++ incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/LongPropertyTest.java Wed Feb 16 07:20:44 2005
@@ -0,0 +1,236 @@
+/*
+ * 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;
+
+import javax.jcr.PropertyType;
+import javax.jcr.RepositoryException;
+import javax.jcr.ValueFormatException;
+import javax.jcr.Value;
+import java.util.Calendar;
+import java.io.IOException;
+import java.io.BufferedInputStream;
+import java.io.InputStream;
+
+/**
+ * Tests a long property. If the workspace does not contain a node with a long
+ * property a {@link org.apache.jackrabbit.test.NotExecutableException} is
+ * thrown.
+ *
+ * @test
+ * @sources LongPropertyTest.java
+ * @executeClass org.apache.jackrabbit.test.api.LongPropertyTest
+ * @keywords level1
+ */
+public class LongPropertyTest extends AbstractPropertyTest {
+
+ /**
+ * Returns {@link javax.jcr.PropertyType#LONG}.
+ *
+ * @return {@link javax.jcr.PropertyType#LONG}.
+ */
+ protected int getPropertyType() {
+ return PropertyType.LONG;
+ }
+
+ /**
+ * Tests that Property.getLong() delivers the same as Value.getLong() and if
+ * in case of a multivalue property a ValueFormatException is thrown.
+ */
+ public void testValue() throws RepositoryException {
+ if (multiple) {
+ try {
+ prop.getLong();
+ fail("Property.getLong() called on a multivalue property " +
+ "should throw a ValueFormatException.");
+ } catch (ValueFormatException vfe) {
+ // ok
+ }
+ }
+ long l = prop.getValue().getLong();
+ long ll = prop.getLong();
+ assertEquals("Value.getLong() and Property.getLong() return different values.", l, ll);
+ }
+
+ /**
+ * 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
+ }
+ }
+
+ /**
+ * Tests conversion from Long type to Date type.
+ */
+ public void testGetDate() throws RepositoryException {
+ Value val = PropertyUtil.getValue(prop);
+ Calendar calendar = val.getDate();
+ assertEquals("Conversion from Long value to Date value is not correct.",
+ val.getLong(), calendar.getTimeInMillis());
+ }
+
+ /**
+ * Tests conversion from Long type to Double type.
+ */
+ public void testGetDouble() throws RepositoryException {
+ Value val = PropertyUtil.getValue(prop);
+ double d = val.getDouble();
+ assertEquals("Conversion from Long value to Double value is not correct.",
+ new Long(val.getLong()).doubleValue(), d, 0d);
+ }
+
+ /**
+ * Tests conversion from Long type to Binary type.
+ */
+ public void testGetStream() throws RepositoryException, IOException {
+ Value val = PropertyUtil.getValue(prop);
+ BufferedInputStream in = new BufferedInputStream(val.getStream());
+ Value otherVal = prop.getValue();
+ InputStream ins = null;
+ byte[] utf8bytes = otherVal.getString().getBytes(UTF8);
+ // if yet utf-8 encoded these bytes should be equal
+ // to the ones received from the stream
+ int i = 0;
+ byte b[] = new byte[1];
+ while (in.read(b) != -1) {
+ assertEquals("Long as a Stream is not utf-8 encoded.",
+ utf8bytes[i], b[0]);
+ i++;
+ }
+ try {
+ val.getLong();
+ fail("Non stream method call after stream method call " +
+ "should throw an IllegalStateException.");
+ } catch (IllegalStateException ise) {
+ //ok
+ }
+ try {
+ ins = otherVal.getStream();
+ fail("Stream method call after a non stream method call " +
+ "should throw an IllegalStateException.");
+ } catch (IllegalStateException ise) {
+ // ok
+ } finally {
+ if (in != null) {
+ in.close();
+ }
+ if (ins != null) {
+ ins.close();
+ }
+ }
+ }
+
+ /**
+ * Tests the conversion from a Long to a String Value.
+ */
+ public void testGetString() throws RepositoryException {
+ Value val = PropertyUtil.getValue(prop);
+ String str = val.getString();
+ String otherStr = new Long(val.getLong()).toString();
+ assertEquals("Conversion from a Long value to a String value is not correct",
+ str, otherStr);
+ }
+
+ /**
+ * Tests if Value.getType() returns the same as Property.getType() and also
+ * tests that prop.getDefinition().getRequiredType() returns the same type
+ * in case it is not of Undefined type.
+ */
+ public void testGetType() throws RepositoryException {
+ assertTrue("Value.getType() returns wrong type.",
+ PropertyUtil.checkGetType(prop, PropertyType.LONG));
+ }
+
+ /**
+ * Tests failure of conversion from Long type to Reference type.
+ */
+ public void testAsReference() throws RepositoryException {
+ if (!multiple) {
+ try {
+ prop.getNode();
+ fail("Conversion from a Double value to a Reference value " +
+ "should throw a ValueFormatException.");
+ } catch (ValueFormatException vfe) {
+ //ok
+ }
+ } else {
+ try {
+ prop.getNode();
+ fail("Property.getNode() called on a multivalue property " +
+ "should throw a ValueFormatException.");
+ } catch (ValueFormatException vfe) {
+ // ok
+ }
+ }
+ }
+
+ /**
+ * Tests the Property.getLength() method. The length returned is either -1
+ * or it is the length of the string received by conversion.
+ */
+ public void testGetLength() throws RepositoryException {
+ if (multiple) {
+ try {
+ prop.getLength();
+ fail("Property.getLength() called on a multivalue property " +
+ "should throw a ValueFormatException.");
+
+ } catch (ValueFormatException vfe) {
+ // ok
+ }
+ } else {
+ long length = prop.getLength();
+ if (length > -1) {
+ assertEquals("Property.getLength() returns wrong number of bytes.",
+ length, prop.getString().length());
+ }
+ }
+ }
+
+ /**
+ * Tests the Property.getLengths() method. The returned values are either -1
+ * or the lengths of the according conversions to strings.
+ */
+ public void testGetLengths() throws RepositoryException {
+ if (multiple) {
+ Value[] values = prop.getValues();
+ long[] lengths = prop.getLengths();
+ for (int i = 0; i < lengths.length; i++) {
+ if (lengths[i] > -1) {
+ assertEquals("Property.getLengths() returns " +
+ "wrong array of the lengths of a multivalue property.",
+ values[i].getString().length(), lengths[i]);
+ }
+ }
+ } else {
+ try {
+ prop.getLengths();
+ fail("Property.getLengths() called on a sinlge value property " +
+ "should throw a ValueFormatException.");
+
+ } catch (ValueFormatException vfe) {
+ // ok
+ }
+ }
+ }
+}
\ No newline at end of file
Propchange: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/LongPropertyTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/NamePropertyTest.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/NamePropertyTest.java?view=auto&rev=154030
==============================================================================
--- incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/NamePropertyTest.java (added)
+++ incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/NamePropertyTest.java Wed Feb 16 07:20:44 2005
@@ -0,0 +1,149 @@
+/*
+ * 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;
+
+import javax.jcr.RepositoryException;
+import javax.jcr.Value;
+import javax.jcr.ValueFormatException;
+import javax.jcr.PropertyType;
+
+/**
+ * Tests a date property. If the workspace does not contain a node with a date
+ * property a {@link org.apache.jackrabbit.test.NotExecutableException} is
+ * thrown.
+ *
+ * @test
+ * @sources NamePropertyTest.java
+ * @executeClass org.apache.jackrabbit.test.api.NamePropertyTest
+ * @keywords level1
+ */
+public class NamePropertyTest extends AbstractPropertyTest {
+
+ /**
+ * Returns {@link javax.jcr.PropertyType#NAME}.
+ * @return {@link javax.jcr.PropertyType#NAME}.
+ */
+ protected int getPropertyType() {
+ return PropertyType.NAME;
+ }
+
+ /**
+ * Tests conversion from Name type to String type.
+ * @throws RepositoryException
+ */
+ public void testGetString() throws RepositoryException {
+ Value val = PropertyUtil.getValue(prop);
+ assertTrue("Not a valid Name property: "+prop.getName(),
+ PropertyUtil.checkNameFormat(val.getString(),session));
+
+ }
+ /**
+ * Tests failure of conversion from Name type to Boolean type.
+ * @throws RepositoryException
+ */
+ public void testGetBoolean() throws RepositoryException {
+ try {
+ Value val = PropertyUtil.getValue(prop);
+ val.getBoolean();
+ fail("Conversion from a Name value to a Boolean value " +
+ "should throw a ValueFormatException.");
+ } catch (ValueFormatException vfe) {
+ //ok
+ }
+ }
+
+ /**
+ * Tests failure of conversion from Name type to Date type.
+ * @throws RepositoryException
+ */
+ public void testGetDate() throws RepositoryException {
+ try {
+ Value val = PropertyUtil.getValue(prop);
+ val.getDate();
+ fail("Conversion from a Name value to a Date value " +
+ "should throw a ValueFormatException.");
+ } catch (ValueFormatException vfe) {
+ //ok
+ }
+ }
+
+ /**
+ * 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
+ }
+ }
+
+ /**
+ * Tests failure of conversion from Name type to Long type.
+ * @throws RepositoryException
+ */
+ public void testGetLong() throws RepositoryException {
+ try {
+ Value val = PropertyUtil.getValue(prop);
+ val.getLong();
+ fail("Conversion from a Name value to a Long value " +
+ "should throw a ValueFormatException.");
+ } catch (ValueFormatException vfe) {
+ //ok
+ }
+ }
+
+ /**
+ * Tests if Value.getType() returns the same as Property.getType()
+ * and also tests that prop.getDefinition().getRequiredType() returns
+ * the same type in case it is not of Undefined type.
+ * @throws RepositoryException
+ */
+ public void testGetType() throws RepositoryException {
+ assertTrue("Value.getType() returns wrong type.",
+ PropertyUtil.checkGetType(prop, PropertyType.NAME));
+ }
+
+ /**
+ * Tests failure of conversion from Name type to Reference type.
+ * @throws RepositoryException
+ */
+ public void testAsReference() throws RepositoryException {
+ if (!multiple) {
+ try {
+ prop.getNode();
+ fail("Conversion from a Name value to a Reference value " +
+ "should throw a ValueFormatException.");
+ } catch (ValueFormatException vfe) {
+ //ok
+ }
+ }
+ else {
+ try {
+ prop.getNode();
+ fail("Property.getNode() called on a multivalue property " +
+ "should throw a ValueFormatException.");
+ } catch (ValueFormatException vfe) {
+ // ok
+ }
+ }
+ }
+}
\ No newline at end of file
Propchange: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/NamePropertyTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/PathPropertyTest.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/PathPropertyTest.java?view=auto&rev=154030
==============================================================================
--- incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/PathPropertyTest.java (added)
+++ incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/PathPropertyTest.java Wed Feb 16 07:20:44 2005
@@ -0,0 +1,143 @@
+/*
+ * 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;
+
+import javax.jcr.RepositoryException;
+import javax.jcr.Value;
+import javax.jcr.ValueFormatException;
+import javax.jcr.PropertyType;
+
+/**
+ * Tests a path property. If the workspace does not contain a node with a path
+ * property a {@link org.apache.jackrabbit.test.NotExecutableException} is
+ * thrown.
+ *
+ * @test
+ * @sources PathPropertyTest.java
+ * @executeClass org.apache.jackrabbit.test.api.PathPropertyTest
+ * @keywords level1
+ */
+public class PathPropertyTest extends AbstractPropertyTest {
+
+ /**
+ * Returns {@link javax.jcr.PropertyType#PATH}.
+ * @return {@link javax.jcr.PropertyType#PATH}.
+ */
+ protected int getPropertyType() {
+ return PropertyType.PATH;
+ }
+
+ /**
+ * Tests conversion from Path type to String type
+ * and if the resulting string has correct format.
+ */
+ public void testGetString() throws RepositoryException {
+ Value val = PropertyUtil.getValue(prop);
+ assertTrue("Not a valid Path property: "+prop.getName(),
+ PropertyUtil.checkPathFormat(val.getString(), session));
+ }
+
+ /**
+ * Tests failure of conversion from Path type to Boolean type.
+ */
+ public void testGetBoolean() throws RepositoryException {
+ try {
+ Value val = PropertyUtil.getValue(prop);
+ val.getBoolean();
+ fail("Conversion from a Path value to a Boolean value " +
+ "should throw a ValueFormatException.");
+ } catch (ValueFormatException vfe) {
+ //ok
+ }
+ }
+
+ /**
+ * Tests failure of conversion from Path type to Date type.
+ */
+ public void testGetDate() throws RepositoryException {
+ try {
+ Value val = PropertyUtil.getValue(prop);
+ val.getDate();
+ fail("Conversion from a Path value to a Date value " +
+ "should throw a ValueFormatException.");
+ } catch (ValueFormatException vfe) {
+ //ok
+ }
+ }
+
+ /**
+ * Tests failure from Path type to Double type.
+ */
+ public void testGetDouble() throws RepositoryException {
+ try {
+ Value val = PropertyUtil.getValue(prop);
+ val.getDouble();
+ fail("Conversion from a Path value to a Double value " +
+ "should throw a ValueFormatException.");
+ } catch (ValueFormatException vfe) {
+ //ok
+ }
+ }
+
+ /**
+ * Tests failure of conversion from Path type to Long type.
+ */
+ public void testGetLong() throws RepositoryException {
+ try {
+ Value val = PropertyUtil.getValue(prop);
+ val.getLong();
+ fail("Conversion from a Path value to a Long value " +
+ "should throw a ValueFormatException.");
+ } catch (ValueFormatException vfe) {
+ //ok
+ }
+ }
+
+ /**
+ * Tests if Value.getType() returns the same as Property.getType()
+ * and also tests that prop.getDefinition().getRequiredType() returns
+ * the same type in case it is not of Undefined type.
+ */
+ public void testGetType() throws RepositoryException {
+ assertTrue("Value.getType() returns wrong type.",
+ PropertyUtil.checkGetType(prop, PropertyType.PATH));
+ }
+
+ /**
+ * Tests failure of conversion from Path type to Reference type.
+ */
+ public void testAsReference() throws RepositoryException {
+ if (!multiple) {
+ try {
+ prop.getNode();
+ fail("Conversion from a Path value to a Reference value " +
+ "should throw a ValueFormatException.");
+ } catch (ValueFormatException vfe) {
+ //ok
+ }
+ }
+ else {
+ try {
+ prop.getNode();
+ fail("Property.getNode() called on a multivalue property " +
+ "should throw a ValueFormatException.");
+ } catch (ValueFormatException vfe) {
+ // ok
+ }
+ }
+ }
+}
\ No newline at end of file
Propchange: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/PathPropertyTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/PropertyUtil.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/PropertyUtil.java?view=auto&rev=154030
==============================================================================
--- incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/PropertyUtil.java (added)
+++ incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/PropertyUtil.java Wed Feb 16 07:20:44 2005
@@ -0,0 +1,308 @@
+/*
+ * 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;
+
+import javax.jcr.Property;
+import javax.jcr.Session;
+import javax.jcr.Node;
+import javax.jcr.RepositoryException;
+import javax.jcr.ValueFormatException;
+import javax.jcr.PropertyType;
+import javax.jcr.NodeIterator;
+import javax.jcr.PropertyIterator;
+import javax.jcr.Value;
+import javax.jcr.NamespaceException;
+import javax.jcr.NamespaceRegistry;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.io.BufferedInputStream;
+
+/**
+ * This class provides various utility methods that are used by the property
+ * test cases.
+ */
+class PropertyUtil {
+
+ private static final Pattern PATH_PATTERN = Pattern.compile("(\\.)|(\\.\\.)|(([^ /:\\[\\]*'\"|](?:[^/:\\[\\]*'\"|]*[^ /:\\[\\]*'\"|])?):)?([^ /:\\[\\]*'\"|](?:[^/:\\[\\]*'\"|]*[^ /:\\[\\]*'\"|])?)(\\[([1-9]\\d*)\\])?");
+ private static final Pattern NAME_PATTERN = Pattern.compile("(([^ /:\\[\\]*'\"|](?:[^/:\\[\\]*'\"|]*[^ /:\\[\\]*'\"|])?):)?([^ /:\\[\\]*'\"|](?:[^/:\\[\\]*'\"|]*[^ /:\\[\\]*'\"|])?)");
+
+ private static final String dateFormatPattern = "[0-9][0-9][0-9][0-9]-(0[1-9]|1[0-2])-(0[1-9]|1[0-9]||2[0-9]|3[01])T([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9].[0-9][0-9][0-9](Z|[+-]([0-1][0-9]|2[0-3]):[0-5][0-9])";
+ private static final Pattern DATE_PATTERN = Pattern.compile(dateFormatPattern);
+
+ private static final Pattern UUID_PATTERN = Pattern.compile("\\p{XDigit}{8}-\\p{XDigit}{4}-\\p{XDigit}{4}-\\p{XDigit}{4}-\\p{XDigit}{12}");
+
+ /**
+ * Private constructor to disable instantiation.
+ */
+ private PropertyUtil() {
+ }
+
+ /**
+ * Traverses a tree below a given node searching for a property with a given
+ * type
+ *
+ * @param node the node to start traverse
+ * @param type the property type to search for
+ * @return the property found or null if no property is found
+ */
+ public static Property searchProp(Session session, Node node, int type)
+ throws RepositoryException, ValueFormatException {
+
+ Property prop = null;
+ int propType = PropertyType.UNDEFINED;
+ if (prop == null) {
+ for (PropertyIterator props = node.getProperties(); props.hasNext();) {
+ Property property = props.nextProperty();
+ propType = property.getType();
+ if (propType == type) {
+ prop = property;
+ break;
+ }
+ }
+ }
+ if (prop == null) {
+ for (NodeIterator nodes = node.getNodes(); nodes.hasNext();) {
+ Node n = nodes.nextNode();
+ prop = searchProp(session, n, type);
+ if (prop != null) {
+ break;
+ }
+ }
+ }
+ return prop;
+ }
+
+ /**
+ * Returns the value of a property. If <code>prop</code> is multi valued
+ * this method returns the first value.
+ *
+ * @param prop the property from which to return the value.
+ * @return the value of the property.
+ */
+ public static Value getValue(Property prop) throws RepositoryException {
+ Value val;
+ if (prop.getDefinition().isMultiple()) {
+ Value[] vals = prop.getValues();
+ if (vals.length > 0) {
+ val = vals[0];
+ } else {
+ val = null;
+ }
+ } else {
+ val = prop.getValue();
+ }
+ return val;
+ }
+
+ /**
+ * checks if the given name follows the NAME syntax rules and if a present
+ * prefix is mapped to a registered namespace
+ *
+ * @param name the string to test
+ */
+ public static boolean checkNameFormat(String name, Session session) throws RepositoryException {
+ if (name == null || name.length() == 0) {
+ return false;
+ } else {
+ NamespaceRegistry nsr = session.getWorkspace().getNamespaceRegistry();
+ boolean prefixOk = true;
+ // validate name element
+ Matcher matcher = NAME_PATTERN.matcher(name);
+ // validate namespace prefixes if present
+ String[] split = name.split(":");
+ if (split.length > 1) {
+ String prefix = split[0];
+ try {
+ nsr.getURI(prefix);
+ } catch (NamespaceException nse) {
+ prefixOk = false;
+ }
+ }
+ return matcher.matches() && prefixOk;
+ }
+ }
+
+ /**
+ * Checks if the given path follows the path syntax rules.
+ *
+ * @param jcrPath the string to test
+ */
+ public static boolean checkPathFormat(String jcrPath, Session session) throws RepositoryException {
+ if (jcrPath == null || jcrPath.length() == 0) {
+ return false;
+ } else if (jcrPath.equals("/")) {
+ return true;
+ } else {
+ NamespaceRegistry nsr = session.getWorkspace().getNamespaceRegistry();
+ boolean match = false;
+ boolean prefixOk = true;
+ // split path into path elements and validate each of them
+ String[] elems = jcrPath.split("/", -1);
+ for (int i = jcrPath.startsWith("/") ? 1 : 0; i < elems.length; i++) {
+ // validate path element
+ String elem = elems[i];
+ Matcher matcher = PATH_PATTERN.matcher(elem);
+ match = matcher.matches();
+ if (!match) {
+ break;
+ }
+ // validate namespace prefixes if present
+ String[] split = elem.split(":");
+ if (split.length > 1) {
+ String prefix = split[0];
+ try {
+ nsr.getURI(prefix);
+ } catch (NamespaceException nse) {
+ prefixOk = false;
+ break;
+ }
+ }
+ }
+ return match && prefixOk;
+ }
+ }
+
+ /**
+ * Checks if the String is a valid date in string format.
+ *
+ * @param str the string to test.
+ * @return <code>true</code> if <code>str</code> is a valid date format.
+ */
+ public static boolean isDateFormat(String str) {
+ return DATE_PATTERN.matcher(str).matches();
+ }
+
+ /**
+ * Checks if the String is a UUID.
+ *
+ * @param str the string to test.
+ * @return <code>true</code> if <code>str</code> is a UUID.
+ */
+ public static boolean isUUID(String str) {
+ return UUID_PATTERN.matcher(str).matches();
+ }
+
+ /**
+ * Counts the number of bytes of a Binary value.
+ *
+ * @param val the binary value.
+ * @return the number of bytes or -1 in case of any exception
+ */
+ public static long countBytes(Value val) {
+ int length = 0;
+ try {
+ BufferedInputStream bin = new BufferedInputStream(val.getStream());
+ while (bin.read() != -1) {
+ length++;
+ }
+ bin.close();
+ } catch (Exception e) {
+ length = -1;
+ }
+ return length;
+ }
+
+ /**
+ * Helper method to test the type received with Value.getType() and
+ * Property.getType() .
+ */
+ public static boolean checkGetType(Property prop, int propType) throws RepositoryException {
+ Value val = getValue(prop);
+ boolean samePropType = (val.getType() == propType);
+ int requiredType = prop.getDefinition().getRequiredType();
+ if (requiredType != PropertyType.UNDEFINED) {
+ samePropType = (val.getType() == requiredType);
+ }
+ return samePropType;
+ }
+
+ /**
+ * Helper method to compare the equality of two values for equality with the
+ * fulfilling of the equality conditions. These conditions for the values
+ * are to have the same type and the same string representation.
+ *
+ * @param val1 first value
+ * @param val2 second value
+ * @return true if the equals method is equivalent to the normative
+ * definition of value equality, false in the other case.
+ */
+ public static boolean equalValues(Value val1, Value val2) throws RepositoryException {
+
+ boolean isEqual = val1.equals(val2);
+ boolean conditions = false;
+ try {
+ conditions = (val1.getType() == val2.getType())
+ && val1.getString().equals(val2.getString());
+ } catch (ValueFormatException vfe) {
+ return false;
+ }
+ return (isEqual == conditions);
+ }
+
+ /**
+ * Helper method to assure that no property with a null value exist.
+ *
+ * @param node the node to start the search from.
+ * @return <code>true</code> if a null value property is found;
+ * <code>false</code> in the other case.
+ */
+ public static boolean nullValues(Node node) throws RepositoryException {
+ boolean nullValue = false;
+ for (PropertyIterator props = node.getProperties(); props.hasNext();) {
+ Property property = props.nextProperty();
+ if (!property.getDefinition().isMultiple()) {
+ nullValue = (property.getValue() == null);
+ if (nullValue) {
+ break;
+ }
+ }
+ }
+
+ if (!nullValue) {
+ for (NodeIterator nodes = node.getNodes(); nodes.hasNext();) {
+ Node n = nodes.nextNode();
+ nullValue = nullValues(n);
+ }
+ }
+ return nullValue;
+ }
+
+ /**
+ * Helper method to find a multivalue property.
+ *
+ * @param node the node to start the search from.
+ * @return a multivalue property or null if not found any.
+ */
+ public static Property searchMultivalProp(Node node) throws RepositoryException {
+ Property multiVal = null;
+ for (PropertyIterator props = node.getProperties(); props.hasNext();) {
+ Property property = props.nextProperty();
+ if (property.getDefinition().isMultiple()) {
+ multiVal = property;
+ break;
+ }
+ }
+
+ if (multiVal == null) {
+ for (NodeIterator nodes = node.getNodes(); nodes.hasNext();) {
+ Node n = nodes.nextNode();
+ multiVal = searchMultivalProp(n);
+ }
+ }
+ return multiVal;
+ }
+}
\ No newline at end of file
Propchange: incubator/jackrabbit/trunk/src/test/org/apache/jackrabbit/test/api/PropertyUtil.java
------------------------------------------------------------------------------
svn:eol-style = native