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