Skip to content
This repository has been archived by the owner on Jan 3, 2019. It is now read-only.

Commit

Permalink
NotificationInvocationHandlerTest should behave better now. Re-added …
Browse files Browse the repository at this point in the history
…to configX tests.
  • Loading branch information
Edwin Shin committed Dec 18, 2009
1 parent 694b6a1 commit 9015729
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 70 deletions.
22 changes: 4 additions & 18 deletions fcrepo-integrationtest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,7 @@
<includes>

<include>**/AtomAPIMMessageTest.class</include>

<!--
Hangs intermittently
<include>**/NotificationInvocationHandlerTest.class</include>
-->
<include>**/NotificationInvocationHandlerTest.class</include>

<!--
Has bug in source-code
Expand Down Expand Up @@ -257,17 +253,13 @@

<includes>

<!--
Hangs intermititently
<include>**/NotificationInvocationHandlerTest.class</include>
-->

<!--
Has bug in source-code
<include>**/SimpleDeploymentTests.class</include>
-->

<include>**/AtomAPIMMessageTest.class</include>
<include>**/NotificationInvocationHandlerTest.class</include>
<include>**/DOTranslationUtilityTest.class</include>
<include>**/TestIngest.class</include>
<include>**/TestAPIA.class</include>
Expand Down Expand Up @@ -377,10 +369,7 @@

<includes>

<!--
Hangs intermittently
<include>**/NotificationInvocationHandlerTest.class</include>
-->
<include>**/NotificationInvocationHandlerTest.class</include>

<!--
Has bug in source-code
Expand Down Expand Up @@ -500,10 +489,7 @@

<includes>

<!--
Hangs intermittently
<include>**/NotificationInvocationHandlerTest.class</include>
-->
<include>**/NotificationInvocationHandlerTest.class</include>

<!--
Has bug in source-code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,33 @@
* detailed in the license directory at the root of the source tree (also
* available online at http://fedora-commons.org/license/).
*/

package fedora.server.messaging;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.CountDownLatch;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import javax.naming.Context;

import javax.jms.TextMessage;
import junit.framework.JUnit4TestAdapter;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import junit.framework.JUnit4TestAdapter;

import fedora.server.MockContext;
import fedora.server.errors.MessagingException;
import fedora.server.management.Management;
import fedora.server.management.MockManagementDelegate;
import fedora.server.messaging.Messaging.MessageType;
import fedora.server.proxy.ProxyFactory;

import fedora.test.FedoraTestCase;

/**
Expand All @@ -39,13 +42,19 @@ public class NotificationInvocationHandlerTest

private JMSManager jmsMgr;

public static junit.framework.Test suite() {
return new JUnit4TestAdapter(NotificationInvocationHandlerTest.class);
}

@Override
@Before
public void setUp() throws Exception {
Properties props = new Properties();
props.setProperty(Context.INITIAL_CONTEXT_FACTORY,
props
.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.activemq.jndi.ActiveMQInitialContextFactory");
props.setProperty(Context.PROVIDER_URL,
props
.setProperty(Context.PROVIDER_URL,
"vm://localhost?broker.useShutdownHook=false&broker.persistent=false");
props.setProperty(JMSManager.CONNECTION_FACTORY_NAME,
"ConnectionFactory");
Expand All @@ -63,42 +72,87 @@ public void tearDown() throws Exception {
}

@Test
public void testAPIMMessages() throws Exception {

new Thread() {

@Override
public void run() {
try {
Map<String, List<String>> mdMap =
new HashMap<String, List<String>>();
List<String> destinations = new ArrayList<String>();
destinations.add("fedora.apim.update");
mdMap.put(MessageType.apimUpdate.toString(), destinations);
Messaging msg =
new MessagingImpl(getBaseURL(), mdMap, jmsMgr);
Object[] invocationHandlers =
{new NotificationInvocationHandler(msg)};
Management mgmt = new MockManagementDelegate();
Management proxy =
(Management) ProxyFactory
.getProxy(mgmt, invocationHandlers);
proxy.purgeObject(new MockContext(),
"demo:test",
null,
false);
} catch (Exception e) {
fail(e.getMessage());
}
public void testAPIMPubAndReceipt() throws Exception {

// Create a listener
CountDownLatch listeningSignal = new CountDownLatch(1);
CountDownLatch doneSignal = new CountDownLatch(1);
Thread listener = new ListenerThread(listeningSignal, doneSignal);
listener.start();
listeningSignal.await();

// Publish an APIM event
try {
Map<String, List<String>> mdMap =
new HashMap<String, List<String>>();
List<String> destinations = new ArrayList<String>();
destinations.add("fedora.apim.update");
mdMap.put(MessageType.apimUpdate.toString(), destinations);
Messaging msg = new MessagingImpl(getBaseURL(), mdMap, jmsMgr);
Object[] invocationHandlers =
{new NotificationInvocationHandler(msg)};
Management mgmt = new MockManagementDelegate();
Management proxy =
(Management) ProxyFactory
.getProxy(mgmt, invocationHandlers);
proxy.purgeObject(new MockContext(), "demo:test", null, false);
} catch (Exception e) {
listener.interrupt();
throw e;
}
// await message receipt & evaluation
doneSignal.await();
}

class ListenerThread
extends Thread
implements MessageListener {

private final CountDownLatch listeningSignal;

private final CountDownLatch doneSignal;

ListenerThread(CountDownLatch listeningSignal, CountDownLatch doneSignal) {
this.listeningSignal = listeningSignal;
this.doneSignal = doneSignal;
}

@Override
public void run() {
try {
jmsMgr.listen("fedora.apim.update", this);
} catch (MessagingException e) {
fail(e.getMessage());
} finally {
listeningSignal.countDown();
}
}.start();
}

TextMessage message = (TextMessage) jmsMgr.listen("fedora.apim.update");
APIMMessage apim = new AtomAPIMMessage(message.getText());
assertEquals("demo:test", apim.getPID());
}
@Override
public void interrupt() {
try {
jmsMgr.close();
} catch (MessagingException e) {
fail(e.getMessage());
} finally {
super.interrupt();
}
}

public static junit.framework.Test suite() {
return new JUnit4TestAdapter(NotificationInvocationHandlerTest.class);
/**
*
* {@inheritDoc}
*/
public void onMessage(Message msg) {
try {
TextMessage message = (TextMessage) msg;
APIMMessage apim = new AtomAPIMMessage(message.getText());
assertEquals("demo:test", apim.getPID());
} catch (Exception e) {
fail(e.getMessage());
} finally {
doneSignal.countDown();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package fedora.server.messaging;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
Expand All @@ -15,10 +14,6 @@
import java.util.Properties;
import java.util.Set;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import javax.jms.BytesMessage;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
Expand All @@ -33,6 +28,9 @@
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.log4j.Logger;

Expand Down Expand Up @@ -60,7 +58,7 @@ public class JMSManager {
public static final String CONNECTION_FACTORY_NAME = "connection.factory.name";

// Default connection factory name, used if no connection factory name is specified
private String defaultConnectionFactoryName = "ConnectionFactory";
private final String defaultConnectionFactoryName = "ConnectionFactory";

// JNDI related data
protected Context jndi = null;
Expand All @@ -77,7 +75,7 @@ public class JMSManager {
protected Map<String, MessageConsumer> durableSubscriptions =
new HashMap<String, MessageConsumer>();

private Properties jndiProps;
private final Properties jndiProps;

// Destination type determines the method by which messages are transferred
public static enum DestinationType {
Expand Down Expand Up @@ -564,7 +562,7 @@ public void stop(String destName) throws MessagingException {
// Look for an existing destination for the given destination
//
JMSDestination jmsDest =
(JMSDestination) jmsDestinations.get(destName);
jmsDestinations.get(destName);
if (jmsDest != null) {
// Close out all JMS related state
//
Expand Down Expand Up @@ -825,13 +823,13 @@ protected JMSDestination getJMSDestination(String name)
throws MessagingException {
// Look for an existing Destination for the given name
//
JMSDestination jmsDest = (JMSDestination) jmsDestinations.get(name);
JMSDestination jmsDest = jmsDestinations.get(name);

// If not found, create it now
//
if (jmsDest == null) {
this.createDestination(name, defaultDestinationType);
jmsDest = (JMSDestination) jmsDestinations.get(name);
jmsDest = jmsDestinations.get(name);
}

return jmsDest;
Expand Down
17 changes: 15 additions & 2 deletions fcrepo-server/src/test/java/fedora/server/messaging/Listener.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,31 @@

import java.util.Properties;

import javax.naming.Context;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import javax.naming.Context;

/**
* Test class for listening to a JMS topic.
*
* @author Edwin Shin
* @version $Id$
*/
public class Listener
implements MessageListener {

private final JMSManager jmsMgr;

/**
* Create a Listener object for the specified JMS topic.
*
* @param topicName the JMS topic to listen to, e.g. "fedora.apim.update"
* @param jndiProps Properties file that contains, at a minimum, values for
* Context.INITIAL_CONTEXT_FACTORY and Context.PROVIDER_URL
* @throws Exception
*/
public Listener(String topicName, Properties jndiProps)
throws Exception {
jmsMgr = new JMSManager(jndiProps);
Expand Down

0 comments on commit 9015729

Please sign in to comment.