17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.glass.ui.monocle;
27
28 import junit.framework.AssertionFailedError;
29
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.Formatter;
33 import java.util.List;
34
35 public class TestLog {
36
37 private static final long DEFAULT_TIMEOUT = 3000l;
38
39 private static final List<String> log = new ArrayList<>();
40 private static final Object lock = new Object();
41
42 private static long startTime = System.currentTimeMillis();
43
44 public static void log(String s) {
45 synchronized (lock) {
46 if (TestApplication.isVerbose()) {
47 System.out.println(timestamp() + " TestLog: " + s);
48 }
49 log.add(s);
50 lock.notifyAll();
51 }
52 }
53
54 public static void format(String format, Object... args) {
55 log(new Formatter().format(format, args).toString());
56 }
57
58 public static List<String> getLog() {
59 return new ArrayList<>(log);
60 }
61
62 public static void clear() {
63 synchronized (lock) {
64 log.clear();
65 }
66 }
138 if (isMatch) {
139 return line;
140 }
141 }
142 }
143 return null;
144 }
145
146 public static boolean checkLog(String s) {
147 return checkLog(new String[] {s}, 0, true) != null;
148 }
149
150 public static boolean checkLogContaining(String s) {
151 return checkLog(new String[] {s}, 0, false) != null;
152 }
153
154 public static void assertLog(String s) {
155 synchronized (lock) {
156 if (!checkLog(s)) {
157 String err = "No line '" + s + "' in log";
158 if (TestApplication.isVerbose()) {
159 System.out.println(err);
160 }
161 throw new AssertionFailedError(err);
162 }
163 }
164 }
165
166 public static void assertLogContaining(String s) {
167 synchronized (lock) {
168 if (!checkLogContaining(s)) {
169 String err = "No line containing '" + s + "' in log";
170 if (TestApplication.isVerbose()) {
171 System.out.println(err);
172 }
173 throw new AssertionFailedError(err);
174 }
175 }
176 }
177
178 private static String waitForLog(String[] s, long timeout, boolean exact) throws InterruptedException {
179 long startTime = System.currentTimeMillis();
180 long timeNow = startTime;
181 long endTime = timeNow + (long) (timeout * TestApplication.getTimeScale());
182 String line;
183 String logString = Arrays.toString(s).substring(1, Arrays.toString(s).length() - 1);
184 synchronized (lock) {
185 int index = 0;
186 while ((line = checkLog(s, index, exact)) == null) {
187 index = log.size();
188 if (endTime - timeNow > 0) {
189 lock.wait(endTime - timeNow);
190 }
191 timeNow = System.currentTimeMillis();
192 if (timeNow >= endTime) {
193 String message = "Timed out after " + (timeNow - startTime)
194 + "ms waiting for '" + logString + "'";
195 if (!TestApplication.isVerbose()) {
196 System.out.flush();
197 System.err.flush();
198 for (String logLine: log) {
199 System.out.println(logLine);
200 }
201 }
202 System.out.println(message);
203 throw new AssertionFailedError(message);
204 }
205 }
206 }
207 long matchTime = System.currentTimeMillis() - startTime;
208 if (TestApplication.isVerbose()) {
209 if (exact) {
210 System.out.println("TestLog matched '"
211 + logString + "' in "
212 + matchTime + "ms");
213
214 } else {
215 System.out.println("TestLog matched '"
216 + logString + "' with '"
217 + line + "' in "
218 + matchTime + "ms");
219 }
220 }
221 return line;
222 }
223
224 public static String waitForLog(String s, long timeout) throws InterruptedException {
225 return waitForLog(new String [] {s}, timeout, true);
226 }
227
228 public static String waitForLogContaining(String s, long timeout) throws InterruptedException {
|
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.glass.ui.monocle;
27
28 import junit.framework.AssertionFailedError;
29
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.Formatter;
33 import java.util.List;
34
35 public class TestLog {
36
37 private static final boolean verbose = Boolean.getBoolean("verbose");
38 private static final double timeScale = Double.parseDouble(
39 System.getProperty("timeScale", "1"));
40
41 private static final long DEFAULT_TIMEOUT = 3000l;
42
43 private static final List<String> log = new ArrayList<>();
44 private static final Object lock = new Object();
45
46 private static long startTime = System.currentTimeMillis();
47
48 public static void log(String s) {
49 synchronized (lock) {
50 if (verbose) {
51 System.out.println(timestamp() + " TestLog: " + s);
52 }
53 log.add(s);
54 lock.notifyAll();
55 }
56 }
57
58 public static void format(String format, Object... args) {
59 log(new Formatter().format(format, args).toString());
60 }
61
62 public static List<String> getLog() {
63 return new ArrayList<>(log);
64 }
65
66 public static void clear() {
67 synchronized (lock) {
68 log.clear();
69 }
70 }
142 if (isMatch) {
143 return line;
144 }
145 }
146 }
147 return null;
148 }
149
150 public static boolean checkLog(String s) {
151 return checkLog(new String[] {s}, 0, true) != null;
152 }
153
154 public static boolean checkLogContaining(String s) {
155 return checkLog(new String[] {s}, 0, false) != null;
156 }
157
158 public static void assertLog(String s) {
159 synchronized (lock) {
160 if (!checkLog(s)) {
161 String err = "No line '" + s + "' in log";
162 if (verbose) {
163 System.out.println(err);
164 }
165 throw new AssertionFailedError(err);
166 }
167 }
168 }
169
170 public static void assertLogContaining(String s) {
171 synchronized (lock) {
172 if (!checkLogContaining(s)) {
173 String err = "No line containing '" + s + "' in log";
174 if (verbose) {
175 System.out.println(err);
176 }
177 throw new AssertionFailedError(err);
178 }
179 }
180 }
181
182 private static String waitForLog(String[] s, long timeout, boolean exact) throws InterruptedException {
183 long startTime = System.currentTimeMillis();
184 long timeNow = startTime;
185 long endTime = timeNow + (long) (timeout * timeScale);
186 String line;
187 String logString = Arrays.toString(s).substring(1, Arrays.toString(s).length() - 1);
188 synchronized (lock) {
189 int index = 0;
190 while ((line = checkLog(s, index, exact)) == null) {
191 index = log.size();
192 if (endTime - timeNow > 0) {
193 lock.wait(endTime - timeNow);
194 }
195 timeNow = System.currentTimeMillis();
196 if (timeNow >= endTime) {
197 String message = "Timed out after " + (timeNow - startTime)
198 + "ms waiting for '" + logString + "'";
199 if (!verbose) {
200 System.out.flush();
201 System.err.flush();
202 for (String logLine: log) {
203 System.out.println(logLine);
204 }
205 }
206 System.out.println(message);
207 throw new AssertionFailedError(message);
208 }
209 }
210 }
211 long matchTime = System.currentTimeMillis() - startTime;
212 if (verbose) {
213 if (exact) {
214 System.out.println("TestLog matched '"
215 + logString + "' in "
216 + matchTime + "ms");
217
218 } else {
219 System.out.println("TestLog matched '"
220 + logString + "' with '"
221 + line + "' in "
222 + matchTime + "ms");
223 }
224 }
225 return line;
226 }
227
228 public static String waitForLog(String s, long timeout) throws InterruptedException {
229 return waitForLog(new String [] {s}, timeout, true);
230 }
231
232 public static String waitForLogContaining(String s, long timeout) throws InterruptedException {
|