Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #96 from futures/CodeCleanup
Browse files Browse the repository at this point in the history
Code cleanup
  • Loading branch information
ajs6f committed Jul 5, 2013
2 parents 83a2f2f + 8ce7d7d commit b34101d
Show file tree
Hide file tree
Showing 32 changed files with 1,076 additions and 781 deletions.
117 changes: 62 additions & 55 deletions fcrepo-http-commons/src/test/java/org/fcrepo/AbstractResourceTest.java
Expand Up @@ -16,10 +16,14 @@

package org.fcrepo;

import static org.fcrepo.AbstractResource.toPath;
import static org.fcrepo.test.util.PathSegmentImpl.createPathList;
import static org.fcrepo.test.util.TestHelpers.setField;
import static org.fcrepo.utils.NamespaceTools.getNamespaceRegistry;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.powermock.api.mockito.PowerMockito.mockStatic;

import java.util.List;
Expand All @@ -31,12 +35,11 @@

import org.fcrepo.identifiers.PidMinter;
import org.fcrepo.services.NodeService;
import org.fcrepo.test.util.PathSegmentImpl;
import org.fcrepo.test.util.TestHelpers;
import org.fcrepo.utils.NamespaceTools;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.modeshape.jcr.api.NamespaceRegistry;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
Expand All @@ -47,131 +50,135 @@
@PrepareForTest({NamespaceTools.class})
public class AbstractResourceTest {

AbstractResource testObj;
private AbstractResource testObj;

@Mock
private NodeService mockNodes;

@Mock
private PidMinter mockPids;

@Mock
private UriInfo mockUris;

@Mock
private NamespaceRegistry mockNames;

@Before
public void setUp() {
testObj = new AbstractResource() {

};
initMocks(this);
testObj = new AbstractResource() {};
}

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

@Test
public void testSetPidMinter()throws Exception {
PidMinter mockPids = mock(PidMinter.class);
TestHelpers.setField(testObj, "pidMinter", mockPids);
public void testSetPidMinter() throws Exception {
setField(testObj, "pidMinter", mockPids);
assertEquals(mockPids, testObj.pidMinter);
}

@Test
public void testSetNodeService() throws Exception{
NodeService mockNodes = mock(NodeService.class);
TestHelpers.setField(testObj, "nodeService", mockNodes);
public void testSetNodeService() throws Exception {
setField(testObj, "nodeService", mockNodes);
assertEquals(mockNodes, testObj.nodeService);
}

@Test
public void testSetUriInfo() throws Exception{
UriInfo mockUris = mock(UriInfo.class);
TestHelpers.setField(testObj, "uriInfo", mockUris);
public void testSetUriInfo() throws Exception {
setField(testObj, "uriInfo", mockUris);
assertEquals(mockUris, testObj.uriInfo);
}

@Test
public void testToPath() {
List<PathSegment> pathList =
PathSegmentImpl.createPathList("foo", "", "bar", "baz");
final List<PathSegment> pathList =
createPathList("foo", "", "bar", "baz");
// empty path segments ('//') should be suppressed
String expected = "/foo/bar/baz";
String actual = AbstractResource.toPath(pathList);
final String expected = "/foo/bar/baz";
final String actual = toPath(pathList);
assertEquals(expected, actual);
}

@Test
public void testToPathWorkspace() {
List<PathSegment> pathList =
PathSegmentImpl.createPathList("workspace:abc", "bar", "baz");
final List<PathSegment> pathList =
createPathList("workspace:abc", "bar", "baz");
// workspaces should be ignored
String expected = "/bar/baz";
String actual = AbstractResource.toPath(pathList);
final String expected = "/bar/baz";
final String actual = toPath(pathList);
assertEquals(expected, actual);
}

@Test
public void testToPathWorkspaceInSomeOtherSegment() {
List<PathSegment> pathList =
PathSegmentImpl.createPathList("asdf", "workspace:abc", "bar",
"baz");
final List<PathSegment> pathList =
createPathList("asdf", "workspace:abc", "bar", "baz");
// workspaces should be ignored
String expected = "/asdf/workspace:abc/bar/baz";
String actual = AbstractResource.toPath(pathList);
final String expected = "/asdf/workspace:abc/bar/baz";
final String actual = toPath(pathList);
assertEquals(expected, actual);
}

@Test
public void testToPathWorkspaceWithEmptyPrefix() {
List<PathSegment> pathList =
PathSegmentImpl.createPathList("", "workspace:abc", "bar",
"baz");
final List<PathSegment> pathList =
createPathList("", "workspace:abc", "bar", "baz");
// workspaces should be ignored
String expected = "/bar/baz";
String actual = AbstractResource.toPath(pathList);
final String expected = "/bar/baz";
final String actual = toPath(pathList);
assertEquals(expected, actual);
}

@Test
public void testToPathTransaction() {
List<PathSegment> pathList =
PathSegmentImpl.createPathList("tx:abc", "bar", "baz");
final List<PathSegment> pathList =
createPathList("tx:abc", "bar", "baz");
// workspaces should be ignored
String expected = "/bar/baz";
String actual = AbstractResource.toPath(pathList);
final String expected = "/bar/baz";
final String actual = toPath(pathList);
assertEquals(expected, actual);
}

@Test
public void testToPathTxInSomeOtherSegment() {
List<PathSegment> pathList =
PathSegmentImpl.createPathList("asdf", "tx:abc", "bar", "baz");
final List<PathSegment> pathList =
createPathList("asdf", "tx:abc", "bar", "baz");
// workspaces should be ignored
String expected = "/asdf/tx:abc/bar/baz";
String actual = AbstractResource.toPath(pathList);
final String expected = "/asdf/tx:abc/bar/baz";
final String actual = toPath(pathList);
assertEquals(expected, actual);
}

@Test
public void testToPathTxWithEmptyPrefix() {
List<PathSegment> pathList =
PathSegmentImpl.createPathList("", "tx:abc", "bar", "baz");
final List<PathSegment> pathList =
createPathList("", "tx:abc", "bar", "baz");
// workspaces should be ignored
String expected = "/bar/baz";
String actual = AbstractResource.toPath(pathList);
final String expected = "/bar/baz";
final String actual = toPath(pathList);
assertEquals(expected, actual);
}

@Test
public void testToPathUuid() {
List<PathSegment> pathList = PathSegmentImpl.createPathList("[foo]");
String expected = "[foo]";
String actual = AbstractResource.toPath(pathList);
final List<PathSegment> pathList = createPathList("[foo]");
final String expected = "[foo]";
final String actual = toPath(pathList);
assertEquals(expected, actual);
}

@Test
public void testToPathEmpty() {
List<PathSegment> pathList = PathSegmentImpl.createPathList();
final List<PathSegment> pathList = createPathList();
// empty path segments ('//') should be suppressed
String expected = "/";
String actual = AbstractResource.toPath(pathList);
final String expected = "/";
final String actual = toPath(pathList);
assertEquals(expected, actual);
}
}
Expand Up @@ -20,6 +20,7 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;

import java.net.URI;

Expand All @@ -34,6 +35,7 @@

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;

import com.hp.hpl.jena.rdf.model.Resource;
Expand All @@ -45,12 +47,24 @@ public class HttpGraphSubjectsTest {

private String testPath = "/foo/bar";

@Mock
private Session mockSession;

@Mock private UriInfo ui ;
@Mock private UriBuilder ub;

@Mock
private Workspace mockWorkspace;

@Mock private Resource mockSubject;

@Mock
private Node mockNode;

@Before
public void setUp() {
initMocks(this);
final UriInfo uris = getUriInfoImpl(testPath);
mockSession = mock(Session.class);
testObj =
new HttpGraphSubjects(MockNodeController.class, uris,
mockSession);
Expand All @@ -59,9 +73,7 @@ public void setUp() {
@Test
public void testGetGraphSubject() throws RepositoryException {
final String expected = "http://localhost:8080/fcrepo/rest" + testPath;
final Node mockNode = mock(Node.class);
when(mockNode.getPath()).thenReturn(testPath);
final Workspace mockWorkspace = mock(Workspace.class);
when(mockWorkspace.getName()).thenReturn("default");
when(mockSession.getWorkspace()).thenReturn(mockWorkspace);
when(mockNode.getSession()).thenReturn(mockSession);
Expand All @@ -75,15 +87,12 @@ public void testGetGraphSubject() throws RepositoryException {
@Test
public void testGetNodeFromGraphSubject() throws PathNotFoundException,
RepositoryException {
final Session mockSession = mock(Session.class);
final Node mockNode = mock(Node.class);

when(mockSession.nodeExists(testPath)).thenReturn(true);
when(mockSession.getNode(testPath)).thenReturn(mockNode);
final Workspace mockWorkspace = mock(Workspace.class);
when(mockWorkspace.getName()).thenReturn("default");
when(mockSession.getWorkspace()).thenReturn(mockWorkspace);
// test a good subject
final Resource mockSubject = mock(Resource.class);
when(mockSubject.getURI()).thenReturn(
"http://localhost:8080/fcrepo/rest" + testPath);
when(mockSubject.isURIResource()).thenReturn(true);
Expand Down Expand Up @@ -111,7 +120,6 @@ public void testGetNodeFromGraphSubject() throws PathNotFoundException,

@Test
public void testIsFedoraGraphSubject() {
final Resource mockSubject = mock(Resource.class);
when(mockSubject.getURI()).thenReturn(
"http://localhost:8080/fcrepo/rest/foo");
when(mockSubject.isURIResource()).thenReturn(true);
Expand Down
Expand Up @@ -19,9 +19,9 @@
import static com.google.common.collect.ImmutableBiMap.of;
import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;

import java.util.Map;

Expand All @@ -32,6 +32,7 @@
import org.fcrepo.rdf.GraphSubjects;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.springframework.context.ApplicationContext;

import com.hp.hpl.jena.query.Dataset;
Expand All @@ -44,33 +45,35 @@ public class HttpTripleUtilTest {

private Dataset dataset;

@Mock
private UriInfo mockUriInfo;

@Mock
private GraphSubjects mockSubjects;

@Mock
private UriAwareResourceModelFactory mockBean1;

@Mock
private UriAwareResourceModelFactory mockBean2;

@Mock
private ApplicationContext mockContext;

@Mock
private FedoraResource mockResource;

@Before
public void setUp() {
mockContext = mock(ApplicationContext.class);
initMocks(this);
testObj = new HttpTripleUtil();
testObj.setApplicationContext(mockContext);

dataset = DatasetFactory.create(ModelFactory.createDefaultModel());
mockUriInfo = mock(UriInfo.class);
mockSubjects = mock(GraphSubjects.class);

dataset = DatasetFactory.create(createDefaultModel());
}

@Test
public void shouldAddTriplesFromRegisteredBeans()
throws RepositoryException {
final FedoraResource mockResource = mock(FedoraResource.class);

final UriAwareResourceModelFactory mockBean1 =
mock(UriAwareResourceModelFactory.class);
final UriAwareResourceModelFactory mockBean2 =
mock(UriAwareResourceModelFactory.class);
final Map<String, UriAwareResourceModelFactory> mockBeans =
of("doesnt", mockBean1, "matter", mockBean2);
when(mockContext.getBeansOfType(UriAwareResourceModelFactory.class))
Expand Down
Expand Up @@ -39,7 +39,6 @@
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.sparql.core.DatasetImpl;
import com.sun.jersey.core.util.MultivaluedMapImpl;

public class RdfProviderTest {

Expand Down Expand Up @@ -81,15 +80,15 @@ public void testGetSize() {

}

@SuppressWarnings({"unchecked", "rawtypes"})
@SuppressWarnings("unchecked")
@Test
public void testWriteTo() throws WebApplicationException,
IllegalArgumentException, IOException {
final ByteArrayOutputStream outStream = new ByteArrayOutputStream();

rdfProvider.writeTo(testData, Dataset.class, mock(Type.class), null,
valueOf("application/rdf+xml"),
(MultivaluedMap) new MultivaluedMapImpl(), outStream);
valueOf("application/rdf+xml"), mock(MultivaluedMap.class),
outStream);
final byte[] results = outStream.toByteArray();
assertTrue("Got no output from serialization!", results.length > 0);
assertTrue("Couldn't find test RDF-object mentioned!", new String(
Expand Down

0 comments on commit b34101d

Please sign in to comment.