001: /*
002: * $Id: DispatcherTest.java,v 1.2 2004/12/02 13:58:01 fornp1 Exp $
003: *
004: * Copyright (c) 2004 Patric Fornasier, Pawel Kowalski
005: * Berne University of Applied Sciences
006: * School of Engineering and Information Technology
007: * All rights reserved.
008: */
009: package bexee.core;
010:
011: import junit.framework.TestCase;
012: import bexee.model.process.BPELProcess;
013: import bexee.model.process.impl.BPELProcessImpl;
014:
015: /**
016: * @version $Revision: 1.2 $, $Date: 2004/12/02 13:58:01 $
017: * @author Patric Fornasier
018: * @author Pawel Kowalski
019: */
020: public class DispatcherTest extends TestCase {
021:
022: private static final String SYNC_RESULT = "RESULT";
023:
024: /**
025: * Test if synchronous dispatching works.
026: */
027: public void testDispatchSynchronous() throws Exception {
028:
029: // create process context
030: ProcessContext ctx = new ProcessContext();
031:
032: // create synchronous BPEL process
033: BPELProcess process = new BPELProcessImpl();
034: process.setSynchronous(true);
035:
036: // create mock dispatcher with process context BPEL process
037: MockDispatcher dispatcher = new MockDispatcher(ctx, process);
038:
039: // get result
040: Object result = dispatcher.dispatch();
041:
042: // check
043: assertEquals(SYNC_RESULT, result);
044: }
045:
046: /**
047: * Test if asynchronous dispatching works.
048: */
049: public void testDispatchAsynchronous() throws Exception {
050:
051: // create process context
052: ProcessContext ctx = new ProcessContext();
053:
054: // create asynchronous BPEL process
055: BPELProcess process = new BPELProcessImpl();
056: process.setSynchronous(false);
057:
058: // create mock dispatcher with process context BPEL process
059: MockDispatcher dispatcher = new MockDispatcher(ctx, process);
060:
061: // get result
062: Object result = dispatcher.dispatch();
063:
064: // check
065: assertEquals(Dispatcher.ASYNC_RESULT, result);
066: }
067:
068: /**
069: * Test if <code>Dispatcher</code> throws an exception if no
070: * <code>BPELProcess</code> was found.
071: */
072: public void testDispatchNoBPELProcess() {
073:
074: // set for clarity
075: MockDispatcher dispatcher = new MockDispatcher(null, null);
076:
077: try {
078: dispatcher.dispatch();
079: fail("Dispatcher should throw an exception if BPELProcess is null");
080: } catch (DispatcherException e) {
081: // nothing to do here
082: }
083: }
084:
085: /**
086: * Test if <code>Dispatcher</code> throws an exception if no
087: * <code>ProcessContext</code> was found.
088: */
089: public void testDispatchNoProcessContext() {
090:
091: // set for clarity
092: MockDispatcher dispatcher = new MockDispatcher(null, null);
093:
094: try {
095: dispatcher.dispatch();
096: fail("Dispatcher should throw an exception if ProcessContext is null");
097: } catch (DispatcherException e) {
098: // nothing to do here
099: }
100: }
101:
102: /**
103: *
104: * @version $Revision: 1.2 $, $Date: 2004/12/02 13:58:01 $
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: }
|