< prev index next >

modules/graphics/src/test/java/com/sun/glass/ui/monocle/TestLogShim.java

Print this page
rev 9504 : need to fix test log and others


   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) {


 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 }


   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 java.util.ArrayList;
  29 import java.util.Arrays;
  30 import java.util.Formatter;
  31 import java.util.List;
  32 
  33 public class TestLogShim {
  34 
  35     private static final boolean verbose = Boolean.getBoolean("verbose");
  36     private static final double timeScale = Double.parseDouble(
  37             System.getProperty("timeScale", "1"));
  38 
  39     private static final long DEFAULT_TIMEOUT = 3000l;
  40 
  41     private static final List<String> log = new ArrayList<>();
  42     private static final Object lock = new Object();
  43 
  44     private static long startTime = System.currentTimeMillis();
  45 
  46     public static class TestLogAssertion extends Exception {
  47         public TestLogAssertion(String message) {
  48             super(message);
  49         }
  50     }
  51 
  52     public static void log(String s) {
  53         synchronized (lock) {
  54             if (verbose) {
  55                 System.out.println(timestamp() + " TestLog: " + s);
  56             }
  57             log.add(s);
  58             lock.notifyAll();
  59         }
  60     }
  61 
  62     public static void format(String format, Object... args) {
  63         log(new Formatter().format(format, args).toString());
  64     }
  65 
  66     public static  List<String> getLog() {
  67         return new ArrayList<>(log);
  68     }
  69 
  70     public static void clear() {
  71         synchronized (lock) {


 142                         isMatch = false;
 143                         break;
 144                     }
 145                 }
 146                 if (isMatch) {
 147                     return line;
 148                 }
 149             }
 150         }
 151         return null;
 152     }
 153 
 154     public static boolean checkLog(String s) {
 155         return checkLog(new String[] {s}, 0, true) != null;
 156     }
 157 
 158     public static boolean checkLogContaining(String s) {
 159         return checkLog(new String[] {s}, 0, false) != null;
 160     }
 161 
 162     public static void assertLog(String s) throws TestLogAssertion {
 163         synchronized (lock) {
 164             if (!checkLog(s)) {
 165                 String err = "No line '" + s + "' in log";
 166                 if (verbose) {
 167                     System.out.println(err);
 168                 }
 169                 throw new TestLogAssertion(err);
 170             }
 171         }
 172     }
 173 
 174     public static void assertLogContaining(String s) throws TestLogAssertion {
 175         synchronized (lock) {
 176             if (!checkLogContaining(s)) {
 177                 String err = "No line containing '" + s + "' in log";
 178                 if (verbose) {
 179                     System.out.println(err);
 180                 }
 181                 throw new TestLogAssertion(err);
 182             }
 183         }
 184     }
 185 
 186     private static String waitForLog(String[] s, long timeout, boolean exact) throws InterruptedException, TestLogAssertion {
 187         long startTime = System.currentTimeMillis();
 188         long timeNow = startTime;
 189         long endTime = timeNow + (long) (timeout * timeScale);
 190         String line;
 191         String logString = Arrays.toString(s).substring(1, Arrays.toString(s).length() - 1);
 192         synchronized (lock) {
 193             int index = 0;
 194             while ((line = checkLog(s, index, exact)) == null) {
 195                 index = log.size();
 196                 if (endTime - timeNow > 0) {
 197                     lock.wait(endTime - timeNow);
 198                 }
 199                 timeNow = System.currentTimeMillis();
 200                 if (timeNow >= endTime) {
 201                     String message = "Timed out after " + (timeNow - startTime)
 202                             + "ms waiting for '" + logString + "'";
 203                     if (!verbose) {
 204                         System.out.flush();
 205                         System.err.flush();
 206                         for (String logLine: log) {
 207                             System.out.println(logLine);
 208                         }
 209                     }
 210                     System.out.println(message);
 211                     throw new TestLogAssertion(message);
 212                 }
 213             }
 214         }
 215         long matchTime = System.currentTimeMillis() - startTime;
 216         if (verbose) {
 217             if (exact) {
 218                 System.out.println("TestLog matched '"
 219                         + logString + "' in "
 220                         + matchTime + "ms");
 221 
 222             } else {
 223                 System.out.println("TestLog matched '"
 224                         + logString + "' with '"
 225                         + line + "' in "
 226                         + matchTime + "ms");
 227             }
 228         }
 229         return line;
 230     }
 231 
 232     public static String waitForLog(String s, long timeout) throws InterruptedException, TestLogAssertion {
 233         return waitForLog(new String [] {s}, timeout, true);
 234     }
 235 
 236     public static String waitForLogContaining(String s, long timeout) throws InterruptedException, TestLogAssertion {
 237         return waitForLog(new String [] {s}, timeout, false);
 238     }
 239 
 240     public static String waitForLog(String format, Object... args) throws InterruptedException, TestLogAssertion {
 241         return waitForLog(new Formatter().format(format, args).toString(),
 242                           DEFAULT_TIMEOUT);
 243     }
 244 
 245     public static String waitForLogContaining(String format, Object... args) throws InterruptedException, TestLogAssertion {
 246         return waitForLogContaining(new Formatter().format(format, args).toString(),
 247                           DEFAULT_TIMEOUT);
 248     }
 249 
 250     public static String waitForLogContainingSubstrings(String... s) throws InterruptedException, TestLogAssertion {
 251         return waitForLog(s, DEFAULT_TIMEOUT, false);
 252     }
 253 }
< prev index next >