1   /*
2    * $Id: DispatcherTest.java,v 1.1 2004/12/15 14:18:12 patforna Exp $
3    *
4    * Copyright (c) 2004 Patric Fornasier, Pawel Kowalski
5    * Berne University of Applied Sciences
6    * School of Engineering and Information Technology
7    * All rights reserved.
8    */
9   package bexee.core;
10  
11  import junit.framework.TestCase;
12  import bexee.model.process.BPELProcess;
13  import bexee.model.process.impl.BPELProcessImpl;
14  
15  /***
16   * @version $Revision: 1.1 $, $Date: 2004/12/15 14:18:12 $
17   * @author Patric Fornasier
18   * @author Pawel Kowalski
19   */
20  public class DispatcherTest extends TestCase {
21  
22      private static final String SYNC_RESULT = "RESULT";
23  
24      /***
25       * Test if synchronous dispatching works.
26       */
27      public void testDispatchSynchronous() throws Exception {
28  
29          // create process context
30          ProcessContext ctx = new ProcessContext();
31  
32          // create synchronous BPEL process
33          BPELProcess process = new BPELProcessImpl();
34          process.setSynchronous(true);
35  
36          // create mock dispatcher with process context BPEL process
37          MockDispatcher dispatcher = new MockDispatcher(ctx, process);
38  
39          // get result
40          Object result = dispatcher.dispatch();
41  
42          // check
43          assertEquals(SYNC_RESULT, result);
44      }
45  
46      /***
47       * Test if asynchronous dispatching works.
48       */
49      public void testDispatchAsynchronous() throws Exception {
50  
51          // create process context
52          ProcessContext ctx = new ProcessContext();
53  
54          // create asynchronous BPEL process
55          BPELProcess process = new BPELProcessImpl();
56          process.setSynchronous(false);
57  
58          // create mock dispatcher with process context BPEL process
59          MockDispatcher dispatcher = new MockDispatcher(ctx, process);       
60  
61          // get result
62          Object result = dispatcher.dispatch();
63  
64          // check
65          assertEquals(Dispatcher.ASYNC_RESULT, result);
66      }
67  
68      /***
69       * Test if <code>Dispatcher</code> throws an exception if no
70       * <code>BPELProcess</code> was found.
71       */
72      public void testDispatchNoBPELProcess() {
73  
74          // set for clarity
75          MockDispatcher dispatcher = new MockDispatcher(null, null); 
76  
77          try {
78              dispatcher.dispatch();
79              fail("Dispatcher should throw an exception if BPELProcess is null");
80          } catch (DispatcherException e) {
81              // nothing to do here
82          }
83      }
84  
85      /***
86       * Test if <code>Dispatcher</code> throws an exception if no
87       * <code>ProcessContext</code> was found.
88       */
89      public void testDispatchNoProcessContext() {
90  
91          // set for clarity
92          MockDispatcher dispatcher = new MockDispatcher(null, null); 
93  
94          try {
95              dispatcher.dispatch();
96              fail("Dispatcher should throw an exception if ProcessContext is null");
97          } catch (DispatcherException e) {
98              // nothing to do here
99          }
100     }
101 
102     /***
103      * 
104      * @version $Revision: 1.1 $, $Date: 2004/12/15 14:18:12 $
105      * @author Patric Fornasier
106      * @author Pawel Kowalski
107      */
108     private class MockDispatcher extends Dispatcher {
109 
110         private ProcessContext ctx;
111 
112         private BPELProcess process;
113 
114         /***
115          * Create a new <code>MockDispatcher</code> object
116          * @param ctx the <code>ProcessContext</code>
117          * @param process the <code>BPELProcess</code>
118          */
119         public MockDispatcher(ProcessContext ctx, BPELProcess process) {
120             super(null);
121             instance = new ProcessInstance(process, ctx);            
122         }
123 
124         /***
125          * Do nothing
126          */
127         protected BPELProcess getBPELProcess(BexeeMessage message) {
128             return null;
129         }
130 
131         /***
132          * Do nothing
133          */
134         protected ProcessContext getProcessContext(BexeeMessage message) {
135             return null;
136         }
137 
138         /***
139          * Simulate the synchronous call to the <code>ProcessController</code>
140          */
141         public void run() {
142 
143             ProcessContext ctx = instance.getContext();
144             BPELProcess process = instance.getProcess();
145 
146             if (process.isSynchronous()) {
147                 ctx.setResult(SYNC_RESULT);
148             }
149         }
150 
151     }
152 
153 }