1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.test;
17
18 import org.kuali.rice.core.api.lifecycle.Lifecycle;
19
20 import java.lang.annotation.ElementType;
21 import java.lang.annotation.Inherited;
22 import java.lang.annotation.Retention;
23 import java.lang.annotation.RetentionPolicy;
24 import java.lang.annotation.Target;
25 import java.util.ArrayList;
26 import java.util.List;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public class BaselineTestCase extends BaseModuleTestCase {
54
55
56
57 public static enum Mode {
58 CLEAR_DB, ROLLBACK_CLEAR_DB, ROLLBACK, NONE
59 }
60
61 @Target({ElementType.TYPE})
62 @Inherited
63 @Retention(RetentionPolicy.RUNTIME)
64 public @interface BaselineMode {
65 Mode value();
66 }
67
68 private Mode mode = Mode.NONE;
69
70
71
72
73
74
75
76 protected static boolean dirty = false;
77
78
79 public BaselineTestCase(String moduleName) {
80 super(moduleName);
81 readModeAnnotation();
82 }
83
84
85
86
87 public BaselineTestCase(String moduleName, Mode mode) {
88 super(moduleName);
89 if (mode == null) throw new IllegalArgumentException("Mode cannot be null");
90 this.mode = mode;
91 }
92
93 private void readModeAnnotation() {
94 BaselineMode m = this.getClass().getAnnotation(BaselineMode.class);
95 if (m != null) {
96 if (m.value() != null) {
97 mode = m.value();
98 }
99 }
100 }
101
102
103
104
105 protected Mode getMode() {
106 return mode;
107 }
108
109
110
111
112
113 @Override
114 public void setUp() throws Exception {
115 super.setUp();
116 dirty = true;
117 }
118
119 @Override
120 protected List<Lifecycle> getPerTestLifecycles() {
121 switch (mode) {
122 case ROLLBACK_CLEAR_DB: return getRollbackClearDbPerTestLifecycles();
123 case ROLLBACK: return getRollbackTestLifecycles();
124 case CLEAR_DB: return getClearDbPerTestLifecycles();
125 case NONE: return super.getPerTestLifecycles();
126 default:
127 throw new RuntimeException("Invalid mode specified: " + mode);
128 }
129 }
130
131
132
133
134 protected List<Lifecycle> getClearDbPerTestLifecycles() {
135 List<Lifecycle> lifecycles = super.getPerTestLifecycles();
136 lifecycles.add(0, new ClearDatabaseLifecycle(getPerTestTablesToClear(), getPerTestTablesNotToClear()));
137 return lifecycles;
138 }
139
140 protected List<String> getPerTestTablesToClear() {
141 return new ArrayList<String>();
142 }
143
144 protected List<String> getPerTestTablesNotToClear() {
145 return new ArrayList<String>();
146 }
147
148
149
150
151 protected List<Lifecycle> getRollbackClearDbPerTestLifecycles() {
152 List<Lifecycle> lifecycles = super.getPerTestLifecycles();
153 lifecycles.add(0, new TransactionalLifecycle() {
154 @Override
155 public void stop() throws Exception {
156 super.stop();
157 dirty = false;
158 }
159
160 });
161
162
163 if (dirty) {
164 log.warn("Previous test case did not clean up the database; clearing database...");
165 lifecycles.add(0, new ClearDatabaseLifecycle(getPerTestTablesToClear(), getPerTestTablesNotToClear()));
166 }
167 return lifecycles;
168 }
169
170
171
172
173 protected List<Lifecycle> getRollbackTestLifecycles() {
174 List<Lifecycle> lifecycles = super.getPerTestLifecycles();
175 lifecycles.add(0, new TransactionalLifecycle());
176 return lifecycles;
177 }
178 }