source: java/main/src/test/java/com/framsticks/test/TestConfiguration.java @ 193

Last change on this file since 193 was 193, checked in by Maciej Komosinski, 10 years ago

Set svn:eol-style native for all textual files

  • Property svn:eol-style set to native
File size: 2.8 KB
Line 
1package com.framsticks.test;
2
3import java.util.HashSet;
4import java.util.LinkedList;
5import java.util.List;
6import java.util.Set;
7
8import org.apache.logging.log4j.Logger;
9import org.apache.logging.log4j.LogManager;
10import org.testng.annotations.*;
11
12import com.framsticks.params.Source;
13import com.framsticks.parsers.FileSource;
14import com.framsticks.util.ExceptionHandler;
15import com.framsticks.util.FramsticksException;
16import com.framsticks.util.dispatching.Dispatcher;
17import com.framsticks.util.dispatching.Dispatching;
18import com.framsticks.util.dispatching.Dispatching.Waiter;
19import com.framsticks.util.dispatching.ExceptionDispatcherHandler;
20
21// import static org.fest.assertions.Assertions.*;
22
23public 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}
Note: See TracBrowser for help on using the repository browser.