Skip to content

Commit

Permalink
test cleanup; unit testing jms topic publisher
Browse files Browse the repository at this point in the history
  • Loading branch information
barmintor committed Apr 25, 2013
1 parent eba4259 commit 8857b58
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 20 deletions.
Expand Up @@ -65,26 +65,23 @@ public class AtomJMSIT implements MessageListener {

static Parser parser = new Abdera().getParser();

private Entry entry;
private volatile Entry entry;

final private Logger logger = LoggerFactory.getLogger(AtomJMSIT.class);

@Test
public void testAtomStream() throws LoginException, RepositoryException {
public void testAtomStream() throws LoginException, RepositoryException, InterruptedException {
Session session = repository.login();
session.getRootNode().addNode("test1").addMixin(FEDORA_OBJECT);
session.save();
session.logout();

// we're relying on the onMessage handler to populate entry
while (entry == null) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
if (entry == null) { // must not have rec'vd event yet
synchronized(this) {
this.wait(1000);
}
}
session.logout();

if (entry == null) fail("Waited a second, got no messages");
List<Category> categories = copyOf(entry.getCategories("xsd:string"));
final String title = entry.getTitle();
entry = null;
Expand All @@ -102,24 +99,22 @@ public void testAtomStream() throws LoginException, RepositoryException {
@Test
public void testDatastreamTerm() throws NoSuchNodeTypeException,
VersionException, ConstraintViolationException, LockException,
ItemExistsException, PathNotFoundException, RepositoryException {
ItemExistsException, PathNotFoundException, RepositoryException, InterruptedException {
Session session = repository.login();
final Node object = session.getRootNode().addNode("test2");
object.addMixin(FEDORA_OBJECT);
final Node ds = object.addNode("DATASTREAM");
ds.addMixin(FEDORA_DATASTREAM);
ds.addNode(JCR_CONTENT).setProperty(JCR_DATA, "fake data");
session.save();
session.logout();

// we're relying on the onMessage handler to populate entry
while (entry == null) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
if (entry == null) { // must not have rec'vd event yet
synchronized(this) {
this.wait(1000);
}
}
session.logout();
if (entry == null) fail("Waited a second, got no messages");

List<Category> categories = copyOf(entry.getCategories("xsd:string"));
entry = null;
String path = null;
Expand Down Expand Up @@ -155,7 +150,9 @@ public void onMessage(Message message) {
logger.error("Exception receiving message: {}", e);
fail(e.getMessage());
}

synchronized(this) {
this.notify();
}
}

@Before
Expand Down
@@ -0,0 +1,109 @@
package org.fcrepo.observer;

import static org.mockito.Matchers.any;
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 java.lang.reflect.Field;

import javax.jcr.Repository;
import javax.jcr.observation.Event;
import javax.jms.Connection;
import javax.jms.Message;
import javax.jms.MessageProducer;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;

import com.google.common.eventbus.EventBus;

public class JMSTopicPublisherTest {

@Mock
JMSTopicPublisher testObj;

@Mock
JMSEventMessageFactory mockEvents;

@Mock
MessageProducer mockProducer;

@Mock
ActiveMQConnectionFactory mockConnections;

@Mock
Repository mockRepo;

@Mock
EventBus mockBus;

@Before
public void setUp() throws Exception {
testObj = new JMSTopicPublisher();
mockEvents = mock(JMSEventMessageFactory.class);
mockProducer = mock(MessageProducer.class);
mockConnections = mock(ActiveMQConnectionFactory.class);
mockRepo = mock(Repository.class);
mockBus = mock(EventBus.class);
Field setField = JMSTopicPublisher.class.getDeclaredField("eventFactory");
setField.setAccessible(true);
setField.set(testObj, mockEvents);
setField = JMSTopicPublisher.class.getDeclaredField("producer");
setField.setAccessible(true);
setField.set(testObj, mockProducer);
setField = JMSTopicPublisher.class.getDeclaredField("connectionFactory");
setField.setAccessible(true);
setField.set(testObj, mockConnections);
setField = JMSTopicPublisher.class.getDeclaredField("repo");
setField.setAccessible(true);
setField.set(testObj, mockRepo);
setField = JMSTopicPublisher.class.getDeclaredField("eventBus");
setField.setAccessible(true);
setField.set(testObj, mockBus);

}

@Test
public void testAcquireConnections() throws Exception {
Connection mockConn = mock(Connection.class);
javax.jms.Session mockSession = mock(javax.jms.Session.class);
when(mockConnections.createConnection()).thenReturn(mockConn);
when(mockConn.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE)).thenReturn(mockSession);
testObj.acquireConnections();
verify(mockBus).register(any());
verify(mockRepo).login();
}

@Test
public void testPublishJCREvent() throws Exception {
Message mockMsg = mock(Message.class);
Event mockEvent = mock(Event.class);
when(mockEvents.getMessage(
eq(mockEvent),
any(javax.jcr.Session.class),
any(javax.jms.Session.class))).thenReturn(mockMsg);
testObj.publishJCREvent(mockEvent);
}

@Test
public void testReleaseConnections() throws Exception {
Connection mockConn = mock(Connection.class);
javax.jms.Session mockJmsSession = mock(javax.jms.Session.class);
javax.jcr.Session mockJcrSession = mock(javax.jcr.Session.class);
Field setField = JMSTopicPublisher.class.getDeclaredField("connection");
setField.setAccessible(true);
setField.set(testObj, mockConn);
setField = JMSTopicPublisher.class.getDeclaredField("session");
setField.setAccessible(true);
setField.set(testObj, mockJcrSession);
setField = JMSTopicPublisher.class.getDeclaredField("jmsSession");
setField.setAccessible(true);
setField.set(testObj, mockJmsSession);
testObj.releaseConnections();
verify(mockJcrSession).logout();
}
}

0 comments on commit 8857b58

Please sign in to comment.