View Javadoc

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    * Used to produce text strings during tests, those strings are then consumed by the
10   * test case.
11   * <p><p>
12   * Used to control that everything is OK in tested objects / classes.
13   * 
14   * @author Jimmy
15   */
16  public class TestOutput {
17  	
18  	/**
19  	 * Flag whether to print the output to console.
20  	 */
21  	private static boolean log = false;
22  	
23  	/**
24  	 * Queue with strings somebody produce.
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  	 * Adds next String to the queue.
36  	 * @param output
37  	 */
38  	public void push(String output) {		
39  		this.output.add(output);
40  	}
41  	
42  	/**
43  	 * Consume many strings from the output - if not equals, returns false as failure.
44  	 * @param outputs
45  	 * @return
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  	 * Consume string symbol from the queue - false is failure (string didn't match).
56  	 * @param output
57  	 * @return
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  	 * Try to consume many strings, false - one of them didn't match, failure.
89  	 * @param output
90  	 * @return
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 	 * Consume output in any order, returns false if first output.length string in 
101 	 * the queue can't be matched to the output strings.
102 	 * @param output
103 	 * @return
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 	 * Whether the queue is clear.
140 	 * @return
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 	 * Clear the queue.
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 	 * Do we print the messages to the console (simple logging),
171 	 * @return
172 	 */
173 	public static boolean isLog() {
174 		return log;
175 	}
176 
177 	/**
178 	 * Set simple console logging.
179 	 * @param log
180 	 */
181 	public static void setLog(boolean log) {
182 		TestOutput.log = log;
183 	}
184 
185 }