1 package cz.cuni.amis.tests;
2
3 import java.util.HashSet;
4 import java.util.LinkedList;
5 import java.util.List;
6 import java.util.Set;
7 import java.util.logging.Logger;
8
9
10
11
12
13
14
15
16
17 public class ActivityLog {
18
19 private static final String NEW_LINE = System.getProperty("line.separator");
20
21 private static interface IActivityCheck {
22
23 public void add(String... activities);
24
25 public void check(String activity);
26
27 public boolean checkNoException(String activity);
28
29 public boolean isEmpty();
30
31 }
32
33 private static abstract class AbstractCheck implements IActivityCheck {
34
35 public void check(String activity) {
36 if (checkNoException(activity)) return;
37 throw new RuntimeException("Unexpected activity: " + activity + NEW_LINE + this);
38 }
39
40 }
41
42 private static class AnyOrder extends AbstractCheck {
43
44 private Set<String> activities = new HashSet<String>();
45
46 public AnyOrder(String... activities) {
47 add(activities);
48 }
49
50 @Override
51 public void add(String... activities) {
52 for (String activity : activities) {
53 this.activities.add(activity);
54 }
55 }
56
57 public String toString() {
58 StringBuffer sb = new StringBuffer();
59 sb.append("ActivityAnyOrder[");
60 if (activities.size() == 0) {
61 sb.append("NOTHING! activity.size() == 0");
62 } else {
63 for (String a : activities) {
64 sb.append(NEW_LINE);
65 sb.append(a);
66 }
67 }
68 sb.append(NEW_LINE);
69 sb.append("]");
70 return sb.toString();
71 }
72
73 @Override
74 public boolean checkNoException(String activity) {
75 if (this.activities.contains(activity)) {
76 this.activities.remove(activity);
77 return true;
78 } else {
79 return false;
80 }
81 }
82
83 @Override
84 public boolean isEmpty() {
85 return this.activities.size() == 0;
86 }
87
88 }
89
90 private static class ExactOrder extends AbstractCheck {
91
92 private List<String> activities = new LinkedList<String>();
93
94 public ExactOrder(String... activities) {
95 add(activities);
96 }
97
98 @Override
99 public void add(String... activities) {
100 for (String activity : activities) {
101 this.activities.add(activity);
102 }
103 }
104
105 public String toString() {
106 StringBuffer sb = new StringBuffer();
107 sb.append("ActivityExactOrder[");
108 if (activities.size() == 0) {
109 sb.append(" NOTHING! activity.size() == 0");
110 } else {
111 for (String a : activities) {
112 sb.append(NEW_LINE);
113 sb.append(" ");
114 sb.append(a);
115 }
116 }
117 sb.append(NEW_LINE);
118 sb.append("]");
119 return sb.toString();
120 }
121
122 @Override
123 public boolean checkNoException(String activity) {
124 if (isEmpty()) return false;
125 String checkingActivity = this.activities.get(0);
126 if (checkingActivity.equals(activity)) {
127 this.activities.remove(0);
128 return true;
129 } else {
130 return false;
131 }
132 }
133
134 @Override
135 public boolean isEmpty() {
136 return this.activities.size() == 0;
137 }
138
139 }
140
141 private List<IActivityCheck> activities = new LinkedList<IActivityCheck>();
142
143 private Logger log;
144
145 public ActivityLog(Logger log) {
146 this.log = log;
147 }
148
149 public ActivityLog expectAnyOrder(String... activities) {
150 if (activities == null || activities.length == 0) return this;
151 this.activities.add(new AnyOrder(activities));
152 return this;
153 }
154
155 public ActivityLog expectExactOrder(String... activities) {
156 if (activities == null || activities.length == 0) return this;
157 this.activities.add(new ExactOrder(activities));
158 return this;
159 }
160
161 public synchronized void activity(String... activity) {
162 for (String a : activity) {
163 if (activityNoException(a)) {
164 continue;
165 }
166 throw new RuntimeException("Unexpected activity: " + a + NEW_LINE + this);
167 }
168 }
169
170 public synchronized boolean activityNoException(String... activity) {
171 for (String a : activity) {
172 if (isEmpty()) return false;
173 if (activities.get(0).isEmpty()) {
174 activities.remove(0);
175 if (isEmpty()) return false;
176 }
177 if (!activities.get(0).checkNoException(a)) return false;
178 log.info("Expected activity: " + a);
179 while (!isEmpty() && activities.get(0).isEmpty()) activities.remove(0);
180 }
181 return true;
182 }
183
184 public boolean isEmpty() {
185 return this.activities.size() == 0;
186 }
187
188 public String toString() {
189 StringBuffer sb = new StringBuffer();
190 sb.append("ActivityLog[");
191 if (activities.size() == 0) {
192 sb.append(NEW_LINE);
193 sb.append("No more activities expected!");
194 } else {
195 for (IActivityCheck a : activities) {
196 sb.append(NEW_LINE);
197 sb.append(a);
198 }
199 }
200 sb.append(NEW_LINE);
201 sb.append("]");
202 return sb.toString();
203 }
204
205 public void checkNoMoreActivityExpected() {
206 if (isEmpty()) {
207 log.info("No more activity expected, OK!");
208 return;
209 }
210 throw new RuntimeException("More activity expected!" + NEW_LINE + this);
211 }
212
213 }