|
1 |
| package com.agical.rmock.core.expectation.section; |
|
2 |
| |
|
3 |
| import java.util.ArrayList; |
|
4 |
| import java.util.Iterator; |
|
5 |
| import java.util.List; |
|
6 |
| |
|
7 |
| import com.agical.rmock.core.ExpectationVisitor; |
|
8 |
| import com.agical.rmock.core.Section; |
|
9 |
| import com.agical.rmock.core.Visitable; |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| public class OrderedSection implements Section { |
|
18 |
| private List visitables = new ArrayList(); |
|
19 |
| private Visitable currentVisitable; |
|
20 |
| private final String description; |
|
21 |
| |
|
22 |
75
| public OrderedSection(String description) {
|
|
23 |
75
| this.description = description;
|
|
24 |
| } |
|
25 |
| |
|
26 |
157
| public void add(Visitable visitable) {
|
|
27 |
157
| this.visitables.add( visitable );
|
|
28 |
157
| if(currentVisitable == null) {
|
|
29 |
58
| currentVisitable = visitable;
|
|
30 |
| } |
|
31 |
| } |
|
32 |
| |
|
33 |
172
| public boolean accept(ExpectationVisitor visitableVisitor, boolean available) {
|
|
34 |
172
| visitableVisitor.enteredSection(this);
|
|
35 |
172
| boolean visitableIsCurrentVisitable = false;
|
|
36 |
172
| boolean lastWasCurrentVisitable = false;
|
|
37 |
172
| for (Iterator iter = visitables.iterator(); iter.hasNext();) {
|
|
38 |
437
| Visitable visitable = (Visitable) iter.next();
|
|
39 |
437
| boolean canMatch = visitable.canMatch();
|
|
40 |
437
| if(visitable == currentVisitable) {
|
|
41 |
172
| visitableIsCurrentVisitable = true;
|
|
42 |
| |
|
43 |
| } |
|
44 |
437
| if (lastWasCurrentVisitable && currentVisitable.isSatisfied()){
|
|
45 |
1
| currentVisitable = visitable;
|
|
46 |
1
| visitableIsCurrentVisitable = true;
|
|
47 |
| } |
|
48 |
437
| boolean continueMatch = visitable.accept(visitableVisitor, available && canMatch && visitable == currentVisitable);
|
|
49 |
437
| if (!continueMatch) {
|
|
50 |
152
| if(iter.hasNext() && !visitable.canMatch()) {
|
|
51 |
95
| currentVisitable = (Visitable) iter.next();
|
|
52 |
| } |
|
53 |
152
| return false;
|
|
54 |
| } |
|
55 |
| |
|
56 |
285
| lastWasCurrentVisitable = visitableIsCurrentVisitable;
|
|
57 |
| } |
|
58 |
20
| visitableVisitor.exitedSection();
|
|
59 |
20
| return true;
|
|
60 |
| } |
|
61 |
| |
|
62 |
| |
|
63 |
18
| public boolean canMatch() {
|
|
64 |
18
| return currentVisitable == null ? false : currentVisitable.canMatch();
|
|
65 |
| } |
|
66 |
| |
|
67 |
87
| public boolean isSatisfied() {
|
|
68 |
87
| if (currentVisitable == null) {
|
|
69 |
2
| return true;
|
|
70 |
| } |
|
71 |
85
| boolean satisfied = currentVisitable.isSatisfied();
|
|
72 |
85
| if (satisfied) {
|
|
73 |
83
| Iterator visitableIterator = visitables.iterator();
|
|
74 |
83
| Visitable visitable;
|
|
75 |
| |
|
76 |
83
| positionIteratorOnCurrent(visitableIterator);
|
|
77 |
| |
|
78 |
| |
|
79 |
83
| while(satisfied && visitableIterator.hasNext()) {
|
|
80 |
2
| satisfied = ((Visitable)visitableIterator.next()).isSatisfied();
|
|
81 |
| } |
|
82 |
| } |
|
83 |
| |
|
84 |
85
| return satisfied;
|
|
85 |
| } |
|
86 |
| |
|
87 |
83
| private void positionIteratorOnCurrent(Iterator visitableIterator) {
|
|
88 |
83
| Visitable visitable;
|
|
89 |
83
| do {
|
|
90 |
261
| visitable = (Visitable) visitableIterator.next();
|
|
91 |
| } |
|
92 |
261
| while(visitable != currentVisitable);
|
|
93 |
| } |
|
94 |
| |
|
95 |
104
| public String getDescription() {
|
|
96 |
104
| return description;
|
|
97 |
| } |
|
98 |
| |
|
99 |
2
| public String getType() {
|
|
100 |
2
| return "Ordered section";
|
|
101 |
| } |
|
102 |
| } |