Index: jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/BinaryPropertyTest.java =================================================================== --- jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/BinaryPropertyTest.java (revision 779233) +++ jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/BinaryPropertyTest.java (working copy) @@ -18,12 +18,10 @@ import org.apache.jackrabbit.test.NotExecutableException; -import javax.jcr.PropertyType; -import javax.jcr.Value; -import javax.jcr.RepositoryException; -import javax.jcr.ValueFormatException; +import javax.jcr.*; import java.io.InputStream; import java.io.IOException; +import java.io.ByteArrayOutputStream; /** * Tests a binary property. If the workspace does not contain a node with a @@ -89,6 +87,38 @@ } /** + * Tests that when Binary.getStream() is called a second time a new stream + * object is returned. + */ + public void testSameStreamJcr2() throws RepositoryException, IOException { + Value val = PropertyUtil.getValue(prop); + Binary bin = val.getBinary(); + InputStream in = bin.getStream(); + InputStream in2 = bin.getStream(); + try { + assertNotSame("Value.getStream() called on a new value " + + "object should return a different Stream object.", in, in2); + //check if both streams can be read independently but contain the same bytes + int n,n2; + while ((n = in.read()) != -1) { + n2 = in2.read(); + assertEquals("streams from the same binary object should have identical content", n, n2); + } + assertEquals("streams from the same binary object should have identical content", -1, in2.read()); + } finally { + // cleaning up + try { + in.close(); + } catch (IOException ignore) {} + if (in2 != in) { + try { + in2.close(); + } catch (IOException ignore) {} + } + } + } + + /** * Tests the failure of calling Property.getStream() on a multivalue * property. */ @@ -110,6 +140,22 @@ } /** + * Tests the failure of calling Property.getBinary() on a multivalue + * property. + */ + public void testMultiValueJcr2() throws RepositoryException, IOException { + if (multiple) { + try { + prop.getBinary(); + fail("Calling getStream() on a multivalue property " + + "should throw a ValueFormatException."); + } catch (ValueFormatException vfe) { + // ok + } + } + } + + /** * 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. @@ -145,6 +191,35 @@ } /** + * Tests that Value.getStream() delivers the same as Value.getBinary.getStream(). + * We check this by reading each byte of the two streams and assuring that + * they are equal. + */ + public void testValueJcr2() throws IOException, RepositoryException { + Value val = PropertyUtil.getValue(prop); + InputStream in = val.getStream(); + InputStream in2 = val.getBinary().getStream(); + try { + int b = in.read(); + while (b != -1) { + int b2 = in2.read(); + assertEquals("Value.getStream() and Value.getBinary().getStream() " + + "return different values.", b, b2); + b = in.read(); + } + assertEquals("Value.getStream() and Value.getBinary().getStream() " + + "return different values.", -1, in2.read()); + } finally { + try { + in.close(); + } catch (IOException ignore) {} + try { + in2.close(); + } catch (IOException ignore) {} + } + } + + /** * Tests conversion from Binary type to Boolean type. This is done via * String conversion. */ @@ -295,6 +370,19 @@ } /** + * Tests the Binary.getSize() method. + */ + public void testGetLengthJcr2() throws RepositoryException { + Value val = PropertyUtil.getValue(prop); + long length = val.getBinary().getSize(); + long bytes = PropertyUtil.countBytes(prop.getValue()); + if (bytes != -1) { + assertEquals("Binary.getSize() returns wrong number of bytes.", + bytes, length); + } + } + + /** * Tests the Property.getLengths() method. The test is successful, if either * -1 is returned */ @@ -319,4 +407,52 @@ } } } + + /** + * Tests the Binary.read() method. + */ + + public void testRandomAccess() throws RepositoryException, IOException { + Value val = PropertyUtil.getValue(prop); + Binary bin = val.getBinary(); + byte[] buf = new byte[0x1000]; + + //verify that reading behind EOF returns -1 + assertEquals("reading behind EOF must return -1", -1, bin.read(buf, bin.getSize())); + + //read content using Binary.read() + ByteArrayOutputStream out = new ByteArrayOutputStream(); + for (int cnt, pos = 0; (cnt = bin.read(buf, pos)) > 0; pos += cnt) { + out.write(buf, 0, cnt); + } + byte[] content = out.toByteArray(); + assertEquals("unexpected content length", bin.getSize(), content.length); + + //verify against stream + InputStream in = val.getStream(); + try { + int k = 0; + for (int b; (b = in.read()) != -1; k++) { + assertEquals("Value.getStream().read() and Value.getBinary().read() " + + "return different values.", b, content[k]); + } + assertEquals("unexpected content length", k, content.length); + } finally { + try { + in.close(); + } catch (IOException ignore) {} + } + + //verify random access + buf = new byte[1]; + assertTrue("unexpected result of Value.getBinary.read()", -1 != bin.read(buf, 0)); + assertEquals("unexpected result of Value.getBinary.read()", content[0], buf[0]); + if (content.length > 0) { + assertTrue("unexpected result of Value.getBinary.read()", -1 != bin.read(buf, content.length - 1)); + assertEquals("unexpected result of Value.getBinary.read()", content[content.length - 1], buf[0]); + assertTrue("unexpected result of Value.getBinary.read()", -1 != bin.read(buf, 0)); + assertEquals("unexpected result of Value.getBinary.read()", content[0], buf[0]); + } + } + } \ No newline at end of file Index: jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/SetValueBinaryTest.java =================================================================== --- jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/SetValueBinaryTest.java (revision 779233) +++ jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/SetValueBinaryTest.java (working copy) @@ -19,10 +19,7 @@ import org.apache.jackrabbit.test.AbstractJCRTest; import org.apache.jackrabbit.test.NotExecutableException; -import javax.jcr.Property; -import javax.jcr.Value; -import javax.jcr.RepositoryException; -import javax.jcr.Node; +import javax.jcr.*; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.IOException; @@ -113,6 +110,21 @@ } /** + * Test the persistence of a property modified with an BinaryValue parameter + * and saved from the Session + */ + public void testBinarySessionJcr2() throws RepositoryException, IOException { + property1.setValue(value); + superuser.save(); + InputStream in = property1.getValue().getBinary().getStream(); + try { + compareStream(data, in); + } finally { + in.close(); + } + } + + /** * Test the persistence of a property modified with an input stream * parameter and saved from the parent Node */ @@ -133,6 +145,23 @@ } /** + * Test the persistence of a property modified with an input stream + * parameter and saved from the parent Node + */ + public void testBinaryParentJcr2() throws RepositoryException, IOException { + Binary bin = value.getBinary(); + property1.setValue(bin); + node.save(); + bin = property1.getValue().getBinary(); + InputStream in = bin.getStream(); + try { + compareStream(data, in); + } finally { + in.close(); + } + } + + /** * Test the deletion of a property by assigning it a null value, saved from * the Session */ @@ -141,7 +170,7 @@ throw new NotExecutableException("property " + property1.getName() + " can not be removed"); } - property1.setValue((InputStream) null); + property1.setValue((Value) null); superuser.save(); try {