1 /* 2 * Copyright (C) 2008 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.collect.testing; 18 19 import com.google.common.annotations.GwtCompatible; 20 import junit.framework.TestCase; 21 22 /** 23 * This abstract base class for testers allows the framework to inject needed information after 24 * JUnit constructs the instances. 25 * 26 * <p>This class is emulated in GWT. 27 * 28 * @param <G> the type of the test generator required by this tester. An instance of G should 29 * somehow provide an instance of the class under test, plus any other information required to 30 * parameterize the test. 31 * @author George van den Driessche 32 */ 33 @GwtCompatible 34 public class AbstractTester<G> extends TestCase { 35 private G subjectGenerator; 36 private String suiteName; 37 private Runnable setUp; 38 private Runnable tearDown; 39 40 // public so that it can be referenced in generated GWT tests. 41 @Override setUp()42 public void setUp() throws Exception { 43 if (setUp != null) { 44 setUp.run(); 45 } 46 } 47 48 // public so that it can be referenced in generated GWT tests. 49 @Override tearDown()50 public void tearDown() throws Exception { 51 if (tearDown != null) { 52 tearDown.run(); 53 } 54 } 55 56 // public so that it can be referenced in generated GWT tests. init(G subjectGenerator, String suiteName, Runnable setUp, Runnable tearDown)57 public final void init(G subjectGenerator, String suiteName, Runnable setUp, Runnable tearDown) { 58 this.subjectGenerator = subjectGenerator; 59 this.suiteName = suiteName; 60 this.setUp = setUp; 61 this.tearDown = tearDown; 62 } 63 64 // public so that it can be referenced in generated GWT tests. init(G subjectGenerator, String suiteName)65 public final void init(G subjectGenerator, String suiteName) { 66 init(subjectGenerator, suiteName, null, null); 67 } 68 getSubjectGenerator()69 public G getSubjectGenerator() { 70 return subjectGenerator; 71 } 72 73 /** Returns the name of the test method invoked by this test instance. */ getTestMethodName()74 public final String getTestMethodName() { 75 return super.getName(); 76 } 77 78 @Override getName()79 public String getName() { 80 return Platform.format("%s[%s]", super.getName(), suiteName); 81 } 82 } 83