Skip to content

Commit

Permalink
Unit test clean-up: statics
Browse files Browse the repository at this point in the history
Resolves: https://www.pivotaltracker.com/story/show/58814930

Removed unneeded static methods from NamespaceTools
Removed an unneeded static from FedoraTypesUtils
Removed getBinary methods
Removing now-unneeded static mocking
  • Loading branch information
ajs6f authored and Andrew Woods committed Nov 6, 2013
1 parent e79b15c commit 92461f3
Show file tree
Hide file tree
Showing 17 changed files with 108 additions and 319 deletions.
Expand Up @@ -29,7 +29,6 @@
import java.net.URI;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
Expand Down Expand Up @@ -121,16 +120,6 @@ public abstract class AbstractResource {
*/
protected static final JcrTools jcrTools = new JcrTools(true);

/**
* Initialize our resource
* @todo is this actually needed?
* @throws RepositoryException
*/
@PostConstruct
public void initialize() throws RepositoryException {

}

/**
* Convert a JAX-RS list of PathSegments to a JCR path
*
Expand Down
Expand Up @@ -19,17 +19,10 @@
import static org.fcrepo.http.commons.AbstractResource.toPath;
import static org.fcrepo.http.commons.test.util.PathSegmentImpl.createPathList;
import static org.fcrepo.http.commons.test.util.TestHelpers.setField;
import static org.fcrepo.kernel.utils.NamespaceTools.getNamespaceRegistry;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.powermock.api.mockito.PowerMockito.mockStatic;

import java.util.List;

import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.ws.rs.core.PathSegment;
import javax.ws.rs.core.UriInfo;

Expand Down Expand Up @@ -70,13 +63,6 @@ public void setUp() {
testObj = new AbstractResource() {};
}

@Test
public void testInitialize() throws RepositoryException {
mockStatic(NamespaceTools.class);
when(getNamespaceRegistry(any(Session.class))).thenReturn(mockNames);
testObj.initialize();
}

@Test
public void testSetPidMinter() throws Exception {
setField(testObj, "pidMinter", mockPids);
Expand Down
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 @@ -25,7 +25,6 @@
import static org.fcrepo.kernel.RdfLexicon.SEARCH_HAS_MORE;
import static org.fcrepo.kernel.RdfLexicon.SEARCH_HAS_TOTAL_RESULTS;
import static org.fcrepo.kernel.utils.FedoraTypesUtils.getRepositoryCount;
import static org.fcrepo.kernel.utils.NamespaceTools.getNamespaceRegistry;
import static org.fcrepo.metrics.RegistryService.getMetrics;
import static org.slf4j.LoggerFactory.getLogger;

Expand Down Expand Up @@ -161,7 +160,8 @@ public NodeTypeIterator getAllNodeTypes(final Session session)
public static Map<String, String> getRepositoryNamespaces(
final Session session) throws RepositoryException {

final NamespaceRegistry reg = getNamespaceRegistry(session);
final NamespaceRegistry reg =
session.getWorkspace().getNamespaceRegistry();
return asMap(newHashSet(reg.getPrefixes()),
new Function<String, String>() {

Expand Down
Expand Up @@ -31,18 +31,15 @@
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;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.ValueFactory;
import javax.jcr.nodetype.NodeType;
import javax.jcr.nodetype.NodeTypeManager;
import javax.jcr.nodetype.PropertyDefinition;
Expand All @@ -57,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 @@ -239,24 +235,6 @@ public boolean apply(final Node n) {
}
};

/**
* Retrieves a JCR {@link ValueFactory} for use with a @ link Node}
*/
public static Function<Node, ValueFactory> getValueFactory =
new Function<Node, ValueFactory>() {

@Override
public ValueFactory apply(final Node n) {
try {
checkArgument(n != null,
"null has no ValueFactory associated with it!");
return n.getSession().getValueFactory();
} catch (final RepositoryException e) {
throw propagate(e);
}
}
};

/**
* Map a JCR property to an RDF property with the right namespace URI and
* local name
Expand Down Expand Up @@ -291,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
Expand Up @@ -28,7 +28,6 @@
import static javax.jcr.PropertyType.WEAKREFERENCE;
import static org.fcrepo.kernel.RdfLexicon.HAS_MEMBER_OF_RESULT;
import static org.fcrepo.kernel.RdfLexicon.REPOSITORY_NAMESPACE;
import static org.fcrepo.kernel.utils.FedoraTypesUtils.getValueFactory;
import static org.fcrepo.kernel.utils.NamespaceTools.getNamespaceRegistry;
import static org.slf4j.LoggerFactory.getLogger;

Expand Down Expand Up @@ -366,7 +365,7 @@ public boolean isInternalProperty(final Node subjectNode,
*/
public Value createValue(final Node node, final RDFNode data, final int type)
throws RepositoryException {
final ValueFactory valueFactory = getValueFactory.apply(node);
final ValueFactory valueFactory = node.getSession().getValueFactory();
assert (valueFactory != null);

if (data.isURIResource()
Expand Down Expand Up @@ -455,7 +454,8 @@ public String getPropertyNameFromPredicate(final Node node, final com.hp.hpl.jen
final String namespace =
getJcrNamespaceForRDFNamespace(predicate.getNameSpace());

final NamespaceRegistry namespaceRegistry = getNamespaceRegistry(node);
final NamespaceRegistry namespaceRegistry =
getNamespaceRegistry.apply(node);

assert (namespaceRegistry != null);

Expand Down
Expand Up @@ -17,11 +17,8 @@

import static com.google.common.base.Preconditions.checkArgument;

import javax.jcr.Item;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;

import org.modeshape.jcr.api.NamespaceRegistry;

import com.google.common.base.Function;
Expand All @@ -34,28 +31,6 @@
*/
public abstract class NamespaceTools {

/**
* Get the namespace registry for the session
* @param session
* @return
* @throws RepositoryException
*/
public static NamespaceRegistry getNamespaceRegistry(final Session session)
throws RepositoryException {
return (NamespaceRegistry) session.getWorkspace().getNamespaceRegistry();
}

/**
* Get the namespace registry for the given node
* @param item
* @return
* @throws RepositoryException
*/
public static NamespaceRegistry getNamespaceRegistry(final Item item)
throws RepositoryException {
return getNamespaceRegistry(item.getSession());
}

/**
* We need the Modeshape NamespaceRegistry, because it allows us to register
* anonymous namespaces.
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 @@ -56,7 +56,6 @@
import org.fcrepo.kernel.rdf.impl.DefaultGraphSubjects;
import org.fcrepo.kernel.utils.FedoraTypesUtils;
import org.fcrepo.kernel.utils.JcrRdfTools;
import org.fcrepo.kernel.utils.NamespaceTools;
import org.fcrepo.kernel.utils.iterators.RdfStream;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -85,8 +84,7 @@
"com.hp.hpl.jena.*",
"com.codahale.metrics.*"
})
@PrepareForTest({NamespaceTools.class, JcrRdfTools.class,
FedoraTypesUtils.class})
@PrepareForTest({JcrRdfTools.class, FedoraTypesUtils.class})
public class FedoraResourceTest {

private FedoraResource testObj;
Expand Down

0 comments on commit 92461f3

Please sign in to comment.