Class Waiter


  • public class Waiter
    extends java.lang.Object
    Waits on a test, carrying out assertions, until being resumed.
    • Constructor Summary

      Constructors 
      Constructor Description
      Waiter()
      Creates a new Waiter.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void assertEquals​(java.lang.Object expected, java.lang.Object actual)
      Asserts that the expected values equals the actual value
      void assertFalse​(boolean condition)
      Asserts that the condition is false.
      void assertNotNull​(java.lang.Object object)
      Asserts that the object is not null.
      void assertNull​(java.lang.Object object)
      Asserts that the object is null.
      <T> void assertThat​(T actual, org.hamcrest.Matcher<? super T> matcher)
      Asserts that actual satisfies the condition specified by matcher.
      void assertTrue​(boolean condition)
      Asserts that the condition is true.
      void await()
      Waits until resume() is called, or the test is failed.
      void await​(long delay)
      Waits until the delay has elapsed, resume() is called, or the test is failed.
      void await​(long delay, int expectedResumes)
      Waits until the delay has elapsed, resume() is called expectedResumes times, or the test is failed.
      void await​(long delay, java.util.concurrent.TimeUnit timeUnit)
      Waits until the delay has elapsed, resume() is called, or the test is failed.
      void await​(long delay, java.util.concurrent.TimeUnit timeUnit, int expectedResumes)
      Waits until the delay has elapsed, resume() is called expectedResumes times, or the test is failed.
      void fail()
      Fails the current test.
      void fail​(java.lang.String reason)
      Fails the current test for the given reason.
      void fail​(java.lang.Throwable reason)
      Fails the current test with the given reason, sets the number of expected resumes to 0, and throws the reason as an AssertionError in the main test thread and in the current thread.
      private java.lang.String format​(java.lang.Object expected, java.lang.Object actual)  
      void resume()
      Resumes the waiter when the expected number of resume() calls have occurred.
      void rethrow​(java.lang.Throwable failure)
      Rethrows the failure in the main test thread and in the current thread.
      private static void sneakyThrow​(java.lang.Throwable t)  
      private static <T extends java.lang.Throwable>
      void
      sneakyThrow2​(java.lang.Throwable t)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • remainingResumes

        private java.util.concurrent.atomic.AtomicInteger remainingResumes
      • failure

        private volatile java.lang.Throwable failure
    • Constructor Detail

      • Waiter

        public Waiter()
        Creates a new Waiter.
    • Method Detail

      • assertEquals

        public void assertEquals​(java.lang.Object expected,
                                 java.lang.Object actual)
        Asserts that the expected values equals the actual value
        Throws:
        java.lang.AssertionError - when the assertion fails
      • assertFalse

        public void assertFalse​(boolean condition)
        Asserts that the condition is false.
        Throws:
        java.lang.AssertionError - when the assertion fails
      • assertNotNull

        public void assertNotNull​(java.lang.Object object)
        Asserts that the object is not null.
        Throws:
        java.lang.AssertionError - when the assertion fails
      • assertNull

        public void assertNull​(java.lang.Object object)
        Asserts that the object is null.
        Throws:
        java.lang.AssertionError - when the assertion fails
      • assertTrue

        public void assertTrue​(boolean condition)
        Asserts that the condition is true.
        Throws:
        java.lang.AssertionError - when the assertion fails
      • assertThat

        public <T> void assertThat​(T actual,
                                   org.hamcrest.Matcher<? super T> matcher)
        Asserts that actual satisfies the condition specified by matcher.
        Throws:
        java.lang.AssertionError - when the assertion fails
      • await

        public void await()
                   throws java.util.concurrent.TimeoutException,
                          java.lang.InterruptedException
        Waits until resume() is called, or the test is failed.
        Throws:
        java.util.concurrent.TimeoutException - if the operation times out while waiting
        java.lang.InterruptedException - if the operations is interrupted while waiting
        java.lang.AssertionError - if any assertion fails while waiting
      • await

        public void await​(long delay)
                   throws java.util.concurrent.TimeoutException,
                          java.lang.InterruptedException
        Waits until the delay has elapsed, resume() is called, or the test is failed.
        Parameters:
        delay - Delay to wait in milliseconds
        Throws:
        java.util.concurrent.TimeoutException - if the operation times out while waiting
        java.lang.InterruptedException - if the operations is interrupted while waiting
        java.lang.AssertionError - if any assertion fails while waiting
      • await

        public void await​(long delay,
                          java.util.concurrent.TimeUnit timeUnit)
                   throws java.util.concurrent.TimeoutException,
                          java.lang.InterruptedException
        Waits until the delay has elapsed, resume() is called, or the test is failed.
        Parameters:
        delay - Delay to wait for
        timeUnit - TimeUnit to delay for
        Throws:
        java.util.concurrent.TimeoutException - if the operation times out while waiting
        java.lang.InterruptedException - if the operations is interrupted while waiting
        java.lang.AssertionError - if any assertion fails while waiting
      • await

        public void await​(long delay,
                          int expectedResumes)
                   throws java.util.concurrent.TimeoutException,
                          java.lang.InterruptedException
        Waits until the delay has elapsed, resume() is called expectedResumes times, or the test is failed.
        Parameters:
        delay - Delay to wait for in milliseconds
        expectedResumes - Number of times resume() is expected to be called before the awaiting thread is resumed
        Throws:
        java.util.concurrent.TimeoutException - if the operation times out while waiting
        java.lang.InterruptedException - if the operations is interrupted while waiting
        java.lang.AssertionError - if any assertion fails while waiting
      • await

        public void await​(long delay,
                          java.util.concurrent.TimeUnit timeUnit,
                          int expectedResumes)
                   throws java.util.concurrent.TimeoutException,
                          java.lang.InterruptedException
        Waits until the delay has elapsed, resume() is called expectedResumes times, or the test is failed.
        Parameters:
        delay - Delay to wait for
        timeUnit - TimeUnit to delay for
        expectedResumes - Number of times resume() is expected to be called before the awaiting thread is resumed
        Throws:
        java.util.concurrent.TimeoutException - if the operation times out while waiting
        java.lang.InterruptedException - if the operations is interrupted while waiting
        java.lang.AssertionError - if any assertion fails while waiting
      • resume

        public void resume()
        Resumes the waiter when the expected number of resume() calls have occurred.
      • fail

        public void fail()
        Fails the current test.
        Throws:
        java.lang.AssertionError
      • fail

        public void fail​(java.lang.String reason)
        Fails the current test for the given reason.
        Throws:
        java.lang.AssertionError
      • fail

        public void fail​(java.lang.Throwable reason)
        Fails the current test with the given reason, sets the number of expected resumes to 0, and throws the reason as an AssertionError in the main test thread and in the current thread.
        Throws:
        java.lang.AssertionError - wrapping the reason
      • rethrow

        public void rethrow​(java.lang.Throwable failure)
        Rethrows the failure in the main test thread and in the current thread. Differs from fail(Throwable) which wraps a failure in an AssertionError before throwing.
        Throws:
        java.lang.Throwable - the failure
      • sneakyThrow

        private static void sneakyThrow​(java.lang.Throwable t)
      • sneakyThrow2

        private static <T extends java.lang.Throwable> void sneakyThrow2​(java.lang.Throwable t)
                                                                  throws T extends java.lang.Throwable
        Throws:
        T extends java.lang.Throwable
      • format

        private java.lang.String format​(java.lang.Object expected,
                                        java.lang.Object actual)