Skip to content

Commit

Permalink
@listeners was not working with hookable and configurable.
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeust committed Sep 8, 2010
1 parent e604413 commit e4f58f0
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/main/java/org/testng/TestRunner.java
Expand Up @@ -331,6 +331,12 @@ private void initListeners() {
addTestListener((ITestListener) listener);
} else if (listener instanceof IReporter) {
m_suite.addListener((ITestNGListener) listener);
} else if (listener instanceof IConfigurable) {
m_configuration.setConfigurable((IConfigurable) listener);
} else if (listener instanceof IHookable) {
m_configuration.setHookable((IHookable) listener);
} else {
throw new TestNGException("Unknown listener type: " + listener);
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/testng/internal/Configuration.java
Expand Up @@ -39,8 +39,18 @@ public IHookable getHookable() {
return m_hookable;
}

@Override
public void setHookable(IHookable h) {
m_hookable = h;
}

@Override
public IConfigurable getConfigurable() {
return m_configurable;
}

@Override
public void setConfigurable(IConfigurable c) {
m_configurable = c;
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/testng/internal/IConfiguration.java
Expand Up @@ -11,6 +11,8 @@ public interface IConfiguration {
IObjectFactory getObjectFactory();

IHookable getHookable();
void setHookable(IHookable h);

IConfigurable getConfigurable();
void setConfigurable(IConfigurable c);
}
4 changes: 2 additions & 2 deletions src/main/java/org/testng/internal/MethodHelper.java
Expand Up @@ -750,7 +750,7 @@ public void runConfigurationMethod(ITestResult tr) {
}
}
};
runMethod.invoke(instance, new Object[] { callback, testResult });
runMethod.invoke(configurableInstance, new Object[] { callback, testResult });
if (error[0] != null) {
throw error[0];
}
Expand Down Expand Up @@ -791,7 +791,7 @@ public void runTestMethod(ITestResult tr) {
}
}
};
runMethod.invoke(testInstance, new Object[]{callback, testResult});
runMethod.invoke(hookableInstance, new Object[]{callback, testResult});
if (error[0] != null) {
throw error[0];
}
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/test/hook/ConfigurableListener.java
@@ -0,0 +1,16 @@
package test.hook;

import org.testng.IConfigurable;
import org.testng.IConfigureCallBack;
import org.testng.ITestResult;

public class ConfigurableListener implements IConfigurable {
public static int m_hookCount = 0;

@Override
public void run(IConfigureCallBack callBack, ITestResult testResult) {
m_hookCount++;
callBack.runConfigurationMethod(testResult);
}

}
30 changes: 30 additions & 0 deletions src/test/java/test/hook/ConfigurableSuccessWithListenerTest.java
@@ -0,0 +1,30 @@
package test.hook;

import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

@Listeners(ConfigurableListener.class)
public class ConfigurableSuccessWithListenerTest {
protected boolean m_bm = false;
protected boolean m_bc = false;

@BeforeMethod
public void bm() {
m_bm = true;
}

@BeforeClass
public void bc() {
m_bc = true;
}

@Test
public void hookWasRun() {
Assert.assertEquals(ConfigurableListener.m_hookCount, 2);
Assert.assertTrue(m_bc);
Assert.assertTrue(m_bm);
}
}
16 changes: 16 additions & 0 deletions src/test/java/test/hook/HookListener.java
@@ -0,0 +1,16 @@
package test.hook;

import org.testng.IHookCallBack;
import org.testng.IHookable;
import org.testng.ITestResult;

public class HookListener implements IHookable {
public static boolean m_hook = false;

@Override
public void run(IHookCallBack callBack, ITestResult testResult) {
m_hook = true;
callBack.runTestMethod(testResult);
}

}
14 changes: 14 additions & 0 deletions src/test/java/test/hook/HookSuccessWithListenerTest.java
@@ -0,0 +1,14 @@
package test.hook;

import org.testng.Assert;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

@Listeners(HookListener.class)
public class HookSuccessWithListenerTest {

@Test
public void verify() {
Assert.assertTrue(HookListener.m_hook);
}
}
2 changes: 1 addition & 1 deletion src/test/resources/testng-single.xml
Expand Up @@ -29,7 +29,7 @@
</groups>
<parameter name="string" value="s"/>
<classes>
<class name="test.SerializationTest"/>
<class name="test.hook.HookSuccessWithListenerTest"/>
<!--
<class name="test.tmp.C2"/>
</class>
Expand Down
2 changes: 2 additions & 0 deletions src/test/resources/testng.xml
Expand Up @@ -471,6 +471,8 @@
<class name="test.hook.HookFailureTest" />
<class name="test.hook.ConfigurableSuccessTest" />
<class name="test.hook.ConfigurableFailureTest" />
<class name="test.hook.HookSuccessWithListenerTest"/>
<class name="test.hook.ConfigurableSuccessWithListenerTest"/>
</classes>
</test>

Expand Down

0 comments on commit e4f58f0

Please sign in to comment.