Skip to content

Commit

Permalink
Merge pull request #94 from futures/NextSourceCleanup
Browse files Browse the repository at this point in the history
More source code cleanup
  • Loading branch information
ajs6f committed Jul 4, 2013
2 parents 6ddee90 + 41429f1 commit 885c7fe
Show file tree
Hide file tree
Showing 39 changed files with 521 additions and 527 deletions.
Expand Up @@ -21,13 +21,13 @@
import javax.jcr.Node;

/**
* Interface for retrieving a Dublin Core XML output
* from a node
* Interface for retrieving a Dublin Core XML output from a node
*/
public interface DCGenerator {

/**
* Get the Dublin Core content as an InputStream
*
* @param node node to retrieve a Dublin Core serialization for
* @return
*/
Expand Down
Expand Up @@ -16,12 +16,11 @@

package org.fcrepo.generator.dublincore;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
Expand All @@ -31,12 +30,12 @@
import org.slf4j.Logger;

/**
* Derive a Dublin Core document from the JCR properties within
* the DC namespace.
* Derive a Dublin Core document from the JCR properties within the DC
* namespace.
*/
public class JcrPropertiesGenerator implements DCGenerator {

private static final Logger logger =
private static final Logger LOGGER =
getLogger(JcrPropertiesGenerator.class);

public static final String[] SALIENT_DC_PROPERTY_NAMESPACES =
Expand Down Expand Up @@ -73,10 +72,9 @@ public InputStream getStream(final Node node) {

str.append("</oai_dc:dc>");

return new ByteArrayInputStream(str.toString().getBytes(
StandardCharsets.UTF_8));
return new ByteArrayInputStream(str.toString().getBytes(UTF_8));
} catch (final RepositoryException e) {
logger.error("Repository exception: {}", e);
LOGGER.error("Repository exception: {}", e);
return null;
}
}
Expand Down
Expand Up @@ -34,7 +34,7 @@
*/
public class WellKnownDatastreamGenerator implements DCGenerator {

private static final Logger logger =
private static final Logger LOGGER =
getLogger(WellKnownDatastreamGenerator.class);

private String wellKnownDsid;
Expand All @@ -46,7 +46,7 @@ public InputStream getStream(final Node node) {
return getContentInputStream(node);
} catch (final RepositoryException e) {

logger.warn("logged exception", e);
LOGGER.warn("logged exception", e);

return null;
}
Expand Down
Expand Up @@ -16,12 +16,11 @@

package org.fcrepo.generator.dublincore;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import javax.jcr.Node;

import org.slf4j.Logger;
Expand All @@ -31,14 +30,14 @@
*/
public class WorstCaseGenerator implements DCGenerator {

private static final Logger logger = getLogger(WorstCaseGenerator.class);
private static final Logger LOGGER = getLogger(WorstCaseGenerator.class);

@Override
public InputStream getStream(final Node node) {
logger.debug("Writing an empty oai dc document");
LOGGER.debug("Writing an empty oai dc document");
final String str =
"<oai_dc:dc xmlns:oai_dc=\"http://www.openarchives.org/OAI/2.0/oai_dc/\" />";

return new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8));
return new ByteArrayInputStream(str.getBytes(UTF_8));
}
}
Expand Up @@ -16,15 +16,16 @@

package org.fcrepo.generator;

import static java.util.Arrays.asList;
import static org.fcrepo.test.util.PathSegmentImpl.createPathList;
import static org.fcrepo.test.util.TestHelpers.mockSession;
import static org.fcrepo.test.util.TestHelpers.setField;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.InputStream;
import java.util.Arrays;

import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException;
Expand All @@ -33,7 +34,6 @@
import org.fcrepo.FedoraResource;
import org.fcrepo.generator.dublincore.DCGenerator;
import org.fcrepo.services.NodeService;
import org.fcrepo.test.util.TestHelpers;
import org.junit.Before;
import org.junit.Test;

Expand All @@ -51,19 +51,19 @@ public class DublinCoreGeneratorTest {
public void setUp() throws RepositoryException, NoSuchFieldException {
mockNodeService = mock(NodeService.class);
testObj = new DublinCoreGenerator();
TestHelpers.setField(testObj, "nodeService", mockNodeService);
setField(testObj, "nodeService", mockNodeService);

mockSession = TestHelpers.mockSession(testObj);
TestHelpers.setField(testObj, "session", mockSession);
mockSession = mockSession(testObj);
setField(testObj, "session", mockSession);
mockGenerator = mock(DCGenerator.class);
testObj.dcgenerators = Arrays.asList(mockGenerator);
testObj.dcgenerators = asList(mockGenerator);
}

@Test
public void testGetObjectAsDublinCore() throws RepositoryException {
testObj.dcgenerators = Arrays.asList(mockGenerator);
InputStream mockIS = mock(InputStream.class);
FedoraResource mockResource = mock(FedoraResource.class);
testObj.dcgenerators = asList(mockGenerator);
final InputStream mockIS = mock(InputStream.class);
final FedoraResource mockResource = mock(FedoraResource.class);
when(mockResource.getNode()).thenReturn(mock(Node.class));
when(mockNodeService.getObject(mockSession, "/objects/foo"))
.thenReturn(mockResource);
Expand All @@ -74,13 +74,13 @@ public void testGetObjectAsDublinCore() throws RepositoryException {

@Test
public void testNoGenerators() {
testObj.dcgenerators = Arrays.asList(new DCGenerator[0]);
testObj.dcgenerators = asList(new DCGenerator[0]);
try {
testObj.getObjectAsDublinCore(createPathList("objects", "foo"));
fail("Should have failed without a generator configured!");
} catch (PathNotFoundException ex) {
} catch (final PathNotFoundException ex) {
// this is what we expect
} catch (RepositoryException e) {
} catch (final RepositoryException e) {
fail("unexpected RepositoryException: " + e.getMessage());
}
}
Expand Down
Expand Up @@ -16,6 +16,7 @@

package org.fcrepo.generator.dublincore;

import static org.fcrepo.generator.dublincore.JcrPropertiesGenerator.SALIENT_DC_PROPERTY_NAMESPACES;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -58,8 +59,7 @@ public void testGetStreamNoProperties() throws Exception {

final PropertyIterator mockIterator = mock(PropertyIterator.class);
when(mockIterator.hasNext()).thenReturn(false);
when(
mockNode.getProperties(JcrPropertiesGenerator.SALIENT_DC_PROPERTY_NAMESPACES))
when(mockNode.getProperties(SALIENT_DC_PROPERTY_NAMESPACES))
.thenReturn(mockIterator);

final InputStream inputStream =
Expand Down Expand Up @@ -97,8 +97,7 @@ public void testGetStreamSingleValuedProperties() throws Exception {
when(mockIterator.nextProperty()).thenReturn(mockProperty1,
mockProperty2);

when(
mockNode.getProperties(JcrPropertiesGenerator.SALIENT_DC_PROPERTY_NAMESPACES))
when(mockNode.getProperties(SALIENT_DC_PROPERTY_NAMESPACES))
.thenReturn(mockIterator);

final InputStream inputStream =
Expand Down Expand Up @@ -138,8 +137,7 @@ public void testGetStreamMultivaluedProperties() throws Exception {

when(mockIterator.nextProperty()).thenReturn(mockProperty1);

when(
mockNode.getProperties(JcrPropertiesGenerator.SALIENT_DC_PROPERTY_NAMESPACES))
when(mockNode.getProperties(SALIENT_DC_PROPERTY_NAMESPACES))
.thenReturn(mockIterator);

final InputStream inputStream =
Expand Down
Expand Up @@ -45,20 +45,20 @@ public void setUp() {

@Test
public void testGetStreamAbsent() {
Node mockNode = mock(Node.class);
InputStream actual = testObj.getStream(mockNode);
final Node mockNode = mock(Node.class);
final InputStream actual = testObj.getStream(mockNode);
assertTrue(actual == null);
}

@Test
public void testGetStreamPresent() throws Exception {
String dsid = "foo";
final String dsid = "foo";
testObj.setWellKnownDsid(dsid);
Node mockNode = mock(Node.class);
Node mockDS = mock(Node.class);
Node mockCN = mock(Node.class);
Binary mockB = mock(Binary.class);
Property mockD = mock(Property.class);
final Node mockNode = mock(Node.class);
final Node mockDS = mock(Node.class);
final Node mockCN = mock(Node.class);
final Binary mockB = mock(Binary.class);
final Property mockD = mock(Property.class);
when(mockNode.hasNode(dsid)).thenReturn(true);
when(mockNode.getNode(dsid)).thenReturn(mockDS);
when(mockDS.getNode(JCR_CONTENT)).thenReturn(mockCN);
Expand All @@ -71,11 +71,11 @@ public void testGetStreamPresent() throws Exception {
@Test
public void testSetWellKnownDsid() throws Exception {
testObj.setWellKnownDsid("foo");
Field field =
final Field field =
WellKnownDatastreamGenerator.class
.getDeclaredField("wellKnownDsid");
field.setAccessible(true);
String actual = (String) field.get(testObj);
final String actual = (String) field.get(testObj);
assertEquals("foo", actual);
}
}
Expand Up @@ -44,9 +44,9 @@ public void setUp() throws JAXBException {

@Test
public void testGetStream() throws Exception {
Node mockNode = mock(Node.class);
InputStream out = testObj.getStream(mockNode);
OaiDublinCore actual =
final Node mockNode = mock(Node.class);
final InputStream out = testObj.getStream(mockNode);
final OaiDublinCore actual =
(OaiDublinCore) context.createUnmarshaller().unmarshal(out);
assertTrue(actual != null);
}
Expand Down
30 changes: 12 additions & 18 deletions fcrepo-http-api/src/test/java/org/fcrepo/api/FedoraContentTest.java
Expand Up @@ -48,7 +48,6 @@
import org.fcrepo.services.DatastreamService;
import org.fcrepo.services.NodeService;
import org.fcrepo.test.util.TestHelpers;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.modeshape.jcr.api.JcrConstants;
Expand Down Expand Up @@ -76,22 +75,17 @@ public void setUp() throws Exception {
TestHelpers.setField(testObj, "session", mockSession);
}

@After
public void tearDown() {

}

@Test
public void testPutContent() throws RepositoryException, IOException,
InvalidChecksumException, URISyntaxException {
InvalidChecksumException, URISyntaxException {
final String pid = "FedoraDatastreamsTest1";
final String dsId = "testDS";
final String dsContent = "asdf";
final String dsPath = "/" + pid + "/" + dsId;
final InputStream dsContentStream = IOUtils.toInputStream(dsContent);
final Node mockNode = mock(Node.class);
when(mockNode.isNew()).thenReturn(true);
Node mockContentNode = mock(Node.class);
final Node mockContentNode = mock(Node.class);
when(mockNode.getNode(JcrConstants.JCR_CONTENT)).thenReturn(
mockContentNode);
when(mockContentNode.getPath()).thenReturn(dsPath + "/jcr:content");
Expand All @@ -112,7 +106,7 @@ public void testPutContent() throws RepositoryException, IOException,

@Test
public void testCreateContent() throws RepositoryException, IOException,
InvalidChecksumException, URISyntaxException {
InvalidChecksumException, URISyntaxException {
final String pid = "FedoraDatastreamsTest1";
final String dsId = "xyz";
final String dsContent = "asdf";
Expand All @@ -121,16 +115,15 @@ public void testCreateContent() throws RepositoryException, IOException,
final Node mockNode = mock(Node.class);

when(mockNode.isNew()).thenReturn(true);
Node mockContentNode = mock(Node.class);
final Node mockContentNode = mock(Node.class);
when(mockNode.getNode(JcrConstants.JCR_CONTENT)).thenReturn(
mockContentNode);
when(mockContentNode.getPath()).thenReturn(dsPath + "/jcr:content");
when(mockNodeService.exists(mockSession, dsPath)).thenReturn(false);
when(
mockDatastreams.createDatastreamNode(any(Session.class),
eq(dsPath), anyString(), any(InputStream.class),
eq((URI) null))).thenReturn(
mockNode);
eq((URI) null))).thenReturn(mockNode);
when(mockDatastreams.exists(mockSession, dsPath)).thenReturn(true);
final Response actual =
testObj.create(createPathList(pid, dsId), null,
Expand All @@ -143,28 +136,29 @@ public void testCreateContent() throws RepositoryException, IOException,

@Test
public void testCreateContentAtMintedPath() throws RepositoryException,
IOException, InvalidChecksumException, URISyntaxException, NoSuchFieldException {
IOException, InvalidChecksumException, URISyntaxException,
NoSuchFieldException {
final String pid = "FedoraDatastreamsTest1";
final String dsId = "fcr:new";
final String dsContent = "asdf";
final String dsPath = "/" + pid + "/" + dsId;
final InputStream dsContentStream = IOUtils.toInputStream(dsContent);
final Node mockNode = mock(Node.class);

PidMinter mockMinter = mock(PidMinter.class);
final PidMinter mockMinter = mock(PidMinter.class);
when(mockMinter.mintPid()).thenReturn("xyz");
TestHelpers.setField(testObj, "pidMinter", mockMinter);
when(mockNode.isNew()).thenReturn(true);
Node mockContentNode = mock(Node.class);
final Node mockContentNode = mock(Node.class);
when(mockNode.getNode(JcrConstants.JCR_CONTENT)).thenReturn(
mockContentNode);
when(mockContentNode.getPath()).thenReturn(dsPath + "/jcr:content");
when(mockNodeService.exists(mockSession, dsPath)).thenReturn(false);
when(
mockDatastreams.createDatastreamNode(any(Session.class),
eq("/" + pid + "/xyz"), anyString(),
any(InputStream.class), eq((URI) null)
)).thenReturn(mockNode);
any(InputStream.class), eq((URI) null))).thenReturn(
mockNode);
when(mockDatastreams.exists(mockSession, dsPath)).thenReturn(true);
final Response actual =
testObj.create(createPathList(pid, dsId), null,
Expand All @@ -177,7 +171,7 @@ public void testCreateContentAtMintedPath() throws RepositoryException,

@Test
public void testModifyContent() throws RepositoryException, IOException,
InvalidChecksumException, URISyntaxException {
InvalidChecksumException, URISyntaxException {
final String pid = "FedoraDatastreamsTest1";
final String dsId = "testDS";
final String dsContent = "asdf";
Expand Down

0 comments on commit 885c7fe

Please sign in to comment.