Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
More code cleanup
  • Loading branch information
ajs6f committed May 15, 2013
1 parent 72629b9 commit 14d4b99
Show file tree
Hide file tree
Showing 10 changed files with 364 additions and 280 deletions.
Expand Up @@ -175,7 +175,7 @@ public void testAddVersionLabel() throws RepositoryException {
verify(mockVersionHistory).addVersionLabel("uuid", "v1.0.0", true);
}

private static void setField(final String name, final Class clazz,
private static void setField(final String name, final Class<?> clazz,
final Object value, final Object object) {
try {
final Field field = clazz.getDeclaredField(name);
Expand Down
@@ -1,9 +1,9 @@

package org.fcrepo.integration;

import java.io.ByteArrayInputStream;

import javax.inject.Inject;
import javax.jcr.Node;
import javax.jcr.Repository;
import javax.jcr.Session;

Expand All @@ -17,37 +17,32 @@
@ContextConfiguration({"/spring-test/repo.xml"})
public class PolicyDecisionPointDemoIT extends AbstractIT {

@Inject
Repository repo;

@Inject
DatastreamService datastreamService;
@Inject
Repository repo;

@Inject
ObjectService objectService;
@Inject
DatastreamService datastreamService;

@Test
public void shouldDemonstratePolicyDecisionPoints() throws Exception {
@Inject
ObjectService objectService;

Session session = repo.login();
@Test
public void shouldDemonstratePolicyDecisionPoints() throws Exception {

PolicyDecisionPoint pt = new PolicyDecisionPoint();
pt.addPolicy(new MimeTypePolicy("image/tiff", "tiff-store"));
final Session session = repo.login();

final Node dsNode =
datastreamService.createDatastreamNode(session,
"/testDatastreamPolicyNode",
"application/octet-stream",
new ByteArrayInputStream("asdf".getBytes()));
final PolicyDecisionPoint pt = new PolicyDecisionPoint();
pt.addPolicy(new MimeTypePolicy("image/tiff", "tiff-store"));

final Node dsNodeTiff =
datastreamService.createDatastreamNode(session,
"/testDatastreamPolicyNode",
"image/tiff",
new ByteArrayInputStream("1234".getBytes()));
datastreamService.createDatastreamNode(session,
"/testDatastreamPolicyNode", "application/octet-stream",
new ByteArrayInputStream("asdf".getBytes()));

session.save();
datastreamService.createDatastreamNode(session,
"/testDatastreamPolicyNode", "image/tiff",
new ByteArrayInputStream("1234".getBytes()));

session.save();

}
}
}
Expand Up @@ -42,8 +42,10 @@ public void setUp() throws Exception {

@Test
public void shouldApplyToObject() throws Exception {
@SuppressWarnings("unchecked")
final Predicate<Node> mockFuncTrue = mock(Predicate.class);
when(mockFuncTrue.apply(any(Node.class))).thenReturn(true);
@SuppressWarnings("unchecked")
final Predicate<Node> mockFuncFalse = mock(Predicate.class);
final Predicate<Node> holdDS = FedoraTypesUtils.isFedoraDatastream;
final Predicate<Node> holdO = FedoraTypesUtils.isFedoraObject;
Expand All @@ -65,8 +67,10 @@ public void shouldApplyToObject() throws Exception {

@Test
public void shouldApplyToDatastream() throws Exception {
@SuppressWarnings("unchecked")
final Predicate<Node> mockFuncTrue = mock(Predicate.class);
when(mockFuncTrue.apply(any(Node.class))).thenReturn(true);
@SuppressWarnings("unchecked")
final Predicate<Node> mockFuncFalse = mock(Predicate.class);
final Predicate<Node> holdDS = FedoraTypesUtils.isFedoraDatastream;
final Predicate<Node> holdO = FedoraTypesUtils.isFedoraObject;
Expand Down Expand Up @@ -101,6 +105,7 @@ public void shouldNotApplyToNonExistentNodes() throws Exception {

@Test
public void shouldNotApplyToSystemNodes() throws Exception {
@SuppressWarnings("unchecked")
final Predicate<Node> mockFuncFalse = mock(Predicate.class);
final Predicate<Node> holdDS = FedoraTypesUtils.isFedoraDatastream;
final Predicate<Node> holdO = FedoraTypesUtils.isFedoraObject;
Expand Down
@@ -1,11 +1,14 @@

package org.fcrepo.observer;

import static com.google.common.collect.Iterables.filter;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.mockStatic;

import java.lang.reflect.Field;
import java.util.Arrays;
Expand Down Expand Up @@ -35,60 +38,63 @@
public class SimpleObserverTest {

private SimpleObserver testObj;

ObservationManager mockOM;

private static void setField(String name, SimpleObserver obj, Object val) throws Exception {
Field field = SimpleObserver.class.getDeclaredField(name);

private static void setField(final String name, final SimpleObserver obj,
final Object val) throws Exception {
final Field field = SimpleObserver.class.getDeclaredField(name);
field.setAccessible(true);
field.set(obj, val);
}

@Before
public void setUp() throws Exception {
testObj = new SimpleObserver();
testObj = new SimpleObserver();
}

@Test
public void testBuildListener() throws Exception {
Repository mockRepository = mock(Repository.class);
Session mockSession = mock(Session.class);
Workspace mockWS = mock(Workspace.class);
final Repository mockRepository = mock(Repository.class);
final Session mockSession = mock(Session.class);
final Workspace mockWS = mock(Workspace.class);
mockOM = mock(ObservationManager.class);
when(mockWS.getObservationManager()).thenReturn(mockOM);
when(mockSession.getWorkspace()).thenReturn(mockWS);
when(mockRepository.login()).thenReturn(mockSession);
setField("repository", testObj, mockRepository);
testObj.buildListener();
verify(mockOM).addEventListener(testObj, SimpleObserver.EVENT_TYPES , "/", true, null, null, false);
verify(mockOM).addEventListener(testObj, SimpleObserver.EVENT_TYPES,
"/", true, null, null, false);
}


@SuppressWarnings("unchecked")
@Test
public void testOnEvent() throws Exception {
EventBus mockBus = mock(EventBus.class);
EventFilter mockFilter = mock(EventFilter.class);
final EventBus mockBus = mock(EventBus.class);
final EventFilter mockFilter = mock(EventFilter.class);
setField("eventBus", testObj, mockBus);
setField("eventFilter", testObj, mockFilter);
Event mockEvent = mock(Event.class);
EventIterator mockEvents = mock(EventIterator.class);
List<Event> iterable = Arrays.asList(new Event[]{mockEvent});
PowerMockito.mockStatic(Iterables.class);
when(Iterables.filter(any(Iterable.class), eq(mockFilter))).thenReturn(iterable);
final Event mockEvent = mock(Event.class);
final EventIterator mockEvents = mock(EventIterator.class);
final List<Event> iterable = Arrays.asList(new Event[] {mockEvent});
mockStatic(Iterables.class);
when(filter(any(Iterable.class), eq(mockFilter))).thenReturn(iterable);
testObj.onEvent(mockEvents);
verify(mockBus).post(any(Event.class));
}


@SuppressWarnings("unchecked")
@Test
public void testOnEventAllFiltered() throws Exception {
EventBus mockBus = mock(EventBus.class);
EventFilter mockFilter = mock(EventFilter.class);
final EventBus mockBus = mock(EventBus.class);
final EventFilter mockFilter = mock(EventFilter.class);
setField("eventBus", testObj, mockBus);
setField("eventFilter", testObj, mockFilter);
Event mockEvent = mock(Event.class);
EventIterator mockEvents = mock(EventIterator.class);
List<Event> iterable = Arrays.asList(new Event[0]);
final EventIterator mockEvents = mock(EventIterator.class);
final List<Event> iterable = Arrays.asList(new Event[0]);
PowerMockito.mockStatic(Iterables.class);
when(Iterables.filter(any(Iterable.class), eq(mockFilter))).thenReturn(iterable);
when(filter(any(Iterable.class), eq(mockFilter))).thenReturn(iterable);
testObj.onEvent(mockEvents);
verify(mockBus, never()).post(any(Event.class));
}
Expand Down
Expand Up @@ -219,8 +219,8 @@ public void shouldRetrieveLowLevelCacheStoresForBinaryKey()
public void shouldRetrieveLowLevelCacheStoresForCompositeStore()
throws RepositoryException, CacheLoaderException {

final Cache ispnCache1 = mock(Cache.class);
final Cache ispnCache2 = mock(Cache.class);
final Cache<?, ?> ispnCache1 = mock(Cache.class);
final Cache<?, ?> ispnCache2 = mock(Cache.class);
final CacheStore ispnCacheStore1 = mock(CacheStore.class);
final CacheStore ispnCacheStore2 = mock(CacheStore.class);
final BinaryStore plainBinaryStore = mock(BinaryStore.class);
Expand Down
Expand Up @@ -25,17 +25,14 @@
public class ObjectServiceTest implements FedoraJcrTypes {

private Session mockSession;

private Node mockRoot;

private ObjectService testObj;

@Before
public void setUp() throws RepositoryException {
testObj = new ObjectService();
mockSession = mock(Session.class);
mockRoot = mock(Node.class);
when(mockSession.getRootNode()).thenReturn(mockRoot);
mockSession = mock(Session.class);
mockRoot = mock(Node.class);
when(mockSession.getRootNode()).thenReturn(mockRoot);
}

@After
Expand All @@ -48,15 +45,16 @@ public void testCreateObject() throws Exception {
final String testPath = "/foo";
when(mockRoot.getNode(testPath.substring(1))).thenReturn(mockNode);
final ObjectService testObj = new ObjectService();
final Node actual = testObj.createObject(mockSession, testPath).getNode();
final Node actual =
testObj.createObject(mockSession, testPath).getNode();
assertEquals(mockNode, actual);
}

@Test
public void testGetObject() throws RepositoryException {
final Session mockSession = mock(Session.class);
final Node mockNode = mock(Node.class);
final String testPath = "/foo";
final String testPath = "/foo";
when(mockSession.getNode(testPath)).thenReturn(mockNode);
final ObjectService testObj = new ObjectService();
testObj.getObject(mockSession, "/foo");
Expand All @@ -66,7 +64,7 @@ public void testGetObject() throws RepositoryException {
@Test
public void testGetObjectNode() throws RepositoryException {
final Session mockSession = mock(Session.class);
final String testPath = "/foo";
final String testPath = "/foo";
final ObjectService testObj = new ObjectService();
testObj.getObjectNode(mockSession, "/foo");
verify(mockSession).getNode(testPath);
Expand Down
@@ -1,6 +1,7 @@

package org.fcrepo.services;

import static org.fcrepo.services.RepositoryService.getRepositoryNamespaces;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.anyString;
Expand Down Expand Up @@ -161,26 +162,25 @@ public void testGetAllNodeTypes() throws RepositoryException {

@Test
public void testGetRepositoryNamespaces() throws RepositoryException {
final Map<String, String> actual =
testObj.getRepositoryNamespaces(mockSession);
final Map<String, String> actual = getRepositoryNamespaces(mockSession);
assertEquals(expectedNS, actual);
}

@Test
public void testExists() throws RepositoryException {
String existsPath = "/foo/bar/exists";
final String existsPath = "/foo/bar/exists";
when(mockSession.nodeExists(existsPath)).thenReturn(true);
assertEquals(true, testObj.exists(mockSession, existsPath));
assertEquals(false, testObj.exists(mockSession, "/foo/bar"));
}

@Test
public void testIsFile() throws RepositoryException {
String filePath = "/foo/bar/file";
String folderPath = "/foo/bar/folder";
Node mockFile = mock(Node.class);
final String filePath = "/foo/bar/file";
final String folderPath = "/foo/bar/folder";
final Node mockFile = mock(Node.class);
when(mockFile.isNodeType("nt:file")).thenReturn(true);
Node mockFolder = mock(Node.class);
final Node mockFolder = mock(Node.class);
when(mockFolder.isNodeType("nt:file")).thenReturn(false);
when(mockSession.getNode(filePath)).thenReturn(mockFile);
when(mockSession.getNode(folderPath)).thenReturn(mockFolder);
Expand Down

0 comments on commit 14d4b99

Please sign in to comment.