1 package test.annotationtransformer; 2 3 import org.assertj.core.api.iterable.Extractor; 4 import org.testng.Assert; 5 import org.testng.IAnnotationTransformer; 6 import org.testng.ITestResult; 7 import org.testng.TestListenerAdapter; 8 import org.testng.TestNG; 9 import org.testng.annotations.Test; 10 import org.testng.xml.Parser; 11 import org.testng.xml.XmlSuite; 12 13 import test.SimpleBaseTest; 14 15 import java.io.ByteArrayInputStream; 16 import java.util.Arrays; 17 import java.util.Collection; 18 import java.util.List; 19 20 import static org.assertj.core.api.Assertions.assertThat; 21 22 public class AnnotationTransformerTest extends SimpleBaseTest { 23 24 private static final Extractor NAME_EXTRACTOR = new Extractor<ITestResult, String>() { 25 @Override 26 public String extract(ITestResult input) { 27 return input.getName(); 28 } 29 }; 30 31 /** 32 * Make sure that without a transformer in place, a class-level 33 * annotation invocationCount is correctly used. 34 */ 35 @Test verifyAnnotationWithoutTransformer()36 public void verifyAnnotationWithoutTransformer() { 37 TestNG tng = create(AnnotationTransformerSampleTest.class); 38 tng.setPreserveOrder(true); 39 40 TestListenerAdapter tla = new TestListenerAdapter(); 41 tng.addListener(tla); 42 43 tng.run(); 44 45 assertThat(tla.getPassedTests()).extracting(NAME_EXTRACTOR) 46 .containsExactly( 47 "five", 48 "four", "four", "four", "four", "four", 49 "three", "three", "three", "three", "three", 50 "two", "two" 51 ); 52 assertThat(tla.getFailedTests()).extracting(NAME_EXTRACTOR) 53 .containsExactly("verify"); 54 } 55 56 /** 57 * Test a transformer on a method-level @Test 58 */ 59 @Test verifyAnnotationTransformerMethod()60 public void verifyAnnotationTransformerMethod() { 61 TestNG tng = create(AnnotationTransformerSampleTest.class); 62 tng.setPreserveOrder(true); 63 64 MyTransformer transformer = new MyTransformer(); 65 tng.setAnnotationTransformer(transformer); 66 67 TestListenerAdapter tla = new TestListenerAdapter(); 68 tng.addListener(tla); 69 70 tng.run(); 71 72 assertThat(transformer.getMethodNames()).contains("two", "three", "four", "five", "verify"); 73 74 assertThat(tla.getPassedTests()).extracting(NAME_EXTRACTOR) 75 .containsExactly( 76 "five", "five", "five", "five", "five", 77 "four", "four", "four", "four", 78 "three", "three", "three", 79 "two", "two", 80 "verify" 81 ); 82 assertThat(tla.getFailedTests()).isEmpty(); 83 } 84 85 @Test verifyAnnotationTransformerHasOnlyOneNonNullArgument()86 public void verifyAnnotationTransformerHasOnlyOneNonNullArgument() { 87 TestNG tng = create(AnnotationTransformerSampleTest.class); 88 89 MyParamTransformer transformer = new MyParamTransformer(); 90 tng.setAnnotationTransformer(transformer); 91 92 tng.run(); 93 94 assertThat(transformer.isSuccess()).isTrue(); 95 } 96 97 @Test verifyMyParamTransformerOnlyOneNonNull()98 public void verifyMyParamTransformerOnlyOneNonNull() { 99 assertThat(MyParamTransformer.onlyOneNonNull(null, null, null)).isFalse(); 100 assertThat(MyParamTransformer.onlyOneNonNull( 101 MyParamTransformer.class, MyParamTransformer.class.getConstructors()[0], null)).isFalse(); 102 assertThat(MyParamTransformer.onlyOneNonNull(MyParamTransformer.class, null, null)).isTrue(); 103 } 104 105 /** 106 * Without an annotation transformer, we should have zero 107 * passed tests and one failed test called "one". 108 */ 109 @Test verifyAnnotationTransformerClass2()110 public void verifyAnnotationTransformerClass2() { 111 runTest(null, null, "one"); 112 } 113 114 /** 115 * With an annotation transformer, we should have one passed 116 * test called "one" and zero failed tests. 117 */ 118 @Test verifyAnnotationTransformerClass()119 public void verifyAnnotationTransformerClass() { 120 runTest(new MyTimeOutTransformer(), "one", null); 121 } 122 runTest(IAnnotationTransformer transformer, String passedName, String failedName)123 private void runTest(IAnnotationTransformer transformer, 124 String passedName, String failedName) 125 { 126 MySuiteListener.triggered = false; 127 MySuiteListener2.triggered = false; 128 TestNG tng = new TestNG(); 129 tng.setVerbose(0); 130 if (transformer != null) { 131 tng.setAnnotationTransformer(transformer); 132 } 133 tng.setTestClasses(new Class[] { AnnotationTransformerClassSampleTest.class}); 134 TestListenerAdapter tla = new TestListenerAdapter(); 135 tng.addListener(tla); 136 137 tng.run(); 138 139 List<ITestResult> results = 140 passedName != null ? tla.getPassedTests() : tla.getFailedTests(); 141 String name = passedName != null ? passedName : failedName; 142 143 Assert.assertEquals(results.size(), 1); 144 Assert.assertEquals(name, results.get(0).getMethod().getMethodName()); 145 Assert.assertTrue(MySuiteListener.triggered); 146 Assert.assertFalse(MySuiteListener2.triggered); 147 } 148 149 @Test verifyListenerAnnotationTransformerClass()150 public void verifyListenerAnnotationTransformerClass() { 151 MySuiteListener.triggered = false; 152 MySuiteListener2.triggered = false; 153 TestNG tng = new TestNG(); 154 tng.setVerbose(0); 155 tng.setAnnotationTransformer(new MyListenerTransformer()); 156 tng.setTestClasses(new Class[]{AnnotationTransformerClassSampleTest.class}); 157 158 tng.run(); 159 Assert.assertFalse(MySuiteListener.triggered); 160 Assert.assertTrue(MySuiteListener2.triggered); 161 } 162 163 @Test verifyConfigurationTransformer()164 public void verifyConfigurationTransformer() { 165 TestNG tng = new TestNG(); 166 tng.setAnnotationTransformer(new ConfigurationTransformer()); 167 tng.setVerbose(0); 168 tng.setTestClasses(new Class[] { ConfigurationSampleTest.class}); 169 TestListenerAdapter tla = new TestListenerAdapter(); 170 tng.addListener(tla); 171 172 tng.run(); 173 174 Assert.assertEquals(ConfigurationSampleTest.getBefore(), "correct"); 175 } 176 177 @Test verifyDataProviderTransformer()178 public void verifyDataProviderTransformer() { 179 TestNG tng = create(); 180 tng.setAnnotationTransformer(new DataProviderTransformer()); 181 tng.setTestClasses(new Class[] { AnnotationTransformerDataProviderSampleTest.class}); 182 TestListenerAdapter tla = new TestListenerAdapter(); 183 tng.addListener(tla); 184 185 tng.run(); 186 187 Assert.assertEquals(tla.getPassedTests().size(), 1); 188 } 189 190 @Test verifyFactoryTransformer()191 public void verifyFactoryTransformer() { 192 TestNG tng = create(); 193 tng.setAnnotationTransformer(new FactoryTransformer()); 194 tng.setTestClasses(new Class[] { AnnotationTransformerFactorySampleTest.class}); 195 TestListenerAdapter tla = new TestListenerAdapter(); 196 tng.addListener(tla); 197 198 tng.run(); 199 200 Assert.assertEquals(tla.getPassedTests().size(), 1); 201 } 202 203 @Test(description = "Test for issue #605") verifyInvocationCountTransformer()204 public void verifyInvocationCountTransformer() { 205 TestNG tng = create(); 206 tng.setTestClasses(new Class[] { AnnotationTransformerInvocationCountTest.class }); 207 TestListenerAdapter tla = new TestListenerAdapter(); 208 tng.addListener(tla); 209 210 tng.run(); 211 212 Assert.assertEquals(tla.getPassedTests().size(), 3); 213 214 tng = create(); 215 tng.setAnnotationTransformer(new AnnotationTransformerInvocationCountTest.InvocationCountTransformer(5)); 216 tng.setTestClasses(new Class[]{AnnotationTransformerInvocationCountTest.class}); 217 tla = new TestListenerAdapter(); 218 tng.addListener(tla); 219 220 tng.run(); 221 222 Assert.assertEquals(tla.getPassedTests().size(), 5); 223 } 224 225 @Test annotationTransformerInXmlShouldBeRun()226 public void annotationTransformerInXmlShouldBeRun() throws Exception { 227 String xml = "<suite name=\"SingleSuite\" >" + 228 " <listeners>" + 229 " <listener class-name=\"test.annotationtransformer.AnnotationTransformerInTestngXml\" />" + 230 " </listeners>" + 231 " <test enabled=\"true\" name=\"SingleTest\">" + 232 " <classes>" + 233 " <class name=\"test.annotationtransformer.AnnotationTransformerInTestngXml\" />" + 234 " </classes>" + 235 " </test>" + 236 "</suite>" 237 ; 238 239 ByteArrayInputStream is = new ByteArrayInputStream(xml.getBytes()); 240 Collection<XmlSuite> suites = new Parser(is).parse(); 241 242 TestNG tng = create(); 243 tng.setXmlSuites(Arrays.asList(suites.toArray(new XmlSuite[0]))); 244 TestListenerAdapter tla = new TestListenerAdapter(); 245 tng.addListener(tla); 246 247 tng.run(); 248 249 Assert.assertEquals(tla.getPassedTests().size(), 1); 250 251 } 252 } 253