[weld-commits] Weld SVN: r5104 - in extensions/trunk/se/src/test: java/org/jboss/weld/environment/se/test/decorators and 1 other directories.

weld-commits at lists.jboss.org weld-commits at lists.jboss.org
Fri Nov 20 05:15:07 EST 2009


Author: peteroyle
Date: 2009-11-20 05:15:06 -0500 (Fri, 20 Nov 2009)
New Revision: 5104

Added:
   extensions/trunk/se/src/test/java/org/jboss/weld/environment/se/test/DecoratorsTest.java
   extensions/trunk/se/src/test/java/org/jboss/weld/environment/se/test/decorators/
   extensions/trunk/se/src/test/java/org/jboss/weld/environment/se/test/decorators/CarDoor.java
   extensions/trunk/se/src/test/java/org/jboss/weld/environment/se/test/decorators/CarDoorAlarm.java
   extensions/trunk/se/src/test/java/org/jboss/weld/environment/se/test/decorators/Door.java
   extensions/trunk/se/src/test/java/org/jboss/weld/environment/se/test/decorators/HouseDoor.java
Modified:
   extensions/trunk/se/src/test/java/org/jboss/weld/environment/se/test/InterceptorsTest.java
   extensions/trunk/se/src/test/resources/META-INF/beans.xml
Log:
Added tests for Decorators in SE

Added: extensions/trunk/se/src/test/java/org/jboss/weld/environment/se/test/DecoratorsTest.java
===================================================================
--- extensions/trunk/se/src/test/java/org/jboss/weld/environment/se/test/DecoratorsTest.java	                        (rev 0)
+++ extensions/trunk/se/src/test/java/org/jboss/weld/environment/se/test/DecoratorsTest.java	2009-11-20 10:15:06 UTC (rev 5104)
@@ -0,0 +1,97 @@
+/**
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat, Inc. and/or its affiliates, 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.weld.environment.se.test;
+
+import javax.enterprise.inject.spi.BeanManager;
+import javax.enterprise.util.AnnotationLiteral;
+
+import org.jboss.weld.environment.se.StartMain;
+import org.jboss.weld.environment.se.events.Shutdown;
+import org.jboss.weld.environment.se.test.decorators.CarDoor;
+import org.jboss.weld.environment.se.test.decorators.Door;
+import org.jboss.weld.environment.se.test.decorators.CarDoorAlarm;
+import org.jboss.weld.environment.se.test.decorators.HouseDoor;
+import org.jboss.weld.environment.se.util.WeldManagerUtils;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+/**
+ * 
+ * @author Peter Royle
+ */
+public class DecoratorsTest
+{
+
+    public static String[] ARGS_EMPTY = new String[]
+    {
+    };
+
+    /**
+     * Test that decorators work as expected in SE.
+     */
+    @Test
+    public void testDecorators()
+    {
+        String[] args = ARGS_EMPTY;
+        BeanManager manager = new StartMain(args).go();
+
+        CarDoor carDoor = WeldManagerUtils.getInstanceByType(manager, CarDoor.class);
+        Assert.assertNotNull(carDoor);
+
+        // the car door is alarmed
+        CarDoorAlarm.alarmActivated = false;
+        Assert.assertFalse(CarDoorAlarm.alarmActivated);
+        testDoor(carDoor);
+        Assert.assertTrue(CarDoorAlarm.alarmActivated);
+
+        HouseDoor houseDoor = WeldManagerUtils.getInstanceByType(manager, HouseDoor.class);
+        Assert.assertNotNull(carDoor);
+
+        // the house door is not alarmed
+        CarDoorAlarm.alarmActivated = false;
+        Assert.assertFalse(CarDoorAlarm.alarmActivated);
+        testDoor(houseDoor);
+        Assert.assertFalse(CarDoorAlarm.alarmActivated);
+
+        shutdownManager(manager);
+    }
+
+    private void testDoor(Door door)
+    {
+        Assert.assertTrue(door.open());
+        Assert.assertTrue(door.isOpen());
+        Assert.assertFalse(door.close());
+        Assert.assertFalse(door.isOpen());
+        Assert.assertTrue(door.lock());
+        Assert.assertTrue(door.isLocked());
+        Assert.assertFalse(door.open());
+        Assert.assertFalse(door.isOpen());
+    }
+
+    private void shutdownManager(BeanManager manager)
+    {
+        manager.fireEvent(manager, new ShutdownAnnotation());
+    }
+
+    private static class ShutdownAnnotation extends AnnotationLiteral<Shutdown>
+    {
+
+        public ShutdownAnnotation()
+        {
+        }
+    }
+}

Modified: extensions/trunk/se/src/test/java/org/jboss/weld/environment/se/test/InterceptorsTest.java
===================================================================
--- extensions/trunk/se/src/test/java/org/jboss/weld/environment/se/test/InterceptorsTest.java	2009-11-20 07:12:28 UTC (rev 5103)
+++ extensions/trunk/se/src/test/java/org/jboss/weld/environment/se/test/InterceptorsTest.java	2009-11-20 10:15:06 UTC (rev 5104)
@@ -21,12 +21,7 @@
 
 import org.jboss.weld.environment.se.StartMain;
 import org.jboss.weld.environment.se.events.Shutdown;
-import org.jboss.weld.environment.se.test.beans.CustomEvent;
-import org.jboss.weld.environment.se.test.beans.InitObserverTestBean;
 import org.jboss.weld.environment.se.test.beans.InterceptorTestBean;
-import org.jboss.weld.environment.se.test.beans.MainTestBean;
-import org.jboss.weld.environment.se.test.beans.ObserverTestBean;
-import org.jboss.weld.environment.se.test.beans.ParametersTestBean;
 import org.jboss.weld.environment.se.test.interceptors.AggregatingInterceptor;
 import org.jboss.weld.environment.se.test.interceptors.RecordingInterceptor;
 import org.jboss.weld.environment.se.util.WeldManagerUtils;
@@ -40,80 +35,46 @@
 public class InterceptorsTest
 {
 
-   public static String[] ARGS_EMPTY = new String[] {};
+    public static String[] ARGS_EMPTY = new String[]
+    {
+    };
 
-   /**
-    * Test that interceptors work as expected in SE.
-    */
-   @Test
-   public void testInterceptors()
-   {
-      String[] args = ARGS_EMPTY;
-      BeanManager manager = new StartMain(args).go();
+    /**
+     * Test that interceptors work as expected in SE.
+     */
+    @Test
+    public void testInterceptors()
+    {
+        String[] args = ARGS_EMPTY;
+        BeanManager manager = new StartMain(args).go();
 
-      InterceptorTestBean intTestBean = WeldManagerUtils.getInstanceByType(manager, InterceptorTestBean.class);
-      Assert.assertNotNull(intTestBean);
+        InterceptorTestBean intTestBean = WeldManagerUtils.getInstanceByType(manager, InterceptorTestBean.class);
+        Assert.assertNotNull(intTestBean);
 
-      intTestBean.doSomethingRecorded();
-      System.out.println(RecordingInterceptor.methodsRecorded);
-      System.out.println(AggregatingInterceptor.methodsCalled);
-      Assert.assertTrue(RecordingInterceptor.methodsRecorded.contains("doSomethingRecorded"));
+        intTestBean.doSomethingRecorded();
+        System.out.println(RecordingInterceptor.methodsRecorded);
+        System.out.println(AggregatingInterceptor.methodsCalled);
+        Assert.assertTrue(RecordingInterceptor.methodsRecorded.contains("doSomethingRecorded"));
 
-      intTestBean.doSomethingRecordedAndAggregated();
-      System.out.println(RecordingInterceptor.methodsRecorded);
-      System.out.println(AggregatingInterceptor.methodsCalled);
+        intTestBean.doSomethingRecordedAndAggregated();
+        System.out.println(RecordingInterceptor.methodsRecorded);
+        System.out.println(AggregatingInterceptor.methodsCalled);
 
-      Assert.assertEquals(1, AggregatingInterceptor.methodsCalled);
+        Assert.assertEquals(1, AggregatingInterceptor.methodsCalled);
 
-      shutdownManager(manager);
-   }
+        shutdownManager(manager);
+    }
 
-   /**
-    * Test of main method, of class StartMain when no command-line args are
-    * provided.
-    */
-   @Test
-   public void testMainEmptyArgs()
-   {
-      BeanManager manager = new StartMain(ARGS_EMPTY).go();
+    private void shutdownManager(BeanManager manager)
+    {
+        manager.fireEvent(manager, new ShutdownAnnotation());
+    }
 
-      MainTestBean mainTestBean = WeldManagerUtils.getInstanceByType(manager, MainTestBean.class);
-      Assert.assertNotNull(mainTestBean);
+    private static class ShutdownAnnotation extends AnnotationLiteral<Shutdown>
+    {
 
-      ParametersTestBean paramsBean = mainTestBean.getParametersTestBean();
-      Assert.assertNotNull(paramsBean);
-      Assert.assertNotNull(paramsBean.getParameters());
-
-      shutdownManager(manager);
-   }
-
-   @Test
-   public void testObservers()
-   {
-      InitObserverTestBean.reset();
-      ObserverTestBean.reset();
-
-      BeanManager manager = new StartMain(ARGS_EMPTY).go();
-      manager.fireEvent(new CustomEvent());
-
-      Assert.assertTrue(ObserverTestBean.isBuiltInObserved());
-      Assert.assertTrue(ObserverTestBean.isCustomObserved());
-      Assert.assertTrue(ObserverTestBean.isInitObserved());
-
-      Assert.assertTrue(InitObserverTestBean.isInitObserved());
-   }
-
-   private void shutdownManager(BeanManager manager)
-   {
-      manager.fireEvent(manager, new ShutdownAnnotation());
-   }
-
-   private static class ShutdownAnnotation extends AnnotationLiteral<Shutdown>
-   {
-
-      public ShutdownAnnotation()
-      {
-      }
-   }
-
+        public ShutdownAnnotation()
+        {
+        }
+    }
 }

Added: extensions/trunk/se/src/test/java/org/jboss/weld/environment/se/test/decorators/CarDoor.java
===================================================================
--- extensions/trunk/se/src/test/java/org/jboss/weld/environment/se/test/decorators/CarDoor.java	                        (rev 0)
+++ extensions/trunk/se/src/test/java/org/jboss/weld/environment/se/test/decorators/CarDoor.java	2009-11-20 10:15:06 UTC (rev 5104)
@@ -0,0 +1,37 @@
+/**
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat, Inc. and/or its affiliates, 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.weld.environment.se.test.decorators;
+
+/**
+ *
+ * @author Peter Royle
+ */
+public class CarDoor extends Door {
+
+    private boolean isWindowOpen = false;
+
+    public boolean openWindow() {
+        isWindowOpen = true;
+        return isWindowOpen;
+    }
+    public boolean closeWindow() {
+        isWindowOpen = false;
+        return isWindowOpen;
+    }
+
+}

Added: extensions/trunk/se/src/test/java/org/jboss/weld/environment/se/test/decorators/CarDoorAlarm.java
===================================================================
--- extensions/trunk/se/src/test/java/org/jboss/weld/environment/se/test/decorators/CarDoorAlarm.java	                        (rev 0)
+++ extensions/trunk/se/src/test/java/org/jboss/weld/environment/se/test/decorators/CarDoorAlarm.java	2009-11-20 10:15:06 UTC (rev 5104)
@@ -0,0 +1,45 @@
+/**
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat, Inc. and/or its affiliates, 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.weld.environment.se.test.decorators;
+
+import javax.decorator.Decorator;
+import javax.decorator.Delegate;
+import javax.inject.Inject;
+
+/**
+ * Decorates a door so that an alarm is activated if the door is attempted
+ * to be opened while it is locked.
+ * @author Peter Royle
+ */
+ at Decorator
+public class CarDoorAlarm
+{
+
+    @Inject @Delegate
+    CarDoor door;
+    public static boolean alarmActivated = false;
+
+    public boolean open()
+    {
+        if (door.isLocked())
+        {
+            alarmActivated = true;
+            System.out.println("AWOOGA"); // (there's an emergency going on)
+        }
+        return door.open(); // lock will still be used
+    }
+}

Added: extensions/trunk/se/src/test/java/org/jboss/weld/environment/se/test/decorators/Door.java
===================================================================
--- extensions/trunk/se/src/test/java/org/jboss/weld/environment/se/test/decorators/Door.java	                        (rev 0)
+++ extensions/trunk/se/src/test/java/org/jboss/weld/environment/se/test/decorators/Door.java	2009-11-20 10:15:06 UTC (rev 5104)
@@ -0,0 +1,73 @@
+/**
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat, Inc. and/or its affiliates, 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.weld.environment.se.test.decorators;
+
+/**
+ * Represents door with a lock.
+ * @author Peter Royle
+ */
+public abstract class Door
+{
+
+    private boolean isOpen = false;
+    private boolean isLocked = false;
+
+    /**
+     * Open and close the door. Can't open a locked door.
+     */
+    public boolean open()
+    {
+        if (!isLocked)
+        {
+            isOpen = true;
+        }
+        return isOpen;
+    }
+
+    public boolean close()
+    {
+        isOpen = false;
+        return isOpen;
+    }
+
+    public boolean isOpen()
+    {
+        return isOpen;
+    }
+
+    /**
+     * Lock and unlock the door. Can't lock an open door.
+     */
+    public boolean lock()
+    {
+        if (!isOpen)
+        {
+            isLocked = true;
+        }
+        return isLocked;
+    }
+
+    public void unlock()
+    {
+        isLocked = false;
+    }
+
+    public boolean isLocked()
+    {
+        return isLocked;
+    }
+}

Added: extensions/trunk/se/src/test/java/org/jboss/weld/environment/se/test/decorators/HouseDoor.java
===================================================================
--- extensions/trunk/se/src/test/java/org/jboss/weld/environment/se/test/decorators/HouseDoor.java	                        (rev 0)
+++ extensions/trunk/se/src/test/java/org/jboss/weld/environment/se/test/decorators/HouseDoor.java	2009-11-20 10:15:06 UTC (rev 5104)
@@ -0,0 +1,26 @@
+/**
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat, Inc. and/or its affiliates, 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.weld.environment.se.test.decorators;
+
+/**
+ *
+ * @author Peter Royle
+ */
+public class HouseDoor extends Door {
+
+}

Modified: extensions/trunk/se/src/test/resources/META-INF/beans.xml
===================================================================
--- extensions/trunk/se/src/test/resources/META-INF/beans.xml	2009-11-20 07:12:28 UTC (rev 5103)
+++ extensions/trunk/se/src/test/resources/META-INF/beans.xml	2009-11-20 10:15:06 UTC (rev 5104)
@@ -21,4 +21,7 @@
         <class>org.jboss.weld.environment.se.test.interceptors.RecordingInterceptor</class>
         <class>org.jboss.weld.environment.se.test.interceptors.AggregatingInterceptor</class>
     </interceptors>
+    <decorators>
+        <class>org.jboss.weld.environment.se.test.decorators.CarDoorAlarm</class>
+    </decorators>
 </beans>



More information about the weld-commits mailing list