You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by st...@apache.org on 2010/03/04 10:48:46 UTC
svn commit: r918915 - in /jackrabbit/trunk:
jackrabbit-core/src/test/java/org/apache/jackrabbit/core/value/
jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/
Author: stefan
Date: Thu Mar 4 09:48:45 2010
New Revision: 918915
URL: http://svn.apache.org/viewvc?rev=918915&view=rev
Log:
JCR-2511: Value#getBinary() and #getStream() return internal representation for type PATH and NAME
Added:
jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/value/PathTest.java
Modified:
jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/value/TestAll.java
jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/QValueValue.java
Added: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/value/PathTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/value/PathTest.java?rev=918915&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/value/PathTest.java (added)
+++ jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/value/PathTest.java Thu Mar 4 09:48:45 2010
@@ -0,0 +1,90 @@
+/*
+ * 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.core.value;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.jackrabbit.test.AbstractJCRTest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.jcr.Binary;
+import javax.jcr.Property;
+import javax.jcr.PropertyType;
+import javax.jcr.RepositoryException;
+import javax.jcr.Value;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * <code>PathTest</code>...
+ */
+public class PathTest extends AbstractJCRTest {
+
+ /**
+ * logger instance
+ */
+ private static final Logger log = LoggerFactory.getLogger(PathTest.class);
+
+ Property prop;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ prop = testRootNode.setProperty(propertyName1, "/a/b/c", PropertyType.PATH);
+ }
+
+ public void testGetBinary() throws RepositoryException, IOException {
+ Binary binary = prop.getBinary();
+ byte[] bytes = new byte[(int) binary.getSize()];
+ binary.read(bytes, 0);
+ binary.dispose();
+
+ assertEquals(prop.getString(), new String(bytes, "UTF-8"));
+ }
+
+ public void testGetBinaryFromValue() throws RepositoryException, IOException {
+ Value v = superuser.getValueFactory().createValue("/a/b/c", PropertyType.PATH);
+ Binary binary = v.getBinary();
+
+ byte[] bytes = new byte[(int) binary.getSize()];
+ binary.read(bytes, 0);
+ binary.dispose();
+
+ assertEquals(prop.getString(), new String(bytes, "UTF-8"));
+ }
+
+ public void testGetStream() throws RepositoryException, IOException {
+ InputStream in = prop.getStream();
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ IOUtils.copy(in, out);
+ IOUtils.closeQuietly(in);
+
+ assertEquals(prop.getString(), new String(out.toByteArray(), "UTF-8"));
+ }
+
+ public void testGetStreamFromValue() throws RepositoryException, IOException {
+ Value v = superuser.getValueFactory().createValue("/a/b/c", PropertyType.PATH);
+ InputStream in = v.getStream();
+
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ IOUtils.copy(in, out);
+ IOUtils.closeQuietly(in);
+
+ assertEquals(prop.getString(), new String(out.toByteArray(), "UTF-8"));
+ }
+}
Modified: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/value/TestAll.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/value/TestAll.java?rev=918915&r1=918914&r2=918915&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/value/TestAll.java (original)
+++ jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/value/TestAll.java Thu Mar 4 09:48:45 2010
@@ -35,6 +35,7 @@
suite.addTestSuite(BinaryValueTest.class);
suite.addTestSuite(InternalValueFactoryTest.class);
suite.addTestSuite(InternalValueTest.class);
+ suite.addTestSuite(PathTest.class);
return suite;
}
Modified: jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/QValueValue.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/QValueValue.java?rev=918915&r1=918914&r2=918915&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/QValueValue.java (original)
+++ jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/QValueValue.java Thu Mar 4 09:48:45 2010
@@ -17,6 +17,7 @@
package org.apache.jackrabbit.spi.commons.value;
import java.io.ByteArrayInputStream;
+import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
@@ -100,7 +101,38 @@
* @see javax.jcr.Value#getBinary()
*/
public Binary getBinary() throws RepositoryException {
- return qvalue.getBinary();
+ // JCR-2511 Value#getBinary() and #getStream() return internal representation for type PATH and NAME
+ if (getType() == PropertyType.NAME || getType() == PropertyType.PATH) {
+ // qualified name/path value needs to be resolved,
+ // delegate conversion to getString() method
+ try {
+ final byte[] value = getString().getBytes("UTF-8");
+ return new Binary() {
+ public int read(byte[] b, long position) {
+ if (position >= value.length) {
+ return -1;
+ } else {
+ int p = (int) position;
+ int n = Math.min(b.length, value.length - p);
+ System.arraycopy(value, p, b, 0, n);
+ return n;
+ }
+ }
+ public InputStream getStream() {
+ return new ByteArrayInputStream(value);
+ }
+ public long getSize() {
+ return value.length;
+ }
+ public void dispose() {
+ }
+ };
+ } catch (UnsupportedEncodingException ex) {
+ throw new RepositoryException("UTF-8 is not supported", ex);
+ }
+ } else {
+ return qvalue.getBinary();
+ }
}
/**
@@ -130,14 +162,11 @@
public InputStream getStream() throws IllegalStateException, RepositoryException {
if (stream == null) {
if (getType() == PropertyType.NAME || getType() == PropertyType.PATH) {
- // needs namespace mapping
+ // qualified name/path value needs to be resolved
try {
- String l_s = getType() == PropertyType.NAME
- ? resolver.getJCRName(qvalue.getName())
- : resolver.getJCRPath(qvalue.getPath());
- stream = new ByteArrayInputStream(l_s.getBytes("UTF-8"));
+ stream = new ByteArrayInputStream(getString().getBytes("UTF-8"));
} catch (UnsupportedEncodingException ex) {
- throw new RepositoryException(ex);
+ throw new RepositoryException("UTF-8 is not supported", ex);
}
} else {
stream = qvalue.getStream();
@@ -151,10 +180,10 @@
*/
public String getString() throws RepositoryException {
if (getType() == PropertyType.NAME) {
- // needs formatting
+ // qualified name value needs to be resolved
return resolver.getJCRName(qvalue.getName());
} else if (getType() == PropertyType.PATH) {
- // needs formatting
+ // qualified path value needs to be resolved
return resolver.getJCRPath(qvalue.getPath());
} else {
return qvalue.getString();