diff --git a/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java b/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java
index 9fba4cd..f0f5ea2 100644
a
|
b
|
package org.openstreetmap.josm.testutils;
|
3 | 3 | |
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; |
14 | 13 | import org.junit.runners.model.Statement; |
… |
… |
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
32 | 31 | * @author Michael Zangl |
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; |
38 | 37 | private APIType useAPI = APIType.NONE; |
… |
… |
public class JOSMTestRules implements TestRule {
|
45 | 44 | * @return this instance, for easy chaining |
46 | 45 | */ |
47 | 46 | public JOSMTestRules noTimeout() { |
48 | | timeout = null; |
| 47 | timeout = -1; |
49 | 48 | return this; |
50 | 49 | } |
51 | 50 | |
… |
… |
public class JOSMTestRules implements TestRule {
|
55 | 54 | * @return this instance, for easy chaining |
56 | 55 | */ |
57 | 56 | public JOSMTestRules timeout(int millis) { |
58 | | timeout = Timeout.millis(millis); |
| 57 | timeout = millis; |
59 | 58 | return this; |
60 | 59 | } |
61 | 60 | |
… |
… |
public class JOSMTestRules implements TestRule {
|
134 | 133 | } |
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); |
| 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); |
151 | 141 | } |
| 142 | statement = new CreateJosmEnvironment(statement); |
152 | 143 | if (josmHome != null) { |
153 | 144 | statement = josmHome.apply(statement, description); |
154 | 145 | } |
… |
… |
public class JOSMTestRules implements TestRule {
|
252 | 243 | System.gc(); |
253 | 244 | } |
254 | 245 | |
| 246 | private final class CreateJosmEnvironment extends Statement { |
| 247 | private final Statement base; |
| 248 | |
| 249 | private CreateJosmEnvironment(Statement base) { |
| 250 | this.base = base; |
| 251 | } |
| 252 | |
| 253 | @Override |
| 254 | public void evaluate() throws Throwable { |
| 255 | before(); |
| 256 | try { |
| 257 | base.evaluate(); |
| 258 | } finally { |
| 259 | after(); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
255 | 264 | enum APIType { |
256 | 265 | NONE, FAKE, DEV |
257 | 266 | } |
| 267 | |
| 268 | /** |
| 269 | * The junit timeout statement has problems when switchting timezones. This one does not. |
| 270 | * @author Michael Zangl |
| 271 | * @since xxx |
| 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 | |
| 302 | private static class TimeoutThread extends Thread { |
| 303 | public boolean isDone; |
| 304 | private Statement original; |
| 305 | private Throwable exceptionCaught; |
| 306 | |
| 307 | private TimeoutThread(Statement original) { |
| 308 | super("Timeout runner"); |
| 309 | this.original = original; |
| 310 | } |
| 311 | |
| 312 | public Throwable getExecutionException() { |
| 313 | return exceptionCaught; |
| 314 | } |
| 315 | |
| 316 | @Override |
| 317 | public void run() { |
| 318 | try { |
| 319 | original.evaluate(); |
| 320 | isDone = true; |
| 321 | } catch (Throwable e) { |
| 322 | exceptionCaught = e; |
| 323 | } |
| 324 | } |
| 325 | } |
258 | 326 | } |
diff --git a/test/unit/org/openstreetmap/josm/tools/ExifReaderTest.java b/test/unit/org/openstreetmap/josm/tools/ExifReaderTest.java
index 658ba4f..0fcf036 100644
a
|
b
|
public class ExifReaderTest {
|
34 | 34 | */ |
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; |
40 | 40 | |
diff --git a/test/unit/org/openstreetmap/josm/tools/date/DateUtilsTest.java b/test/unit/org/openstreetmap/josm/tools/date/DateUtilsTest.java
index 06803a7..23373d2 100644
a
|
b
|
public class DateUtilsTest {
|
23 | 23 | |
24 | 24 | /** |
25 | 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") |