Changeset 10569 in josm for trunk/test/unit/org/openstreetmap
- Timestamp:
- 2016-07-20T00:25:18+02:00 (8 years ago)
- Location:
- trunk/test/unit/org/openstreetmap/josm
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java
r10553 r10569 4 4 import java.io.File; 5 5 import java.io.IOException; 6 import java.text.MessageFormat; 6 7 import java.util.TimeZone; 7 8 8 import org.junit.rules.DisableOnDebug;9 9 import org.junit.rules.TemporaryFolder; 10 10 import org.junit.rules.TestRule; 11 import org.junit.rules.Timeout;12 11 import org.junit.runner.Description; 13 12 import org.junit.runners.model.InitializationError; … … 33 32 */ 34 33 public class JOSMTestRules implements TestRule { 35 private Timeout timeout = Timeout.seconds(10);34 private int timeout = 10 * 1000; 36 35 private TemporaryFolder josmHome; 37 36 private boolean usePreferences = false; … … 46 45 */ 47 46 public JOSMTestRules noTimeout() { 48 timeout = null;47 timeout = -1; 49 48 return this; 50 49 } … … 56 55 */ 57 56 public JOSMTestRules timeout(int millis) { 58 timeout = Timeout.millis(millis);57 timeout = millis; 59 58 return this; 60 59 } … … 135 134 136 135 @Override 137 public Statement apply(final Statement base, Description description) { 138 Statement statement = new Statement() { 139 @Override 140 public void evaluate() throws Throwable { 141 before(); 142 try { 143 base.evaluate(); 144 } finally { 145 after(); 146 } 147 } 148 }; 149 if (timeout != null) { 150 statement = new DisableOnDebug(timeout).apply(statement, description); 151 } 136 public Statement apply(Statement base, Description description) { 137 Statement statement = base; 138 if (timeout > 0) { 139 // TODO: new DisableOnDebug(timeout) 140 statement = new FailOnTimeoutStatement(statement, timeout); 141 } 142 statement = new CreateJosmEnvironment(statement); 152 143 if (josmHome != null) { 153 144 statement = josmHome.apply(statement, description); … … 254 245 } 255 246 247 private final class CreateJosmEnvironment extends Statement { 248 private final Statement base; 249 250 private CreateJosmEnvironment(Statement base) { 251 this.base = base; 252 } 253 254 @Override 255 public void evaluate() throws Throwable { 256 before(); 257 try { 258 base.evaluate(); 259 } finally { 260 after(); 261 } 262 } 263 } 264 256 265 enum APIType { 257 266 NONE, FAKE, DEV 258 267 } 268 269 /** 270 * The junit timeout statement has problems when switchting timezones. This one does not. 271 * @author Michael Zangl 272 */ 273 private static class FailOnTimeoutStatement extends Statement { 274 275 private int timeout; 276 private Statement original; 277 278 FailOnTimeoutStatement(Statement original, int timeout) { 279 this.original = original; 280 this.timeout = timeout; 281 } 282 283 @Override 284 public void evaluate() throws Throwable { 285 TimeoutThread thread = new TimeoutThread(original); 286 thread.setDaemon(true); 287 thread.start(); 288 thread.join(timeout); 289 thread.interrupt(); 290 if (!thread.isDone) { 291 Throwable exception = thread.getExecutionException(); 292 if (exception != null) { 293 throw exception; 294 } else { 295 throw new Exception(MessageFormat.format("Test timed out after {0}ms", timeout)); 296 } 297 } 298 } 299 } 300 301 private static final class TimeoutThread extends Thread { 302 public boolean isDone; 303 private Statement original; 304 private Throwable exceptionCaught; 305 306 private TimeoutThread(Statement original) { 307 super("Timeout runner"); 308 this.original = original; 309 } 310 311 public Throwable getExecutionException() { 312 return exceptionCaught; 313 } 314 315 @Override 316 public void run() { 317 try { 318 original.evaluate(); 319 isDone = true; 320 } catch (Throwable e) { 321 exceptionCaught = e; 322 } 323 } 324 } 259 325 } -
trunk/test/unit/org/openstreetmap/josm/tools/ExifReaderTest.java
r10513 r10569 35 35 @Rule 36 36 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") 37 public JOSMTestRules test = new JOSMTestRules() .timeout(60000);37 public JOSMTestRules test = new JOSMTestRules(); 38 38 39 39 private File orientationSampleFile, directionSampleFile; -
trunk/test/unit/org/openstreetmap/josm/tools/date/DateUtilsTest.java
r10563 r10569 23 23 24 24 /** 25 * Set the timezone and no timeout (junit timeout task seems to have problems with it when switching time zones). 25 * Set the timezone and timeout. 26 * <p> 27 * Timeouts need to be disabled because we change the time zone. 26 28 */ 27 29 @Rule 28 30 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") 29 public JOSMTestRules test = new JOSMTestRules().i18n().preferences() .noTimeout();31 public JOSMTestRules test = new JOSMTestRules().i18n().preferences(); 30 32 31 33 /**
Note:
See TracChangeset
for help on using the changeset viewer.