New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support @Listeners in annotation transformer #900
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.testng; | ||
|
||
import org.testng.annotations.IListenersAnnotation; | ||
|
||
public interface IAnnotationTransformer3 extends IAnnotationTransformer2 { | ||
|
||
void transform(IListenersAnnotation annotation, Class testClass); | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.testng.annotations; | ||
|
||
import org.testng.ITestNGListener; | ||
|
||
public interface IListenersAnnotation extends IAnnotation { | ||
|
||
Class<? extends ITestNGListener>[] getValue(); | ||
|
||
void setValue(Class<? extends ITestNGListener>[] value); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,7 @@ | ||
package org.testng.internal.annotations; | ||
|
||
import org.testng.ITestNGListener; | ||
import org.testng.annotations.IAnnotation; | ||
import org.testng.annotations.IListenersAnnotation; | ||
|
||
public interface IListeners extends IAnnotation { | ||
Class<? extends ITestNGListener>[] getValue(); | ||
public interface IListeners extends IListenersAnnotation { | ||
|
||
void setValue(Class<? extends ITestNGListener>[] value); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -123,6 +123,8 @@ public void verifyAnnotationTransformerClass() { | |
private void runTest(IAnnotationTransformer transformer, | ||
String passedName, String failedName) | ||
{ | ||
MySuiteListener.triggered = false; | ||
MySuiteListener2.triggered = false; | ||
TestNG tng = new TestNG(); | ||
tng.setVerbose(0); | ||
if (transformer != null) { | ||
|
@@ -140,8 +142,23 @@ private void runTest(IAnnotationTransformer transformer, | |
|
||
Assert.assertEquals(results.size(), 1); | ||
Assert.assertEquals(name, results.get(0).getMethod().getMethodName()); | ||
Assert.assertTrue(MySuiteListener.triggered); | ||
Assert.assertFalse(MySuiteListener2.triggered); | ||
} | ||
|
||
@Test | ||
public void verifyListenerAnnotationTransformerClass() { | ||
MySuiteListener.triggered = false; | ||
MySuiteListener2.triggered = false; | ||
TestNG tng = new TestNG(); | ||
tng.setVerbose(0); | ||
tng.setAnnotationTransformer(new MyListenerTransformer()); | ||
tng.setTestClasses(new Class[]{AnnotationTransformerClassSampleTest.class}); | ||
|
||
tng.run(); | ||
Assert.assertFalse(MySuiteListener.triggered); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe you could make the test stronger by actually having the transformer set the listeners to another class and assert that this class was called instead of just testing the original listener wasn't called? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, why not. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
Assert.assertTrue(MySuiteListener2.triggered); | ||
} | ||
|
||
@Test | ||
public void verifyConfigurationTransformer() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package test.annotationtransformer; | ||
|
||
import org.testng.IAnnotationTransformer3; | ||
import org.testng.annotations.IConfigurationAnnotation; | ||
import org.testng.annotations.IDataProviderAnnotation; | ||
import org.testng.annotations.IFactoryAnnotation; | ||
import org.testng.annotations.IListenersAnnotation; | ||
import org.testng.annotations.ITestAnnotation; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.Method; | ||
|
||
public class MyListenerTransformer implements IAnnotationTransformer3 { | ||
|
||
@Override | ||
public void transform(IListenersAnnotation annotation, Class testClass) { | ||
annotation.setValue(new Class[]{MySuiteListener2.class}); | ||
} | ||
|
||
@Override | ||
public void transform(IConfigurationAnnotation annotation, Class testClass, | ||
Constructor testConstructor, Method testMethod) {} | ||
|
||
@Override | ||
public void transform(IDataProviderAnnotation annotation, Method method) {} | ||
|
||
@Override | ||
public void transform(IFactoryAnnotation annotation, Method method) {} | ||
|
||
@Override | ||
public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, | ||
Method testMethod) {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package test.annotationtransformer; | ||
|
||
import org.testng.ISuite; | ||
import org.testng.ISuiteListener; | ||
|
||
public class MySuiteListener implements ISuiteListener { | ||
|
||
public static boolean triggered = false; | ||
|
||
@Override | ||
public void onStart(ISuite suite) { | ||
triggered = true; | ||
} | ||
|
||
@Override | ||
public void onFinish(ISuite suite) {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package test.annotationtransformer; | ||
|
||
import org.testng.ISuite; | ||
import org.testng.ISuiteListener; | ||
|
||
public class MySuiteListener2 implements ISuiteListener { | ||
|
||
public static boolean triggered = false; | ||
|
||
@Override | ||
public void onStart(ISuite suite) { | ||
triggered = true; | ||
} | ||
|
||
@Override | ||
public void onFinish(ISuite suite) {} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is it necessary to introduce this interface in the hierarchy?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because
IListeners
is located into theinternal
package.There is already some "duplication" between the
annotation
package andinternal.annotation
: I've just follow the current pattern :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I change something?