01: /*
02: * $Id: AbstractActivityTestCase.java 654 2006-01-25 09:11:56Z hengels $
03: * (c) Copyright 2004 con:cern development team.
04: *
05: * This file is part of con:cern (http://concern.org).
06: *
07: * con:cern is free software; you can redistribute it and/or modify
08: * it under the terms of the GNU Lesser General Public License
09: * as published by the Free Software Foundation; either version 2.1
10: * of the License, or (at your option) any later version.
11: *
12: * Please see COPYING for the complete licence.
13: */
14: package org.concern.controller.test;
15:
16: import junit.framework.TestCase;
17: import org.concern.Controller;
18:
19: public abstract class AbstractActivityTestCase extends TestCase {
20: protected TestController controller;
21: protected Object subject;
22:
23: public AbstractActivityTestCase(String name) {
24: super (name);
25: }
26:
27: protected void setUp() throws Exception {
28: controller = createController();
29: if (controller == null)
30: throw new NullPointerException(
31: "non null controller required");
32: try {
33: controller.getTransactionManager().begin();
34: subject = createSubject();
35: if (subject == null)
36: throw new NullPointerException(
37: "non null subject required");
38: String userValue = controller.getLoader().idOf(subject);
39: controller.createSubject(userValue);
40: } catch (Exception e) {
41: e.printStackTrace();
42: throw e;
43: } finally {
44: controller.getTransactionManager().commit();
45: }
46: }
47:
48: protected void tearDown() throws Exception {
49: try {
50: controller.getTransactionManager().begin();
51: String userValue = controller.getLoader().idOf(subject);
52: if (controller.getSubjects(Controller.STATE_RUNNING)
53: .contains(userValue))
54: controller.destroySubject(userValue);
55: destroySubject(subject);
56: } catch (Exception e) {
57: e.printStackTrace();
58: throw e;
59: } finally {
60: controller.getTransactionManager().commit();
61: }
62: controller.stop();
63: }
64:
65: public void testExecution() throws Exception {
66: try {
67: controller.getTransactionManager().begin();
68: String userValue = controller.getLoader().idOf(subject);
69: assertTrue("precondition of " + getActivityName()
70: + " doesn't match", controller.matchPrecondition(
71: userValue, getActivityName()));
72: controller.executeSynchronousActivity(userValue,
73: getActivityName());
74: assertTrue("postcondition of " + getActivityName()
75: + " doesn't match", controller.matchPostcondition(
76: userValue, getActivityName()));
77: checkForSideEffects(userValue);
78: } catch (Exception e) {
79: e.printStackTrace();
80: throw e;
81: } finally {
82: controller.getTransactionManager().commit();
83: }
84: }
85:
86: protected abstract void checkForSideEffects(String userValue)
87: throws Exception;
88:
89: protected abstract TestController createController()
90: throws Exception;
91:
92: protected abstract Object createSubject() throws Exception;
93:
94: protected abstract void destroySubject(Object subject)
95: throws Exception;
96:
97: protected abstract String getActivityName();
98: }
|