Assert.java

  1. /**
  2.  * Powerunit - A JDK1.8 test framework
  3.  * Copyright (C) 2014 Mathieu Boretti.
  4.  *
  5.  * This file is part of Powerunit
  6.  *
  7.  * Powerunit is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation, either version 3 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * Powerunit is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with Powerunit. If not, see <http://www.gnu.org/licenses/>.
  19.  */
  20. package ch.powerunit;

  21. import java.util.concurrent.Callable;
  22. import java.util.function.BiFunction;
  23. import java.util.function.Function;

  24. import ch.powerunit.impl.AssertThatExceptionImpl;
  25. import ch.powerunit.impl.AssertThatIterableImpl;
  26. import ch.powerunit.impl.AssertThatObjectImpl;
  27. import ch.powerunit.impl.AssertThatStringImpl;
  28. import ch.powerunit.impl.FailureImpl;

  29. /**
  30.  * This is the assert features.
  31.  *
  32.  * @author borettim
  33.  *
  34.  */
  35. interface Assert {

  36.     /**
  37.      * Assert the value of an object.
  38.      * <p>
  39.      * For instance
  40.      *
  41.      * <pre>
  42.      * assertThat(myObject).is(myOtherObject);
  43.      * </pre>
  44.      *
  45.      * This will check that <code>myObject</code> is <code>myOtherObject</code>
  46.      * (using the <code>equalTo</code> Hamcrest matcher). <br>
  47.      * <br>
  48.      * <i>By default, assertThat can only be used from the main thread of the
  49.      * test ; When used from another thread, the assertion will be lost. In the
  50.      * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
  51.      * annotation is used, the assertion may not be lost, in case the thread use
  52.      * an assertion method from the test object instance. </i>
  53.      *
  54.      * @param <T>
  55.      *            the object type.
  56.      * @param obj
  57.      *            the object
  58.      * @return {@link AssertThatCastableObject the assert DSL on this object}
  59.      * @see Test#fastFail() The documentation of the <code>fastFail</code>
  60.      *      attribute of the <code>@Test</code> annotation, regarding the action
  61.      *      done by this assertion.
  62.      */
  63.     default <T> AssertThatCastableObject<T> assertThat(T obj) {
  64.         return assertThat(null, obj);
  65.     }

  66.     /**
  67.      * Assert the value of an object.
  68.      * <p>
  69.      * For instance
  70.      *
  71.      * <pre>
  72.      * assertThat(&quot;msg&quot;, myObject).is(myOtherObject);
  73.      * </pre>
  74.      *
  75.      * This will check that <code>myObject</code> is <code>myOtherObject</code>
  76.      * (using the <code>equalTo</code> Hamcrest matcher). <br>
  77.      * <br>
  78.      * <i>By default, assertThat can only be used from the main thread of the
  79.      * test ; When used from another thread, the assertion will be lost. In the
  80.      * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
  81.      * annotation is used, the assertion may not be lost, in case the thread use
  82.      * an assertion method from the test object instance. </i>
  83.      *
  84.      * @param <T>
  85.      *            the object type.
  86.      * @param msg
  87.      *            a message
  88.      * @param obj
  89.      *            the object
  90.      * @return {@link AssertThatCastableObject the assert DSL on this object}
  91.      * @see Test#fastFail() The documentation of the <code>fastFail</code>
  92.      *      attribute of the <code>@Test</code> annotation, regarding the action
  93.      *      done by this assertion.
  94.      */
  95.     default <T> AssertThatCastableObject<T> assertThat(String msg, T obj) {
  96.         return new AssertThatObjectImpl<T>(this, true, msg, () -> obj);
  97.     }

  98.     /**
  99.      * Assert the value of a String.
  100.      * <p>
  101.      * For instance
  102.      *
  103.      * <pre>
  104.      * assertThat(myString).is(&quot;&quot;);
  105.      * </pre>
  106.      *
  107.      * This will check that <code>myString</code> is <code>""</code> (using the
  108.      * <code>equalTo</code> Hamcrest matcher). <br>
  109.      * <br>
  110.      * <i>By default, assertThat can only be used from the main thread of the
  111.      * test ; When used from another thread, the assertion will be lost. In the
  112.      * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
  113.      * annotation is used, the assertion may not be lost, in case the thread use
  114.      * an assertion method from the test object instance. </i>
  115.      *
  116.      * @param obj
  117.      *            the String
  118.      * @return {@link AssertThatString the assert DSL on this object}
  119.      * @see Test#fastFail() The documentation of the <code>fastFail</code>
  120.      *      attribute of the <code>@Test</code> annotation, regarding the action
  121.      *      done by this assertion.
  122.      */
  123.     default AssertThatString assertThat(String obj) {
  124.         return assertThat(null, obj);
  125.     }

  126.     /**
  127.      * Assert the value of a String.
  128.      * <p>
  129.      * For instance
  130.      *
  131.      * <pre>
  132.      * assertThat(&quot;msg&quot;, myString).is(&quot;&quot;);
  133.      * </pre>
  134.      *
  135.      * This will check that <code>myString</code> is <code>""</code> (using the
  136.      * <code>equalTo</code> Hamcrest matcher). <br>
  137.      * <br>
  138.      * <i>By default, assertThat can only be used from the main thread of the
  139.      * test ; When used from another thread, the assertion will be lost. In the
  140.      * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
  141.      * annotation is used, the assertion may not be lost, in case the thread use
  142.      * an assertion method from the test object instance. </i>
  143.      *
  144.      * @param msg
  145.      *            a message
  146.      * @param obj
  147.      *            the String
  148.      * @return {@link AssertThatString the assert DSL on this object}
  149.      * @see Test#fastFail() The documentation of the <code>fastFail</code>
  150.      *      attribute of the <code>@Test</code> annotation, regarding the action
  151.      *      done by this assertion.
  152.      */
  153.     default AssertThatString assertThat(String msg, String obj) {
  154.         return new AssertThatStringImpl(this, true, msg, () -> obj);
  155.     }

  156.     /**
  157.      * Assert on an iterable object.
  158.      * <p>
  159.      * For instance
  160.      *
  161.      * <pre>
  162.      * assertThatIterable(myIterable).hasSize(0);
  163.      * </pre>
  164.      *
  165.      * This will check that <code>myIterable</code> has a size of 0. <br>
  166.      * <br>
  167.      * <i>By default, assertThat can only be used from the main thread of the
  168.      * test ; When used from another thread, the assertion will be lost. In the
  169.      * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
  170.      * annotation is used, the assertion may not be lost, in case the thread use
  171.      * an assertion method from the test object instance. </i>
  172.      *
  173.      * @param <T>
  174.      *            the element type.
  175.      * @param obj
  176.      *            the object (Iterable)
  177.      * @return {@link AssertThatIterable the assert DSL on this iterable}
  178.      * @see Test#fastFail() The documentation of the <code>fastFail</code>
  179.      *      attribute of the <code>@Test</code> annotation, regarding the action
  180.      *      done by this assertion.
  181.      */
  182.     default <T> AssertThatIterable<T> assertThatIterable(Iterable<T> obj) {
  183.         return assertThatIterable(null, obj);
  184.     }

  185.     /**
  186.      * Assert on an iterable object.
  187.      * <p>
  188.      * For instance
  189.      *
  190.      * <pre>
  191.      * assertThatIterable(&quot;msg&quot;, myIterable).hasSize(0);
  192.      * </pre>
  193.      *
  194.      * This will check that <code>myIterable</code> has a size of 0. <br>
  195.      * <br>
  196.      * <i>By default, assertThat can only be used from the main thread of the
  197.      * test ; When used from another thread, the assertion will be lost. In the
  198.      * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
  199.      * annotation is used, the assertion may not be lost, in case the thread use
  200.      * an assertion method from the test object instance. </i>
  201.      *
  202.      * @param <T>
  203.      *            the element type.
  204.      * @param msg
  205.      *            a message
  206.      * @param obj
  207.      *            the object (Iterable)
  208.      * @return {@link AssertThatIterable the assert DSL on this iterable}
  209.      * @see Test#fastFail() The documentation of the <code>fastFail</code>
  210.      *      attribute of the <code>@Test</code> annotation, regarding the action
  211.      *      done by this assertion.
  212.      */
  213.     default <T> AssertThatIterable<T> assertThatIterable(String msg,
  214.             Iterable<T> obj) {
  215.         return new AssertThatIterableImpl<T>(this, true, msg, () -> obj);
  216.     }

  217.     /**
  218.      * Assert on a function.
  219.      * <p>
  220.      * The purpose of this variant of <i>assertThat</i> provides a way to apply
  221.      * a function on some input and to check the result.
  222.      * <p>
  223.      * For instance
  224.      *
  225.      * <pre>
  226.      * assertThatFunction((a) -&gt; a + &quot;x&quot;, &quot;b&quot;).is(&quot;bx&quot;)
  227.      * </pre>
  228.      *
  229.      * This will pass the <code>b</code> string to the passed function (which
  230.      * add a <code>x</code> add the end of the string and then it will check
  231.      * that this string is <code>bx</code> (which is the case). <br>
  232.      * <br>
  233.      * <i>By default, assertThat can only be used from the main thread of the
  234.      * test ; When used from another thread, the assertion will be lost. In the
  235.      * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
  236.      * annotation is used, the assertion may not be lost, in case the thread use
  237.      * an assertion method from the test object instance. </i>
  238.      *
  239.      * @param <T>
  240.      *            the object type of the input of the function
  241.      * @param <R>
  242.      *            the object type of the result
  243.      * @param function
  244.      *            the function
  245.      * @param input
  246.      *            the input to the function
  247.      * @return {@link AssertThatCastableObject then assert DSL on the result of
  248.      *         the function}
  249.      * @see Test#fastFail() The documentation of the <code>fastFail</code>
  250.      *      attribute of the <code>@Test</code> annotation, regarding the action
  251.      *      done by this assertion.
  252.      */
  253.     default <T, R> AssertThatCastableObject<R> assertThatFunction(
  254.             Function<T, R> function, T input) {
  255.         return new AssertThatObjectImpl<R>(this, true, null,
  256.                 () -> function.apply(input));
  257.     }

  258.     /**
  259.      * Assert on a function.
  260.      * <p>
  261.      * The purpose of this variant of <i>assertThat</i> provides a way to apply
  262.      * a function on some input and to check the result.
  263.      * <p>
  264.      * For instance
  265.      *
  266.      * <pre>
  267.      * assertThatFunction(&quot;msg&quot;, (a) -&gt; a + &quot;x&quot;, &quot;b&quot;).is(&quot;bx&quot;)
  268.      * </pre>
  269.      *
  270.      * This will pass the <code>b</code> string to the passed function (which
  271.      * add a <code>x</code> add the end of the string and then it will check
  272.      * that this string is <code>bx</code> (which is the case). <br>
  273.      * <br>
  274.      * <i>By default, assertThat can only be used from the main thread of the
  275.      * test ; When used from another thread, the assertion will be lost. In the
  276.      * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
  277.      * annotation is used, the assertion may not be lost, in case the thread use
  278.      * an assertion method from the test object instance. </i>
  279.      *
  280.      * @param <T>
  281.      *            the object type of the input of the function
  282.      * @param <R>
  283.      *            the object type of the result
  284.      * @param msg
  285.      *            a message
  286.      * @param function
  287.      *            the function
  288.      * @param input
  289.      *            the input to the function
  290.      * @return {@link AssertThatCastableObject then assert DSL on the result of
  291.      *         the function}
  292.      * @see Test#fastFail() The documentation of the <code>fastFail</code>
  293.      *      attribute of the <code>@Test</code> annotation, regarding the action
  294.      *      done by this assertion.
  295.      */
  296.     default <T, R> AssertThatCastableObject<R> assertThatFunction(String msg,
  297.             Function<T, R> function, T input) {
  298.         return new AssertThatObjectImpl<R>(this, true, msg,
  299.                 () -> function.apply(input));
  300.     }

  301.     /**
  302.      * Assert on a bifunction.
  303.      * <p>
  304.      * The purpose of this variant of <i>assertThat</i> provides a way to apply
  305.      * a bifunction on some input and to check the result.
  306.      * <p>
  307.      * For instance
  308.      *
  309.      * <pre>
  310.      * assertThatBiFunction((a, b) -&gt; a + b, &quot;a&quot;, &quot;b&quot;).is(&quot;ab&quot;)
  311.      * </pre>
  312.      *
  313.      * This will pass the <code>b</code> string to the passed function (which
  314.      * add a <code>x</code> add the end of the string and then it will check
  315.      * that this string is <code>bx</code> (which is the case). <br>
  316.      * <br>
  317.      * <i>By default, assertThat can only be used from the main thread of the
  318.      * test ; When used from another thread, the assertion will be lost. In the
  319.      * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
  320.      * annotation is used, the assertion may not be lost, in case the thread use
  321.      * an assertion method from the test object instance. </i>
  322.      *
  323.      * @param <T>
  324.      *            the object type of the first input of the function
  325.      * @param <U>
  326.      *            the object type fo the second input of the function
  327.      * @param <R>
  328.      *            the object type of the result
  329.      * @param function
  330.      *            the function
  331.      * @param input1
  332.      *            the first input to the function
  333.      * @param input2
  334.      *            the second input to the function
  335.      * @return {@link AssertThatCastableObject then assert DSL on the result of
  336.      *         the bifunction}
  337.      * @see Test#fastFail() The documentation of the <code>fastFail</code>
  338.      *      attribute of the <code>@Test</code> annotation, regarding the action
  339.      *      done by this assertion.
  340.      */
  341.     default <T, U, R> AssertThatCastableObject<R> assertThatBiFunction(
  342.             BiFunction<T, U, R> function, T input1, U input2) {
  343.         return new AssertThatObjectImpl<R>(this, true, null,
  344.                 () -> function.apply(input1, input2));
  345.     }

  346.     /**
  347.      * Assert on a bifunction.
  348.      * <p>
  349.      * The purpose of this variant of <i>assertThat</i> provides a way to apply
  350.      * a bifunction on some input and to check the result.
  351.      * <p>
  352.      * For instance
  353.      *
  354.      * <pre>
  355.      * assertThatBiFunction((a, b) -&gt; a + b, &quot;a&quot;, &quot;b&quot;).is(&quot;ab&quot;)
  356.      * </pre>
  357.      *
  358.      * This will pass the <code>b</code> string to the passed function (which
  359.      * add a <code>x</code> add the end of the string and then it will check
  360.      * that this string is <code>bx</code> (which is the case). <br>
  361.      * <br>
  362.      * <i>By default, assertThat can only be used from the main thread of the
  363.      * test ; When used from another thread, the assertion will be lost. In the
  364.      * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
  365.      * annotation is used, the assertion may not be lost, in case the thread use
  366.      * an assertion method from the test object instance. </i>
  367.      *
  368.      * @param <T>
  369.      *            the object type of the first input of the function
  370.      * @param <U>
  371.      *            the object type fo the second input of the function
  372.      * @param <R>
  373.      *            the object type of the result
  374.      * @param msg
  375.      *            a message
  376.      * @param function
  377.      *            the function
  378.      * @param input1
  379.      *            the first input to the function
  380.      * @param input2
  381.      *            the second input to the function
  382.      * @return {@link AssertThatObject then assert DSL on the result of the
  383.      *         bifunction}
  384.      * @see Test#fastFail() The documentation of the <code>fastFail</code>
  385.      *      attribute of the <code>@Test</code> annotation, regarding the action
  386.      *      done by this assertion.
  387.      */
  388.     default <T, U, R> AssertThatCastableObject<R> assertThatBiFunction(
  389.             String msg, BiFunction<T, U, R> function, T input1, U input2) {
  390.         return new AssertThatObjectImpl<R>(this, true, msg,
  391.                 () -> function.apply(input1, input2));
  392.     }

  393.     /**
  394.      * Assert that a statement (a piece of code) throw an exception.
  395.      * <p>
  396.      * The goal of <code>assertWhen</code> is to provide a way to validate that
  397.      * an exception is thrown.
  398.      * <p>
  399.      * For instance
  400.      *
  401.      * <pre>
  402.      * assertWhen((p) -&gt; {
  403.      *  throw new Throwable(&quot;test&quot;);
  404.      * }).throwException(exceptionMessage(&quot;test&quot;));
  405.      * </pre>
  406.      *
  407.      * Will run a piece of code that always thrown an exception and then
  408.      * validate that the message of the exception is <code>test</code>. <br>
  409.      * <br>
  410.      * <i>By default, assertThat can only be used from the main thread of the
  411.      * test ; When used from another thread, the assertion will be lost. In the
  412.      * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
  413.      * annotation is used, the assertion may not be lost, in case the thread use
  414.      * an assertion method from the test object instance. </i>
  415.      *
  416.      * @param underTest
  417.      *            the {@link Statement} <code>(p)-&gt;{}</code>
  418.      * @return {@link AssertThatException the assert DSL on the exception}
  419.      * @param <T>
  420.      *            the exception type
  421.      * @see Test#fastFail() The documentation of the <code>fastFail</code>
  422.      *      attribute of the <code>@Test</code> annotation, regarding the action
  423.      *      done by this assertion.
  424.      */
  425.     default <T extends Throwable> AssertThatException<T> assertWhen(
  426.             Statement<?, T> underTest) {
  427.         return assertWhen(null, underTest, null);
  428.     }

  429.     /**
  430.      * Assert that a statement (a piece of code) throw an exception.
  431.      * <p>
  432.      * The goal of <code>assertWhen</code> is to provide a way to validate that
  433.      * an exception is thrown.
  434.      * <p>
  435.      * For instance, having this method :
  436.      *
  437.      * <pre>
  438.      * public static void method1() throws Exception {
  439.      *  throw new Exception(&quot;demo1&quot;);
  440.      * }
  441.      * </pre>
  442.      *
  443.      * It can be tested by :
  444.      *
  445.      * <pre>
  446.      * &#064;Test
  447.      * public void testNoArgNoReturn() {
  448.      *  assertWhen(asStatement(DemoAssertThatExceptionTest::method1))
  449.      *          .throwException(exceptionMessage(&quot;demo1&quot;));
  450.      * }
  451.      * </pre>
  452.      *
  453.      * <i>By default, assertThat can only be used from the main thread of the
  454.      * test ; When used from another thread, the assertion will be lost. In the
  455.      * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
  456.      * annotation is used, the assertion may not be lost, in case the thread use
  457.      * an assertion method from the test object instance. </i>
  458.      *
  459.      * @param underTest
  460.      *            the {@link Callable}
  461.      * @return {@link AssertThatException the assert DSL on the exception}
  462.      * @param <T>
  463.      *            the exception type
  464.      * @see Test#fastFail() The documentation of the <code>fastFail</code>
  465.      *      attribute of the <code>@Test</code> annotation, regarding the action
  466.      *      done by this assertion.
  467.      * @since 0.4.0
  468.      */
  469.     default <T extends Exception> AssertThatException<T> assertWhen(
  470.             Callable<?> underTest) {
  471.         return assertWhen(null, (p) -> {
  472.             underTest.call();
  473.         }, null);
  474.     }

  475.     /**
  476.      * Assert that a statement (a piece of code) throw an exception.
  477.      * <p>
  478.      * The goal of <code>assertWhen</code> is to provide a way to validate that
  479.      * an exception is thrown.
  480.      * <p>
  481.      * For instance
  482.      *
  483.      * <pre>
  484.      * assertWhen((p) -&gt; {
  485.      *  throw new Throwable(&quot;test&quot;);
  486.      * }, null).throwException(exceptionMessage(&quot;test&quot;));
  487.      * </pre>
  488.      *
  489.      * Will run a piece of code, passing null as parameter, that always thrown
  490.      * an exception and then validate that the message of the exception is
  491.      * <code>test</code>. <br>
  492.      * <br>
  493.      * <i>By default, assertThat can only be used from the main thread of the
  494.      * test ; When used from another thread, the assertion will be lost. In the
  495.      * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
  496.      * annotation is used, the assertion may not be lost, in case the thread use
  497.      * an assertion method from the test object instance. </i>
  498.      *
  499.      * @param underTest
  500.      *            the {@link Statement} <code>(p)-&gt;{}</code>
  501.      * @param param
  502.      *            the parameter for the statement underTest
  503.      * @param <P>
  504.      *            the type of the parameter
  505.      * @param <T>
  506.      *            the exception type
  507.      * @return {@link AssertThatException the assert DSL on the exception}
  508.      * @see Test#fastFail() The documentation of the <code>fastFail</code>
  509.      *      attribute of the <code>@Test</code> annotation, regarding the action
  510.      *      done by this assertion.
  511.      *
  512.      */
  513.     default <P, T extends Throwable> AssertThatException<T> assertWhen(
  514.             Statement<P, T> underTest, P param) {
  515.         return assertWhen(null, underTest, param);
  516.     }

  517.     /**
  518.      * Assert that a statement (a piece of code) throw an exception.
  519.      * <p>
  520.      * The goal of <code>assertWhen</code> is to provide a way to validate that
  521.      * an exception is thrown.
  522.      * <p>
  523.      * For instance
  524.      *
  525.      * <pre>
  526.      * assertWhen(&quot;msg&quot;, (p) -&gt; {
  527.      *  throw new Throwable(&quot;test&quot;);
  528.      * }).throwException(exceptionMessage(&quot;test&quot;));
  529.      * </pre>
  530.      *
  531.      * Will run a piece of code that always thrown an exception and then
  532.      * validate that the message of the exception is <code>test</code>. <br>
  533.      * <br>
  534.      * <i>By default, assertThat can only be used from the main thread of the
  535.      * test ; When used from another thread, the assertion will be lost. In the
  536.      * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
  537.      * annotation is used, the assertion may not be lost, in case the thread use
  538.      * an assertion method from the test object instance. </i>
  539.      *
  540.      * @param msg
  541.      *            a message
  542.      * @param underTest
  543.      *            the statement <code>(p)-&gt;{}</code>
  544.      * @return {@link AssertThatException the assert DSL on the exception}
  545.      * @param <T>
  546.      *            the exception type
  547.      * @see Test#fastFail() The documentation of the <code>fastFail</code>
  548.      *      attribute of the <code>@Test</code> annotation, regarding the action
  549.      *      done by this assertion.
  550.      */
  551.     default <T extends Throwable> AssertThatException<T> assertWhen(String msg,
  552.             Statement<?, T> underTest) {
  553.         return assertWhen(msg, underTest, null);
  554.     }

  555.     /**
  556.      * Assert that a statement (a piece of code) throw an exception.
  557.      * <p>
  558.      * The goal of <code>assertWhen</code> is to provide a way to validate that
  559.      * an exception is thrown.
  560.      * <p>
  561.      *
  562.      * <br>
  563.      * <i>By default, assertThat can only be used from the main thread of the
  564.      * test ; When used from another thread, the assertion will be lost. In the
  565.      * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
  566.      * annotation is used, the assertion may not be lost, in case the thread use
  567.      * an assertion method from the test object instance. </i>
  568.      *
  569.      * @param msg
  570.      *            a message
  571.      * @param underTest
  572.      *            the callable
  573.      * @return {@link AssertThatException the assert DSL on the exception}
  574.      * @param <T>
  575.      *            the exception type
  576.      * @see Test#fastFail() The documentation of the <code>fastFail</code>
  577.      *      attribute of the <code>@Test</code> annotation, regarding the action
  578.      *      done by this assertion.
  579.      * @since 0.4.0
  580.      */
  581.     default <T extends Exception> AssertThatException<T> assertWhen(String msg,
  582.             Callable<?> underTest) {
  583.         return assertWhen(msg, (p) -> {
  584.             underTest.call();
  585.         }, null);
  586.     }

  587.     /**
  588.      * Assert that a statement (a piece of code) throw an exception.
  589.      * <p>
  590.      * The goal of <code>assertWhen</code> is to provide a way to validate that
  591.      * an exception is thrown.
  592.      * <p>
  593.      * For instance
  594.      *
  595.      * <pre>
  596.      * assertWhen(&quot;msg&quot;, (p) -&gt; {
  597.      *  throw new Throwable(&quot;test&quot;);
  598.      * }, null).throwException(exceptionMessage(&quot;test&quot;));
  599.      * </pre>
  600.      *
  601.      * Will run a piece of code, passing null as parameter, that always thrown
  602.      * an exception and then validate that the message of the exception is
  603.      * <code>test</code>. <br>
  604.      * <br>
  605.      * <i>By default, assertThat can only be used from the main thread of the
  606.      * test ; When used from another thread, the assertion will be lost. In the
  607.      * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
  608.      * annotation is used, the assertion may not be lost, in case the thread use
  609.      * an assertion method from the test object instance. </i>
  610.      *
  611.      * @param msg
  612.      *            a message
  613.      * @param underTest
  614.      *            the statement <code>(p)-&gt;{}</code>
  615.      * @param param
  616.      *            the parameter for the statement underTest
  617.      * @param <P>
  618.      *            the type of the parameter
  619.      * @param <T>
  620.      *            the exception type
  621.      * @return {@link AssertThatException the assert DSL on the exception}
  622.      * @see Test#fastFail() The documentation of the <code>fastFail</code>
  623.      *      attribute of the <code>@Test</code> annotation, regarding the action
  624.      *      done by this assertion.
  625.      *
  626.      */
  627.     default <P, T extends Throwable> AssertThatException<T> assertWhen(
  628.             String msg, Statement<P, T> underTest, P param) {
  629.         return new AssertThatExceptionImpl<P, T>(this, true, underTest, param,
  630.                 msg);
  631.     }

  632.     /**
  633.      * Assert that a function throw an exception. As {@link Function} signature
  634.      * doesn't throws exception, it should be a RuntimeException. <br>
  635.      * <br>
  636.      * <i>By default, assertThat can only be used from the main thread of the
  637.      * test ; When used from another thread, the assertion will be lost. In the
  638.      * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
  639.      * annotation is used, the assertion may not be lost, in case the thread use
  640.      * an assertion method from the test object instance. </i>
  641.      *
  642.      * @param msg
  643.      *            a message
  644.      * @param function
  645.      *            the function to be executed and that should return an
  646.      *            exception
  647.      * @param param
  648.      *            the parameter to be passed to the function.
  649.      * @param <P>
  650.      *            the type of the parameter
  651.      * @param <T>
  652.      *            the exception type
  653.      * @return {@link AssertThatException the assert DSL on the exception}
  654.      * @since 0.3.0
  655.      * @see Test#fastFail() The documentation of the <code>fastFail</code>
  656.      *      attribute of the <code>@Test</code> annotation, regarding the action
  657.      *      done by this assertion.
  658.      */
  659.     default <P, T extends RuntimeException> AssertThatException<T> assertWhenFunction(
  660.             String msg, Function<P, ?> function, P param) {
  661.         return assertWhen(msg, (p) -> function.apply(p), param);
  662.     }

  663.     /**
  664.      * Assert that a function throw an exception. As {@link Function} signature
  665.      * doesn't throws exception, it should be a RuntimeException. <br>
  666.      * <br>
  667.      * <i>By default, assertThat can only be used from the main thread of the
  668.      * test ; When used from another thread, the assertion will be lost. In the
  669.      * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
  670.      * annotation is used, the assertion may not be lost, in case the thread use
  671.      * an assertion method from the test object instance. </i>
  672.      *
  673.      * @param function
  674.      *            the function to be executed and that should return an
  675.      *            exception
  676.      * @param param
  677.      *            the parameter to be passed to the function.
  678.      * @param <P>
  679.      *            the type of the parameter
  680.      * @param <T>
  681.      *            the exception type
  682.      * @return {@link AssertThatException the assert DSL on the exception}
  683.      * @since 0.3.0
  684.      * @see Test#fastFail() The documentation of the <code>fastFail</code>
  685.      *      attribute of the <code>@Test</code> annotation, regarding the action
  686.      *      done by this assertion.
  687.      */
  688.     default <P, T extends RuntimeException> AssertThatException<T> assertWhenFunction(
  689.             Function<P, ?> function, P param) {
  690.         return assertWhen((p) -> function.apply(p), param);
  691.     }

  692.     /**
  693.      * Assert that a bifunction throw an exception. As {@link BiFunction}
  694.      * signature doesn't throws exception, it should be a RuntimeException. <br>
  695.      * <br>
  696.      * <i>By default, assertThat can only be used from the main thread of the
  697.      * test ; When used from another thread, the assertion will be lost. In the
  698.      * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
  699.      * annotation is used, the assertion may not be lost, in case the thread use
  700.      * an assertion method from the test object instance. </i>
  701.      *
  702.      * @param msg
  703.      *            a message
  704.      * @param bifunction
  705.      *            the bifunction to be executed and that should return an
  706.      *            exception
  707.      * @param param1
  708.      *            the first parameter to be used.
  709.      * @param param2
  710.      *            the second parameter to be used.
  711.      * @return {@link AssertThatException the assert DSL on the exception}
  712.      * @param <P1>
  713.      *            the type of the first parameter
  714.      * @param <P2>
  715.      *            the type of the second parameter
  716.      * @param <T>
  717.      *            the exception type
  718.      * @since 0.3.0
  719.      * @see Test#fastFail() The documentation of the <code>fastFail</code>
  720.      *      attribute of the <code>@Test</code> annotation, regarding the action
  721.      *      done by this assertion.
  722.      */
  723.     default <P1, P2, T extends RuntimeException> AssertThatException<T> assertWhenBiFunction(
  724.             String msg, BiFunction<P1, P2, ?> bifunction, P1 param1, P2 param2) {
  725.         return assertWhen(msg, (p) -> bifunction.apply(param1, param2));
  726.     }

  727.     /**
  728.      * Assert that a bifunction throw an exception. As {@link BiFunction}
  729.      * signature doesn't throws exception, it should be a RuntimeException. <br>
  730.      * <br>
  731.      * <i>By default, assertThat can only be used from the main thread of the
  732.      * test ; When used from another thread, the assertion will be lost. In the
  733.      * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
  734.      * annotation is used, the assertion may not be lost, in case the thread use
  735.      * an assertion method from the test object instance. </i>
  736.      *
  737.      * @param bifunction
  738.      *            the bifunction to be executed and that should return an
  739.      *            exception
  740.      * @param param1
  741.      *            the first parameter to be used.
  742.      * @param param2
  743.      *            the second parameter to be used.
  744.      * @return {@link AssertThatException the assert DSL on the exception}
  745.      * @param <P1>
  746.      *            the type of the first parameter
  747.      * @param <P2>
  748.      *            the type of the second parameter
  749.      * @param <T>
  750.      *            the exception type
  751.      * @since 0.3.0
  752.      * @see Test#fastFail() The documentation of the <code>fastFail</code>
  753.      *      attribute of the <code>@Test</code> annotation, regarding the action
  754.      *      done by this assertion.
  755.      */
  756.     default <P1, P2, T extends RuntimeException> AssertThatException<T> assertWhenBiFunction(
  757.             BiFunction<P1, P2, ?> bifunction, P1 param1, P2 param2) {
  758.         return assertWhen((p) -> bifunction.apply(param1, param2));
  759.     }

  760.     /**
  761.      * Always produce a failure.
  762.      * <p>
  763.      * For instance :
  764.      *
  765.      * <pre>
  766.      * fail();
  767.      * </pre>
  768.      *
  769.      * will immediately fail the current test. <br>
  770.      * <br>
  771.      * <i>By default, fail can only be used from the main thread of the test ;
  772.      * When used from another thread, the assertion will be lost. In the case
  773.      * the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
  774.      * annotation is used, the assertion may not be lost, in case the thread use
  775.      * an assertion method from the test object instance. </i>
  776.      *
  777.      * @return Depending on {@link Test#fastFail()} : If <code>true</code>, then
  778.      *         fail the test, else, return false and the test will be failed
  779.      *         later.
  780.      * @see Test#fastFail() The documentation of the <code>fastFail</code>
  781.      *      attribute of the <code>@Test</code> annotation, regarding the action
  782.      *      done by this failure.
  783.      */
  784.     default boolean fail() {
  785.         return fail("Manual failure");
  786.     }

  787.     /**
  788.      * Always produce a failure.
  789.      * <p>
  790.      * For instance :
  791.      *
  792.      * <pre>
  793.      * fail(&quot;my message&quot;);
  794.      * </pre>
  795.      *
  796.      * will immediately fail the current test. <br>
  797.      * <br>
  798.      * <i>By default, fail can only be used from the main thread of the test ;
  799.      * When used from another thread, the assertion will be lost. In the case
  800.      * the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
  801.      * annotation is used, the assertion may not be lost, in case the thread use
  802.      * an assertion method from the test object instance. </i>
  803.      *
  804.      * @param msg
  805.      *            a message
  806.      * @return Depending on {@link Test#fastFail()} : If <code>true</code>, then
  807.      *         fail the test, else, return false and the test will be failed
  808.      *         later.
  809.      * @see Test#fastFail() The documentation of the <code>fastFail</code>
  810.      *      attribute of the <code>@Test</code> annotation, regarding the action
  811.      *      done by this failure.
  812.      */
  813.     default boolean fail(String msg) {
  814.         return FailureImpl.fail(this, new AssertionError(msg));
  815.     }

  816.     /**
  817.      * Always produce a failure.
  818.      * <p>
  819.      * For instance :
  820.      *
  821.      * <pre>
  822.      * fail(&quot;my message&quot;, t);
  823.      * </pre>
  824.      *
  825.      * will immediately fail the current test. <br>
  826.      * <br>
  827.      * <i>By default, fail can only be used from the main thread of the test ;
  828.      * When used from another thread, the assertion will be lost. In the case
  829.      * the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
  830.      * annotation is used, the assertion may not be lost, in case the thread use
  831.      * an assertion method from the test object instance. </i>
  832.      *
  833.      * @param msg
  834.      *            a message
  835.      * @param innerError
  836.      *            the error cause
  837.      * @return Depending on {@link Test#fastFail()} : If <code>true</code>, then
  838.      *         fail the test, else, return false and the test will be failed
  839.      *         later.
  840.      * @see Test#fastFail() The documentation of the <code>fastFail</code>
  841.      *      attribute of the <code>@Test</code> annotation, regarding the action
  842.      *      done by this failure.
  843.      */
  844.     default boolean fail(String msg, Throwable innerError) {
  845.         return FailureImpl.fail(this, new AssertionError(msg, innerError));
  846.     }

  847.     /**
  848.      * Always produce a failure.
  849.      * <p>
  850.      * For instance :
  851.      *
  852.      * <pre>
  853.      * fail(t);
  854.      * </pre>
  855.      *
  856.      * will immediately fail the current test. <br>
  857.      * <br>
  858.      * <i>By default, fail can only be used from the main thread of the test ;
  859.      * When used from another thread, the assertion will be lost. In the case
  860.      * the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
  861.      * annotation is used, the assertion may not be lost, in case the thread use
  862.      * an assertion method from the test object instance. </i>
  863.      *
  864.      * @param innerError
  865.      *            the error cause
  866.      * @return Depending on {@link Test#fastFail()} : If <code>true</code>, then
  867.      *         fail the test, else, return false and the test will be failed
  868.      *         later.
  869.      * @see Test#fastFail() The documentation of the <code>fastFail</code>
  870.      *      attribute of the <code>@Test</code> annotation, regarding the action
  871.      *      done by this failure.
  872.      */
  873.     default boolean fail(Throwable innerError) {
  874.         return FailureImpl.fail(this,
  875.                 new AssertionError(innerError.getMessage(), innerError));
  876.     }

  877. }