001/*
002 * Copyright (C) 2008 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect.testing;
018
019import com.google.common.annotations.GwtCompatible;
020import com.google.common.annotations.GwtIncompatible;
021import com.google.common.annotations.J2ktIncompatible;
022import junit.framework.TestCase;
023import org.checkerframework.checker.nullness.qual.Nullable;
024
025/**
026 * This abstract base class for testers allows the framework to inject needed information after
027 * JUnit constructs the instances.
028 *
029 * <p>This class is emulated in GWT.
030 *
031 * @param <G> the type of the test generator required by this tester. An instance of G should
032 *     somehow provide an instance of the class under test, plus any other information required to
033 *     parameterize the test.
034 * @author George van den Driessche
035 */
036@GwtCompatible(emulated = true)
037@ElementTypesAreNonnullByDefault
038public class AbstractTester<G> extends TestCase {
039  private G subjectGenerator;
040  private String suiteName;
041  private @Nullable Runnable setUp;
042  private @Nullable Runnable tearDown;
043
044  // public so that it can be referenced in generated GWT tests.
045  @Override
046  public void setUp() throws Exception {
047    if (setUp != null) {
048      setUp.run();
049    }
050  }
051
052  // public so that it can be referenced in generated GWT tests.
053  @Override
054  public void tearDown() throws Exception {
055    if (tearDown != null) {
056      tearDown.run();
057    }
058  }
059
060  // public so that it can be referenced in generated GWT tests.
061  public final void init(
062      G subjectGenerator, String suiteName, @Nullable Runnable setUp, @Nullable Runnable tearDown) {
063    this.subjectGenerator = subjectGenerator;
064    this.suiteName = suiteName;
065    this.setUp = setUp;
066    this.tearDown = tearDown;
067  }
068
069  // public so that it can be referenced in generated GWT tests.
070  public final void init(G subjectGenerator, String suiteName) {
071    init(subjectGenerator, suiteName, null, null);
072  }
073
074  public G getSubjectGenerator() {
075    return subjectGenerator;
076  }
077
078  /** Returns the name of the test method invoked by this test instance. */
079  @J2ktIncompatible
080  @GwtIncompatible // not used under GWT, and super.getName() is not available under J2CL
081  public final String getTestMethodName() {
082    return super.getName();
083  }
084
085  @J2ktIncompatible
086  @GwtIncompatible // not used under GWT, and super.getName() is not available under J2CL
087  @Override
088  public String getName() {
089    return Platform.format("%s[%s]", super.getName(), suiteName);
090  }
091}