1 /* 2 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 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 } 67 68 public static void reset() { 69 synchronized (lock) { 70 log.clear(); 71 startTime = System.currentTimeMillis(); 72 } 73 } 74 75 public static String timestamp() { 76 long time = System.currentTimeMillis() - startTime; 77 StringBuffer sb = new StringBuffer().append(time); 78 while (sb.length() < 4) { 79 sb.insert(0, "0"); 80 } 81 while (sb.length() < 8) { 82 sb.insert(0, " "); 83 } 84 sb.insert(sb.length() - 3, "."); 85 return sb.toString(); 86 } 87 88 public static Object getLock() { 89 return lock; 90 } 91 92 public static int countLog(String s, int startIndex, boolean exact) { 93 int count = 0; 94 for (int i = startIndex; i < log.size(); i++) { 95 String line = log.get(i); 96 if (exact) { 97 if (line.equals(s)) { 98 count ++; 99 } 100 } else { 101 if (line.indexOf(s) >= 0) { 102 count ++; 103 } 104 } 105 } 106 return count; 107 } 108 109 public static int countLog(String s) { 110 return countLog(s, 0, true); 111 } 112 113 public static int countLogContaining(String s) { 114 return countLog(s, 0, false); 115 } 116 117 private static String checkLog(String[] matches, int startIndex, boolean exact) { 118 for (int i = startIndex; i < log.size(); i++) { 119 String line = log.get(i); 120 if (matches.length == 1) { 121 if (exact) { 122 if (line.equals(matches[0])) { 123 return line; 124 } 125 } else { 126 if (line.indexOf(matches[0]) >= 0) { 127 return line; 128 } 129 } 130 } else { 131 boolean isMatch = true; 132 for (String match : matches) { 133 if (line.indexOf(match) < 0) { 134 isMatch = false; 135 break; 136 } 137 } 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 { 229 return waitForLog(new String [] {s}, timeout, false); 230 } 231 232 public static String waitForLog(String format, Object... args) throws InterruptedException { 233 return waitForLog(new Formatter().format(format, args).toString(), 234 DEFAULT_TIMEOUT); 235 } 236 237 public static String waitForLogContaining(String format, Object... args) throws InterruptedException { 238 return waitForLogContaining(new Formatter().format(format, args).toString(), 239 DEFAULT_TIMEOUT); 240 } 241 242 public static String waitForLogContainingSubstrings(String... s) throws InterruptedException { 243 return waitForLog(s, DEFAULT_TIMEOUT, false); 244 } 245 }