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