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