001: package org.testng;
002:
003: import java.lang.reflect.Method;
004: import java.util.ArrayList;
005: import java.util.HashMap;
006: import java.util.List;
007: import java.util.Map;
008:
009: import org.testng.internal.ConfigurationMethod;
010: import org.testng.internal.NoOpTestClass;
011: import org.testng.internal.RunInfo;
012: import org.testng.internal.TestNGMethod;
013: import org.testng.internal.Utils;
014: import org.testng.internal.annotations.IAnnotationFinder;
015:
016: /**
017: * This class represents a test class:
018: * - The test methods
019: * - The configuration methods (test and method)
020: * - The class file
021: *
022: * @author <a href="mailto:cedric@beust.com">Cedric Beust</a>
023: * @author <a href='mailto:the_mindstorm@evolva.ro'>Alexandru Popescu</a>
024: */
025: public class TestClass extends NoOpTestClass implements ITestClass {
026: /* generated */
027: private static final long serialVersionUID = -8077917128278361294L;
028: transient private IAnnotationFinder m_annotationFinder = null;
029: // The Strategy used to locate test methods (TestNG, JUnit, etc...)
030: transient private ITestMethodFinder m_testMethodFinder = null;
031:
032: transient protected Map<Class, Class> m_testClasses = new HashMap<Class, Class>();
033: transient protected Map<Class, Object[]> m_instanceMap = new HashMap<Class, Object[]>();
034:
035: private IClass m_iClass = null;
036: private RunInfo m_runInfo = null;
037:
038: public TestClass(IClass cls, String testName,
039: ITestMethodFinder testMethodFinder,
040: IAnnotationFinder annotationFinder, RunInfo runInfo) {
041: init(cls, testName, testMethodFinder, annotationFinder, runInfo);
042: }
043:
044: public TestClass(IClass cls, TestClass tc) {
045: init(cls, tc.getTestName(), tc.getTestMethodFinder(), tc
046: .getAnnotationFinder(), tc.getRunInfo());
047: }
048:
049: public String getTestName() {
050: return m_testName;
051: }
052:
053: public IAnnotationFinder getAnnotationFinder() {
054: return m_annotationFinder;
055: }
056:
057: private void init(IClass cls, String testName,
058: ITestMethodFinder testMethodFinder,
059: IAnnotationFinder annotationFinder, RunInfo runInfo) {
060: log(3, "Creating TestClass for " + cls);
061: m_iClass = cls;
062: m_testClass = cls.getRealClass();
063: m_testName = testName;
064: m_runInfo = runInfo;
065: m_testMethodFinder = testMethodFinder;
066: m_annotationFinder = annotationFinder;
067: initMethods();
068: initTestClassesAndInstances();
069: }
070:
071: private void initTestClassesAndInstances() {
072: //
073: // TestClasses and instances
074: //
075: Object[] instances = getInstances(false);
076: for (Object instance : instances) {
077: Class cls = instance.getClass();
078: if (null == m_testClasses.get(cls)) {
079: m_testClasses.put(cls, cls);
080: m_instanceMap.put(cls, instances);
081: }
082: }
083:
084: }
085:
086: public Object[] getInstances(boolean create) {
087: return m_iClass.getInstances(create);
088: }
089:
090: public long[] getInstanceHashCodes() {
091: return m_iClass.getInstanceHashCodes();
092: }
093:
094: public int getInstanceCount() {
095: return m_iClass.getInstanceCount();
096: }
097:
098: public void addInstance(Object instance) {
099: m_iClass.addInstance(instance);
100: }
101:
102: private void initMethods() {
103: ITestNGMethod[] methods = m_testMethodFinder
104: .getTestMethods(m_testClass);
105: m_testMethods = createTestMethods(methods);
106:
107: m_beforeSuiteMethods = ConfigurationMethod
108: .createSuiteConfigurationMethods(m_testMethodFinder
109: .getBeforeSuiteMethods(m_testClass),
110: m_annotationFinder, true);
111: m_afterSuiteMethods = ConfigurationMethod
112: .createSuiteConfigurationMethods(m_testMethodFinder
113: .getAfterSuiteMethods(m_testClass),
114: m_annotationFinder, false);
115:
116: m_beforeTestConfMethods = ConfigurationMethod
117: .createTestConfigurationMethods(
118: m_testMethodFinder
119: .getBeforeTestConfigurationMethods(m_testClass),
120: m_annotationFinder, true);
121: m_afterTestConfMethods = ConfigurationMethod
122: .createTestConfigurationMethods(m_testMethodFinder
123: .getAfterTestConfigurationMethods(m_testClass),
124: m_annotationFinder, false);
125:
126: m_beforeClassMethods = ConfigurationMethod
127: .createClassConfigurationMethods(m_testMethodFinder
128: .getBeforeClassMethods(m_testClass),
129: m_annotationFinder, true);
130: m_afterClassMethods = ConfigurationMethod
131: .createClassConfigurationMethods(m_testMethodFinder
132: .getAfterClassMethods(m_testClass),
133: m_annotationFinder, false);
134:
135: m_beforeGroupsMethods = ConfigurationMethod
136: .createBeforeConfigurationMethods(
137: m_testMethodFinder
138: .getBeforeGroupsConfigurationMethods(m_testClass),
139: m_annotationFinder, true);
140: m_afterGroupsMethods = ConfigurationMethod
141: .createAfterConfigurationMethods(
142: m_testMethodFinder
143: .getAfterGroupsConfigurationMethods(m_testClass),
144: m_annotationFinder, false);
145:
146: m_beforeTestMethods = ConfigurationMethod
147: .createTestMethodConfigurationMethods(
148: m_testMethodFinder
149: .getBeforeTestMethods(m_testClass),
150: m_annotationFinder, true);
151: m_afterTestMethods = ConfigurationMethod
152: .createTestMethodConfigurationMethods(
153: m_testMethodFinder
154: .getAfterTestMethods(m_testClass),
155: m_annotationFinder, false);
156:
157: }
158:
159: /**
160: * Create the test methods that belong to this class (rejects
161: * all those that belong to a different class).
162: */
163: private ITestNGMethod[] createTestMethods(ITestNGMethod[] methods) {
164: List<ITestNGMethod> vResult = new ArrayList<ITestNGMethod>();
165: for (ITestNGMethod tm : methods) {
166: Method m = tm.getMethod();
167: if (m.getDeclaringClass().isAssignableFrom(m_testClass)) {
168: log(4, "Adding method " + tm + " on TestClass "
169: + m_testClass);
170: vResult
171: .add(new TestNGMethod(
172: /* tm.getRealClass(), */m,
173: m_annotationFinder));
174: } else {
175: log(4, "Rejecting method " + tm + " for TestClass "
176: + m_testClass);
177: }
178: }
179:
180: ITestNGMethod[] result = vResult
181: .toArray(new ITestNGMethod[vResult.size()]);
182: return result;
183: }
184:
185: private RunInfo getRunInfo() {
186: return m_runInfo;
187: }
188:
189: public ITestMethodFinder getTestMethodFinder() {
190: return m_testMethodFinder;
191: }
192:
193: private void log(int level, String s) {
194: Utils.log("TestClass", level, s);
195: }
196:
197: private static void ppp(String s) {
198: System.out.println("[TestClass] " + s);
199: }
200:
201: public void dump() {
202: ppp("\n======\nTESTCLASS: " + m_testClass.getName());
203: for (ITestNGMethod m : m_beforeClassMethods) {
204: ppp("BeforeClass : " + m);
205: }
206: for (ITestNGMethod m : m_beforeTestMethods) {
207: ppp("BeforeMethod:\t" + m);
208: }
209: for (ITestNGMethod m : m_testMethods) {
210: ppp("Test :\t\t" + m);
211: }
212: for (ITestNGMethod m : m_afterTestMethods) {
213: ppp("AfterMethod :\t" + m);
214: }
215: for (ITestNGMethod m : m_afterClassMethods) {
216: ppp("AfterClass : " + m);
217: }
218: ppp("\n======\n");
219: }
220:
221: @Override
222: public String toString() {
223: return "[TestClass " + m_testClass + "]";
224: }
225:
226: }
|