- Timestamp:
- 07/04/13 20:29:50 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
java/main/src/test/java/com/framsticks/test/TestConfiguration.java
r90 r96 1 1 package com.framsticks.test; 2 2 3 import java.util.HashSet; 3 4 import java.util.LinkedList; 4 5 import java.util.List; 6 import java.util.Set; 5 7 6 8 import org.apache.log4j.Logger; … … 8 10 import org.testng.annotations.*; 9 11 12 import com.framsticks.util.FramsticksException; 10 13 import com.framsticks.util.dispatching.Dispatcher; 11 import com.framsticks.util.dispatching.RunAt; 14 import com.framsticks.util.dispatching.Dispatching; 15 import com.framsticks.util.dispatching.Dispatching.Waiter; 16 import com.framsticks.util.dispatching.ExceptionHandler; 17 import com.framsticks.util.dispatching.ExceptionResultHandler; 18 19 import static org.fest.assertions.Assertions.*; 12 20 13 21 public class TestConfiguration { … … 21 29 } 22 30 23 private final List<As yncAssert<?>> asyncAssertions = new LinkedList<AsyncAssert<?>>();31 private final List<AssertionError> asyncAssertions = new LinkedList<>(); 24 32 25 public class AsyncAssert<C> extends RunAt<C> { 26 final RunAt<? extends C> runnable; 27 boolean done = false; 28 AssertionError assertion; 33 public ExceptionHandler createExceptionHandler() { 34 return new ExceptionHandler() { 35 @Override 36 public boolean handle(Dispatcher<?> dispatcher, Throwable throwable) { 37 AssertionError ae; 38 if (AssertionError.class.isInstance(throwable)) { 39 ae = AssertionError.class.cast(throwable); 40 } else { 41 ae = new AssertionError(); 42 ae.initCause(throwable); 43 } 44 synchronized (asyncAssertions) { 45 asyncAssertions.add(ae); 46 } 47 return true; 48 } 49 }; 50 } 29 51 30 /** 31 * @param runnable 32 */ 33 public AsyncAssert(RunAt<? extends C> runnable) { 34 this.runnable = runnable; 35 } 36 37 @Override 38 public void run() { 52 @AfterMethod 53 public void waitForWaiters() { 54 for (Waiter w : waiters) { 39 55 try { 40 runnable.run(); 41 } catch (AssertionError e) { 42 assertion = e; 43 } 44 synchronized (this) { 45 done = true; 46 this.notifyAll(); 56 w.waitFor(); 57 } catch (FramsticksException e) { 58 AssertionError ae = new AssertionError(); 59 ae.initCause(e); 60 asyncAssertions.add(ae); 47 61 } 48 62 } 49 63 } 50 64 51 public <C> void assertDispatch(Dispatcher<C> dispatcher, RunAt<? extends C> runnable) {52 AsyncAssert<C> assertion = new AsyncAssert<C>(runnable);65 @AfterMethod(timeOut = 1000, dependsOnMethods = "waitForWaiters") 66 public void processAsyncAssertions() { 53 67 synchronized (asyncAssertions) { 54 asyncAssertions.add(assertion); 55 } 56 dispatcher.dispatch(assertion); 57 } 58 59 @BeforeMethod 60 public void clearAsyncAsserts() { 61 synchronized (asyncAssertions) { 62 asyncAssertions.clear(); 68 if (asyncAssertions.isEmpty()) { 69 return; 70 } 71 AssertionError a = asyncAssertions.get(0); 72 asyncAssertions.remove(0); 73 throw a; 63 74 } 64 75 } 65 76 66 @AfterMethod(timeOut = 5000) 67 public void waitForAsyncAsserts() { 68 while (true) { 69 AsyncAssert<?> assertion; 70 synchronized (asyncAssertions) { 71 if (asyncAssertions.isEmpty()) { 72 return; 73 } 74 assertion = asyncAssertions.get(0); 75 asyncAssertions.remove(0); 77 final Set<Waiter> waiters = new HashSet<>(); 78 79 @BeforeMethod 80 public void clearWaiters() { 81 waiters.clear(); 82 } 83 84 protected Dispatching.Waiter produceWaiter(double timeOut) { 85 Waiter waiter = new Waiter(timeOut); 86 waiters.add(waiter); 87 return waiter; 88 } 89 90 public static ExceptionResultHandler failOnException() { 91 return new ExceptionResultHandler() { 92 @Override 93 public void handle(FramsticksException e) { 94 assertThat(e).isNull(); 76 95 } 77 synchronized (assertion) { 78 while (!assertion.done) { 79 try { 80 assertion.wait(); 81 } catch (InterruptedException ignored) { 82 } 83 } 84 if (assertion.assertion != null) { 85 throw assertion.assertion; 86 } 87 } 88 } 96 }; 97 89 98 } 90 99 }
Note: See TracChangeset
for help on using the changeset viewer.