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 junit.framework.TestCase;
022import org.checkerframework.checker.nullness.qual.Nullable;
023
024/**
025 * This abstract base class for testers allows the framework to inject needed information after
026 * JUnit constructs the instances.
027 *
028 * <p>This class is emulated in GWT.
029 *
030 * @param <G> the type of the test generator required by this tester. An instance of G should
031 *     somehow provide an instance of the class under test, plus any other information required to
032 *     parameterize the test.
033 * @author George van den Driessche
034 */
035@GwtCompatible(emulated = true)
036public class AbstractTester<G> extends TestCase {
037  private G subjectGenerator;
038  private String suiteName;
039  private @Nullable Runnable setUp;
040  private @Nullable Runnable tearDown;
041
042  // public so that it can be referenced in generated GWT tests.
043  @Override
044  public void setUp() throws Exception {
045    if (setUp != null) {
046      setUp.run();
047    }
048  }
049
050  // public so that it can be referenced in generated GWT tests.
051  @Override
052  public void tearDown() throws Exception {
053    if (tearDown != null) {
054      tearDown.run();
055    }
056  }
057
058  // public so that it can be referenced in generated GWT tests.
059  public final void init(
060      G subjectGenerator, String suiteName, @Nullable Runnable setUp, @Nullable Runnable tearDown) {
061    this.subjectGenerator = subjectGenerator;
062    this.suiteName = suiteName;
063    this.setUp = setUp;
064    this.tearDown = tearDown;
065  }
066
067  // public so that it can be referenced in generated GWT tests.
068  public final void init(G subjectGenerator, String suiteName) {
069    init(subjectGenerator, suiteName, null, null);
070  }
071
072  public G getSubjectGenerator() {
073    return subjectGenerator;
074  }
075
076  /** Returns the name of the test method invoked by this test instance. */
077  @GwtIncompatible // not used under GWT, and super.getName() is not available under J2CL
078  public final String getTestMethodName() {
079    return super.getName();
080  }
081
082  @GwtIncompatible // not used under GWT, and super.getName() is not available under J2CL
083  @Override
084  public String getName() {
085    return Platform.format("%s[%s]", super.getName(), suiteName);
086  }
087}