Author: dallen6
Date: 2008-10-27 15:46:07 -0400 (Mon, 27 Oct 2008)
New Revision: 184
Modified:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/DeferredEventNotification.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventBus.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventObserver.java
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/DeferredEventNotificationTest.java
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventBusTest.java
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventObserverTest.java
Log:
Completed the unit test for EventBus and cleaned up some of the other event related code.
Modified:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/DeferredEventNotification.java
===================================================================
---
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/DeferredEventNotification.java 2008-10-27
18:47:42 UTC (rev 183)
+++
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/DeferredEventNotification.java 2008-10-27
19:46:07 UTC (rev 184)
@@ -10,10 +10,10 @@
* @author David Allen
*
*/
-public class DeferredEventNotification<T> implements Synchronization
+public class DeferredEventNotification implements Synchronization
{
- private Observer<T> observer;
- private T event;
+ private Observer<?> observer;
+ private Object event;
/**
* Creates a new deferred event notifier.
@@ -22,12 +22,20 @@
* @param observer The observer to be notified
* @param event The event being fired
*/
- public DeferredEventNotification(T event, Observer<T> observer)
+ public DeferredEventNotification(Object event, Observer<?> observer)
{
this.observer = observer;
this.event = event;
}
+ /**
+ * @return the observer
+ */
+ public final Observer<?> getObserver()
+ {
+ return observer;
+ }
+
public void afterCompletion(int arg0)
{
// The event is already delivered before completion
@@ -36,7 +44,8 @@
public void beforeCompletion()
{
// Execute the observer method on the event
- observer.notify(event);
+ // TODO It is impossible to use a template variable for this observer, so notify
cannot be called
+ //observer.notify(event);
}
}
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventBus.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventBus.java 2008-10-27
18:47:42 UTC (rev 183)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventBus.java 2008-10-27
19:46:07 UTC (rev 184)
@@ -11,10 +11,9 @@
import javax.transaction.SystemException;
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
+import javax.webbeans.Current;
import javax.webbeans.Observer;
-import org.jboss.webbeans.util.JNDI;
-
/**
* The event bus is where observers are registered and events are fired.
*
@@ -24,31 +23,27 @@
public class EventBus
{
private final Map<Class<?>, ArrayList<EventObserver<?>>>
registeredObservers;
- private final TransactionManager tm;
- private String tmName = "java:/TransactionManager";
+
+ @Current
+ private TransactionManager transactionManager;
/**
* Initializes a new instance of the EventBus. This includes looking up the
* transaction manager which is needed to defer events till the end of a
- * transaction. TODO Should be able to configure JNDI name of transaction
- * manager
- *
+ * transaction.
*/
public EventBus()
{
registeredObservers = new HashMap<Class<?>,
ArrayList<EventObserver<?>>>();
- tm = JNDI.lookup(tmName, TransactionManager.class);
}
/**
* Adds an observer to the event bus so that it receives event notifications.
- * The event information is already encapsulated as part of the observer.
*
* @param o
* The observer that should receive events
*/
- public <T> void addObserver(Observer<T> o, Class<T> eventType,
- Annotation... bindings)
+ public <T> void addObserver(Observer<T> observer, Class<T>
eventType, Annotation... bindings)
{
ArrayList<EventObserver<?>> l = registeredObservers.get(eventType);
if (l == null)
@@ -56,7 +51,7 @@
l = new ArrayList<EventObserver<?>>();
registeredObservers.put(eventType, l);
}
- EventObserver<T> eventObserver = new EventObserver<T>(o, eventType,
bindings);
+ EventObserver<T> eventObserver = new EventObserver<T>(observer,
eventType, bindings);
if (!l.contains(eventObserver))
l.add(eventObserver);
}
@@ -75,15 +70,15 @@
* @throws IllegalStateException
* @throws RollbackException
*/
- public void deferEvent(Object event, Observer<Object> o)
+ public void deferEvent(Object event, Observer<?> o)
throws SystemException, IllegalStateException, RollbackException
{
- if (tm != null)
+ if (transactionManager != null)
{
// Get the current transaction associated with the thread
- Transaction t = tm.getTransaction();
+ Transaction t = transactionManager.getTransaction();
if (t != null)
- t.registerSynchronization(new DeferredEventNotification<Object>(
+ t.registerSynchronization(new DeferredEventNotification(
event, o));
}
}
@@ -104,8 +99,8 @@
Set<Observer<T>> results = new HashSet<Observer<T>>();
for (EventObserver<?> observer : registeredObservers.get(event.getClass()))
{
- // TODO Verify bindings match before adding
- results.add((Observer<T>) observer.getObserver());
+ if (observer.isObserverInterested(bindings))
+ results.add((Observer<T>) observer.getObserver());
}
return results;
}
@@ -129,4 +124,13 @@
}
}
}
+
+ /**
+ * TODO Remove this once injection of the transaction manager works.
+ * @param transactionManager the TransactionManager to set
+ */
+ public final void setTransactionManager(TransactionManager transactionManager)
+ {
+ this.transactionManager = transactionManager;
+ }
}
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventObserver.java
===================================================================
---
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventObserver.java 2008-10-27
18:47:42 UTC (rev 183)
+++
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventObserver.java 2008-10-27
19:46:07 UTC (rev 184)
@@ -1,7 +1,6 @@
package org.jboss.webbeans.event;
import java.lang.annotation.Annotation;
-import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
@@ -81,7 +80,7 @@
// TODO This logic needs to be in injectable
boolean result = true;
// Check each binding specified by this observer against those provided
- if (this.eventBindings != null)
+ if (this.eventBindings.length > 0)
{
if ((bindings != null) && (bindings.length > 0))
{
@@ -91,52 +90,62 @@
int eventBindingIndex = bindingsArray.indexOf(annotation);
if (eventBindingIndex >= 0)
{
- // TODO Use annotation equality
- result = annotationsMatch(annotation,
bindingsArray.get(eventBindingIndex));
+ //result = annotationsMatch(annotation,
bindingsArray.get(eventBindingIndex));
+ result = annotation.equals(bindingsArray.get(eventBindingIndex));
} else
{
result = false;
break;
}
}
+ } else
+ {
+ result = false;
}
}
return result;
}
- /**
- * Compares two annotation bindings for equivalence. This means that all member
values, if any,
- * must match.
- * @param annotation The first annotation to compare
- * @param annotation2 The second annotation to compare
- * @return
- *
+ /* (non-Javadoc)
+ * @see java.lang.Object#hashCode()
*/
- private boolean annotationsMatch(Annotation annotation,
- Annotation annotation2)
+ @Override
+ public int hashCode()
{
- boolean result = true;
- if (annotation.getClass().getDeclaredMethods().length > 0)
- {
- for (Method annotationValue : annotation.getClass().getDeclaredMethods())
- {
- try
- {
- if
(!annotationValue.invoke(annotation).equals(annotationValue.invoke(annotation2)))
- {
- result = false;
- break;
- }
- } catch (Exception e)
- {
- result = false;
- break;
- }
- }
- }
+ final int prime = 31;
+ int result = 1;
+ result = prime * result
+ + ((eventType == null) ? 0 : eventType.hashCode());
+ result = prime * result + ((observer == null) ? 0 : observer.hashCode());
return result;
}
- // TODO Implement equals and hashCode
-
+ /* (non-Javadoc)
+ * @see java.lang.Object#equals(java.lang.Object)
+ */
+ @Override
+ public boolean equals(Object obj)
+ {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ EventObserver<?> other = (EventObserver<?>) obj;
+ if (eventType == null)
+ {
+ if (other.eventType != null)
+ return false;
+ } else if (!eventType.equals(other.eventType))
+ return false;
+ if (observer == null)
+ {
+ if (other.observer != null)
+ return false;
+ } else if (!observer.equals(other.observer))
+ return false;
+ return true;
+ }
+
}
Modified:
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/DeferredEventNotificationTest.java
===================================================================
---
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/DeferredEventNotificationTest.java 2008-10-27
18:47:42 UTC (rev 183)
+++
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/DeferredEventNotificationTest.java 2008-10-27
19:46:07 UTC (rev 184)
@@ -80,7 +80,7 @@
Observer<Event> observer = new MockObserverImpl<Event>(tuna, om,
Event.class);
((MockObserverImpl<Event>) observer).setInstance(observerInstance);
Event event = new Event();
- DeferredEventNotification<Event> deferredNotification = new
DeferredEventNotification<Event>(event, observer);
+ DeferredEventNotification deferredNotification = new
DeferredEventNotification(event, observer);
deferredNotification.beforeCompletion();
assert observerInstance.notified;
}
Modified: ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventBusTest.java
===================================================================
---
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventBusTest.java 2008-10-27
18:47:42 UTC (rev 183)
+++
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventBusTest.java 2008-10-27
19:46:07 UTC (rev 184)
@@ -1,12 +1,190 @@
package org.jboss.webbeans.test;
+import java.util.Set;
+
+import javax.transaction.HeuristicMixedException;
+import javax.transaction.HeuristicRollbackException;
+import javax.transaction.InvalidTransactionException;
+import javax.transaction.NotSupportedException;
+import javax.transaction.RollbackException;
+import javax.transaction.Synchronization;
+import javax.transaction.SystemException;
+import javax.transaction.Transaction;
+import javax.transaction.TransactionManager;
+import javax.transaction.xa.XAResource;
+import javax.webbeans.Observer;
+
+import org.jboss.webbeans.event.DeferredEventNotification;
+import org.jboss.webbeans.event.EventBus;
+import org.jboss.webbeans.test.beans.DangerCall;
+import org.jboss.webbeans.test.bindings.TameAnnotationLiteral;
+import org.testng.annotations.Test;
+
/**
* Tests for the EventBus implementation used by the Web Beans RI.
*
* @author David Allen
*
*/
+@SpecVersion("20081024-PDR")
public class EventBusTest extends AbstractTest
{
+ public class AnObserver<T> implements Observer<T>
+ {
+ public void notify(T event)
+ {
+ }
+ }
+ private Synchronization registeredSynch;
+
+ /**
+ * Tests adding an observer to the event bus and verified that it can still
+ * be retrieved for a corresponding event.
+ */
+ @Test(groups = "eventbus")
+ public void testAddObserver()
+ {
+ EventBus eventBus = new EventBus();
+ Observer<DangerCall> observer = new AnObserver<DangerCall>();
+ eventBus.addObserver(observer, DangerCall.class);
+ DangerCall event = new DangerCall();
+
+ Set<Observer<DangerCall>> observerSet = eventBus.getObservers(event);
+ assert observerSet.size() == 1;
+ assert observerSet.iterator().next().equals(observer);
+
+ // Add another observer for the same event, but with an event binding
+ observer = new AnObserver<DangerCall>();
+ eventBus.addObserver(observer, DangerCall.class, new TameAnnotationLiteral());
+ observerSet = eventBus.getObservers(event);
+ assert observerSet.size() == 1;
+ observerSet = eventBus.getObservers(event, new TameAnnotationLiteral());
+ assert observerSet.size() == 2;
+ }
+
+ /**
+ * Tests the remove operation and verifies that the observer is no longer
+ * registered for events.
+ */
+ @Test(groups = "eventbus")
+ public void testRemoveObserver()
+ {
+ EventBus eventBus = new EventBus();
+ Observer<DangerCall> observer = new AnObserver<DangerCall>();
+ eventBus.addObserver(observer, DangerCall.class);
+ eventBus.removeObserver(observer, DangerCall.class);
+ assert eventBus.getObservers(new DangerCall()).isEmpty();
+ }
+
+ /**
+ * Tests the deferred event feature associated with transactions.
+ */
+ @Test(groups = "eventbus")
+ public void testDeferEvent()
+ {
+ // Setup a transaction manager for this test and inject into the event bus
+ TransactionManager tm = new TransactionManager() {
+ public void begin() throws NotSupportedException, SystemException
+ {
+ }
+
+ public void commit() throws RollbackException,
+ HeuristicMixedException, HeuristicRollbackException,
+ SecurityException, IllegalStateException, SystemException
+ {
+ }
+
+ public int getStatus() throws SystemException
+ {
+ return 0;
+ }
+
+ public Transaction getTransaction() throws SystemException
+ {
+ return new Transaction() {
+
+ public void commit() throws RollbackException,
+ HeuristicMixedException, HeuristicRollbackException,
+ SecurityException, IllegalStateException, SystemException
+ {
+ }
+
+ public boolean delistResource(XAResource arg0, int arg1)
+ throws IllegalStateException, SystemException
+ {
+ return false;
+ }
+
+ public boolean enlistResource(XAResource arg0)
+ throws RollbackException, IllegalStateException,
+ SystemException
+ {
+ return false;
+ }
+
+ public int getStatus() throws SystemException
+ {
+ return 0;
+ }
+
+ public void registerSynchronization(Synchronization synchronization)
+ throws RollbackException, IllegalStateException,
+ SystemException
+ {
+ registeredSynch = synchronization;
+ }
+
+ public void rollback() throws IllegalStateException,
+ SystemException
+ {
+ }
+
+ public void setRollbackOnly() throws IllegalStateException,
+ SystemException
+ {
+ }
+
+ };
+ }
+
+ public void resume(Transaction arg0)
+ throws InvalidTransactionException, IllegalStateException,
+ SystemException
+ {
+ }
+
+ public void rollback() throws IllegalStateException,
+ SecurityException, SystemException
+ {
+ }
+
+ public void setRollbackOnly() throws IllegalStateException,
+ SystemException
+ {
+ }
+
+ public void setTransactionTimeout(int arg0) throws SystemException
+ {
+ }
+
+ public Transaction suspend() throws SystemException
+ {
+ return null;
+ }
+
+ };
+ EventBus eventBus = new EventBus();
+ eventBus.setTransactionManager(tm);
+ Observer<DangerCall> observer = new AnObserver<DangerCall>();
+ try
+ {
+ eventBus.deferEvent(new DangerCall(), observer);
+ } catch (Exception e)
+ {
+ }
+
+ assert this.registeredSynch != null;
+ assert
((DeferredEventNotification)this.registeredSynch).getObserver().equals(observer);
+ }
}
Modified:
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventObserverTest.java
===================================================================
---
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventObserverTest.java 2008-10-27
18:47:42 UTC (rev 183)
+++
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventObserverTest.java 2008-10-27
19:46:07 UTC (rev 184)
@@ -41,6 +41,7 @@
assert wrappedObserver.getEventBindings().length == 1;
assert wrappedObserver.isObserverInterested(new TameAnnotationLiteral());
assert !wrappedObserver.isObserverInterested(new
AnimalStereotypeAnnotationLiteral());
+ assert !wrappedObserver.isObserverInterested();
assert wrappedObserver.isObserverInterested(new TameAnnotationLiteral(), new
RoleBinding("Admin"));
// Perform some tests with binding values (7.7.1)