1 package cz.cuni.amis.utils;
2
3 import java.util.HashSet;
4 import java.util.LinkedList;
5 import java.util.Queue;
6 import java.util.Set;
7
8
9
10
11
12
13
14
15
16 public class TestOutput {
17
18
19
20
21 private static boolean log = false;
22
23
24
25
26 private Queue<String> output = new LinkedList<String>();
27
28 private String name;
29
30 public TestOutput(String name) {
31 this.name = name;
32 }
33
34
35
36
37
38 public void push(String output) {
39 this.output.add(output);
40 }
41
42
43
44
45
46
47 public boolean consumeMany(String... outputs) {
48 for (String output : outputs) {
49 if (!consume(output)) return false;
50 }
51 return true;
52 }
53
54
55
56
57
58
59 public boolean consume(String output) {
60 if (log) System.out.println("CONSUME "+name+" expected : " + String.valueOf(output));
61 if (this.output.size() == 0) {
62 if (log) System.out.println("CONSUME "+name+": no output, can't consume: " + output);
63 return false;
64 }
65 if (output == null) {
66 String str = this.output.poll();
67 boolean result = str == null;
68 if (log)
69 if (result) {
70 System.out.println("CONSUME "+name+" got : "+String.valueOf(str));
71 } else {
72 System.out.println("CONSUME "+name+" got : "+String.valueOf(str));
73 }
74 return result;
75 }
76 String str = this.output.poll();
77 boolean result = output.equals(str);
78 if (log)
79 if (result) {
80 System.out.println("CONSUME "+name+" got : "+String.valueOf(str));
81 } else {
82 System.out.println("CONSUME "+name+" got : "+String.valueOf(str));
83 }
84 return result;
85 }
86
87
88
89
90
91
92 public boolean consume(String[] output) {
93 for (String str : output) {
94 if (!consume(str)) return false;
95 }
96 return true;
97 }
98
99
100
101
102
103
104
105 public boolean consumeAnyOrder(String[] output) {
106 Set<String> outputSet = new HashSet<String>();
107 for (String str : output) {
108 if (log) System.out.println("CONSUME expected : " + String.valueOf(str));
109 outputSet.add(str);
110 }
111 if (this.output.size() < output.length) {
112 if (log) {
113 System.out.println("CONSUME "+name+": not enough output (size = " + this.output.size() + "), needed " + output.length);
114 while (this.output.size() != 0) {
115 System.out.println("CONSUME "+name+" listing left output: " + String.valueOf(this.output.poll()));
116 }
117 }
118 return false;
119 }
120 for (int i = 0; i < output.length; ++i) {
121 String str = this.output.poll();
122 if (outputSet.contains(str)) {
123 if (log) System.out.println("CONSUME "+name+" got : "+str);
124 outputSet.remove(str);
125 } else {
126 if (log) {
127 System.out.println("CONSUME "+name+" got wrong: " + str);
128 while (this.output.size() != 0) {
129 System.out.println("CONSUME "+name+" listing left output: " + String.valueOf(this.output.poll()));
130 }
131 }
132 return false;
133 }
134 }
135 return true;
136 }
137
138
139
140
141
142 public boolean isClear(boolean printIfNot) {
143 if (log) System.out.println("OUTPUT "+name+" cleared?");
144 if (output.size() == 0) {
145 if (log) System.out.println("OUTPUT "+name+" yes");
146 return true;
147 } else {
148 if (log) System.out.println("OUTPUT "+name+" NO");
149 if (printIfNot && log) {
150 printOutput();
151 }
152 return false;
153 }
154 }
155
156
157
158
159 public void clear() {
160 output.clear();
161 }
162
163 public void printOutput() {
164 for (String str : this.output) {
165 System.out.println("OUTPUT " + name + ": " + str);
166 }
167 }
168
169
170
171
172
173 public static boolean isLog() {
174 return log;
175 }
176
177
178
179
180
181 public static void setLog(boolean log) {
182 TestOutput.log = log;
183 }
184
185 }