You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by an...@apache.org on 2008/10/20 15:48:38 UTC
svn commit: r706285 - in /jackrabbit/trunk:
jackrabbit-spi/src/test/java/org/apache/jackrabbit/spi/
jackrabbit-spi2jcr/src/test/resources/
Author: angela
Date: Mon Oct 20 06:48:37 2008
New Revision: 706285
URL: http://svn.apache.org/viewvc?rev=706285&view=rev
Log:
Add SPI tests for QValueFactory
Added:
jackrabbit/trunk/jackrabbit-spi/src/test/java/org/apache/jackrabbit/spi/QValueFactoryTest.java (with props)
Modified:
jackrabbit/trunk/jackrabbit-spi/src/test/java/org/apache/jackrabbit/spi/TestAll.java
jackrabbit/trunk/jackrabbit-spi2jcr/src/test/resources/repositoryServiceStubImpl.properties
Added: jackrabbit/trunk/jackrabbit-spi/src/test/java/org/apache/jackrabbit/spi/QValueFactoryTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-spi/src/test/java/org/apache/jackrabbit/spi/QValueFactoryTest.java?rev=706285&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-spi/src/test/java/org/apache/jackrabbit/spi/QValueFactoryTest.java (added)
+++ jackrabbit/trunk/jackrabbit-spi/src/test/java/org/apache/jackrabbit/spi/QValueFactoryTest.java Mon Oct 20 06:48:37 2008
@@ -0,0 +1,546 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.spi;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.jcr.RepositoryException;
+import javax.jcr.PropertyType;
+import javax.jcr.ValueFormatException;
+import java.util.Calendar;
+import java.util.Arrays;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.File;
+import java.io.ByteArrayOutputStream;
+import java.io.ByteArrayInputStream;
+import java.io.FileWriter;
+import java.io.OutputStream;
+import java.io.FileOutputStream;
+import java.io.FileInputStream;
+import java.io.ObjectOutputStream;
+import java.io.ObjectInputStream;
+import java.io.BufferedInputStream;
+
+/** <code>QValueFactoryTest</code>... */
+public class QValueFactoryTest extends AbstractSPITest {
+
+ private static Logger log = LoggerFactory.getLogger(QValueFactoryTest.class);
+
+ private QValueFactory factory;
+
+ private final Calendar calendar = Calendar.getInstance();
+ private Path rootPath;
+ private Name testName;
+ private String reference;
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ RepositoryService service = helper.getRepositoryService();
+ factory = service.getQValueFactory();
+
+ rootPath = service.getPathFactory().getRootPath();
+ testName = service.getNameFactory().create(Name.NS_JCR_URI, "data");
+ reference = getProperty("reference");
+ }
+
+
+ public void testIllegalType() throws RepositoryException {
+ try {
+ factory.create("any", 54);
+ fail("54 is not a valid property type");
+ } catch (IllegalArgumentException e) {
+ // ok
+ }
+ }
+
+ //-------------------------------------------------------------< DOUBLE >---
+
+ public void testCreateInvalidDoubleValue() throws RepositoryException {
+ try {
+ factory.create("any", PropertyType.DOUBLE);
+ fail("'any' cannot be converted to a valid double value.");
+ } catch (ValueFormatException e) {
+ // ok
+ }
+ }
+
+ public void testGetDoubleOnBooleanValue() throws RepositoryException {
+ try {
+ QValue v = factory.create(true);
+ v.getDouble();
+ fail("'true' cannot be converted to a valid double value.");
+ } catch (ValueFormatException e) {
+ // ok
+ }
+ }
+
+ //---------------------------------------------------------------< LONG >---
+
+ public void testCreateInvalidLongValue() throws RepositoryException {
+ try {
+ factory.create("any", PropertyType.LONG);
+ fail("'any' cannot be converted to a valid long value.");
+ } catch (ValueFormatException e) {
+ // ok
+ }
+ }
+
+ public void testGetLongOnBooleanValue() throws RepositoryException {
+ try {
+ QValue v = factory.create(true);
+ v.getLong();
+ fail("'true' cannot be converted to a valid long value.");
+ } catch (ValueFormatException e) {
+ // ok
+ }
+ }
+
+ //------------------------------------------------------------< BOOLEAN >---
+ /**
+ * QValueImpl has a final static constant for the TRUE and the FALSE boolean
+ * values. Test if the various create methods use the constants (thus always
+ * return the 'same' object.
+ *
+ * @throws RepositoryException
+ */
+ public void testFinalBooleanValue() throws RepositoryException {
+ assertSame(factory.create(true), factory.create(Boolean.TRUE.toString(), PropertyType.BOOLEAN));
+ assertSame(factory.create(true), factory.create(true));
+
+ assertSame(factory.create(false), factory.create(Boolean.FALSE.toString(), PropertyType.BOOLEAN));
+ assertSame(factory.create(false), factory.create(false));
+ }
+
+ /**
+ * Test if creating Boolean QValue from boolean and from String with boolean
+ * type return equal objects.
+ *
+ * @throws RepositoryException
+ */
+ public void testCreateBooleanValueFromString() throws RepositoryException {
+ QValue v = factory.create(Boolean.TRUE.toString(), PropertyType.BOOLEAN);
+ assertEquals("Creating boolean type QValue from boolean or String must be equal.",
+ factory.create(true), v);
+
+ v = factory.create(Boolean.FALSE.toString(), PropertyType.BOOLEAN);
+ assertEquals("Creating boolean type QValue from boolean or String must be equal.",
+ factory.create(false), v);
+ }
+
+ public void testCreateTrueBooleanValue() throws RepositoryException {
+ QValue v = factory.create(true);
+ assertEquals("Boolean value must be true", Boolean.TRUE.toString(), v.getString());
+ assertEquals("Boolean value must be true", true, v.getBoolean());
+ }
+
+ public void testCreateFalseBooleanValue() throws RepositoryException {
+ QValue v = factory.create(false);
+ assertEquals("Boolean value must be false", Boolean.FALSE.toString(), v.getString());
+ assertEquals("Boolean value must be false", false, v.getBoolean());
+ }
+
+ public void testCreateTrueFromString() throws ValueFormatException, RepositoryException {
+ QValue v = factory.create(Boolean.TRUE.toString(), PropertyType.STRING);
+ assertEquals("Boolean value must be true", true, v.getBoolean());
+ }
+
+ public void testCreateFalseFromString() throws ValueFormatException, RepositoryException {
+ QValue v = factory.create("any", PropertyType.STRING);
+ assertEquals("Boolean value must be false", false, v.getBoolean());
+ }
+
+ public void testReadBooleanAsLong() throws RepositoryException {
+ try {
+ QValue v = factory.create(true);
+ v.getLong();
+ }
+ catch (ValueFormatException e) {
+ return; // ok
+ }
+ assertTrue("Cannot convert value to long", false);
+ }
+
+ //---------------------------------------------------------------< DATE >---
+ public void testNullDateValue() throws IOException, RepositoryException {
+ try {
+ factory.create((Calendar) null);
+ fail();
+ } catch (IllegalArgumentException e) {
+ // ok
+ }
+ try {
+ factory.create(null, PropertyType.DATE);
+ fail();
+ } catch (IllegalArgumentException e) {
+ // ok
+ }
+ }
+
+ public void testDateValueType() throws RepositoryException {
+ QValue v = factory.create(calendar);
+ assertTrue("Type of a date value must be PropertyType.DATE", v.getType() == PropertyType.DATE);
+ }
+ public void testDateValueEquality() throws RepositoryException {
+ QValue v = factory.create(calendar);
+ QValue otherV = factory.create(calendar);
+ assertEquals("Equality of qualified date value must be calculated based on their String representation.", v, otherV);
+ }
+
+ public void testDateValueEquality2() throws RepositoryException {
+ QValue v = factory.create(calendar);
+ QValue otherV = factory.create(v.getString(), PropertyType.DATE);
+ assertEquals("Equality of qualified date value must be calculated based on their String representation.", v, otherV);
+ }
+
+ //----------------------------------------------------------< REFERENCE >---
+
+ public void testNullReferenceValue() throws IOException, RepositoryException {
+ try {
+ factory.create(null, PropertyType.REFERENCE);
+ fail();
+ } catch (IllegalArgumentException e) {
+ // ok
+ }
+ }
+
+ public void testReferenceValueType() throws RepositoryException {
+ if (reference != null) {
+ QValue v = factory.create(reference, PropertyType.REFERENCE);
+ assertTrue("Type of a date value must be PropertyType.REFERENCE.", v.getType() == PropertyType.REFERENCE);
+ } else {
+ log.warn("Configuration entry 'QValueFactoryTest.reference' is missing -> skip test 'testReferenceValueType'.");
+ }
+ }
+
+ public void testReferenceValueEquality() throws RepositoryException {
+ if (reference != null) {
+ QValue v = factory.create(reference, PropertyType.REFERENCE);
+ QValue otherV = factory.create(reference, PropertyType.REFERENCE);
+ assertEquals("Qualified ref values created from the same string must be equal.", v, otherV);
+ } else {
+ log.warn("Configuration entry 'QValueFactoryTest.reference' is missing -> skip test 'testReferenceValueEquality'.");
+ }
+ }
+
+ public void testEqualityDifferentTypes() throws RepositoryException {
+ if (reference != null) {
+ QValue v = factory.create(reference, PropertyType.REFERENCE);
+ QValue v2 = factory.create(reference, PropertyType.STRING);
+ assertFalse(v.equals(v2));
+ } else {
+ log.warn("Configuration entry 'QValueFactoryTest.reference' is missing -> skip test 'testEqualityDifferentTypes'.");
+ }
+ }
+
+
+ //---------------------------------------------------------------< Name >---
+
+ public void testNullNameValue() throws IOException, RepositoryException {
+ try {
+ factory.create((Name) null);
+ fail();
+ } catch (IllegalArgumentException e) {
+ // ok
+ }
+ }
+
+ public void testNameValueType() throws IOException, RepositoryException {
+ QValue v = factory.create(testName);
+ assertTrue(v.getType() == PropertyType.NAME);
+ v = factory.create(testName.toString(), PropertyType.NAME);
+ assertTrue(v.getType() == PropertyType.NAME);
+ }
+
+ public void testNameValueEquality() throws IOException, RepositoryException {
+ QValue v = factory.create(testName);
+ QValue v2 = factory.create(testName.toString(), PropertyType.NAME);
+ assertTrue(v.equals(v2));
+ }
+
+ public void testNameValueGetString() throws IOException, RepositoryException {
+ QValue v = factory.create(testName);
+ assertTrue(v.getString().equals(testName.toString()));
+ }
+
+ public void testNameValueGetName() throws RepositoryException {
+ QValue v = factory.create(testName);
+ assertTrue(v.getName().equals(testName));
+ }
+
+ public void testInvalidNameValue() throws RepositoryException {
+ try {
+ factory.create("abc", PropertyType.NAME);
+ fail("'abc' is not a valid Name -> creating QValue should fail.");
+ } catch (ValueFormatException e) {
+ // ok
+ }
+ }
+
+ public void testAnyValueGetName() throws RepositoryException {
+ try {
+ factory.create(12345).getName();
+ fail("12345 is not a valid Name value -> QValue.getName() should fail.");
+ } catch (ValueFormatException e) {
+ // ok
+ }
+ }
+
+ //---------------------------------------------------------------< Path >---
+
+ public void testNullPathValue() throws IOException, RepositoryException {
+ try {
+ factory.create((Path) null);
+ fail();
+ } catch (IllegalArgumentException e) {
+ // ok
+ }
+ }
+
+ public void testPathValueType() throws IOException, RepositoryException {
+ QValue v = factory.create(rootPath);
+ assertTrue(v.getType() == PropertyType.PATH);
+ v = factory.create(rootPath.toString(), PropertyType.PATH);
+ assertTrue(v.getType() == PropertyType.PATH);
+ }
+
+
+ public void testPathValueEquality() throws IOException, RepositoryException {
+ QValue v = factory.create(rootPath);
+ QValue v2 = factory.create(rootPath.toString(), PropertyType.PATH);
+ assertTrue(v.equals(v2));
+ }
+
+ public void testPathValueGetString() throws IOException, RepositoryException {
+ QValue v = factory.create(rootPath);
+ assertTrue(v.getString().equals(rootPath.toString()));
+ }
+
+ public void testPathValueGetPath() throws RepositoryException {
+ QValue v = factory.create(rootPath);
+ assertTrue(v.getPath().equals(rootPath));
+ }
+
+ public void testInvalidPathValue() throws RepositoryException {
+ try {
+ factory.create("abc", PropertyType.PATH);
+ fail("'abc' is not a valid Path -> creating QValue should fail.");
+ } catch (ValueFormatException e) {
+ // ok
+ }
+ }
+
+ public void testAnyValueGetPath() throws RepositoryException {
+ try {
+ factory.create(12345).getPath();
+ fail("12345 is not a valid Path value -> QValue.getPath() should fail.");
+ } catch (ValueFormatException e) {
+ // ok
+ }
+ }
+
+ //-------------------------------------------------------------< BINARY >---
+
+ public void testNullBinaryValue() throws IOException, RepositoryException {
+ try {
+ factory.create((byte[]) null);
+ fail();
+ } catch (IllegalArgumentException e) {
+ // ok
+ }
+ try {
+ factory.create((InputStream) null);
+ fail();
+ } catch (IllegalArgumentException e) {
+ // ok
+ }
+ try {
+ factory.create((File) null);
+ fail();
+ } catch (IllegalArgumentException e) {
+ // ok
+ }
+ }
+
+ public void testBinaryValueType() throws IOException, RepositoryException {
+ QValue v = factory.create(new byte[] {'a', 'b', 'c'});
+ assertTrue(v.getType() == PropertyType.BINARY);
+ }
+
+
+ public void testBinaryFromByteArray() throws RepositoryException, IOException {
+ QValue v = factory.create(new byte[] {'a', 'b', 'c'});
+
+ assertEquals(PropertyType.BINARY, v.getType());
+ assertEquals(3, v.getLength());
+
+ assertEquals("abc", v.getString());
+
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ spool(out, v.getStream());
+ assertEquals("abc", new String(out.toByteArray()));
+ }
+
+ public void testEmptyBinaryFromByteArray() throws RepositoryException, IOException {
+ QValue v = factory.create(new byte[0]);
+
+ assertEquals(PropertyType.BINARY, v.getType());
+ assertEquals(0, v.getLength());
+
+ assertEquals("", v.getString());
+
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ spool(out, v.getStream());
+ assertEquals("", new String(out.toByteArray()));
+ }
+
+ public void testBinaryFromInputStream() throws RepositoryException, IOException {
+ InputStream in = new ByteArrayInputStream(new byte[] {'a', 'b', 'c'});
+
+ QValue v = factory.create(in);
+
+ assertEquals(PropertyType.BINARY, v.getType());
+ assertEquals(3, v.getLength());
+
+ assertEquals("abc", v.getString());
+
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ spool(out, v.getStream());
+ assertEquals("abc", new String(out.toByteArray()));
+ }
+
+ public void testEmptyBinaryFromInputStream() throws RepositoryException, IOException {
+ InputStream in = new ByteArrayInputStream(new byte[0]);
+
+ QValue v = factory.create(in);
+
+ assertEquals(PropertyType.BINARY, v.getType());
+ assertEquals(0, v.getLength());
+
+ assertEquals("", v.getString());
+
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ spool(out, v.getStream());
+ assertEquals("", new String(out.toByteArray()));
+ }
+
+
+ public void testBinaryFromFile() throws RepositoryException, IOException {
+ File f = File.createTempFile("QValueFactoryImplTest", ".txt");
+ f.deleteOnExit();
+ FileWriter fw = new FileWriter(f);
+ fw.write("abc");
+ fw.close();
+
+ QValue v = factory.create(f);
+
+ assertEquals(PropertyType.BINARY, v.getType());
+ assertEquals(3, v.getLength());
+
+ assertEquals("abc", v.getString());
+
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ spool(out, v.getStream());
+ assertEquals("abc", new String(out.toByteArray()));
+ }
+
+ public void testEmptyBinaryFromFile() throws RepositoryException, IOException {
+ File f = File.createTempFile("QValueFactoryImplTest", ".txt");
+ f.deleteOnExit();
+
+ QValue v = factory.create(f);
+
+ assertEquals(PropertyType.BINARY, v.getType());
+ assertEquals(0, v.getLength());
+
+ assertEquals("", v.getString());
+
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ spool(out, v.getStream());
+ assertEquals("", new String(out.toByteArray()));
+ }
+
+ public void testBinarySerializable() throws Exception {
+ runBinarySerializableTest(1); // 1k
+ runBinarySerializableTest(10); // 10k
+ runBinarySerializableTest(100); // 100k
+ runBinarySerializableTest(1000); // 1M
+ }
+
+ /**
+ * Runs binary serializable test using a stream with a size of kBytes.
+ * @param size in kBytes.
+ */
+ private void runBinarySerializableTest(int size) throws Exception {
+ File tmp = File.createTempFile("test", "bin");
+ OutputStream out = new FileOutputStream(tmp);
+ byte[] stuff = new byte[1024];
+ Arrays.fill(stuff, (byte) 7);
+ for (int i = 0; i < size; i++) {
+ out.write(stuff);
+ }
+ out.close();
+ InputStream in = new FileInputStream(tmp);
+ QValue v = factory.create(in);
+ in.close();
+ tmp.delete();
+ ByteArrayOutputStream bout = new ByteArrayOutputStream();
+ ObjectOutputStream oout = new ObjectOutputStream(bout);
+ oout.writeObject(v);
+ oout.close();
+ ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
+ ObjectInputStream oin = new ObjectInputStream(bin);
+ QValue serValue = (QValue) oin.readObject();
+ try {
+ InputStream in1 = new BufferedInputStream(v.getStream());
+ InputStream in2 = new BufferedInputStream(serValue.getStream());
+ int i;
+ while ((i = in1.read()) > -1) {
+ assertEquals(i, in2.read());
+ }
+ assertEquals(in2.read(), -1);
+ in1.close();
+ in2.close();
+ } finally {
+ v.discard();
+ serValue.discard();
+ }
+ }
+
+ /**
+ *
+ * @param out
+ * @param in
+ * @throws RepositoryException
+ * @throws IOException
+ */
+ private static void spool(OutputStream out, InputStream in) throws RepositoryException, IOException {
+ try {
+ byte[] buffer = new byte[0x2000];
+ int read;
+ while ((read = in.read(buffer)) > 0) {
+ out.write(buffer, 0, read);
+ }
+ } finally {
+ try {
+ in.close();
+ } catch (IOException ignore) {
+ }
+ }
+ }
+}
\ No newline at end of file
Propchange: jackrabbit/trunk/jackrabbit-spi/src/test/java/org/apache/jackrabbit/spi/QValueFactoryTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jackrabbit/trunk/jackrabbit-spi/src/test/java/org/apache/jackrabbit/spi/QValueFactoryTest.java
------------------------------------------------------------------------------
svn:keywords = author date id revision url
Modified: jackrabbit/trunk/jackrabbit-spi/src/test/java/org/apache/jackrabbit/spi/TestAll.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-spi/src/test/java/org/apache/jackrabbit/spi/TestAll.java?rev=706285&r1=706284&r2=706285&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-spi/src/test/java/org/apache/jackrabbit/spi/TestAll.java (original)
+++ jackrabbit/trunk/jackrabbit-spi/src/test/java/org/apache/jackrabbit/spi/TestAll.java Mon Oct 20 06:48:37 2008
@@ -29,6 +29,7 @@
suite.addTestSuite(RepositoryServiceTest.class);
suite.addTestSuite(SessionInfoTest.class);
+ suite.addTestSuite(QValueFactoryTest.class);
return suite;
}
Modified: jackrabbit/trunk/jackrabbit-spi2jcr/src/test/resources/repositoryServiceStubImpl.properties
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-spi2jcr/src/test/resources/repositoryServiceStubImpl.properties?rev=706285&r1=706284&r2=706285&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-spi2jcr/src/test/resources/repositoryServiceStubImpl.properties (original)
+++ jackrabbit/trunk/jackrabbit-spi2jcr/src/test/resources/repositoryServiceStubImpl.properties Mon Oct 20 06:48:37 2008
@@ -26,6 +26,8 @@
org.apache.jackrabbit.spi.readonly.name=
org.apache.jackrabbit.spi.workspacename=default
+org.apache.jackrabbit.spi.QValueFactoryTest.reference=cafebabe-cafe-babe-cafe-babecafebabe
+
# config for jackrabbit repository implementation when testing SPI2JCR
org.apache.jackrabbit.spi2jcr.config=src/test/resources/repository.xml