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.params.Source; |
---|
13 | import com.framsticks.parsers.FileSource; |
---|
14 | import com.framsticks.util.ExceptionHandler; |
---|
15 | import com.framsticks.util.FramsticksException; |
---|
16 | import com.framsticks.util.dispatching.Dispatcher; |
---|
17 | import com.framsticks.util.dispatching.Dispatching; |
---|
18 | import com.framsticks.util.dispatching.Dispatching.Waiter; |
---|
19 | import com.framsticks.util.dispatching.ExceptionDispatcherHandler; |
---|
20 | |
---|
21 | // import static org.fest.assertions.Assertions.*; |
---|
22 | |
---|
23 | public class TestConfiguration { |
---|
24 | |
---|
25 | private static final Logger log = LogManager.getLogger(TestConfiguration.class); |
---|
26 | |
---|
27 | @BeforeClass |
---|
28 | public void setUpConfiguration() { |
---|
29 | log.info("testing {}", this.getClass()); |
---|
30 | } |
---|
31 | |
---|
32 | private final List<AssertionError> asyncAssertions = new LinkedList<>(); |
---|
33 | |
---|
34 | public static AssertionError wrapInAssertion(Throwable throwable) { |
---|
35 | if (throwable instanceof AssertionError) { |
---|
36 | return (AssertionError) throwable; |
---|
37 | } |
---|
38 | |
---|
39 | AssertionError ae = new AssertionError(); |
---|
40 | ae.initCause(throwable); |
---|
41 | return ae; |
---|
42 | } |
---|
43 | |
---|
44 | public void addAsyncAssertion(Throwable throwable) { |
---|
45 | synchronized (asyncAssertions) { |
---|
46 | asyncAssertions.add(wrapInAssertion(throwable)); |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | public ExceptionDispatcherHandler createExceptionHandler() { |
---|
51 | return new ExceptionDispatcherHandler() { |
---|
52 | @Override |
---|
53 | public boolean handle(Dispatcher<?> dispatcher, Throwable throwable) { |
---|
54 | addAsyncAssertion(throwable); |
---|
55 | return true; |
---|
56 | } |
---|
57 | }; |
---|
58 | } |
---|
59 | |
---|
60 | @AfterMethod |
---|
61 | public void waitForWaiters() { |
---|
62 | for (Waiter w : waiters) { |
---|
63 | try { |
---|
64 | w.waitFor(); |
---|
65 | } catch (FramsticksException e) { |
---|
66 | AssertionError ae = new AssertionError(); |
---|
67 | ae.initCause(e); |
---|
68 | asyncAssertions.add(ae); |
---|
69 | } |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | @AfterMethod(timeOut = 1000, dependsOnMethods = "waitForWaiters") |
---|
74 | public void processAsyncAssertions() { |
---|
75 | synchronized (asyncAssertions) { |
---|
76 | if (asyncAssertions.isEmpty()) { |
---|
77 | return; |
---|
78 | } |
---|
79 | AssertionError a = asyncAssertions.get(0); |
---|
80 | asyncAssertions.remove(0); |
---|
81 | throw a; |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | final Set<Waiter> waiters = new HashSet<>(); |
---|
86 | |
---|
87 | @BeforeMethod |
---|
88 | public void clearWaiters() { |
---|
89 | synchronized (waiters) { |
---|
90 | waiters.clear(); |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | protected Dispatching.Waiter produceWaiter(double timeOut) { |
---|
95 | Waiter waiter = new Waiter(timeOut, failOnException); |
---|
96 | synchronized (waiters) { |
---|
97 | waiters.add(waiter); |
---|
98 | } |
---|
99 | return waiter; |
---|
100 | } |
---|
101 | |
---|
102 | public final ExceptionHandler failOnException = new ExceptionHandler() { |
---|
103 | @Override |
---|
104 | public void handle(FramsticksException e) { |
---|
105 | log.error("passing exception as assertion in {}", TestConfiguration.this.getClass(), e); |
---|
106 | addAsyncAssertion(e); |
---|
107 | } |
---|
108 | }; |
---|
109 | |
---|
110 | protected Source getSource(String path) { |
---|
111 | return new FileSource(TestConfiguration.class.getResourceAsStream(path), path); |
---|
112 | } |
---|
113 | } |
---|