Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MODE-1308 Fixed ModeShape to allow any javax.jcr.Binary implementation #232

Merged
merged 2 commits into from
Dec 1, 2011
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 14 additions & 2 deletions modeshape-jcr/src/main/java/org/modeshape/jcr/JcrValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -565,8 +565,20 @@ protected Object convertToType( int type,
return valueFactories.getDecimalFactory().create(value);
case PropertyType.URI:
return valueFactories.getUriFactory().create(value);
case PropertyType.BINARY:
return valueFactories.getBinaryFactory().create(value);
case PropertyType.BINARY: {
//fix for MODE-1308 - any implementations for javax.jcr.Binary should produce valid values
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The if-else should be reversed, because our JcrBinary implements javax.jcr.Binary. So as it stands, we're always converting all Binary implementations to new Binary instances. By checking explicitly for JcrBinary, we'll be able to use the instance directly.

One more minor nit pick: the comment above should probably be moved into the if (value instanceof javax.jcr.Binary) block, and say something like "Support any implementation of javax.jcr.Binary (see MODE-1308)". I know this is a subtle change, but it de-emphasizes this as a 'fix' and emphasizes that this is just part of the logic.

if (value instanceof javax.jcr.Binary) {
javax.jcr.Binary jcrBinary = (javax.jcr.Binary) value;
try {
return valueFactories.getBinaryFactory().create(jcrBinary.getStream(), jcrBinary.getSize());
} catch (RepositoryException e) {
throw new RuntimeException(e);
}
}
else {
return valueFactories.getBinaryFactory().create(value);
}
}
case PropertyType.STRING:
return valueFactories.getStringFactory().create(value);
case PropertyType.UNDEFINED:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,13 @@
*/
package org.modeshape.jcr;

import javax.jcr.*;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.io.ByteArrayInputStream;
import javax.jcr.Binary;
import javax.jcr.Item;
import javax.jcr.ItemNotFoundException;
import javax.jcr.ItemVisitor;
import javax.jcr.Node;
import javax.jcr.Repository;
import javax.jcr.Session;
import java.io.*;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
Expand All @@ -45,7 +39,7 @@
import org.modeshape.jcr.api.Workspace;

/**
*
*
*/
public class AbstractJcrPropertyTest extends AbstractJcrTest {

Expand Down Expand Up @@ -291,4 +285,41 @@ public void shouldNotIncludeBinaryContentsInToString() throws Exception {
assertThat(toString.indexOf("**binary-value") > 0, is(true));
}

@FixFor( "MODE-1308" )
@Test
public void shouldAllowAnyBinaryImplementation() throws Exception {
Node node = rootNode.addNode("nodeWithBinaryProperty", "nt:unstructured");
final String stringValue = "This is the string stringValue";
Binary binaryValue = new Binary() {
public InputStream getStream() throws RepositoryException {
return new ByteArrayInputStream(stringValue.getBytes());
}

public int read( byte[] b, long position ) throws IOException, RepositoryException {
byte[] content = stringValue.getBytes();
int length = b.length + position < content.length ? b.length : (int)(content.length - position);
System.arraycopy(content, (int)position, b, 0, length);
return length;
}

public long getSize() throws RepositoryException {
return stringValue.getBytes().length;
}

public void dispose() {
}
};
node.setProperty("binProp", binaryValue);
Binary nodeValue = node.getProperty("binProp").getBinary();
assertNotNull(nodeValue);
assertEquals(stringValue.getBytes().length, nodeValue.getSize());
byte[] buffer = new byte[100];
int available;
InputStream inputStream = nodeValue.getStream();
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
while ((available = inputStream.read(buffer)) != -1) {
byteOut.write(buffer, 0, available);
}
assertArrayEquals(stringValue.getBytes(), byteOut.toByteArray());
}
}