how to test Privleged code

923 views
Skip to first unread message

crsslc

unread,
Jan 19, 2006, 7:36:29 PM1/19/06
to testng...@googlegroups.com
I have a junit test i'm converting to a testng test, however, the code to test requires jaas authentication. In JUnit this is done with:

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

crsslc

unread,
Jan 20, 2006, 1:16:12 PM1/20/06
to testng...@googlegroups.com
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);
< 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

Cédric Beust ♔

unread,
Jan 20, 2006, 1:24:10 PM1/20/06
to testng...@googlegroups.com
It looks like you are looking on the TestClass if it contains a run(InvokerCallBack) method?

Can you explain what this does exactly and what your client code looks like?

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).

--
Cedric



On 1/20/06, crsslc <testng...@opensymphony.com> wrote:

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);



--
Cédric

crsslc

unread,
Jan 20, 2006, 2:09:48 PM1/20/06
to testng...@googlegroups.com
> It looks like you are looking on the TestClass if it
> contains a
> run(InvokerCallBack) method?
>
Yes


> 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

Cédric Beust ♔

unread,
Jan 28, 2006, 3:38:54 PM1/28/06
to testng...@googlegroups.com
Hi Calvin,

I implemented your request.  I changed a few names (I'm still open to better names, by the way).

Please review the JavaDoc and the tests I implemented below and let me know if you think I captured your intent correctly.

You can download this temporary distribution at:

http://testng.org/testng-4.4.8.zip

package org.testng;

/**
 * If a test class implements this interface, its run() method
 * will be invoked instead of each @Test method found.  The invocation of
 * the test method will then be performed upon invocation of the callBack()
 * method of the IHookCallBack parameter.
 *
 * This is useful to test classes that require JAAS authentication, which can
 * be implemented as follows:
 *
 * <pre>
 * public void run(final IHookCallBack icb) {
 *   // Preferably initialized in a @Configuration method
 *   mySubject = authenticateWithJAAs();
 *  
 *   Subject.doAs(mySubject, new PrivilegedExceptionAction() {
 *     public Object run() {
 *       icb.callback();
 *     }
 *   };
 * }
 * </pre>
 *
 * @author cbeust
 * @date Jan 28, 2006
 */
public interface IHookable {
  public void run(IHookCallBack callBack);
}

package org.testng;

/**
 * A parameter of this type will be passed to the run() method of a IHookable.
 * Invoking runTestMethod() on that parameter will cause the test method currently
 * being diverted to be invoked.
 *
 *  <p>
 * 
 * <b>This interface is not meant to be implemented by clients, only by TestNG.</b>
 *
 * @see org.testng.IHookable
 *
 *
 * @author cbeust
 * @date Jan 28, 2006
 */
public interface IHookCallBack {
 
  /**
   * Invoke the test method currently being hijacked.
   */
  public void runTestMethod();
}

package test.hook;

import org.testng.Assert;
import org.testng.IHookCallBack;
import org.testng.IHookable;
import org.testng.annotations.Configuration;
import org.testng.annotations.Test;

public class HookSuccessTest implements IHookable {
  private boolean m_hook = false;
  private boolean m_testWasRun = false;

  public void run(IHookCallBack callBack) {
    m_hook = true;
    callBack.runTestMethod();
  }

  @Test
  public void verify() {
    m_testWasRun = true;
  }
 
  @Configuration(afterTestMethod = true)
  public void tearDown() {
    Assert.assertTrue(m_hook);
    Assert.assertTrue(m_testWasRun);
  }

  private void ppp(String string) {
    System.out.println("[HookTest] " + string);
  }
 
}

Cédric Beust ♔

unread,
Feb 5, 2006, 2:14:31 PM2/5/06
to testng...@googlegroups.com
Calvin,

Did you get a chance to try it?  Does it fit your needs?

--
Cedric
--
Cédric
Reply all
Reply to author
Forward
0 new messages