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 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     }
  71 
  72     public static void reset() {
  73         synchronized (lock) {
  74             log.clear();
  75             startTime = System.currentTimeMillis();
  76         }
  77     }
  78 
  79     public static String timestamp() {
  80         long time = System.currentTimeMillis() - startTime;
  81         StringBuffer sb = new StringBuffer().append(time);
  82         while (sb.length() < 4) {
  83             sb.insert(0, "0");
  84         }
  85         while (sb.length() < 8) {
  86             sb.insert(0, " ");
  87         }
  88         sb.insert(sb.length() - 3, ".");
  89         return sb.toString();
  90     }
  91 
  92     public static Object getLock() {
  93         return lock;
  94     }
  95 
  96     public static int countLog(String s, int startIndex, boolean exact) {
  97         int count = 0;
  98         for (int i = startIndex; i < log.size(); i++) {
  99             String line  = log.get(i);
 100             if (exact) {
 101                 if (line.equals(s)) {
 102                     count ++;
 103                 }
 104             } else {
 105                 if (line.indexOf(s) >= 0) {
 106                     count ++;
 107                 }
 108             }
 109         }
 110         return count;
 111     }
 112 
 113     public static int countLog(String s) {
 114         return countLog(s, 0, true);
 115     }
 116 
 117     public static int countLogContaining(String s) {
 118         return countLog(s, 0, false);
 119     }
 120 
 121     private static String checkLog(String[] matches, int startIndex, boolean exact) {
 122         for (int i = startIndex; i < log.size(); i++) {
 123             String line  = log.get(i);
 124             if (matches.length == 1) {
 125                 if (exact) {
 126                     if (line.equals(matches[0])) {
 127                         return line;
 128                     }
 129                 } else {
 130                     if (line.indexOf(matches[0]) >= 0) {
 131                         return line;
 132                     }
 133                 }
 134             } else {
 135                 boolean isMatch = true;
 136                 for (String match : matches) {
 137                     if (line.indexOf(match) < 0) {
 138                         isMatch = false;
 139                         break;
 140                     }
 141                 }
 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 {
 233         return waitForLog(new String [] {s}, timeout, false);
 234     }
 235 
 236     public static String waitForLog(String format, Object... args) throws InterruptedException {
 237         return waitForLog(new Formatter().format(format, args).toString(),
 238                           DEFAULT_TIMEOUT);
 239     }
 240 
 241     public static String waitForLogContaining(String format, Object... args) throws InterruptedException {
 242         return waitForLogContaining(new Formatter().format(format, args).toString(),
 243                           DEFAULT_TIMEOUT);
 244     }
 245 
 246     public static String waitForLogContainingSubstrings(String... s) throws InterruptedException {
 247         return waitForLog(s, DEFAULT_TIMEOUT, false);
 248     }
 249 }