|
1 |
| package com.agical.rmock.extension.junit; |
|
2 |
| |
|
3 |
| import java.util.Iterator; |
|
4 |
| |
|
5 |
| import junit.framework.TestResult; |
|
6 |
| import junit.framework.TestSuite; |
|
7 |
| |
|
8 |
| import com.agical.rmock.core.configuration.Configurator; |
|
9 |
| import com.agical.rmock.core.configuration.ConfiguratorImpl; |
|
10 |
| import com.agical.rmock.core.event.TestStructureListener; |
|
11 |
| import com.agical.rmock.core.event.TestSuiteListener; |
|
12 |
| import com.agical.rmock.core.find.CollectionSelector; |
|
13 |
| import com.agical.rmock.core.find.Finder; |
|
14 |
| import com.agical.rmock.core.find.MatchAction; |
|
15 |
| import com.agical.rmock.core.find.iterator.FileSystemClassIterator; |
|
16 |
| import com.agical.rmock.core.hub.Hub; |
|
17 |
| import com.agical.rmock.core.match.Expression; |
|
18 |
| import com.agical.rmock.core.match.constraint.ConstraintFactory; |
|
19 |
| |
|
20 |
| public abstract class DynamicTestSuite extends TestSuite{ |
|
21 |
| private TestSuiteListener testSuiteListener; |
|
22 |
| protected final ConstraintFactory is = new ConstraintFactory(); |
|
23 |
2
| protected Iterator allClasses() {
|
|
24 |
2
| return new FileSystemClassIterator(getClass());
|
|
25 |
| } |
|
26 |
| private Finder finder = new Finder(); |
|
27 |
| protected final MatchAction addAllToSuite = new AddToTestSuiteAction(this); |
|
28 |
| private final Configurator configurator; |
|
29 |
| private boolean suiteIsSetup; |
|
30 |
| private TestStructureListener testStructureListener; |
|
31 |
| private final String testStructureName; |
|
32 |
| |
|
33 |
1
| public DynamicTestSuite(String name) {
|
|
34 |
1
| this(name, ConfiguratorImpl.getInstance());
|
|
35 |
| } |
|
36 |
| |
|
37 |
3
| public DynamicTestSuite(String name, Configurator configurator) {
|
|
38 |
3
| this(name, configurator, null);
|
|
39 |
| } |
|
40 |
| |
|
41 |
4
| public DynamicTestSuite(String name, Configurator configurator, String testStructureName) {
|
|
42 |
4
| super(name);
|
|
43 |
4
| this.configurator = configurator;
|
|
44 |
4
| this.testStructureName = testStructureName;
|
|
45 |
| } |
|
46 |
| |
|
47 |
| protected abstract void setupSuite(); |
|
48 |
| |
|
49 |
2
| protected CollectionSelector forEach(Expression expression) {
|
|
50 |
2
| return finder.forEach(expression);
|
|
51 |
| } |
|
52 |
| |
|
53 |
2
| public void setTestSuiteListener(TestSuiteListener testSuiteListener) {
|
|
54 |
2
| this.testSuiteListener = testSuiteListener;
|
|
55 |
| } |
|
56 |
| |
|
57 |
2
| public int countTestCases() {
|
|
58 |
2
| lazySetupSuite();
|
|
59 |
2
| return super.countTestCases();
|
|
60 |
| } |
|
61 |
| |
|
62 |
3
| public void run(TestResult result) {
|
|
63 |
3
| lazySetupSuite();
|
|
64 |
3
| Hub hub = configurator.getHub();
|
|
65 |
3
| hub.beginScope();
|
|
66 |
3
| hub.connect(this);
|
|
67 |
3
| try {
|
|
68 |
3
| fireBeforeTestStructure();
|
|
69 |
3
| fireBeforeTestSuite();
|
|
70 |
3
| super.run(result);
|
|
71 |
3
| fireAfterTestSuite();
|
|
72 |
3
| fireAfterTestStructure();
|
|
73 |
| } |
|
74 |
| finally { |
|
75 |
3
| hub.endScope();
|
|
76 |
| } |
|
77 |
| } |
|
78 |
| |
|
79 |
3
| private void fireAfterTestStructure() {
|
|
80 |
3
| if( testStructureListener != null && testStructureName != null ) {
|
|
81 |
1
| testStructureListener.afterStructure();
|
|
82 |
| } |
|
83 |
| |
|
84 |
| } |
|
85 |
| |
|
86 |
3
| private void fireBeforeTestStructure() {
|
|
87 |
3
| if( testStructureListener != null && testStructureName != null ) {
|
|
88 |
1
| testStructureListener.beforeStructure(testStructureName);
|
|
89 |
| } |
|
90 |
| } |
|
91 |
| |
|
92 |
5
| private void lazySetupSuite() {
|
|
93 |
5
| if(!suiteIsSetup) {
|
|
94 |
4
| setupSuite();
|
|
95 |
4
| suiteIsSetup = true;
|
|
96 |
| } |
|
97 |
| } |
|
98 |
| |
|
99 |
3
| private void fireBeforeTestSuite() {
|
|
100 |
3
| if (testSuiteListener != null) {
|
|
101 |
2
| testSuiteListener.beforeSuite(this, getName());
|
|
102 |
| } |
|
103 |
| } |
|
104 |
| |
|
105 |
3
| private void fireAfterTestSuite() {
|
|
106 |
3
| if (testSuiteListener != null) {
|
|
107 |
2
| testSuiteListener.afterSuite();
|
|
108 |
| } |
|
109 |
| } |
|
110 |
| |
|
111 |
2
| public void setTestStructureListener(TestStructureListener testStructureListener) {
|
|
112 |
2
| this.testStructureListener = testStructureListener;
|
|
113 |
| } |
|
114 |
| } |