1 package test.mannotation; 2 3 import org.testng.Assert; 4 import org.testng.annotations.Configuration; 5 import org.testng.annotations.IConfigurationAnnotation; 6 import org.testng.annotations.IDataProviderAnnotation; 7 import org.testng.annotations.IExpectedExceptionsAnnotation; 8 import org.testng.annotations.IFactoryAnnotation; 9 import org.testng.annotations.IParametersAnnotation; 10 import org.testng.annotations.ITestAnnotation; 11 import org.testng.annotations.Test; 12 import org.testng.internal.IConfiguration; 13 import org.testng.internal.annotations.IAfterSuite; 14 import org.testng.internal.annotations.IAnnotationFinder; 15 import org.testng.internal.annotations.IBeforeSuite; 16 17 import java.lang.reflect.Constructor; 18 import java.lang.reflect.Method; 19 20 @Test(enabled = true) 21 public class MAnnotationSampleTest { 22 private IConfiguration m_configuration = new org.testng.internal.Configuration(); 23 private IAnnotationFinder m_finder; 24 25 @Configuration(beforeTestClass = true, enabled = true) init()26 public void init() { 27 m_finder = m_configuration.getAnnotationFinder(); 28 } 29 verifyTestClassLevel()30 public void verifyTestClassLevel() { 31 // 32 // Tests on MTest1SampleTest 33 // 34 ITestAnnotation test1 = (ITestAnnotation) m_finder.findAnnotation(MTest1.class, ITestAnnotation.class); 35 Assert.assertTrue(test1.getEnabled()); 36 Assert.assertEquals(test1.getGroups(), new String[] { "group1", "group2" }); 37 Assert.assertTrue(test1.getAlwaysRun()); 38 Assert.assertEquals(test1.getParameters(), new String[] { "param1", "param2" }); 39 Assert.assertEqualsNoOrder(test1.getDependsOnGroups(), new String[] { "dg1", "dg2" }, "depends on groups"); 40 Assert.assertEqualsNoOrder( test1.getDependsOnMethods(), new String[] { "dm1", "dm2" }); 41 Assert.assertEquals(test1.getTimeOut(), 42); 42 Assert.assertEquals(test1.getInvocationCount(), 43); 43 Assert.assertEquals(test1.getSuccessPercentage(), 44); 44 Assert.assertEquals(test1.getThreadPoolSize(), 3); 45 Assert.assertEquals(test1.getDataProvider(), "dp"); 46 Assert.assertEquals(test1.getDescription(), "Class level description"); 47 48 // 49 // Tests on MTest1SampleTest (test defaults) 50 // 51 ITestAnnotation test2 = (ITestAnnotation) m_finder.findAnnotation(MTest2.class, ITestAnnotation.class); 52 // test default for enabled 53 Assert.assertTrue(test2.getEnabled()); 54 Assert.assertFalse(test2.getAlwaysRun()); 55 Assert.assertEquals(test2.getTimeOut(), 0); 56 Assert.assertEquals(test2.getInvocationCount(), 1); 57 Assert.assertEquals(test2.getSuccessPercentage(), 100); 58 Assert.assertEquals(test2.getDataProvider(), ""); 59 } 60 verifyTestMethodLevel()61 public void verifyTestMethodLevel() throws SecurityException, NoSuchMethodException 62 { 63 // 64 // Tests on MTest1SampleTest 65 // 66 Method method = MTest1.class.getMethod("f", new Class[0]); 67 ITestAnnotation test1 = (ITestAnnotation) m_finder.findAnnotation(method, ITestAnnotation.class); 68 Assert.assertTrue(test1.getEnabled()); 69 Assert.assertEqualsNoOrder(test1.getGroups(), new String[] { "group1", "group3", "group4", "group2" }); 70 Assert.assertTrue(test1.getAlwaysRun()); 71 Assert.assertEquals(test1.getParameters(), new String[] { "param3", "param4" }); 72 Assert.assertEqualsNoOrder(test1.getDependsOnGroups(), new String[] { "dg1", "dg2", "dg3", "dg4" }); 73 Assert.assertEqualsNoOrder(test1.getDependsOnMethods(), new String[] { "dm1", "dm2", "dm3", "dm4" }); 74 Assert.assertEquals(test1.getTimeOut(), 142); 75 Assert.assertEquals(test1.getInvocationCount(), 143); 76 Assert.assertEquals(test1.getSuccessPercentage(), 61); 77 Assert.assertEquals(test1.getDataProvider(), "dp2"); 78 Assert.assertEquals(test1.getDescription(), "Method description"); 79 Class[] exceptions = test1.getExpectedExceptions(); 80 Assert.assertEquals(exceptions.length, 1); 81 Assert.assertEquals(exceptions[0], NullPointerException.class); 82 } 83 verifyTestConstructorLevel()84 public void verifyTestConstructorLevel() throws SecurityException, NoSuchMethodException 85 { 86 // 87 // Tests on MTest1SampleTest 88 // 89 Constructor constructor = MTest1.class.getConstructor(new Class[0]); 90 ITestAnnotation test1 = (ITestAnnotation) m_finder.findAnnotation(constructor, ITestAnnotation.class); 91 Assert.assertNotNull(test1); 92 Assert.assertTrue(test1.getEnabled()); 93 Assert.assertEqualsNoOrder(test1.getGroups(), new String[] { "group5", "group1", "group6", "group2" }); 94 Assert.assertTrue(test1.getAlwaysRun()); 95 Assert.assertEquals(test1.getParameters(), new String[] { "param5", "param6" }); 96 Assert.assertEqualsNoOrder(test1.getDependsOnGroups(), new String[] { "dg1", "dg2", "dg5", "dg6" }); 97 Assert.assertEqualsNoOrder(test1.getDependsOnMethods(), new String[] { "dm1", "dm2", "dm5", "dm6" }); 98 Assert.assertEquals(test1.getTimeOut(), 242); 99 Assert.assertEquals(test1.getInvocationCount(), 243); 100 Assert.assertEquals(test1.getSuccessPercentage(), 62); 101 Assert.assertEquals(test1.getDataProvider(), "dp3"); 102 Assert.assertEquals(test1.getDescription(), "Constructor description"); 103 Class[] exceptions = test1.getExpectedExceptions(); 104 Assert.assertEquals(exceptions.length, 1); 105 Assert.assertEquals(exceptions[0], NumberFormatException.class); 106 } 107 verifyConfigurationBefore()108 public void verifyConfigurationBefore() throws SecurityException, NoSuchMethodException 109 { 110 Method method = MTest1.class.getMethod("before", new Class[0]); 111 IConfigurationAnnotation configuration = 112 (IConfigurationAnnotation) m_finder.findAnnotation(method, IConfigurationAnnotation.class); 113 Assert.assertNotNull(configuration); 114 Assert.assertTrue(configuration.getBeforeSuite()); 115 Assert.assertTrue(configuration.getBeforeTestMethod()); 116 Assert.assertTrue(configuration.getBeforeTest()); 117 Assert.assertTrue(configuration.getBeforeTestClass()); 118 Assert.assertFalse(configuration.getAfterSuite()); 119 Assert.assertFalse(configuration.getAfterTestMethod()); 120 Assert.assertFalse(configuration.getAfterTest()); 121 Assert.assertFalse(configuration.getAfterTestClass()); 122 Assert.assertEquals(0, configuration.getAfterGroups().length); 123 String[] bg = configuration.getBeforeGroups(); 124 Assert.assertEquals(bg.length, 2); 125 Assert.assertEqualsNoOrder(bg, new String[] {"b1", "b2"}); 126 127 // Default values 128 Assert.assertTrue(configuration.getEnabled()); 129 Assert.assertTrue(configuration.getInheritGroups()); 130 Assert.assertFalse(configuration.getAlwaysRun()); 131 } 132 verifyConfigurationAfter()133 public void verifyConfigurationAfter() throws SecurityException, NoSuchMethodException 134 { 135 Method method = MTest1.class.getMethod("after", new Class[0]); 136 IConfigurationAnnotation configuration = 137 (IConfigurationAnnotation) m_finder.findAnnotation(method, IConfigurationAnnotation.class); 138 Assert.assertNotNull(configuration); 139 Assert.assertFalse(configuration.getBeforeSuite()); 140 Assert.assertFalse(configuration.getBeforeTestMethod()); 141 Assert.assertFalse(configuration.getBeforeTest()); 142 Assert.assertFalse(configuration.getBeforeTestClass()); 143 Assert.assertTrue(configuration.getAfterSuite()); 144 Assert.assertTrue(configuration.getAfterTestMethod()); 145 Assert.assertTrue(configuration.getAfterTest()); 146 Assert.assertTrue(configuration.getAfterTestClass()); 147 Assert.assertEquals(0, configuration.getBeforeGroups().length); 148 String[] ag = configuration.getAfterGroups(); 149 Assert.assertEquals(ag.length, 2); 150 Assert.assertEqualsNoOrder(ag, new String[] {"a1", "a2"}); 151 152 // Default values 153 Assert.assertTrue(configuration.getEnabled()); 154 Assert.assertTrue(configuration.getInheritGroups()); 155 Assert.assertFalse(configuration.getAlwaysRun()); 156 } 157 verifyConfigurationOthers()158 public void verifyConfigurationOthers() throws SecurityException, NoSuchMethodException 159 { 160 Method method = MTest1.class.getMethod("otherConfigurations", new Class[0]); 161 IConfigurationAnnotation configuration = 162 (IConfigurationAnnotation) m_finder.findAnnotation(method, IConfigurationAnnotation.class); 163 Assert.assertNotNull(configuration); 164 Assert.assertFalse(configuration.getBeforeSuite()); 165 Assert.assertFalse(configuration.getBeforeTestMethod()); 166 Assert.assertFalse(configuration.getBeforeTest()); 167 Assert.assertFalse(configuration.getBeforeTestClass()); 168 Assert.assertFalse(configuration.getAfterSuite()); 169 Assert.assertFalse(configuration.getAfterTestMethod()); 170 Assert.assertFalse(configuration.getAfterTest()); 171 Assert.assertFalse(configuration.getAfterTestClass()); 172 173 Assert.assertFalse(configuration.getEnabled()); 174 Assert.assertEquals(configuration.getParameters(), new String[] { "oparam1", "oparam2" }); 175 Assert.assertEqualsNoOrder(configuration.getGroups(), new String[] { "group1", "ogroup1", "ogroup2", "group2" }, "groups"); 176 Assert.assertEqualsNoOrder(configuration.getDependsOnGroups(), new String[] { "odg1", "odg2" }, "depends on groups"); 177 Assert.assertEqualsNoOrder(configuration.getDependsOnMethods(), new String[] { "odm1", "odm2" }, "depends on methods"); 178 Assert.assertFalse(configuration.getInheritGroups()); 179 Assert.assertTrue(configuration.getAlwaysRun()); 180 Assert.assertEquals(configuration.getDescription(), "beforeSuite description"); 181 } 182 verifyDataProvider()183 public void verifyDataProvider() throws SecurityException, NoSuchMethodException 184 { 185 Method method = MTest1.class.getMethod("otherConfigurations", new Class[0]); 186 IDataProviderAnnotation dataProvider = 187 (IDataProviderAnnotation) m_finder.findAnnotation(method, IDataProviderAnnotation.class); 188 Assert.assertNotNull(dataProvider); 189 Assert.assertEquals(dataProvider.getName(), "dp4"); 190 } 191 verifyExpectedExceptions()192 public void verifyExpectedExceptions() throws SecurityException, NoSuchMethodException 193 { 194 Method method = MTest1.class.getMethod("otherConfigurations", new Class[0]); 195 IExpectedExceptionsAnnotation exceptions= 196 (IExpectedExceptionsAnnotation) m_finder.findAnnotation(method, IExpectedExceptionsAnnotation.class); 197 198 Assert.assertNotNull(exceptions); 199 Assert.assertEquals(exceptions.getValue(), new Class[] { MTest1.class, MTest2.class }); 200 } 201 verifyFactory()202 public void verifyFactory() throws SecurityException, NoSuchMethodException 203 { 204 Method method = MTest1.class.getMethod("factory", new Class[0]); 205 IFactoryAnnotation factory= 206 (IFactoryAnnotation) m_finder.findAnnotation(method, IFactoryAnnotation.class); 207 208 Assert.assertNotNull(factory); 209 Assert.assertEquals(factory.getParameters(), new String[] { "pf1", "pf2" }); 210 } 211 verifyParameters()212 public void verifyParameters() throws SecurityException, NoSuchMethodException 213 { 214 Method method = MTest1.class.getMethod("parameters", new Class[0]); 215 IParametersAnnotation parameters = 216 (IParametersAnnotation) m_finder.findAnnotation(method, IParametersAnnotation.class); 217 218 Assert.assertNotNull(parameters); 219 Assert.assertEquals(parameters.getValue(), new String[] { "pp1", "pp2", "pp3" }); 220 } 221 verifyNewConfigurationBefore()222 public void verifyNewConfigurationBefore() throws SecurityException, NoSuchMethodException 223 { 224 Method method = MTest1.class.getMethod("newBefore", new Class[0]); 225 IConfigurationAnnotation configuration = 226 (IConfigurationAnnotation) m_finder.findAnnotation(method, IBeforeSuite.class); 227 Assert.assertNotNull(configuration); 228 Assert.assertTrue(configuration.getBeforeSuite()); 229 230 // Default values 231 Assert.assertTrue(configuration.getEnabled()); 232 Assert.assertTrue(configuration.getInheritGroups()); 233 Assert.assertFalse(configuration.getAlwaysRun()); 234 } 235 verifyNewConfigurationAfter()236 public void verifyNewConfigurationAfter() throws SecurityException, NoSuchMethodException 237 { 238 Method method = MTest1.class.getMethod("newAfter", new Class[0]); 239 IConfigurationAnnotation configuration = 240 (IConfigurationAnnotation) m_finder.findAnnotation(method, IAfterSuite.class); 241 Assert.assertNotNull(configuration); 242 Assert.assertTrue(configuration.getAfterSuite()); 243 244 // Default values 245 Assert.assertTrue(configuration.getEnabled()); 246 Assert.assertTrue(configuration.getInheritGroups()); 247 Assert.assertFalse(configuration.getAlwaysRun()); 248 } 249 ppp(String s)250 private static void ppp(String s) { 251 System.out.println("[MAnnotationSampleTest] " + s); 252 } 253 } 254