[webbeans-commits] Webbeans SVN: r345 - in ri/trunk/webbeans-ri/src: main/java/org/jboss/webbeans/event and 1 other directories.
webbeans-commits at lists.jboss.org
webbeans-commits at lists.jboss.org
Thu Nov 20 13:18:43 EST 2008
Author: dallen6
Date: 2008-11-20 13:18:43 -0500 (Thu, 20 Nov 2008)
New Revision: 345
Added:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventManager.java
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventManagerTest.java
Removed:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventBus.java
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventBusTest.java
Modified:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventImpl.java
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventTest.java
Log:
Renamed the EventBus as EventManager and fixed the EventImpl to work properly with event types.
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java 2008-11-20 12:02:52 UTC (rev 344)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java 2008-11-20 18:18:43 UTC (rev 345)
@@ -52,7 +52,7 @@
import org.jboss.webbeans.contexts.RequestContext;
import org.jboss.webbeans.contexts.SessionContext;
import org.jboss.webbeans.ejb.DefaultEnterpriseBeanLookup;
-import org.jboss.webbeans.event.EventBus;
+import org.jboss.webbeans.event.EventManager;
import org.jboss.webbeans.exceptions.NameResolutionLocation;
import org.jboss.webbeans.introspector.AnnotatedItem;
import org.jboss.webbeans.introspector.AnnotatedMethod;
@@ -72,7 +72,7 @@
{
private List<Class<? extends Annotation>> enabledDeploymentTypes;
private MetaDataCache metaDataCache;
- private EventBus eventBus;
+ private EventManager eventManager;
private Resolver resolver;
private ContextMap contextMap;
private ProxyPool proxyPool;
@@ -85,7 +85,7 @@
{
this.metaDataCache = new MetaDataCache();
this.beans = new CopyOnWriteArrayList<Bean<?>>();
- this.eventBus = new EventBus(this);
+ this.eventManager = new EventManager();
this.resolver = new Resolver(this);
this.proxyPool = new ProxyPool(this);
this.decorators = new HashSet<Decorator>();
@@ -200,7 +200,7 @@
*/
public <T> Set<Observer<T>> resolveObservers(T event, Annotation... bindings)
{
- return eventBus.getObservers(event, bindings);
+ return eventManager.getObservers(event, bindings);
}
/**
@@ -366,7 +366,7 @@
*/
public <T> Manager addObserver(Observer<T> observer, Class<T> eventType, Annotation... bindings)
{
- this.eventBus.addObserver(observer, eventType, bindings);
+ this.eventManager.addObserver(observer, eventType, bindings);
return this;
}
@@ -386,7 +386,7 @@
{
// TODO Using the eventType TypeLiteral<T>, the Class<T> object must be
// retrieved
- this.eventBus.addObserver(observer, (Class<T>) Reflections.getActualTypeArguments(eventType.getClass())[0], bindings);
+ this.eventManager.addObserver(observer, (Class<T>) Reflections.getActualTypeArguments(eventType.getClass())[0], bindings);
return this;
}
@@ -411,7 +411,7 @@
// parameterized, this
// method is not, so we have to use Observer<Object> for observers.
Set<Observer<Object>> observers = this.resolveObservers(event, bindings);
- this.eventBus.notifyObservers(observers, event);
+ this.eventManager.notifyObservers(observers, event);
}
/**
@@ -580,7 +580,7 @@
*/
public <T> Manager removeObserver(Observer<T> observer, Class<T> eventType, Annotation... bindings)
{
- this.eventBus.removeObserver(observer, eventType, bindings);
+ this.eventManager.removeObserver(observer, eventType, bindings);
return this;
}
@@ -600,7 +600,7 @@
{
// TODO The Class<T> for the event type must be retrieved from the
// TypeLiteral<T> instance
- this.eventBus.removeObserver(observer, (Class<T>) Reflections.getActualTypeArguments(eventType.getClass())[0], bindings);
+ this.eventManager.removeObserver(observer, (Class<T>) Reflections.getActualTypeArguments(eventType.getClass())[0], bindings);
return this;
}
@@ -669,8 +669,8 @@
buffer.append(" " + deploymentType.getName() + "\n");
}
- buffer.append("Event bus:\n");
- buffer.append(eventBus.toString());
+ buffer.append("Event manager:\n");
+ buffer.append(eventManager.toString());
buffer.append("Metadata cache:\n");
buffer.append(metaDataCache.toString());
Deleted: 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-11-20 12:02:52 UTC (rev 344)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventBus.java 2008-11-20 18:18:43 UTC (rev 345)
@@ -1,193 +0,0 @@
-package org.jboss.webbeans.event;
-
-import java.lang.annotation.Annotation;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.Map.Entry;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.CopyOnWriteArrayList;
-
-import javax.transaction.RollbackException;
-import javax.transaction.SystemException;
-import javax.transaction.Transaction;
-import javax.transaction.TransactionManager;
-import javax.webbeans.Observer;
-
-import org.jboss.webbeans.ManagerImpl;
-import org.jboss.webbeans.util.JNDI;
-
-/**
- * The event bus is where observers are registered and events are fired.
- *
- * @author David Allen
- *
- */
-public class EventBus
-{
- private ManagerImpl manager;
- private final Map<Class<?>, CopyOnWriteArrayList<EventObserver<?>>> registeredObservers;
- 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.
- */
- public EventBus(ManagerImpl manager)
- {
- this.manager = manager;
- transactionManager = (TransactionManager) JNDI.lookup("java:/TransactionManager");
- registeredObservers = new ConcurrentHashMap<Class<?>, CopyOnWriteArrayList<EventObserver<?>>>();
- }
-
- /**
- * Adds an observer to the event bus so that it receives event notifications.
- *
- * @param observer The observer that should receive events
- */
- public <T> void addObserver(Observer<T> observer, Class<T> eventType, Annotation... bindings)
- {
- CopyOnWriteArrayList<EventObserver<?>> eventTypeObservers = registeredObservers.get(eventType);
- if (eventTypeObservers == null)
- {
- eventTypeObservers = new CopyOnWriteArrayList<EventObserver<?>>();
- registeredObservers.put(eventType, eventTypeObservers);
- }
- EventObserver<T> eventObserver = new EventObserver<T>(observer, eventType, bindings);
- if (!eventTypeObservers.contains(eventObserver))
- {
- eventTypeObservers.add(eventObserver);
- }
- }
-
- /**
- * Defers delivery of an event till the end of the currently active
- * transaction.
- *
- * @param event The event object to deliver
- * @param observer The observer to receive the event
- * @throws SystemException
- * @throws IllegalStateException
- * @throws RollbackException
- */
- public <T> void deferEvent(T event, Observer<T> observer) throws SystemException, IllegalStateException, RollbackException
- {
- if (transactionManager != null)
- {
- // Get the current transaction associated with the thread
- Transaction transaction = transactionManager.getTransaction();
- if (transaction != null)
- {
- transaction.registerSynchronization(new DeferredEventNotification<T>(event, observer));
- }
- }
- }
-
- /**
- * Resolves the list of observers to be notified for a given event and
- * optional event bindings.
- *
- * @param event The event object
- * @param bindings Optional event bindings
- * @return A set of Observers
- */
- @SuppressWarnings("unchecked")
- public <T> Set<Observer<T>> getObservers(T event, Annotation... bindings)
- {
- Set<Observer<T>> interestedObservers = new HashSet<Observer<T>>();
- for (EventObserver<?> observer : registeredObservers.get(event.getClass()))
- {
- if (observer.isObserverInterested(bindings))
- {
- interestedObservers.add((Observer<T>) observer.getObserver());
- }
- }
- return interestedObservers;
- }
-
- /**
- * Notifies each observer immediately of the event unless a transaction is
- * currently in progress, in which case a deferred event is created and
- * registered.
- *
- * @param <T>
- * @param observers
- * @param event
- */
- public <T> void notifyObservers(Set<Observer<T>> observers, T event)
- {
- for (Observer<T> observer : observers)
- {
- // TODO Replace this with the correct transaction code
- Transaction transaction = null;
- try
- {
- transaction = transactionManager.getTransaction();
- }
- catch (SystemException e)
- {
- }
- if (transaction != null)
- {
- try
- {
- deferEvent(event, observer);
- }
- catch (IllegalStateException e)
- {
- }
- catch (SystemException e)
- {
- }
- catch (RollbackException e)
- {
- // TODO If transaction is being rolled back, perhaps notification
- // should terminate now
- }
- }
- else
- {
- // Notify observer immediately in the same context as this method
- observer.notify(event);
- }
- }
- }
-
- /**
- * Removes an observer from the event bus.
- *
- * @param observer The observer to remove
- */
- public <T> void removeObserver(Observer<T> observer, Class<T> eventType, Annotation... bindings)
- {
- List<EventObserver<?>> observers = registeredObservers.get(eventType);
- for (Iterator<EventObserver<?>> i = observers.iterator(); i.hasNext();)
- {
- if (observer.equals(i.next().getObserver()))
- {
- i.remove();
- break;
- }
- }
- }
-
-
- @Override
- public String toString()
- {
- StringBuffer buffer = new StringBuffer();
- buffer.append("Registered observers: " + registeredObservers.size() + "\n");
- for (Entry<Class<?>, CopyOnWriteArrayList<EventObserver<?>>> entry : registeredObservers.entrySet())
- {
- buffer.append(entry.getKey().getName() + ":\n");
- for (EventObserver<?> observer : entry.getValue())
- {
- buffer.append(" " + observer.toString());
- }
- }
- return buffer.toString();
- }
-}
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventImpl.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventImpl.java 2008-11-20 12:02:52 UTC (rev 344)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventImpl.java 2008-11-20 18:18:43 UTC (rev 345)
@@ -1,7 +1,6 @@
package org.jboss.webbeans.event;
import java.lang.annotation.Annotation;
-import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
@@ -16,8 +15,6 @@
import javax.webbeans.Standard;
import javax.webbeans.manager.Manager;
-import org.jboss.webbeans.util.Reflections;
-
/**
* Implementation of the {@link Event} interface used for the container provided
* Web Bean to be injected for an observable event. See section 7.4 of the JSR
@@ -31,6 +28,7 @@
public class EventImpl<T> implements Event<T>
{
private Collection<? extends Annotation> eventBindings;
+ private Class<T> eventType;
// The current WB manager
@Current
@@ -72,7 +70,6 @@
.fireEvent(event, eventBindings.toArray(new Annotation[0]));
}
- @SuppressWarnings("unchecked")
public void observe(Observer<T> observer, Annotation... bindings)
{
// Register the observer with the web beans manager
@@ -80,8 +77,7 @@
Set<Annotation> eventBindings = new HashSet<Annotation>();
eventBindings.addAll(this.getBindingTypes());
addAnnotationBindings(eventBindings, bindings);
- Type[] observerTypeArguments = Reflections.getActualTypeArguments(observer.getClass());
- webBeansManager.addObserver(observer, (Class<T>) observerTypeArguments[0], bindings);
+ webBeansManager.addObserver(observer, eventType, bindings);
}
/**
@@ -156,5 +152,11 @@
{
this.webBeansManager = manager;
}
+
+ // TODO Use constructor injection
+ public void setEventType(Class<T> eventType)
+ {
+ this.eventType = eventType;
+ }
}
Copied: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventManager.java (from rev 336, ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventBus.java)
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventManager.java (rev 0)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventManager.java 2008-11-20 18:18:43 UTC (rev 345)
@@ -0,0 +1,203 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.jboss.webbeans.event;
+
+import java.lang.annotation.Annotation;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.Map.Entry;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import javax.transaction.RollbackException;
+import javax.transaction.SystemException;
+import javax.transaction.Transaction;
+import javax.transaction.TransactionManager;
+import javax.webbeans.Current;
+import javax.webbeans.Observer;
+
+/**
+ * The event bus is where observers are registered and events are fired.
+ *
+ * @author David Allen
+ *
+ */
+public class EventManager
+{
+ private final Map<Class<?>, CopyOnWriteArrayList<EventObserver<?>>> registeredObservers;
+
+ @Current
+ private TransactionManager transactionManager;
+
+ /**
+ * Initializes a new instance of the EventManager. This includes looking up the
+ * transaction manager which is needed to defer events till the end of a
+ * transaction.
+ */
+ public EventManager()
+ {
+ registeredObservers = new ConcurrentHashMap<Class<?>, CopyOnWriteArrayList<EventObserver<?>>>();
+ }
+
+ /**
+ * Adds an observer to the event bus so that it receives event notifications.
+ *
+ * @param observer
+ * The observer that should receive events
+ */
+ public <T> void addObserver(Observer<T> observer, Class<T> eventType, Annotation... bindings)
+ {
+ CopyOnWriteArrayList<EventObserver<?>> eventTypeObservers = registeredObservers.get(eventType);
+ if (eventTypeObservers == null)
+ {
+ eventTypeObservers = new CopyOnWriteArrayList<EventObserver<?>>();
+ registeredObservers.put(eventType, eventTypeObservers);
+ }
+ EventObserver<T> eventObserver = new EventObserver<T>(observer, eventType, bindings);
+ if (!eventTypeObservers.contains(eventObserver))
+ {
+ eventTypeObservers.add(eventObserver);
+ }
+ }
+
+ /**
+ * Defers delivery of an event till the end of the currently active
+ * transaction.
+ *
+ * @param event The event object to deliver
+ * @param observer The observer to receive the event
+ * @throws SystemException
+ * @throws IllegalStateException
+ * @throws RollbackException
+ */
+ public <T> void deferEvent(T event, Observer<T> observer) throws SystemException, IllegalStateException, RollbackException
+ {
+ if (transactionManager != null)
+ {
+ // Get the current transaction associated with the thread
+ Transaction transaction = transactionManager.getTransaction();
+ if (transaction != null)
+ {
+ transaction.registerSynchronization(new DeferredEventNotification<T>(event, observer));
+ }
+ }
+ }
+
+ /**
+ * Resolves the list of observers to be notified for a given event and
+ * optional event bindings.
+ *
+ * @param event The event object
+ * @param bindings Optional event bindings
+ * @return A set of Observers
+ */
+ @SuppressWarnings("unchecked")
+ public <T> Set<Observer<T>> getObservers(T event, Annotation... bindings)
+ {
+ Set<Observer<T>> interestedObservers = new HashSet<Observer<T>>();
+ for (EventObserver<?> observer : registeredObservers.get(event.getClass()))
+ {
+ if (observer.isObserverInterested(bindings))
+ {
+ interestedObservers.add((Observer<T>) observer.getObserver());
+ }
+ }
+ return interestedObservers;
+ }
+
+ /**
+ * Notifies each observer immediately of the event unless a transaction is
+ * currently in progress, in which case a deferred event is created and
+ * registered.
+ *
+ * @param <T>
+ * @param observers
+ * @param event
+ */
+ public <T> void notifyObservers(Set<Observer<T>> observers, T event)
+ {
+ for (Observer<T> observer : observers)
+ {
+ // TODO Replace this with the correct transaction code
+ Transaction transaction = null;
+ try
+ {
+ transaction = transactionManager.getTransaction();
+ } catch (SystemException e)
+ {
+ }
+ if (transaction != null)
+ {
+ try
+ {
+ deferEvent(event, observer);
+ } catch (IllegalStateException e)
+ {
+ } catch (SystemException e)
+ {
+ } catch (RollbackException e)
+ {
+ // TODO If transaction is being rolled back, perhaps notification should terminate now
+ }
+ } else
+ {
+ // Notify observer immediately in the same context as this method
+ observer.notify(event);
+ }
+ }
+ }
+
+ /**
+ * Removes an observer from the event bus.
+ *
+ * @param observer
+ * The observer to remove
+ */
+ public <T> void removeObserver(Observer<T> observer, Class<T> eventType, Annotation... bindings)
+ {
+ List<EventObserver<?>> observers = registeredObservers.get(eventType);
+ for (Iterator<EventObserver<?>> i = observers.iterator(); i.hasNext();)
+ {
+ if (observer.equals(i.next().getObserver()))
+ {
+ i.remove();
+ break;
+ }
+ }
+ }
+
+
+ @Override
+ public String toString()
+ {
+ StringBuffer buffer = new StringBuffer();
+ buffer.append("Registered observers: " + registeredObservers.size() + "\n");
+ for (Entry<Class<?>, CopyOnWriteArrayList<EventObserver<?>>> entry : registeredObservers.entrySet())
+ {
+ buffer.append(entry.getKey().getName() + ":\n");
+ for (EventObserver<?> observer : entry.getValue())
+ {
+ buffer.append(" " + observer.toString());
+ }
+ }
+ return buffer.toString();
+ }
+}
Property changes on: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventManager.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Deleted: 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-11-20 12:02:52 UTC (rev 344)
+++ ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventBusTest.java 2008-11-20 18:18:43 UTC (rev 345)
@@ -1,190 +0,0 @@
-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
- *
- */
- at SpecVersion("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 = "observerMethod")
- public void testAddObserver()
- {
- EventBus eventBus = new EventBus(manager);
- 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 = {"observerMethod", "broken"})
- public void testRemoveObserver()
- {
- EventBus eventBus = new EventBus(manager);
- Observer<DangerCall> observer = new AnObserver<DangerCall>();
- eventBus.addObserver(observer, DangerCall.class);
- eventBus.removeObserver(observer, DangerCall.class);
- // FIXME CopyOnWrite broke remove, have to check later
- assert eventBus.getObservers(new DangerCall()).isEmpty();
- }
-
- /**
- * Tests the deferred event feature associated with transactions.
- */
- @Test(groups = {"deferredEvent", "broken"})
- 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(manager);
- 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);
- }
-}
Copied: ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventManagerTest.java (from rev 336, ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventBusTest.java)
===================================================================
--- ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventManagerTest.java (rev 0)
+++ ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventManagerTest.java 2008-11-20 18:18:43 UTC (rev 345)
@@ -0,0 +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.EventManager;
+import org.jboss.webbeans.test.beans.DangerCall;
+import org.jboss.webbeans.test.bindings.TameAnnotationLiteral;
+import org.testng.annotations.Test;
+
+/**
+ * Tests for the EventManager implementation used by the Web Beans RI.
+ *
+ * @author David Allen
+ *
+ */
+ at SpecVersion("PDR")
+public class EventManagerTest 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 = "observerMethod")
+ public void testAddObserver()
+ {
+ EventManager eventManager = new EventManager();
+ Observer<DangerCall> observer = new AnObserver<DangerCall>();
+ eventManager.addObserver(observer, DangerCall.class);
+ DangerCall event = new DangerCall();
+
+ Set<Observer<DangerCall>> observerSet = eventManager.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>();
+ eventManager.addObserver(observer, DangerCall.class, new TameAnnotationLiteral());
+ observerSet = eventManager.getObservers(event);
+ assert observerSet.size() == 1;
+ observerSet = eventManager.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 = {"observerMethod", "broken"})
+ public void testRemoveObserver()
+ {
+ EventManager eventManager = new EventManager();
+ Observer<DangerCall> observer = new AnObserver<DangerCall>();
+ eventManager.addObserver(observer, DangerCall.class);
+ eventManager.removeObserver(observer, DangerCall.class);
+ // FIXME CopyOnWrite broke remove, have to check later
+ assert eventManager.getObservers(new DangerCall()).isEmpty();
+ }
+
+ /**
+ * Tests the deferred event feature associated with transactions.
+ */
+ @Test(groups = {"deferredEvent", "broken"})
+ 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;
+// }
+//
+// };
+ EventManager eventManager = new EventManager();
+ Observer<DangerCall> observer = new AnObserver<DangerCall>();
+ try
+ {
+ eventManager.deferEvent(new DangerCall(), observer);
+ } catch (Exception e)
+ {
+ }
+
+ assert this.registeredSynch != null;
+ assert ((DeferredEventNotification)this.registeredSynch).getObserver().equals(observer);
+ }
+}
Property changes on: ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventManagerTest.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Modified: ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventTest.java
===================================================================
--- ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventTest.java 2008-11-20 12:02:52 UTC (rev 344)
+++ ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/EventTest.java 2008-11-20 18:18:43 UTC (rev 345)
@@ -48,6 +48,7 @@
}
}
+ @SuppressWarnings("unchecked")
@BeforeMethod
public void before() throws Exception
{
@@ -107,7 +108,7 @@
* Tests the {@link Event#observe(javax.webbeans.Observer, Annotation...)}
* method with a locally instantiated implementation.
*/
- @Test(groups = {"observerMethod", "broken"})
+ @Test(groups = {"observerMethod"})
@SpecAssertion(section = "7.6")
public void testObserve()
{
@@ -115,6 +116,7 @@
// event object
Annotation[] annotations = new Annotation[] { new TameAnnotationLiteral() };
EventImpl<DangerCall> eventComponent = new EventImpl<DangerCall>();
+ eventComponent.setEventType(DangerCall.class);
eventComponent.setEventBindings(annotations);
eventComponent.setManager(manager);
Observer<DangerCall> observer = new AnObserver<DangerCall>();
More information about the weld-commits
mailing list