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  import junit.framework.TestCase;
21  
22  /**
23   * This abstract base class for testers allows the framework to inject needed
24   * information after JUnit constructs the instances.
25   *
26   * <p>This class is emulated in GWT.
27   *
28   * @param <G> the type of the test generator required by this tester. An
29   * instance of G should somehow provide an instance of the class under test,
30   * plus any other information required to parameterize the test.
31   *
32   * @author George van den Driessche
33   */
34  @GwtCompatible
35  public class AbstractTester<G> extends TestCase {
36    private G subjectGenerator;
37    private String suiteName;
38    private Runnable setUp;
39    private Runnable tearDown;
40  
41    // public so that it can be referenced in generated GWT tests.
42    @Override
43    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
51    public void tearDown() throws Exception {
52      if (tearDown != null) {
53        tearDown.run();
54      }
55    }
56  
57    // public so that it can be referenced in generated GWT tests.
58    public final void init(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
80    public String getName() {
81      return Platform.format("%s[%s]", super.getName(), suiteName);
82    }
83  }