001/* 002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Sun Seng David TAN <[email protected]> 018 */ 019package org.nuxeo.functionaltests; 020 021import org.apache.commons.logging.Log; 022import org.apache.commons.logging.LogFactory; 023 024public abstract class WaitUntil { 025 026 public static final Log log = LogFactory.getLog(WaitUntil.class); 027 028 long timeout; 029 030 public WaitUntil(long timeout) { 031 this.timeout = timeout; 032 } 033 034 public WaitUntil() { 035 // default value 2 secs 036 timeout = 2000; 037 } 038 039 public void waitUntil() { 040 long starttime = System.currentTimeMillis(); 041 Exception lastException = null; 042 043 while (starttime > System.currentTimeMillis() - timeout) { 044 try { 045 if (condition()) { 046 return; 047 } 048 Thread.sleep(100); 049 lastException = null; 050 } catch (Exception e) { 051 log.warn("An exception while testing condition", e); 052 lastException = e; 053 } 054 055 } 056 throw new RuntimeException("Couldn't find element", lastException); 057 } 058 059 public abstract boolean condition(); 060}