001: /*
002: * Copyright 2006-2007, Unitils.org
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: package org.unitils;
017:
018: import org.testng.annotations.*;
019: import static org.unitils.TracingTestListener.*;
020: import org.unitils.core.TestListener;
021: import org.unitils.core.Unitils;
022:
023: /**
024: * TestNG test class containing 2 test methods. This test test-class is used in the {@link UnitilsInvocationTest} tests.
025: *
026: * @author Tim Ducheyne
027: */
028: public class UnitilsTestNGTest_TestClass2 extends UnitilsTestNG {
029:
030: /* Test listener that will record all invocations */
031: private static TracingTestListener tracingTestListener;
032:
033: /**
034: * Sets the tracing test listener that will record all invocations.
035: *
036: * @param testListener the listener
037: */
038: public static void setTracingTestListener(
039: TracingTestListener testListener) {
040: tracingTestListener = testListener;
041: }
042:
043: @BeforeClass
044: public void beforeClass() {
045: addTestInvocation(TEST_BEFORE_CLASS, null);
046: }
047:
048: @AfterClass
049: public void afterClass() {
050: addTestInvocation(TEST_AFTER_CLASS, null);
051: }
052:
053: @BeforeMethod
054: public void setUp() {
055: addTestInvocation(TEST_SET_UP, null);
056: }
057:
058: @AfterMethod
059: public void tearDown() {
060: addTestInvocation(TEST_TEAR_DOWN, null);
061: }
062:
063: @Test
064: public void test1() {
065: addTestInvocation(TEST_METHOD, "test1");
066: }
067:
068: @Test
069: public void test2() {
070: addTestInvocation(TEST_METHOD, "test2");
071: }
072:
073: /**
074: * Records an invocation.
075: *
076: * @param invocation the invocation type, not null
077: * @param testMethodName the actual test name, null if not applicable
078: */
079: private void addTestInvocation(String invocation,
080: String testMethodName) {
081: if (tracingTestListener != null) {
082: tracingTestListener.addTestInvocation(invocation, this ,
083: testMethodName);
084: }
085: }
086:
087: /**
088: * Overridden to install the tracing test listener.
089: *
090: * @return the unitils instance, not null
091: */
092: @Override
093: protected Unitils getUnitils() {
094: if (tracingTestListener != null) {
095: return new Unitils() {
096:
097: @Override
098: public TestListener createTestListener() {
099: return tracingTestListener;
100: }
101: };
102: }
103: return super.getUnitils();
104: }
105: }
|