• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 junit.framework.TestCase;
20 
21 /**
22  * This abstract base class for testers allows the framework to inject needed
23  * information after JUnit constructs the instances.
24  *
25  * <p>This class is emulated in GWT.
26  *
27  * @param <G> the type of the test generator required by this tester. An
28  * instance of G should somehow provide an instance of the class under test,
29  * plus any other information required to parameterize the test.
30  *
31  * @author George van den Driessche
32  */
33 public class AbstractTester<G> extends TestCase {
34   private G subjectGenerator;
35   private String suiteName;
36   private Runnable setUp;
37   private Runnable tearDown;
38 
39   // public so that it can be referenced in generated GWT tests.
setUp()40   @Override public void setUp() throws Exception {
41     if (setUp != null) {
42       setUp.run();
43     }
44   }
45 
46   // public so that it can be referenced in generated GWT tests.
tearDown()47   @Override public void tearDown() throws Exception {
48     if (tearDown != null) {
49       tearDown.run();
50     }
51   }
52 
53   // public so that it can be referenced in generated GWT tests.
init( G subjectGenerator, String suiteName, Runnable setUp, Runnable tearDown)54   public final void init(
55       G subjectGenerator, String suiteName, Runnable setUp, Runnable tearDown) {
56     this.subjectGenerator = subjectGenerator;
57     this.suiteName = suiteName;
58     this.setUp = setUp;
59     this.tearDown = tearDown;
60   }
61 
62   // public so that it can be referenced in generated GWT tests.
init(G subjectGenerator, String suiteName)63   public final void init(G subjectGenerator, String suiteName) {
64     init(subjectGenerator, suiteName, null, null);
65   }
66 
getSubjectGenerator()67   public G getSubjectGenerator() {
68     return subjectGenerator;
69   }
70 
71   /** Returns the name of the test method invoked by this test instance. */
getTestMethodName()72   public final String getTestMethodName() {
73     return super.getName();
74   }
75 
getName()76   @Override public String getName() {
77     return Platform.format("%s[%s]", super.getName(), suiteName);
78   }
79 }
80