[jboss-cvs] JBossAS SVN: r108162 - in projects/ejb3/trunk/timerservice-deployer/src/test/java: org and 7 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Sep 16 04:40:31 EDT 2010


Author: jaikiran
Date: 2010-09-16 04:40:30 -0400 (Thu, 16 Sep 2010)
New Revision: 108162

Added:
   projects/ejb3/trunk/timerservice-deployer/src/test/java/org/
   projects/ejb3/trunk/timerservice-deployer/src/test/java/org/jboss/
   projects/ejb3/trunk/timerservice-deployer/src/test/java/org/jboss/ejb3/
   projects/ejb3/trunk/timerservice-deployer/src/test/java/org/jboss/ejb3/timerservice/
   projects/ejb3/trunk/timerservice-deployer/src/test/java/org/jboss/ejb3/timerservice/deployer/
   projects/ejb3/trunk/timerservice-deployer/src/test/java/org/jboss/ejb3/timerservice/deployer/test/
   projects/ejb3/trunk/timerservice-deployer/src/test/java/org/jboss/ejb3/timerservice/deployer/test/autotimer/
   projects/ejb3/trunk/timerservice-deployer/src/test/java/org/jboss/ejb3/timerservice/deployer/test/autotimer/MockTimerService.java
   projects/ejb3/trunk/timerservice-deployer/src/test/java/org/jboss/ejb3/timerservice/deployer/test/autotimer/SimpleSLSB.java
   projects/ejb3/trunk/timerservice-deployer/src/test/java/org/jboss/ejb3/timerservice/deployer/test/autotimer/unit/
   projects/ejb3/trunk/timerservice-deployer/src/test/java/org/jboss/ejb3/timerservice/deployer/test/autotimer/unit/AutoTimerInitializerTestCase.java
Log:
EJBTHREE-2169 Added a testcase

Added: projects/ejb3/trunk/timerservice-deployer/src/test/java/org/jboss/ejb3/timerservice/deployer/test/autotimer/MockTimerService.java
===================================================================
--- projects/ejb3/trunk/timerservice-deployer/src/test/java/org/jboss/ejb3/timerservice/deployer/test/autotimer/MockTimerService.java	                        (rev 0)
+++ projects/ejb3/trunk/timerservice-deployer/src/test/java/org/jboss/ejb3/timerservice/deployer/test/autotimer/MockTimerService.java	2010-09-16 08:40:30 UTC (rev 108162)
@@ -0,0 +1,143 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.timerservice.deployer.test.autotimer;
+
+import java.io.Serializable;
+import java.lang.reflect.Method;
+import java.util.Collection;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.ejb.EJBException;
+import javax.ejb.ScheduleExpression;
+import javax.ejb.TimerConfig;
+
+import org.jboss.ejb3.timerservice.extension.Timer;
+import org.jboss.ejb3.timerservice.extension.TimerService;
+
+/**
+ * MockTimerService
+ *
+ * @author Jaikiran Pai
+ * @version $Revision: $
+ */
+public class MockTimerService implements TimerService
+{
+
+   private Map<Method, ScheduleExpression> autoTimerInfo = new HashMap<Method, ScheduleExpression>();
+   
+   @Override
+   public Timer getAutoTimer(ScheduleExpression schedule, Method timeoutMethod)
+   {
+      return this.getAutoTimer(schedule, null, timeoutMethod);
+   }
+
+   @Override
+   public Timer getAutoTimer(ScheduleExpression schedule, TimerConfig timerConfig, Method timeoutMethod)
+   {
+      this.autoTimerInfo.put(timeoutMethod, schedule);
+      return null;
+   }
+
+   @Override
+   public javax.ejb.Timer createCalendarTimer(ScheduleExpression schedule) throws IllegalArgumentException,
+         IllegalStateException, EJBException
+   {
+      return null;
+   }
+
+   @Override
+   public javax.ejb.Timer createCalendarTimer(ScheduleExpression schedule, TimerConfig timerConfig)
+         throws IllegalArgumentException, IllegalStateException, EJBException
+   {
+      return null;
+   }
+
+   @Override
+   public javax.ejb.Timer createIntervalTimer(Date initialExpiration, long intervalDuration, TimerConfig timerConfig)
+         throws IllegalArgumentException, IllegalStateException, EJBException
+   {
+      return null;
+   }
+
+   @Override
+   public javax.ejb.Timer createIntervalTimer(long initialDuration, long intervalDuration, TimerConfig timerConfig)
+         throws IllegalArgumentException, IllegalStateException, EJBException
+   {
+      return null;
+   }
+
+   @Override
+   public javax.ejb.Timer createSingleActionTimer(Date expiration, TimerConfig timerConfig)
+         throws IllegalArgumentException, IllegalStateException, EJBException
+   {
+      return null;
+   }
+
+   @Override
+   public javax.ejb.Timer createSingleActionTimer(long duration, TimerConfig timerConfig)
+         throws IllegalArgumentException, IllegalStateException, EJBException
+   {
+      return null;
+   }
+
+   @Override
+   public javax.ejb.Timer createTimer(long duration, Serializable info) throws IllegalArgumentException,
+         IllegalStateException, EJBException
+   {
+      return null;
+   }
+
+   @Override
+   public javax.ejb.Timer createTimer(Date expiration, Serializable info) throws IllegalArgumentException,
+         IllegalStateException, EJBException
+   {
+      return null;
+   }
+
+   @Override
+   public javax.ejb.Timer createTimer(long initialDuration, long intervalDuration, Serializable info)
+         throws IllegalArgumentException, IllegalStateException, EJBException
+   {
+      return null;
+   }
+
+   @Override
+   public javax.ejb.Timer createTimer(Date initialExpiration, long intervalDuration, Serializable info)
+         throws IllegalArgumentException, IllegalStateException, EJBException
+   {
+      return null;
+   }
+
+   @Override
+   public Collection<javax.ejb.Timer> getTimers() throws IllegalStateException, EJBException
+   {
+      return null;
+   }
+
+   public Map<Method, ScheduleExpression> getAutoTimers()
+   {
+      return this.autoTimerInfo;
+   }
+   
+}

Added: projects/ejb3/trunk/timerservice-deployer/src/test/java/org/jboss/ejb3/timerservice/deployer/test/autotimer/SimpleSLSB.java
===================================================================
--- projects/ejb3/trunk/timerservice-deployer/src/test/java/org/jboss/ejb3/timerservice/deployer/test/autotimer/SimpleSLSB.java	                        (rev 0)
+++ projects/ejb3/trunk/timerservice-deployer/src/test/java/org/jboss/ejb3/timerservice/deployer/test/autotimer/SimpleSLSB.java	2010-09-16 08:40:30 UTC (rev 108162)
@@ -0,0 +1,43 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.timerservice.deployer.test.autotimer;
+
+import javax.ejb.Schedule;
+import javax.ejb.Stateless;
+import javax.ejb.Timer;
+
+/**
+ * SimpleSLSB
+ *
+ * @author Jaikiran Pai
+ * @version $Revision: $
+ */
+ at Stateless
+public class SimpleSLSB
+{
+
+   @Schedule(hour = "*", minute = "*", second = "15")
+   public void autoTimerOne(Timer timer)
+   {
+
+   }
+}

Added: projects/ejb3/trunk/timerservice-deployer/src/test/java/org/jboss/ejb3/timerservice/deployer/test/autotimer/unit/AutoTimerInitializerTestCase.java
===================================================================
--- projects/ejb3/trunk/timerservice-deployer/src/test/java/org/jboss/ejb3/timerservice/deployer/test/autotimer/unit/AutoTimerInitializerTestCase.java	                        (rev 0)
+++ projects/ejb3/trunk/timerservice-deployer/src/test/java/org/jboss/ejb3/timerservice/deployer/test/autotimer/unit/AutoTimerInitializerTestCase.java	2010-09-16 08:40:30 UTC (rev 108162)
@@ -0,0 +1,126 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.timerservice.deployer.test.autotimer.unit;
+
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.lang.reflect.AnnotatedElement;
+import java.lang.reflect.Method;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Map;
+
+import javax.ejb.ScheduleExpression;
+import javax.ejb.Timer;
+
+import junit.framework.Assert;
+
+import org.jboss.ejb3.EJBContainer;
+import org.jboss.ejb3.timerservice.deployer.AutoTimerInitializer;
+import org.jboss.ejb3.timerservice.deployer.test.autotimer.MockTimerService;
+import org.jboss.ejb3.timerservice.deployer.test.autotimer.SimpleSLSB;
+import org.jboss.metadata.annotation.creator.ejb.jboss.JBoss50Creator;
+import org.jboss.metadata.annotation.finder.AnnotationFinder;
+import org.jboss.metadata.annotation.finder.DefaultAnnotationFinder;
+import org.jboss.metadata.ejb.jboss.JBossEnterpriseBeanMetaData;
+import org.jboss.metadata.ejb.jboss.JBossMetaData;
+import org.junit.Test;
+
+/**
+ * Tests for {@link AutoTimerInitializer}
+ *
+ * @author Jaikiran Pai
+ * @version $Revision: $
+ */
+public class AutoTimerInitializerTestCase
+{
+
+   /**
+    * Test that the {@link AutoTimerInitializer} creates auto-timers when the appropriate metadata
+    * is available in the {@link EJBContainer}s
+    * 
+    * @throws Exception
+    */
+   @Test
+   public void testAutoTimerCreation() throws Exception
+   {
+      EJBContainer mockContainer = mock(EJBContainer.class);
+      MockTimerService mockTimerService = new MockTimerService();
+      
+      JBossEnterpriseBeanMetaData beanMetaData = this.createMetadata(SimpleSLSB.class.getSimpleName(), SimpleSLSB.class);
+      ClassLoader tccl = Thread.currentThread().getContextClassLoader();
+      
+      // setup the mock(s)
+      when(mockContainer.getXml()).thenReturn(beanMetaData);
+      when(mockContainer.getTimerService()).thenReturn(mockTimerService);
+      doReturn(SimpleSLSB.class).when(mockContainer).getBeanClass();
+      when(mockContainer.getClassloader()).thenReturn(tccl);
+      
+      // initialize auto-timers for the container
+      AutoTimerInitializer autoTimerInitializer = new AutoTimerInitializer();
+      autoTimerInitializer.initializeAutoTimers(mockContainer);
+      
+      // get hold of the auto-timers that were created
+      Map<Method, ScheduleExpression> autoTimers = mockTimerService.getAutoTimers();
+      
+      // test
+      Assert.assertNotNull("No auto-timers were created", autoTimers);
+      Assert.assertEquals("Unexpected number of auto-timers were created", 1, autoTimers.size());
+      
+      // make sure the correct method was identified as an auto-timer method
+      Method expectedAutoTimerMethod = SimpleSLSB.class.getDeclaredMethod("autoTimerOne", new Class<?>[] {Timer.class});
+      Method autoTimerMethod = autoTimers.keySet().iterator().next();
+      Assert.assertEquals("Unexpected auto-timer method", expectedAutoTimerMethod, autoTimerMethod);
+      
+      // check the schedule expression used for creating the auto timer
+      ScheduleExpression expr = autoTimers.get(expectedAutoTimerMethod);
+      String hour = expr.getHour();
+      String minute = expr.getMinute();
+      String second = expr.getSecond();
+      Assert.assertEquals("Unexpected hour for auto-timer schedule", "*", hour);
+      Assert.assertEquals("Unexpected minute for auto-timer schedule", "*", minute);
+      Assert.assertEquals("Unexpected second for auto-timer schedule", "15", second);
+      
+   }
+   
+   /**
+    * Creates and returns the {@link JBossEnterpriseBeanMetaData} for the passed bean class
+    * 
+    * @param beanName The name of the EJB
+    * @param beanClass The EJB class
+    * @return
+    */
+   private JBossEnterpriseBeanMetaData createMetadata(String beanName, Class<?> beanClass)
+   {
+      AnnotationFinder<AnnotatedElement> finder = new DefaultAnnotationFinder<AnnotatedElement>();
+      JBoss50Creator creator = new JBoss50Creator(finder);
+      
+      Collection<Class<?>> classes = new HashSet<Class<?>>();
+      classes.add(beanClass);
+      JBossMetaData metadata = creator.create(classes);
+      
+      return metadata.getEnterpriseBean(beanName);
+      
+   }
+}



More information about the jboss-cvs-commits mailing list