Author: dan.j.allen
Date: 2009-07-23 17:15:23 -0400 (Thu, 23 Jul 2009)
New Revision: 3173
Added:
tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/fires/Billing.java
tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/fires/FireEventTest.java
tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/fires/Housekeeping.java
tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/fires/Item.java
tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/fires/Lifted.java
tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/fires/MiniBar.java
tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/fires/Restored.java
Log:
add tests for section 10.3, event firing using Event#fire()
Added: tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/fires/Billing.java
===================================================================
--- tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/fires/Billing.java
(rev 0)
+++
tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/fires/Billing.java 2009-07-23
21:15:23 UTC (rev 3173)
@@ -0,0 +1,59 @@
+package org.jboss.jsr299.tck.tests.event.fires;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.enterprise.context.RequestScoped;
+import javax.enterprise.event.Observes;
+import javax.enterprise.inject.Any;
+
+@RequestScoped class Billing
+{
+ private boolean active = false;
+
+ private double charge = 0;
+
+ private double miniBarValue = 0d;
+
+ private Set<Item> itemsPurchased = new HashSet<Item>();
+
+ public void billForItem(@Observes @Lifted Item item)
+ {
+ if (itemsPurchased.add(item))
+ {
+ charge += item.getPrice();
+ }
+ }
+
+ public double getCharge()
+ {
+ return charge;
+ }
+
+ public double getMiniBarValue()
+ {
+ return miniBarValue;
+ }
+
+ public boolean isActive()
+ {
+ return active;
+ }
+
+ public void activate(@Observes @Any MiniBar minibar)
+ {
+ active = true;
+ miniBarValue = 0;
+ for (Item item : minibar.getItems())
+ {
+ miniBarValue += item.getPrice();
+ }
+ }
+
+ public void reset()
+ {
+ active = false;
+ itemsPurchased.clear();
+ charge = 0;
+ }
+}
Added:
tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/fires/FireEventTest.java
===================================================================
---
tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/fires/FireEventTest.java
(rev 0)
+++
tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/fires/FireEventTest.java 2009-07-23
21:15:23 UTC (rev 3173)
@@ -0,0 +1,193 @@
+package org.jboss.jsr299.tck.tests.event.fires;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.lang.reflect.TypeVariable;
+
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.event.Event;
+import javax.enterprise.inject.AnnotationLiteral;
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.InjectionPoint;
+
+import org.hibernate.tck.annotations.SpecAssertion;
+import org.jboss.jsr299.tck.AbstractJSR299Test;
+import org.jboss.jsr299.tck.literals.AnyLiteral;
+import org.jboss.testharness.impl.packaging.Artifact;
+import org.testng.annotations.Test;
+
+/**
+ * Tests that verify the event firing behavior of the Event
+ * interface.
+ *
+ * @author Dan Allen
+ */
+@Artifact
+public class FireEventTest extends AbstractJSR299Test
+{
+ /**
+ * This test verifies that the {@link Event} object capable of firing
+ * {@link Item} objects can be injected with the {@link @Any} binding type
+ * and that the injected object can be used to fire an event. The
+ * functionality is verified by checking that the cooresponding observer gets
+ * invoked.
+ */
+ @Test(groups = "events")
+ @SpecAssertion(section = "10.3", id = "a")
+ public void testInjectedAnyEventCanFireEvent()
+ {
+ Billing billing = getInstanceByType(Billing.class);
+ billing.reset();
+ Bean<MiniBar> miniBarBean = getUniqueBean(MiniBar.class);
+ InjectionPoint eventInjection = null;
+ for (InjectionPoint candidate : miniBarBean.getInjectionPoints())
+ {
+ if (candidate.getMember().getName().equals("miniBarEvent"))
+ {
+ eventInjection = candidate;
+ break;
+ }
+ }
+
+ assert eventInjection != null;
+ assert eventInjection.getBindings().size() == 1;
+ assert eventInjection.getBindings().contains(new AnyLiteral());
+
+ CreationalContext<MiniBar> miniBarCc =
getCurrentManager().createCreationalContext(miniBarBean);
+ MiniBar miniBar = miniBarBean.create(miniBarCc);
+ miniBar.stock();
+ assert billing.isActive();
+ assert billing.getMiniBarValue() == 16.00d;
+ }
+
+ /**
+ * This test verifies that the fire() method of the injected {@link Event}
+ * object accepts an event object and that the event object's type is the
+ * same as the the paraterized type on the event field.
+ **/
+ @SpecAssertion(section = "10.3", id = "b")
+ @Test(groups = "events")
+ public void testInjectedEventAcceptsEventObject() throws SecurityException,
NoSuchFieldException, NoSuchMethodException
+ {
+ Billing billing = getInstanceByType(Billing.class);
+ billing.reset();
+ Bean<MiniBar> miniBarBean = getUniqueBean(MiniBar.class);
+ CreationalContext<MiniBar> miniBarCc =
getCurrentManager().createCreationalContext(miniBarBean);
+ MiniBar miniBar = miniBarBean.create(miniBarCc);
+
+ Field eventField = miniBar.getClass().getDeclaredField("miniBarEvent");
+ ParameterizedType eventFieldType = (ParameterizedType)
eventField.getGenericType();
+ assert eventFieldType.getActualTypeArguments().length == 1;
+ assert MiniBar.class.equals(eventFieldType.getActualTypeArguments()[0]);
+ assert Event.class.equals(eventFieldType.getRawType());
+ Method fireMethod = null;
+ @SuppressWarnings("unchecked")
+ Class<Event<Item>> eventFieldClass = (Class<Event<Item>>)
eventFieldType.getRawType();
+ for (Method method : eventFieldClass.getMethods())
+ {
+ if (method.getName().equals("fire") && !method.isSynthetic())
+ {
+ if (fireMethod != null)
+ {
+ assert false : "Expecting exactly one method on Event named
'fire'";
+ }
+ fireMethod = method;
+ }
+ }
+
+ if (fireMethod == null)
+ {
+ assert false : "Expecting exactly one method on Event named
'fire'";
+ }
+
+ assert fireMethod.getParameterTypes().length == 1;
+ assert fireMethod.getGenericParameterTypes().length == 1;
+ // make sure the same type used to parameterize the Event class is referenced in
the fire() method
+ Type fireMethodArgumentType = fireMethod.getGenericParameterTypes()[0];
+ @SuppressWarnings("unchecked")
+ Type eventClassParameterizedType = ((TypeVariable)
fireMethod.getGenericParameterTypes()[0]).getGenericDeclaration().getTypeParameters()[0];
+ assert fireMethodArgumentType.equals(eventClassParameterizedType);
+
+ miniBar.stock();
+ assert billing.isActive();
+ assert billing.getMiniBarValue() == 16.00d;
+ }
+
+ /**
+ * This test verifies that the {@link Event} object representing an
+ * {@link Item} with the {@link @Lifted} binding type is properly injected
+ * and that this object can be used to fire an event. The functionality is
+ * verified by checking that the cooresponding observer gets invoked.
+ */
+ @Test(groups = "events")
+ @SpecAssertion(section = "10.3", id = "c")
+ public void testInjectedEventCanHaveBindings()
+ {
+ Billing billing = getInstanceByType(Billing.class);
+ billing.reset();
+ Bean<MiniBar> miniBarBean = getUniqueBean(MiniBar.class);
+
+ InjectionPoint eventInjection = null;
+ for (InjectionPoint candidate : miniBarBean.getInjectionPoints())
+ {
+ if (candidate.getMember().getName().equals("itemLiftedEvent"))
+ {
+ eventInjection = candidate;
+ break;
+ }
+ }
+
+ assert eventInjection != null;
+ assert eventInjection.getBindings().size() == 1;
+ assert eventInjection.getBindings().contains(new AnnotationLiteral<Lifted>()
{});
+
+ CreationalContext<MiniBar> miniBarCc =
getCurrentManager().createCreationalContext(miniBarBean);
+ MiniBar miniBar = miniBarBean.create(miniBarCc);
+ miniBar.stock();
+ Item chocolate = miniBar.getItemByName("Chocolate");
+ assert chocolate != null;
+ miniBar.liftItem(chocolate);
+ assert billing.getCharge() == chocolate.getPrice();
+ }
+
+ /**
+ * This test verifies that binding types can be specified dynamically
+ * when firing an event using {@link Event#fire()} by first using
+ * the {@link Event#select()} method to retrieve an Event object
+ * with associated binding types.
+ */
+ @Test(groups = "events")
+ @SpecAssertion(section = "10.3", id = "d")
+ public void testInjectedEventCanSpecifyBindingsDynamically()
+ {
+ Billing billing = getInstanceByType(Billing.class);
+ billing.reset();
+ Housekeeping housekeeping = getInstanceByType(Housekeeping.class);
+ Bean<MiniBar> miniBarBean = getUniqueBean(MiniBar.class);
+
+ InjectionPoint eventInjection = null;
+ for (InjectionPoint candidate : miniBarBean.getInjectionPoints())
+ {
+ if (candidate.getMember().getName().equals("itemEvent"))
+ {
+ eventInjection = candidate;
+ break;
+ }
+ }
+
+ assert eventInjection != null;
+ assert eventInjection.getBindings().size() == 1;
+ assert eventInjection.getBindings().contains(new AnyLiteral());
+ CreationalContext<MiniBar> miniBarCc =
getCurrentManager().createCreationalContext(miniBarBean);
+ MiniBar miniBar = miniBarBean.create(miniBarCc);
+ miniBar.stock();
+ Item water = miniBar.liftItemByName("16 oz Water");
+ miniBar.restoreItem(water);
+ assert billing.getCharge() == 1.00d;
+ assert housekeeping.getItemsTainted().size() == 1;
+ assert housekeeping.getItemsTainted().contains(water);
+ }
+
+}
Added:
tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/fires/Housekeeping.java
===================================================================
--- tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/fires/Housekeeping.java
(rev 0)
+++
tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/fires/Housekeeping.java 2009-07-23
21:15:23 UTC (rev 3173)
@@ -0,0 +1,43 @@
+package org.jboss.jsr299.tck.tests.event.fires;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.enterprise.context.RequestScoped;
+import javax.enterprise.event.Observes;
+import javax.enterprise.inject.Any;
+
+@RequestScoped class Housekeeping
+{
+ private Set<Item> itemsTainted = new HashSet<Item>();
+
+ private Set<Item> itemsMissing = new HashSet<Item>();
+
+ public void onItemRemoved(@Observes @Lifted Item item)
+ {
+ itemsMissing.add(item);
+ itemsTainted.remove(item);
+ }
+
+ public void onItemRestored(@Observes @Restored Item item)
+ {
+ itemsMissing.remove(item);
+ itemsTainted.add(item);
+ }
+
+ public Set<Item> getItemsTainted()
+ {
+ return itemsTainted;
+ }
+
+ public Set<Item> getItemsMissing()
+ {
+ return itemsMissing;
+ }
+
+ public void minibarStocked(@Observes @Any MiniBar minibar)
+ {
+ itemsMissing.clear();
+ itemsTainted.clear();
+ }
+}
Added: tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/fires/Item.java
===================================================================
--- tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/fires/Item.java
(rev 0)
+++
tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/fires/Item.java 2009-07-23
21:15:23 UTC (rev 3173)
@@ -0,0 +1,24 @@
+package org.jboss.jsr299.tck.tests.event.fires;
+
+class Item
+{
+ private String name;
+
+ private double price;
+
+ public Item(String name, double price)
+ {
+ this.name = name;
+ this.price = price;
+ }
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public double getPrice()
+ {
+ return price;
+ }
+}
Added: tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/fires/Lifted.java
===================================================================
--- tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/fires/Lifted.java
(rev 0)
+++
tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/fires/Lifted.java 2009-07-23
21:15:23 UTC (rev 3173)
@@ -0,0 +1,21 @@
+package org.jboss.jsr299.tck.tests.event.fires;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.PARAMETER;
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import javax.enterprise.inject.BindingType;
+
+@Target( { TYPE, METHOD, PARAMETER, FIELD })
+@Retention(RUNTIME)
+@Documented
+@BindingType
+@interface Lifted
+{
+}
Added: tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/fires/MiniBar.java
===================================================================
--- tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/fires/MiniBar.java
(rev 0)
+++
tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/fires/MiniBar.java 2009-07-23
21:15:23 UTC (rev 3173)
@@ -0,0 +1,76 @@
+package org.jboss.jsr299.tck.tests.event.fires;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.enterprise.event.Event;
+import javax.enterprise.inject.AnnotationLiteral;
+import javax.enterprise.inject.Any;
+
+class MiniBar
+{
+ private Set<Item> items = new HashSet<Item>();
+
+ @Any Event<MiniBar> miniBarEvent;
+
+ @Lifted Event<Item> itemLiftedEvent;
+
+ @Any Event<Item> itemEvent;
+
+ public Set<Item> getItems()
+ {
+ return items;
+ }
+
+ public Item getItemByName(String name)
+ {
+ for (Item item : items)
+ {
+ if (item.getName().equals(name))
+ {
+ return item;
+ }
+ }
+
+ return null;
+ }
+
+ public Item liftItemByName(String name)
+ {
+ Item item = getItemByName(name);
+ if (item != null)
+ {
+ liftItem(item);
+ }
+ return item;
+ }
+
+ public void liftItem(Item item)
+ {
+ if (!items.contains(item))
+ {
+ throw new IllegalArgumentException("No such item");
+ }
+
+ itemLiftedEvent.fire(item);
+ items.remove(item);
+ }
+
+ public void restoreItem(Item item)
+ {
+ if (items.contains(item))
+ {
+ throw new IllegalArgumentException("Item already restored");
+ }
+
+ itemEvent.select(new AnnotationLiteral<Restored>() {}).fire(item);
+ }
+
+ public void stock()
+ {
+ items.add(new Item("Chocolate", 5.00));
+ items.add(new Item("16 oz Water", 1.00));
+ items.add(new Item("Disposable Camera", 10.00));
+ miniBarEvent.fire(this);
+ }
+}
Added: tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/fires/Restored.java
===================================================================
--- tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/fires/Restored.java
(rev 0)
+++
tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/tests/event/fires/Restored.java 2009-07-23
21:15:23 UTC (rev 3173)
@@ -0,0 +1,21 @@
+package org.jboss.jsr299.tck.tests.event.fires;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.PARAMETER;
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import javax.enterprise.inject.BindingType;
+
+@Target( { TYPE, METHOD, PARAMETER, FIELD })
+@Retention(RUNTIME)
+@Documented
+@BindingType
+@interface Restored
+{
+}
Show replies by date