View Javadoc

1   /*
2    * $Id: QuartzCustomStatefulJobTestCase.java 22498 2011-07-21 13:01:39Z justin.calleja $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.quartz;
12  
13  import static org.junit.Assert.assertTrue;
14  
15  import java.util.Arrays;
16  import java.util.Collection;
17  import java.util.concurrent.CountDownLatch;
18  import java.util.concurrent.TimeUnit;
19  
20  import org.junit.Test;
21  import org.junit.runners.Parameterized.Parameters;
22  import org.mule.tck.AbstractServiceAndFlowTestCase;
23  import org.quartz.JobExecutionContext;
24  import org.quartz.JobExecutionException;
25  import org.quartz.StatefulJob;
26  
27  public class QuartzCustomStatefulJobTestCase extends AbstractServiceAndFlowTestCase
28  {
29      public QuartzCustomStatefulJobTestCase(ConfigVariant variant, String configResources)
30      {
31          super(variant, configResources);
32      }
33  
34   
35      @Parameters
36      public static Collection<Object[]> parameters()
37      {
38          return Arrays.asList(new Object[][]{
39              {ConfigVariant.SERVICE, "quartz-custom-stateful-job-service.xml"},
40              {ConfigVariant.FLOW, "quartz-custom-stateful-job-flow.xml"}
41          });
42      }
43      
44      @Test
45      public void testCustomStatefulJob() throws Exception
46      {
47          CountDownLatch eventLatch = (CountDownLatch) muleContext.getRegistry().lookupObject("latch");
48  
49          // we wait up to 60 seconds here which is WAY too long for one tick but it seems that 
50          // "sometimes" it takes a very long time for Quartz go kick in. Once it starts 
51          // ticking everything is fine.
52          assertTrue(eventLatch.await(60000, TimeUnit.MILLISECONDS));
53      }
54  
55      public static class MyStatefulJob implements StatefulJob
56      {
57          private CountDownLatch latch;
58  
59          public MyStatefulJob(CountDownLatch latch)
60          {
61              super();
62              this.latch = latch;
63          }
64          
65          public void execute(JobExecutionContext context) throws JobExecutionException
66          {
67              assertTrue(context.getJobDetail().isStateful());
68              latch.countDown();
69          }
70      }
71  }