public void run(final TestResult tr)
Subject.doAs(someObject,new PrivilegedExceptionAction()
{
public Obejct run() throws Exception{super.run(tr);return null};
};
}
How do I in testNG wrap a test case in a callback like this?
---------------------------------------------------------------------
Posted via Jive Forums
http://forums.opensymphony.com/thread.jspa?threadID=15067&messageID=29625#29625
Will you commit this?
Index: Invoker.java
===================================================================
RCS file: /cvs/testng/src/main/org/testng/internal/Invoker.java,v
retrieving revision 1.37
diff -r1.37 Invoker.java
379,381c379,418
< if(timeOut <= 0) {
< MethodHelper.invokeMethod(thisMethod, instances[i], parameters);
< testResult.setStatus(ITestResult.SUCCESS);
---
> if(timeOut <= 0)
> {
> Method runMethod = null;
> try
> {
> runMethod = testClass.getRealClass().getMethod("run", new Class[] { InvokerCallback.class });
> }
> catch (NoSuchMethodException e)
> {
> // not found it's ok
> }
> if (runMethod == null)
> {
> MethodHelper.invokeMethod(thisMethod, instances[i], parameters);
> }
> else
> {
> final Method a = thisMethod;
> final Object b = instances[i];
> final Object[] c = parameters;
> final Throwable[] error = new Throwable[1];
> InvokerCallback callback = new InvokerCallback()
> {
> public void callback()
> {
> try
> {
> MethodHelper.invokeMethod(a, b, c);
> }
> catch(Throwable t)
> {
> error[0] = t;
> }
> }
> };
> runMethod.invoke(instances[i],new Object[]{callback});
> if (error[0] != null)
> throw error[0];
> }
> testResult.setStatus(ITestResult.SUCCESS);
456a494,498
> public static interface InvokerCallback
> {
> public void callback();
> }
>
---------------------------------------------------------------------
Posted via Jive Forums
http://forums.opensymphony.com/thread.jspa?threadID=15067&messageID=29791#29791
This patch works for me.
Will you commit this?
Index: Invoker.java
===================================================================
RCS file: /cvs/testng/src/main/org/testng/internal/Invoker.java,v
retrieving revision 1.37
diff -r1.37 Invoker.java
379,381c379,418
< if(timeOut <= 0) {
< MethodHelper.invokeMethod (thisMethod, instances[i], parameters);
> Can you explain what this does exactly and what your
> client code looks like?
This is needed to test JAAS authentication code
The test class would have a method that looks like this:
public void run(final InvokerCallBack icb)
{
Subject.doAs(mySubject,new PrivilegedExceptionAction()
{
public Obejct run()
{
icb.callback();
}
};
}
This method could be placed on a base class along with the setup method so every subclass has this available. The setup method would look like this:
@Configuration(beforeTestClass=true)
public void setup()
{
mySubject = authenticateWithJAAs();
}
The test class (in the subclass) would look like this:
@Test
public void testA()
{
// make a call to an authentication protected method
...
}
eventually a method gets called that needs to check authentication credentials
public void doSomething()
{
Subject s = Subject.getSubject(AccessController.getContext());
...
}
> If we're going to do this, we should be more explicit
> on the contract, in my
> opinion (have the Test class implement that
> interface).
If you would rather have an interface that defines the run method that is fine.
> --
> Cedric
>
>
---------------------------------------------------------------------
Posted via Jive Forums
http://forums.opensymphony.com/thread.jspa?threadID=15067&messageID=29798#29798