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