Skip to content

Commit

Permalink
Removed getBinary methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Nov 6, 2013
1 parent 3b5e47d commit 5270456
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 99 deletions.
7 changes: 4 additions & 3 deletions fcrepo-kernel/src/main/java/org/fcrepo/kernel/Datastream.java
Expand Up @@ -17,7 +17,6 @@

import static com.codahale.metrics.MetricRegistry.name;
import static org.fcrepo.kernel.services.ServiceHelpers.getNodePropertySize;
import static org.fcrepo.kernel.utils.FedoraTypesUtils.getBinary;
import static org.fcrepo.kernel.utils.FedoraTypesUtils.isFedoraDatastream;
import static org.fcrepo.metrics.RegistryService.getMetrics;
import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;
Expand All @@ -41,6 +40,7 @@
import org.fcrepo.kernel.utils.ContentDigest;
import org.modeshape.jcr.api.Binary;
import org.modeshape.jcr.api.JcrConstants;
import org.modeshape.jcr.api.ValueFactory;
import org.slf4j.Logger;

import com.codahale.metrics.Histogram;
Expand Down Expand Up @@ -147,8 +147,9 @@ public void setContent(final InputStream content, final String contentType,
if (storagePolicyDecisionPoint != null) {
hint = storagePolicyDecisionPoint.evaluatePolicies(node);
}

final Binary binary = (Binary) getBinary(node, content, hint);
final ValueFactory modevf =
(ValueFactory) node.getSession().getValueFactory();
final Binary binary = modevf.createBinary(content, hint);

/*
* This next line of code deserves explanation. If we chose for the
Expand Down
Expand Up @@ -31,11 +31,9 @@
import static org.fcrepo.kernel.utils.JcrRdfTools.getRDFNamespaceForJcrNamespace;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.InputStream;
import java.util.Collection;
import java.util.Iterator;

import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.Repository;
Expand All @@ -56,7 +54,6 @@
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
import org.modeshape.jcr.JcrValueFactory;
import org.modeshape.jcr.api.JcrConstants;
import org.modeshape.jcr.api.Namespaced;
import org.slf4j.Logger;
Expand Down Expand Up @@ -272,47 +269,6 @@ public com.hp.hpl.jena.rdf.model.Property apply(
*/
private static final DateTimeFormatter FMT = ISODateTimeFormat.dateTime();

/**
* Creates a JCR {@link Binary}
*
* @param n a {@link Node}
* @param i an {@link InputStream}
* @return a JCR {@link Binary}
*/
public static Binary getBinary(final Node n, final InputStream i) {
try {
checkArgument(n != null,
"null cannot have a Binary created for it!");
checkArgument(i != null,
"null cannot have a Binary created from it!");
return n.getSession().getValueFactory().createBinary(i);
} catch (final RepositoryException e) {
throw propagate(e);
}
}

/**
* Creates a JCR {@link Binary}
*
* @param n a {@link Node}
* @param i an {@link InputStream}
* @return a JCR {@link Binary}
*/
public static Binary getBinary(final Node n, final InputStream i,
final String hint) {
try {
checkArgument(n != null,
"null cannot have a Binary created for it!");
checkArgument(i != null,
"null cannot have a Binary created from it!");
final JcrValueFactory jcrValueFactory =
((JcrValueFactory) n.getSession().getValueFactory());
return jcrValueFactory.createBinary(i, hint);
} catch (final RepositoryException e) {
throw propagate(e);
}
}

/**
* Get the JCR Node Type manager
*
Expand Down
28 changes: 7 additions & 21 deletions fcrepo-kernel/src/test/java/org/fcrepo/kernel/DatastreamTest.java
Expand Up @@ -17,7 +17,6 @@
package org.fcrepo.kernel;

import static org.fcrepo.kernel.Datastream.hasMixin;
import static org.fcrepo.kernel.utils.FedoraTypesUtils.getBinary;
import static org.fcrepo.kernel.utils.TestHelpers.checksumString;
import static org.fcrepo.kernel.utils.TestHelpers.getContentNodeMock;
import static org.fcrepo.kernel.utils.TestHelpers.getPropertyIterator;
Expand All @@ -33,8 +32,6 @@
import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;
import static org.modeshape.jcr.api.JcrConstants.JCR_DATA;
import static org.modeshape.jcr.api.JcrConstants.JCR_MIME_TYPE;
import static org.powermock.api.mockito.PowerMockito.mockStatic;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
Expand All @@ -47,27 +44,19 @@
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.ValueFactory;
import javax.jcr.nodetype.NodeType;

import org.apache.tika.io.IOUtils;
import org.fcrepo.jcr.FedoraJcrTypes;
import org.fcrepo.kernel.exception.InvalidChecksumException;
import org.fcrepo.kernel.utils.FedoraTypesUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.modeshape.jcr.api.ValueFactory;

@RunWith(PowerMockRunner.class)
@PowerMockIgnore({"org.slf4j.*", "javax.xml.parsers.*", "org.apache.xerces.*"})
@PrepareForTest({FedoraTypesUtils.class})
public class DatastreamTest implements FedoraJcrTypes {

private static final String testDsId = "testDs";
Expand Down Expand Up @@ -129,14 +118,12 @@ public void testSetContent() throws RepositoryException,
final org.modeshape.jcr.api.Binary mockBin =
mock(org.modeshape.jcr.api.Binary.class);
final InputStream mockStream = mock(InputStream.class);
mockStatic(FedoraTypesUtils.class);
when(
getBinary(any(Node.class), any(InputStream.class),
any(String.class))).thenReturn(mockBin);
final Node mockContent = getContentNodeMock(8);
when(mockDsNode.getNode(JCR_CONTENT)).thenReturn(mockContent);
when(mockDsNode.getSession()).thenReturn(mockSession);
when(mockSession.getValueFactory()).thenReturn(mockVF);
when(mockVF.createBinary(any(InputStream.class))).thenReturn(mockBin);
when(mockVF.createBinary(any(InputStream.class), any(String.class)))
.thenReturn(mockBin);
final Property mockData = mock(Property.class);
when(mockContent.canAddMixin(FEDORA_BINARY)).thenReturn(true);
when(mockContent.setProperty(JCR_DATA, mockBin)).thenReturn(mockData);
Expand All @@ -152,13 +139,12 @@ public void testSetContentWithChecksumMismatch()
final org.modeshape.jcr.api.Binary mockBin =
mock(org.modeshape.jcr.api.Binary.class);
final InputStream mockStream = mock(InputStream.class);
mockStatic(FedoraTypesUtils.class);
when(getBinary(any(Node.class), any(InputStream.class),
any(String.class))).thenReturn(mockBin);
final Node mockContent = getContentNodeMock(8);
when(mockDsNode.getSession()).thenReturn(mockSession);
when(mockDsNode.getNode(JCR_CONTENT)).thenReturn(mockContent);
when(mockSession.getValueFactory()).thenReturn(mockVF);
when(mockVF.createBinary(any(InputStream.class))).thenReturn(mockBin);
when(mockVF.createBinary(any(InputStream.class), any(String.class)))
.thenReturn(mockBin);
final Property mockData = mock(Property.class);
when(mockContent.canAddMixin(FEDORA_BINARY)).thenReturn(true);
when(mockContent.setProperty(JCR_DATA, mockBin)).thenReturn(mockData);
Expand Down
Expand Up @@ -19,7 +19,6 @@
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createResource;
import static java.util.Arrays.asList;
import static org.fcrepo.kernel.services.ServiceHelpers.getCheckCacheFixityFunction;
import static org.fcrepo.kernel.utils.FedoraTypesUtils.getBinary;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentCaptor.forClass;
Expand Down Expand Up @@ -49,7 +48,6 @@
import org.fcrepo.kernel.rdf.GraphSubjects;
import org.fcrepo.kernel.services.functions.CheckCacheEntryFixity;
import org.fcrepo.kernel.services.policy.StoragePolicyDecisionPoint;
import org.fcrepo.kernel.utils.FedoraTypesUtils;
import org.fcrepo.kernel.utils.FixityResult;
import org.fcrepo.kernel.utils.JcrRdfTools;
import org.fcrepo.kernel.utils.LowLevelCacheEntry;
Expand All @@ -62,6 +60,7 @@
import org.mockito.Matchers;
import org.mockito.Mock;
import org.modeshape.jcr.api.Binary;
import org.modeshape.jcr.api.ValueFactory;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
Expand All @@ -73,8 +72,7 @@
@RunWith(PowerMockRunner.class)
@PowerMockIgnore({"org.slf4j.*", "org.apache.xerces.*", "javax.xml.*",
"org.xml.sax.*", "javax.management.*"})
@PrepareForTest({FedoraTypesUtils.class, ServiceHelpers.class,
JcrRdfTools.class})
@PrepareForTest({JcrRdfTools.class})
public class DatastreamServiceTest implements FedoraJcrTypes {

private static final String MOCK_CONTENT_TYPE = "application/test-data";
Expand All @@ -99,6 +97,9 @@ public class DatastreamServiceTest implements FedoraJcrTypes {

private LowLevelStorageService llStore;

@Mock
private ValueFactory mockValueFactory;

@Before
public void setUp() throws RepositoryException {
initMocks(this);
Expand All @@ -116,17 +117,21 @@ public void testCreateDatastreamNode() throws Exception {
final Binary mockBinary = mock(Binary.class);
when(mockRoot.getNode(testPath.substring(1))).thenReturn(mockNode);
when(mockNode.getNode(JCR_CONTENT)).thenReturn(mockContent);
when(mockNode.getMixinNodeTypes()).thenReturn(new NodeType[] {});
when(mockData.getBinary()).thenReturn(mockBinary);

final InputStream mockIS = mock(InputStream.class);
when(mockContent.setProperty(JCR_DATA, mockBinary))
.thenReturn(mockData);
when(mockContent.getProperty(JCR_DATA)).thenReturn(mockData);
final StoragePolicyDecisionPoint pdp = mock(StoragePolicyDecisionPoint.class);
when(pdp.evaluatePolicies(mockNode)).thenReturn(null);
testObj.setStoragePolicyDecisionPoint(pdp);
mockStatic(FedoraTypesUtils.class);
when(getBinary(eq(mockNode), eq(mockIS), any(String.class)))
.thenReturn(mockBinary);
when(mockNode.getSession().getValueFactory()).thenReturn(
mockValueFactory);
when(
mockValueFactory.createBinary(any(InputStream.class),
any(String.class))).thenReturn(mockBinary);

final Node actual =
testObj.createDatastreamNode(mockSession, testPath,
Expand Down
Expand Up @@ -23,7 +23,6 @@
import static org.fcrepo.jcr.FedoraJcrTypes.FEDORA_OBJECT;
import static org.fcrepo.kernel.utils.FedoraTypesUtils.convertDateToXSDString;
import static org.fcrepo.kernel.utils.FedoraTypesUtils.getBaseVersion;
import static org.fcrepo.kernel.utils.FedoraTypesUtils.getBinary;
import static org.fcrepo.kernel.utils.FedoraTypesUtils.getDefinitionForPropertyName;
import static org.fcrepo.kernel.utils.FedoraTypesUtils.getPredicateForProperty;
import static org.fcrepo.kernel.utils.FedoraTypesUtils.getRepositoryCount;
Expand Down Expand Up @@ -186,29 +185,6 @@ public void testGetPredicateForProperty() throws RepositoryException {
} catch (final RuntimeException e) {} // expected
}

@Test
public void testGetBinary() throws RepositoryException {
when(mockNode.getSession()).thenReturn(mockSession);
when(mockSession.getValueFactory()).thenReturn(mockVF);
getBinary(mockNode, mockInput);
verify(mockVF).createBinary(mockInput);
// try it with hints
when(mockSession.getValueFactory()).thenReturn(mockJVF);
final String mockHint = "storage-hint";
getBinary(mockNode, mockInput, mockHint);
verify(mockJVF).createBinary(mockInput, mockHint);

when(mockNode.getSession()).thenThrow(new RepositoryException());
try {
getBinary(mockNode, mockInput);
fail("Unexpected completion after RepositoryException!");
} catch (final RuntimeException e) {} // expected
try {
getBinary(mockNode, mockInput, mockHint);
fail("Unexpected completion after RepositoryException!");
} catch (final RuntimeException e) {} // expected
}

@Test
public void testGetDefinitionForPropertyName() throws RepositoryException {
final String mockPropertyName = "mock:property";
Expand Down

0 comments on commit 5270456

Please sign in to comment.