View Javadoc
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.test.core;
21  
22  import ch.powerunit.Ignore;
23  import ch.powerunit.Test;
24  import ch.powerunit.TestContext;
25  import ch.powerunit.TestResultListener;
26  import ch.powerunit.TestSuite;
27  import ch.powerunit.impl.DefaultPowerUnitRunnerImpl;
28  
29  public class TestResultListenerTests {
30      public static void main(String[] args) {
31          AllTests.testNoException(
32                  "testResultListenerOK",
33                  () -> {
34                      DefaultPowerUnitRunnerImpl<TestClass> runner = new DefaultPowerUnitRunnerImpl<>(
35                              TestClass.class);
36                      runner.addListener(new Listener());
37                      runner.run();
38                      TestSuite.DSL.assertThat(counter).is(
39                              1000l + 10000 + 100000 + 100000 + 1000000
40                                      + 10000000 + 100000 + 100000000
41                                      + 1000000000 + 100000 + 100000 + 100000000);
42                  });
43      }
44  
45      private static class Listener implements TestResultListener<TestClass> {
46  
47          @Override
48          public void notifySetStart(String setName, String parameters) {
49              counter += 1000;
50          }
51  
52          @Override
53          public void notifySetEnd(String setName, String parameters) {
54              counter += 10000;
55          }
56  
57          @Override
58          public void notifyStart(TestContext<TestClass> context) {
59              counter += 100000;
60          }
61  
62          @Override
63          public void notifySuccess(TestContext<TestClass> context) {
64              counter += 1000000;
65              TestSuite.DSL.assertThat(context.getLocalTestName()).is("success");
66          }
67  
68          @Override
69          public void notifyFailure(TestContext<TestClass> context,
70                  Throwable cause) {
71              counter += 10000000;
72              TestSuite.DSL.assertThat(context.getLocalTestName()).is("failure");
73          }
74  
75          @Override
76          public void notifySkipped(TestContext<TestClass> context) {
77              counter += 100000000;
78              TestSuite.DSL.assertThat(context.getLocalTestName()).is(
79                      TestSuite.DSL.either(TestSuite.DSL.is("skip")).or(
80                              TestSuite.DSL.is("skipOnAssume")));
81          }
82  
83          @Override
84          public void notifyError(TestContext<TestClass> context, Throwable cause) {
85              counter += 1000000000;
86              TestSuite.DSL.assertThat(context.getLocalTestName()).is("error");
87          }
88  
89          @Override
90          public void notifyParameterStart(String setName, String parameterName) {
91              counter += 10000000000l;
92          }
93  
94          @Override
95          public void notifyParameterEnd(String setName, String parameterName) {
96              counter += 100000000000l;
97          }
98  
99      }
100 
101     private static long counter = 0;
102 
103     public static class TestClass implements TestSuite {
104         @Test
105         public void success() {
106         }
107 
108         @Test
109         public void failure() {
110             fail();
111         }
112 
113         @Test
114         public void error() {
115             throw new IllegalArgumentException("xxx");
116         }
117 
118         @Override
119         @Test
120         @Ignore
121         public void skip() {
122 
123         }
124 
125         @Test
126         public void skipOnAssume() {
127             assumeThat(true).is(false);
128         }
129     }
130 }