001: /*
002: * Bossa Workflow System
003: *
004: * $Id: CaseTest.java,v 1.46 2004/03/03 17:19:03 gdvieira Exp $
005: *
006: * Copyright (C) 2003,2004 OpenBR Sistemas S/C Ltda.
007: *
008: * This file is part of Bossa.
009: *
010: * Bossa is free software; you can redistribute it and/or modify it
011: * under the terms of version 2 of the GNU General Public License as
012: * published by the Free Software Foundation.
013: *
014: * This program is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * General Public License for more details.
018: *
019: * You should have received a copy of the GNU General Public
020: * License along with this program; if not, write to the
021: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
022: * Boston, MA 02111-1307, USA.
023: */
024:
025: package com.bigbross.bossa.wfnet;
026:
027: import java.util.HashMap;
028: import java.util.Iterator;
029: import java.util.List;
030: import java.util.Map;
031:
032: import junit.framework.TestCase;
033:
034: import com.bigbross.bossa.BossaException;
035: import com.bigbross.bossa.BossaTestUtil;
036: import com.bigbross.bossa.resource.Resource;
037: import com.bigbross.bossa.resource.ResourceUtil;
038:
039: public class CaseTest extends TestCase {
040:
041: private Resource jdoe;
042:
043: public CaseTest(String name) {
044: super (name);
045: }
046:
047: protected void setUp() {
048: jdoe = ResourceUtil.createResource("jdoe");
049: }
050:
051: static void sameState(Map expected, Map actual) {
052: int matches = 0;
053: for (Iterator i = actual.keySet().iterator(); i.hasNext();) {
054: String key = (String) i.next();
055: if (expected.get(key) != null) {
056: assertEquals(expected.get(key), actual.get(key));
057: matches++;
058: } else {
059: assertEquals(new Integer(0), actual.get(key));
060: }
061: }
062: assertEquals(matches, expected.size());
063: }
064:
065: public void testGetWorkItem() throws Exception {
066: Case caze = WFNetUtil.createCase();
067:
068: WorkItem wi = caze.getWorkItem("a");
069: assertNotNull(wi);
070: assertEquals("a", wi.getId());
071: assertNull(caze.getWorkItem("invalid id"));
072: }
073:
074: public void testGetState() throws Exception {
075: Case caze = WFNetUtil.createCase();
076:
077: Map expected = new HashMap();
078: expected.put("A", new Integer(1));
079: expected.put("B", new Integer(0));
080: expected.put("C", new Integer(0));
081: expected.put("D", new Integer(0));
082: expected.put("E", new Integer(0));
083: expected.put("F", new Integer(0));
084: expected.put("G", new Integer(0));
085: expected.put("H", new Integer(0));
086:
087: CaseTest.sameState(expected, caze.getState());
088: }
089:
090: public void testOpenClose() throws Exception {
091: Case caze = WFNetUtil.createCase();
092:
093: Map expected = new HashMap();
094: expected.put("B", new Integer(1));
095:
096: assertTrue(WFNetUtil.fire(caze, "a", null));
097:
098: CaseTest.sameState(expected, caze.getState());
099: }
100:
101: public void testInvalidOpen() throws Exception {
102: Case caze = WFNetUtil.createCase();
103:
104: Map start = caze.getState();
105:
106: assertFalse(WFNetUtil.fire(caze, "b", null));
107:
108: CaseTest.sameState(start, caze.getState());
109: }
110:
111: public void testOpenCancel() throws Exception {
112: Case caze = WFNetUtil.createCase();
113:
114: Map start = caze.getState();
115:
116: Activity act = caze.open(caze.getWorkItem("a"), jdoe);
117: assertNotNull(act);
118: assertTrue(caze.cancel(act));
119:
120: CaseTest.sameState(start, caze.getState());
121: }
122:
123: public void testInvalidCloseCancel() throws Exception {
124: Case caze = WFNetUtil.createCase();
125:
126: Map expected = new HashMap();
127: expected.put("B", new Integer(1));
128: Activity act = caze.open(caze.getWorkItem("a"), jdoe);
129: assertNotNull(act);
130: assertTrue(caze.close(act, null));
131:
132: CaseTest.sameState(expected, caze.getState());
133:
134: assertFalse(caze.close(act, null));
135: CaseTest.sameState(expected, caze.getState());
136:
137: assertFalse(caze.cancel(act));
138: CaseTest.sameState(expected, caze.getState());
139: }
140:
141: public void testAttributes() throws Exception {
142: Case caze = WFNetUtil.createCase();
143: Map attributes = caze.getAttributes();
144:
145: assertEquals(4, attributes.size());
146: assertEquals(new Boolean(false), attributes.get("SOK"));
147: assertEquals(new Boolean(false), attributes.get("DIR"));
148: assertEquals("", attributes.get("ADIR"));
149: assertEquals(new Boolean(false), attributes.get("OK"));
150: }
151:
152: public void testEdgeEvaluation() throws Exception {
153: Case caze = WFNetUtil.createCase();
154: caze.declare("SOK", new Boolean(true));
155: caze.declare("DIR", new Boolean(false));
156: caze.declare("AVL", new Integer(3));
157:
158: Edge e1 = Edge.newOutput(null, "AVL * SOK || DIR");
159: Edge e2 = Edge.newInput(null, "AVL * SOK && DIR");
160:
161: assertEquals(3, e1.eval(caze));
162: assertEquals(0, e2.eval(caze));
163:
164: e1 = Edge.newInput(null, "AVL * XXX && DIR");
165: try {
166: e1.eval(caze);
167: fail("Undetected undeclared attribute.");
168: } catch (EvaluationException e) {
169: }
170: }
171:
172: public void testEdgeOrientation() throws Exception {
173: Case caze = WFNetUtil.createCase();
174:
175: Edge output = Edge.newOutput(null, "1");
176: Edge input = Edge.newInput(null, "2");
177:
178: assertEquals(1, output.output(caze));
179: assertEquals(2, input.input(caze));
180: assertEquals(0, output.input(caze));
181: assertEquals(0, input.output(caze));
182: }
183:
184: public void testMachineGun() throws Exception {
185: Case caze = WFNetUtil.createCase();
186: HashMap attributes = new HashMap();
187: attributes.put("SOK", new Boolean(true));
188: attributes.put("DIR", new Boolean(true));
189: attributes.put("ADIR", "OK");
190:
191: Map expected = new HashMap();
192: expected.put("G", new Integer(1));
193:
194: assertTrue(WFNetUtil.fire(caze, "a", attributes));
195: assertTrue(WFNetUtil.fire(caze, "b", null));
196: assertTrue(WFNetUtil.fire(caze, "c", null));
197: assertTrue(WFNetUtil.fire(caze, "d", null));
198: assertTrue(WFNetUtil.fire(caze, "e", null));
199:
200: sameState(expected, caze.getState());
201: }
202:
203: public void testSelfLoop() throws Exception {
204: CaseType caseType = new CaseType("selfloop");
205: Place A = caseType.registerPlace("A", 1);
206: Place B = caseType.registerPlace("B");
207: Transition a = caseType.registerTransition("a", "boss");
208: a.input(A, "1");
209: a.output(B, "1");
210: a.output(A, "1");
211: caseType.buildTemplate(null);
212: Case caze = caseType.openCaseImpl(null);
213:
214: Map expected = new HashMap();
215: expected.put("A", new Integer(1));
216: expected.put("B", new Integer(1));
217:
218: assertTrue(WFNetUtil.fire(caze, "a", null));
219:
220: sameState(expected, caze.getState());
221: }
222:
223: public void testZeroTimeout() throws Exception {
224: Case caze = WFNetUtil.createAutoFireCase();
225:
226: Map expected = new HashMap();
227: expected.put("A", new Integer(1));
228: expected.put("B", new Integer(0));
229: expected.put("C", new Integer(0));
230: sameState(expected, caze.getState());
231:
232: expected = new HashMap();
233: expected.put("A", new Integer(0));
234: expected.put("B", new Integer(0));
235: expected.put("C", new Integer(1));
236: assertTrue(WFNetUtil.fire(caze, "a", null));
237: sameState(expected, caze.getState());
238:
239: /* Add a test to stress nested auto firing going beyond case closing. */
240: assertEquals(0, caze.getCaseType().getCases().size());
241: }
242:
243: public void testSetState() throws Exception {
244: Case caze = WFNetUtil.createCase();
245:
246: Map newState = new HashMap();
247: newState.put("A", new Integer(0));
248: newState.put("B", new Integer(0));
249: newState.put("C", new Integer(0));
250: newState.put("D", new Integer(0));
251: newState.put("E", new Integer(1));
252: newState.put("F", new Integer(0));
253: newState.put("G", new Integer(0));
254: newState.put("H", new Integer(0));
255:
256: caze.setStateImpl(newState);
257: CaseTest.sameState(newState, caze.getState());
258:
259: assertFalse(caze.getWorkItem("a").isFireable());
260: List workItens = caze.getWorkItems();
261: assertEquals(1, workItens.size());
262: assertEquals("d", ((WorkItem) workItens.get(0)).getId());
263:
264: newState = new HashMap();
265: newState.put("A", new Integer(1));
266:
267: caze.setStateImpl(newState);
268: assertTrue(caze.getWorkItem("a").isFireable());
269: assertEquals(2, caze.getWorkItems().size());
270: }
271:
272: public void testSetStateZeroTimeout() throws Exception {
273: Case caze = WFNetUtil.createAutoFireCase();
274:
275: Map newState = new HashMap();
276: newState.put("A", new Integer(0));
277: newState.put("B", new Integer(1));
278: newState.put("C", new Integer(0));
279:
280: Map expected = new HashMap();
281: expected.put("A", new Integer(0));
282: expected.put("B", new Integer(0));
283: expected.put("C", new Integer(1));
284:
285: caze.setStateImpl(newState);
286: sameState(expected, caze.getState());
287: }
288:
289: public void testAutomaticCreation() throws Exception {
290: CaseType caseType = BossaTestUtil.createCaseType("test");
291: WorkItem wi = (WorkItem) caseType.getWorkItems(true).get(0);
292: Case template = wi.getCase();
293:
294: Activity a1 = template.open(wi, jdoe);
295: Case caze = a1.getCase();
296: assertEquals(1, caze.getId());
297:
298: assertTrue(caze.cancel(a1));
299:
300: Activity a2 = caze.open(caze.getWorkItem("a"), jdoe);
301: assertEquals(1, a2.getCase().getId());
302: }
303:
304: public void testWorkItensList() throws Exception {
305: Case caze = WFNetUtil.createCase(new int[] { 1, 1, 0, 0, 0, 0,
306: 0, 0 });
307: List workItens = caze.getWorkItems();
308: assertEquals(2, workItens.size());
309:
310: WorkItem w0 = (WorkItem) workItens.get(0);
311: WorkItem w1 = (WorkItem) workItens.get(1);
312: assertNotSame(w0, w1);
313: assertTrue("a".equals(w0.getId()) || "a".equals(w1.getId()));
314: assertTrue("b".equals(w0.getId()) || "b".equals(w1.getId()));
315: }
316:
317: public void testCanPerformWorkItem() throws BossaException {
318: CaseType caseType = new CaseType("canPerform");
319: Place A = caseType.registerPlace("A", 1);
320: Transition a = caseType.registerTransition("a", "bosses");
321: Transition b = caseType.registerTransition("b", "");
322: Transition c = caseType.registerTransition("c", null);
323: a.input(A, "1");
324: b.input(A, "1");
325: c.input(A, "1");
326: caseType.buildTemplate(null);
327: Resource mary = ResourceUtil.createResource("mary");
328: Resource bosses = (Resource) caseType.getResources().get(0);
329: assertEquals("bosses", bosses.getId());
330: bosses.includeImpl(jdoe, false);
331:
332: Case caze = caseType.openCaseImpl(null);
333: assertTrue(caze.getWorkItem("a").canBePerformedBy(jdoe));
334: assertFalse(caze.getWorkItem("a").canBePerformedBy(mary));
335: assertTrue(caze.getWorkItem("b").canBePerformedBy(jdoe));
336: assertTrue(caze.getWorkItem("c").canBePerformedBy(jdoe));
337: }
338:
339: public void testGetActivity() throws Exception {
340: Case caze = WFNetUtil.createCase();
341: WorkItem wi = caze.getWorkItem("a");
342: caze.open(wi, jdoe);
343:
344: Activity act = caze.getActivity(1);
345: assertNotNull(act);
346: assertEquals("a", act.getWorkItemId());
347: assertNull(caze.getActivity(2));
348: }
349:
350: public void testActivitiesList() throws Exception {
351: Case caze = WFNetUtil.createCase(new int[] { 2, 0, 0, 0, 0, 0,
352: 0, 0 });
353: WorkItem wi = caze.getWorkItem("a");
354:
355: caze.open(wi, jdoe);
356: assertEquals(1, caze.getActivities().size());
357:
358: caze.open(wi, jdoe);
359: List activities = caze.getActivities();
360: assertEquals(2, activities.size());
361:
362: Activity a0 = (Activity) activities.get(0);
363: Activity a1 = (Activity) activities.get(1);
364: assertNotSame(a0, a1);
365: assertSame(a0.getTransition(), a1.getTransition());
366: assertEquals(jdoe, a0.getResource());
367: }
368: }
|