View Javadoc
1   /*
2    * Copyright (C) 2008 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect.testing;
18  
19  import com.google.common.annotations.GwtCompatible;
20  
21  import junit.framework.TestCase;
22  
23  /**
24   * This abstract base class for testers allows the framework to inject needed
25   * information after JUnit constructs the instances.
26   *
27   * <p>This class is emulated in GWT.
28   *
29   * @param <G> the type of the test generator required by this tester. An
30   * instance of G should somehow provide an instance of the class under test,
31   * plus any other information required to parameterize the test.
32   *
33   * @author George van den Driessche
34   */
35  @GwtCompatible
36  public class AbstractTester<G> extends TestCase {
37    private G subjectGenerator;
38    private String suiteName;
39    private Runnable setUp;
40    private Runnable tearDown;
41  
42    // public so that it can be referenced in generated GWT tests.
43    @Override public void setUp() throws Exception {
44      if (setUp != null) {
45        setUp.run();
46      }
47    }
48  
49    // public so that it can be referenced in generated GWT tests.
50    @Override public void tearDown() throws Exception {
51      if (tearDown != null) {
52        tearDown.run();
53      }
54    }
55  
56    // public so that it can be referenced in generated GWT tests.
57    public final void init(
58        G subjectGenerator, String suiteName, Runnable setUp, Runnable tearDown) {
59      this.subjectGenerator = subjectGenerator;
60      this.suiteName = suiteName;
61      this.setUp = setUp;
62      this.tearDown = tearDown;
63    }
64  
65    // public so that it can be referenced in generated GWT tests.
66    public final void init(G subjectGenerator, String suiteName) {
67      init(subjectGenerator, suiteName, null, null);
68    }
69  
70    public G getSubjectGenerator() {
71      return subjectGenerator;
72    }
73  
74    /** Returns the name of the test method invoked by this test instance. */
75    public final String getTestMethodName() {
76      return super.getName();
77    }
78  
79    @Override public String getName() {
80      return Platform.format("%s[%s]", super.getName(), suiteName);
81    }
82  }