source:
java/main/src/test/java/com/framsticks/test/TestConfiguration.java
@
105
Last change on this file since 105 was 105, checked in by , 11 years ago | |
---|---|
File size: 2.6 KB |
Line | |
---|---|
1 | package com.framsticks.test; |
2 | |
3 | import java.util.HashSet; |
4 | import java.util.LinkedList; |
5 | import java.util.List; |
6 | import java.util.Set; |
7 | |
8 | import org.apache.logging.log4j.Logger; |
9 | import org.apache.logging.log4j.LogManager; |
10 | import org.testng.annotations.*; |
11 | |
12 | import com.framsticks.util.ExceptionHandler; |
13 | import com.framsticks.util.FramsticksException; |
14 | import com.framsticks.util.dispatching.Dispatcher; |
15 | import com.framsticks.util.dispatching.Dispatching; |
16 | import com.framsticks.util.dispatching.Dispatching.Waiter; |
17 | import com.framsticks.util.dispatching.ExceptionDispatcherHandler; |
18 | |
19 | // import static org.fest.assertions.Assertions.*; |
20 | |
21 | public class TestConfiguration { |
22 | |
23 | private static final Logger log = LogManager.getLogger(TestConfiguration.class); |
24 | |
25 | @BeforeClass |
26 | public void setUpConfiguration() { |
27 | log.info("testing {}", this.getClass()); |
28 | } |
29 | |
30 | private final List<AssertionError> asyncAssertions = new LinkedList<>(); |
31 | |
32 | public static AssertionError wrapInAssertion(Throwable throwable) { |
33 | if (throwable instanceof AssertionError) { |
34 | return (AssertionError) throwable; |
35 | } |
36 | |
37 | AssertionError ae = new AssertionError(); |
38 | ae.initCause(throwable); |
39 | return ae; |
40 | } |
41 | |
42 | public void addAsyncAssertion(Throwable throwable) { |
43 | synchronized (asyncAssertions) { |
44 | asyncAssertions.add(wrapInAssertion(throwable)); |
45 | } |
46 | } |
47 | |
48 | public ExceptionDispatcherHandler createExceptionHandler() { |
49 | return new ExceptionDispatcherHandler() { |
50 | @Override |
51 | public boolean handle(Dispatcher<?> dispatcher, Throwable throwable) { |
52 | addAsyncAssertion(throwable); |
53 | return true; |
54 | } |
55 | }; |
56 | } |
57 | |
58 | @AfterMethod |
59 | public void waitForWaiters() { |
60 | for (Waiter w : waiters) { |
61 | try { |
62 | w.waitFor(); |
63 | } catch (FramsticksException e) { |
64 | AssertionError ae = new AssertionError(); |
65 | ae.initCause(e); |
66 | asyncAssertions.add(ae); |
67 | } |
68 | } |
69 | } |
70 | |
71 | @AfterMethod(timeOut = 1000, dependsOnMethods = "waitForWaiters") |
72 | public void processAsyncAssertions() { |
73 | synchronized (asyncAssertions) { |
74 | if (asyncAssertions.isEmpty()) { |
75 | return; |
76 | } |
77 | AssertionError a = asyncAssertions.get(0); |
78 | asyncAssertions.remove(0); |
79 | throw a; |
80 | } |
81 | } |
82 | |
83 | final Set<Waiter> waiters = new HashSet<>(); |
84 | |
85 | @BeforeMethod |
86 | public void clearWaiters() { |
87 | synchronized (waiters) { |
88 | waiters.clear(); |
89 | } |
90 | } |
91 | |
92 | protected Dispatching.Waiter produceWaiter(double timeOut) { |
93 | Waiter waiter = new Waiter(timeOut, failOnException); |
94 | synchronized (waiters) { |
95 | waiters.add(waiter); |
96 | } |
97 | return waiter; |
98 | } |
99 | |
100 | public final ExceptionHandler failOnException = new ExceptionHandler() { |
101 | @Override |
102 | public void handle(FramsticksException e) { |
103 | log.error("passing exception as assertion in {}", TestConfiguration.this.getClass(), e); |
104 | addAsyncAssertion(e); |
105 | } |
106 | }; |
107 | } |
Note: See TracBrowser
for help on using the repository browser.