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.jspecify.annotations.NullMarked;
024import org.jspecify.annotations.Nullable;
025
026/**
027 * This abstract base class for testers allows the framework to inject needed information after
028 * JUnit constructs the instances.
029 *
030 * <p>This class is emulated in GWT.
031 *
032 * @param <G> the type of the test generator required by this tester. An instance of G should
033 *     somehow provide an instance of the class under test, plus any other information required to
034 *     parameterize the test.
035 * @author George van den Driessche
036 */
037@GwtCompatible(emulated = true)
038@NullMarked
039public class AbstractTester<G> extends TestCase {
040  private G subjectGenerator;
041  private String suiteName;
042  private @Nullable Runnable setUp;
043  private @Nullable Runnable tearDown;
044
045  // public so that it can be referenced in generated GWT tests.
046  @Override
047  public void setUp() throws Exception {
048    if (setUp != null) {
049      setUp.run();
050    }
051  }
052
053  // public so that it can be referenced in generated GWT tests.
054  @Override
055  public void tearDown() throws Exception {
056    if (tearDown != null) {
057      tearDown.run();
058    }
059  }
060
061  // public so that it can be referenced in generated GWT tests.
062  public final void init(
063      G subjectGenerator, String suiteName, @Nullable Runnable setUp, @Nullable Runnable tearDown) {
064    this.subjectGenerator = subjectGenerator;
065    this.suiteName = suiteName;
066    this.setUp = setUp;
067    this.tearDown = tearDown;
068  }
069
070  // public so that it can be referenced in generated GWT tests.
071  public final void init(G subjectGenerator, String suiteName) {
072    init(subjectGenerator, suiteName, null, null);
073  }
074
075  public G getSubjectGenerator() {
076    return subjectGenerator;
077  }
078
079  /** Returns the name of the test method invoked by this test instance. */
080  @J2ktIncompatible
081  @GwtIncompatible // not used under GWT, and super.getName() is not available under J2CL
082  public final String getTestMethodName() {
083    return super.getName();
084  }
085
086  @J2ktIncompatible
087  @GwtIncompatible // not used under GWT, and super.getName() is not available under J2CL
088  @Override
089  public String getName() {
090    return super.getName() + '[' + suiteName + ']';
091  }
092
093  /**
094   * Asserts that the given object is non-null, with a better failure message than {@link
095   * TestCase#assertNull(String, Object)}.
096   *
097   * <p>The {@link TestCase} version (which is from JUnit 3) produces a failure message that does
098   * not include the value of the object.
099   *
100   * @since 33.4.0
101   */
102  public static void assertNull(String message, Object object) {
103    assertEquals(message, null, object);
104  }
105}