View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2014 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package io.wcm.testing.junit.rules.parameterized;
21  
22  /*
23   * (C) 2012 Jens Schauder http://blog.schauderhaft.de/
24   * Code initially published here https://github.com/schauder/parameterizedTestsWithRules
25   * Slightly simplified for wcm.io.
26   */
27  import org.junit.runners.model.Statement;
28  
29  class RepeatedStatement<T> extends Statement {
30  
31    private final Statement test;
32    private final Iterable<T> values;
33    private final AccessibleErrorCollector errorCollector;
34  
35    private final Callback<T> setUpCallback;
36    private final Callback<T> tearDownCallback;
37  
38    RepeatedStatement(final Statement test, final Iterable<T> values,
39        final AccessibleErrorCollector errorCollector,
40        final Callback<T> setUpCallback, final Callback<T> tearDownCallback) {
41      this.test = test;
42      this.values = values;
43      this.errorCollector = errorCollector;
44      this.setUpCallback = setUpCallback;
45      this.tearDownCallback = tearDownCallback;
46    }
47  
48    @Override
49    // CHECKSTYLE:OFF
50    public void evaluate() throws Throwable {
51      // CHECKSTYLE:ON
52      for (T v : this.values) {
53        try {
54          if (this.setUpCallback != null) {
55            this.setUpCallback.execute(v);
56          }
57          this.test.evaluate();
58        }
59        /*CHECKSTYLE:OFF*/ catch (Exception ex) { /*CHECKSTYLE:ON*/
60          this.errorCollector.addError(new AssertionError(buildAssertionMessage("For value " + v, ex), ex));
61        }
62        finally {
63          try {
64            if (this.tearDownCallback != null) {
65              this.tearDownCallback.execute(v);
66            }
67          }
68          /*CHECKSTYLE:OFF*/ catch (Exception ex) { /*CHECKSTYLE:ON*/
69            this.errorCollector.addError(new AssertionError(buildAssertionMessage("For value " + v + " (teardown)", ex), ex));
70          }
71        }
72      }
73      this.errorCollector.verify();
74    }
75  
76    private static String buildAssertionMessage(String msg, Throwable ex) {
77      StringBuilder sb = new StringBuilder()
78          .append(msg)
79          .append(": ");
80      if (ex.getMessage() != null) {
81        sb.append(ex.getMessage());
82      }
83      else {
84        sb.append(ex.getClass().getName());
85      }
86      return sb.toString();
87    }
88  
89  }