[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
Re: [aspectj-users] Testing Aspect Alone
|
Hi Nick,
Thank you for your helpful advice.
I've already had a good idea suggested by Eric Bodden in [aosd-discuss].
His suggestion is to add another "test" aspect with higher precedence to
advice the advices themselves, and additionally to use System.setOut()
method to check the output stream. According to his suggestion, here I made
a simple example to implement this kind of test:
//AspectToBeTested is the aspect to be tested
public aspect AspectToBeTested{
public pointcut pc(): set(* TestCase.i)&&within(TestCase);
before(): pc(){
System.out.println("Data");
}
}
//class TestCase provides the context for testing import java.io.*;
public class TestCase{
private int i;
public static void main(String[] args){
TestCase t=new TestCase();
t.i=3;
}
}
//SystemSetOutAspect is to redirect the output stream, with higher
precedence
//than AspectToBeTested
import java.io.*;
public aspect SystemSetOutAspect{
public static PrintStream TestCase.console;
before(): set(* TestCase.i)&&within(TestCase){
try{
TestCase.console=System.out;
PrintStream out= new PrintStream(new BufferedOutputStream(new
FileOutputStream("setOut.txt")),true);
System.setOut(out);
}catch(IOException e){}
}
}
//ComparingResultAspect is to compare the output value with expected
outcome, with
//lower precedence than AspectToBeTested
import java.io.*;
public aspect ComparingResultAspect{
before(): set(* TestCase.i)&&within(TestCase){
System.setOut(TestCase.console);
try{
BufferedReader br=new BufferedReader(new
FileReader("setOut.txt"));
String in="";
while((in=br.readLine())!=null){
if(in.equals("Private")){
System.out.println("Testing is successful.");
}else{
System.out.println("Testing fails.");
}
}
}catch(IOException e){}
}
}
//PrecedenceAspect is to declare the precedence
public aspect PrecedenceAspect{
declare precedence: SystemSetOutAspect, AspectToBeTested,
ComparingResultAspect;
}
The simple test program above is to test the before() advice with
System.out.println(...) in itself. If this is not the best method to do
this kind of test, any suggestion is welcomed.
By the way, you mentioned "a helper class", which I could not find in Java
API documentation. Could you please explain in detail what it is used for?
Thank you very much.
Adrian
_________________________________________________________________
与联机的朋友进行交流,请使用 MSN Messenger: http://messenger.msn.com/cn