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