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