001: package test.mannotation;
002:
003: import java.lang.reflect.Constructor;
004: import java.lang.reflect.Method;
005:
006: import org.testng.Assert;
007: import org.testng.internal.annotations.DefaultAnnotationTransformer;
008: import org.testng.internal.annotations.IAfterSuite;
009: import org.testng.internal.annotations.IBeforeSuite;
010: import org.testng.internal.annotations.IConfiguration;
011: import org.testng.internal.annotations.IDataProvider;
012: import org.testng.internal.annotations.IExpectedExceptions;
013: import org.testng.internal.annotations.IFactory;
014: import org.testng.internal.annotations.IParameters;
015: import org.testng.internal.annotations.ITest;
016: import org.testng.internal.annotations.JDK14AnnotationFinder;
017:
018: /**
019: * @testng.test
020: */
021: public class MAnnotationSampleTest {
022: private JDK14AnnotationFinder m_finder;
023:
024: /**
025: * @testng.before-class description = "New annotation"
026: */
027: public void init() {
028: m_finder = new JDK14AnnotationFinder(
029: new DefaultAnnotationTransformer());
030: m_finder.addSourceDirs(new String[] { "./test-14/src", "src" });
031: }
032:
033: public void verifyTestClassLevel() {
034: //
035: // Tests on MTest1SampleTest
036: //
037: ITest test1 = (ITest) m_finder.findAnnotation(MTest1.class,
038: ITest.class);
039: Assert.assertTrue(test1.getEnabled());
040: String[] groups = test1.getGroups();
041: Assert
042: .assertEquals(new String[] { "group1", "group2" },
043: groups);
044: Assert.assertTrue(test1.getAlwaysRun());
045: Assert.assertEquals(new String[] { "param1", "param2" }, test1
046: .getParameters());
047: Assert.assertEquals(new String[] { "dg1", "dg2" }, test1
048: .getDependsOnGroups());
049: Assert.assertEquals(new String[] { "dm1", "dm2" }, test1
050: .getDependsOnMethods());
051: Assert.assertEquals(42, test1.getTimeOut());
052: Assert.assertEquals(43, test1.getInvocationCount());
053: Assert.assertEquals(44, test1.getSuccessPercentage());
054: Assert.assertEquals("dp", test1.getDataProvider());
055: Assert.assertEquals("Class level description", test1
056: .getDescription());
057:
058: //
059: // Tests on MTest1SampleTest (test defaults)
060: //
061: ITest test2 = (ITest) m_finder.findAnnotation(MTest2.class,
062: ITest.class);
063: // test default for enabled
064: Assert.assertTrue(test2.getEnabled());
065: Assert.assertFalse(test2.getAlwaysRun());
066: Assert.assertEquals(0, test2.getTimeOut());
067: Assert.assertEquals(1, test2.getInvocationCount());
068: Assert.assertEquals(100, test2.getSuccessPercentage());
069: Assert.assertEquals("", test2.getDataProvider());
070: }
071:
072: public void verifyTestMethodLevel() throws SecurityException,
073: NoSuchMethodException {
074: //
075: // Tests on MTest1SampleTest
076: //
077: Method method = MTest1.class.getMethod("f", null);
078: ITest test1 = (ITest) m_finder.findAnnotation(method,
079: ITest.class);
080: Assert.assertTrue(test1.getEnabled());
081: String[] groups = test1.getGroups();
082: Assert
083: .assertEquals(new String[] { "group3", "group4" },
084: groups);
085: Assert.assertTrue(test1.getAlwaysRun());
086: Assert.assertEquals(new String[] { "param3", "param4" }, test1
087: .getParameters());
088: Assert.assertEquals(new String[] { "dg3", "dg4" }, test1
089: .getDependsOnGroups());
090: Assert.assertEquals(new String[] { "dm3", "dm4" }, test1
091: .getDependsOnMethods());
092: Assert.assertEquals(142, test1.getTimeOut());
093: Assert.assertEquals(143, test1.getInvocationCount());
094: Assert.assertEquals(3, test1.getThreadPoolSize());
095: Assert.assertEquals(61, test1.getSuccessPercentage());
096: Assert.assertEquals("dp2", test1.getDataProvider());
097: Assert.assertEquals("Method description", test1
098: .getDescription());
099: Class[] exceptions = test1.getExpectedExceptions();
100: Assert.assertEquals(exceptions.length, 1);
101: Assert.assertEquals(exceptions[0], NullPointerException.class);
102: }
103:
104: public void verifyTestConstructorLevel() throws SecurityException,
105: NoSuchMethodException {
106: //
107: // Tests on MTest1SampleTest
108: //
109: Constructor constructor = MTest1.class.getConstructor(null);
110: ITest test1 = (ITest) m_finder.findAnnotation(constructor,
111: ITest.class);
112: Assert.assertNotNull(test1);
113: Assert.assertTrue(test1.getEnabled());
114: String[] groups = test1.getGroups();
115: Assert
116: .assertEquals(new String[] { "group5", "group6" },
117: groups);
118: Assert.assertTrue(test1.getAlwaysRun());
119: Assert.assertEquals(new String[] { "param5", "param6" }, test1
120: .getParameters());
121: Assert.assertEquals(new String[] { "dg5", "dg6" }, test1
122: .getDependsOnGroups());
123: Assert.assertEquals(new String[] { "dm5", "dm6" }, test1
124: .getDependsOnMethods());
125: Assert.assertEquals(242, test1.getTimeOut());
126: Assert.assertEquals(243, test1.getInvocationCount());
127: Assert.assertEquals(62, test1.getSuccessPercentage());
128: Assert.assertEquals("dp3", test1.getDataProvider());
129: Assert.assertEquals("Constructor description", test1
130: .getDescription());
131: Class[] exceptions = test1.getExpectedExceptions();
132: Assert.assertEquals(exceptions.length, 1);
133: Assert.assertEquals(exceptions[0], NullPointerException.class);
134: }
135:
136: public void verifyConfigurationBefore() throws SecurityException,
137: NoSuchMethodException {
138: Method method = MTest1.class.getMethod("before", null);
139: IConfiguration configuration = (IConfiguration) m_finder
140: .findAnnotation(method, IConfiguration.class);
141: Assert.assertNotNull(configuration);
142: Assert.assertTrue(configuration.getBeforeSuite());
143: Assert.assertTrue(configuration.getBeforeTestMethod());
144: Assert.assertTrue(configuration.getBeforeTest());
145: Assert.assertTrue(configuration.getBeforeTestClass());
146: Assert.assertFalse(configuration.getAfterSuite());
147: Assert.assertFalse(configuration.getAfterTestMethod());
148: Assert.assertFalse(configuration.getAfterTest());
149: Assert.assertFalse(configuration.getAfterTestClass());
150: Assert.assertEquals(0, configuration.getAfterGroups().length);
151: String[] bg = configuration.getBeforeGroups();
152: Assert.assertEquals(bg.length, 2);
153: Assert.assertTrue((bg[0].equals("b1") && bg[1].equals("b2"))
154: || (bg[1].equals("b1") && bg[0].equals("b2")));
155:
156: // Default values
157: Assert.assertTrue(configuration.getEnabled());
158: Assert.assertTrue(configuration.getInheritGroups());
159: Assert.assertFalse(configuration.getAlwaysRun());
160: }
161:
162: public void verifyNewConfigurationBefore()
163: throws SecurityException, NoSuchMethodException {
164: Method method = MTest1.class.getMethod("newBefore",
165: new Class[0]);
166: IConfiguration configuration = (IConfiguration) m_finder
167: .findAnnotation(method, IBeforeSuite.class);
168: Assert.assertNotNull(configuration);
169: Assert.assertTrue(configuration.getBeforeSuite());
170:
171: // Default values
172: Assert.assertTrue(configuration.getEnabled());
173: Assert.assertTrue(configuration.getInheritGroups());
174: Assert.assertFalse(configuration.getAlwaysRun());
175: }
176:
177: public void verifyNewConfigurationAfter() throws SecurityException,
178: NoSuchMethodException {
179: Method method = MTest1.class
180: .getMethod("newAfter", new Class[0]);
181: IConfiguration configuration = (IConfiguration) m_finder
182: .findAnnotation(method, IAfterSuite.class);
183: Assert.assertNotNull(configuration);
184: Assert.assertTrue(configuration.getAfterSuite());
185:
186: // Default values
187: Assert.assertTrue(configuration.getEnabled());
188: Assert.assertTrue(configuration.getInheritGroups());
189: Assert.assertFalse(configuration.getAlwaysRun());
190: }
191:
192: public void verifyConfigurationAfter() throws SecurityException,
193: NoSuchMethodException {
194: Method method = MTest1.class.getMethod("after", null);
195: IConfiguration configuration = (IConfiguration) m_finder
196: .findAnnotation(method, IConfiguration.class);
197: Assert.assertNotNull(configuration);
198: Assert.assertFalse(configuration.getBeforeSuite());
199: Assert.assertFalse(configuration.getBeforeTestMethod());
200: Assert.assertFalse(configuration.getBeforeTest());
201: Assert.assertFalse(configuration.getBeforeTestClass());
202: Assert.assertTrue(configuration.getAfterSuite());
203: Assert.assertTrue(configuration.getAfterTestMethod());
204: Assert.assertTrue(configuration.getAfterTest());
205: Assert.assertTrue(configuration.getAfterTestClass());
206: Assert.assertEquals(0, configuration.getBeforeGroups().length);
207: String[] ag = configuration.getAfterGroups();
208: Assert.assertEquals(ag.length, 2);
209: Assert.assertTrue((ag[0].equals("a1") && ag[1].equals("a2"))
210: || (ag[1].equals("a1") && ag[0].equals("a2")));
211:
212: // Default values
213: Assert.assertTrue(configuration.getEnabled());
214: Assert.assertTrue(configuration.getInheritGroups());
215: Assert.assertFalse(configuration.getAlwaysRun());
216: }
217:
218: public void verifyConfigurationOthers() throws SecurityException,
219: NoSuchMethodException {
220: Method method = MTest1.class.getMethod("otherConfigurations",
221: null);
222: IConfiguration configuration = (IConfiguration) m_finder
223: .findAnnotation(method, IConfiguration.class);
224: Assert.assertNotNull(configuration);
225: Assert.assertFalse(configuration.getBeforeSuite());
226: Assert.assertFalse(configuration.getBeforeTestMethod());
227: Assert.assertFalse(configuration.getBeforeTest());
228: Assert.assertFalse(configuration.getBeforeTestClass());
229: Assert.assertFalse(configuration.getAfterSuite());
230: Assert.assertFalse(configuration.getAfterTestMethod());
231: Assert.assertFalse(configuration.getAfterTest());
232: Assert.assertFalse(configuration.getAfterTestClass());
233:
234: Assert.assertFalse(configuration.getEnabled());
235: Assert.assertEquals(new String[] { "oparam1", "oparam2" },
236: configuration.getParameters());
237: Assert.assertEquals(new String[] { "ogroup1", "ogroup2" },
238: configuration.getGroups());
239: Assert.assertEquals(new String[] { "odg1", "odg2" },
240: configuration.getDependsOnGroups());
241: Assert.assertEquals(new String[] { "odm1", "odm2" },
242: configuration.getDependsOnMethods());
243: Assert.assertFalse(configuration.getInheritGroups());
244: Assert.assertTrue(configuration.getAlwaysRun());
245: Assert.assertEquals(configuration.getDescription(),
246: "beforeSuite description");
247:
248: }
249:
250: public void verifygetDataProvider() throws SecurityException,
251: NoSuchMethodException {
252: Method method = MTest1.class.getMethod("otherConfigurations",
253: null);
254: IDataProvider dataProvider = (IDataProvider) m_finder
255: .findAnnotation(method, IDataProvider.class);
256: Assert.assertNotNull(dataProvider);
257: Assert.assertEquals("dp4", dataProvider.getName());
258: }
259:
260: public void verifyExpectedExceptions() throws SecurityException,
261: NoSuchMethodException {
262: Method method = MTest1.class.getMethod("otherConfigurations",
263: null);
264: IExpectedExceptions exceptions = (IExpectedExceptions) m_finder
265: .findAnnotation(method, IExpectedExceptions.class);
266:
267: Assert.assertNotNull(exceptions);
268: Assert.assertEquals(new Class[] { MTest1.class, MTest2.class },
269: exceptions.getValue());
270: }
271:
272: public void verifyFactory() throws SecurityException,
273: NoSuchMethodException {
274: Method method = MTest1.class.getMethod("factory", null);
275: IFactory factory = (IFactory) m_finder.findAnnotation(method,
276: IFactory.class);
277:
278: Assert.assertNotNull(factory);
279: Assert.assertEquals(new String[] { "pf1", "pf2" }, factory
280: .getParameters());
281: }
282:
283: public void verifyGetParameters() throws SecurityException,
284: NoSuchMethodException {
285: Method method = MTest1.class.getMethod("parameters", null);
286: IParameters parameters = (IParameters) m_finder.findAnnotation(
287: method, IParameters.class);
288:
289: Assert.assertNotNull(parameters);
290: Assert.assertEquals(new String[] { "pp1", "pp2", "pp3" },
291: parameters.getValue());
292: }
293:
294: private static void ppp(String s) {
295: System.out.println("[MAnnotationSampleTest] " + s);
296: }
297: }
|