// $Id: SurrogateTest.java,v 1.4 2008/11/18 10:33:26 anicoara Exp $
// =====================================================================
package ch.ethz.prose.query;
// used packages
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.TreeMap;
import junit.framework.*;
import ch.ethz.jvmai.*;
import ch.ethz.prose.*;
import ch.ethz.prose.crosscut.Crosscut;
import ch.ethz.prose.crosscut.ThrowCut;
import ch.ethz.prose.engine.*;
import ch.ethz.prose.filter.Exceptions;
import ch.ethz.prose.filter.PointCutter;
/**
* JUnit testcase for classes ClassSurrogate, MethodSurrogate, FieldSurogate
* and JoinPointRequestSurrogate.
*
* @version $Revision: 1.4 $
* @author Philippe Schoch
*/
public class SurrogateTest extends TestCase {
public static abstract class MyTestClass {
public static int fa;
public boolean fb;
public static String fc;
public AspectManager fd;
protected int[] fe;
private Object ff;
public static void ma() {}
public String mb(byte a, short b, int c, long d, float e, double f, boolean g) { return null; }
public static int mc(Object a, String b, AspectManager c, TreeMap d, boolean e, double[] f, Throwable[][][] g) { return 0; }
public abstract Class[] md();
protected void me() {}
private boolean mf() { return true; }
}
public static class MyExtension extends DefaultAspect {
public Crosscut ca = new ThrowCut() {
public void THROW_ARGS() { }
public Class[] potentialCrosscutClasses()
{ Class[] result = { LinkageError.class,
IllegalStateException.class,
InstantiationError.class,
NullPointerException.class,
RuntimeException.class,
ClassFormatError.class,
UnknownError.class};
return result; }
protected PointCutter pointCutter()
{ return ( (Exceptions.type("InstantiationError")) . OR (Exceptions.type("UnknownError")) );}
};
public Crosscut cb = new ThrowCut() {
public void THROW_ARGS() { }
public Class[] potentialCrosscutClasses()
{ Class[] result = { LinkageError.class,
IllegalStateException.class,
InstantiationError.class,
NullPointerException.class,
RuntimeException.class,
ClassFormatError.class,
UnknownError.class};
return result; }
protected PointCutter pointCutter()
{ return ( (Exceptions.type("NullPointerException")) . OR (Exceptions.type("RuntimeException")) );}
};
public Crosscut cc = new ThrowCut() {
public void THROW_ARGS() { }
public Class[] potentialCrosscutClasses()
{ Class[] result = { LinkageError.class,
IllegalStateException.class,
InstantiationError.class,
NullPointerException.class,
RuntimeException.class,
ClassFormatError.class,
UnknownError.class}; return result; }
protected PointCutter pointCutter()
{ return ( (Exceptions.type("ClassFormatError")) . OR (Exceptions.type("UnknownError")) );}
};
}
MyTestClass tstClsObj = null;
Class tstCls = null;
JVMAspectInterface ai = null;
/**
* Construct test with given name.
* @param name test name
*/
public SurrogateTest(String name) {
super(name);
}
/**
* Set up fixture.
*/
protected void setUp() {
try {
ProseSystem.startup();
}
catch (SystemStartupException e) {
Assert.fail("could not start ProseSystem");
}
tstClsObj = new MyTestClass() { public Class[] md() { return null; } };
tstCls = SurrogateTest.MyTestClass.class;
ai = ProseSystem.getAspectManager().getJoinPointManager().getAspectInterface();
}
public void tearDown() {
try {
ProseSystem.teardown();
}
catch (SystemTeardownException e) {
Assert.fail("could not teardown ProseSystem");
}
}
// create a methodSurrogate and recreate the Method.
public void test0010_MethodSurrogate() {
try {
Method m;
MethodSurrogate sur;
Class[] paramTypes;
Method[] mArr = tstCls.getDeclaredMethods();
for (int j=0; j < mArr.length; j++) {
m = mArr[j];
paramTypes = m.getParameterTypes();
sur = new MethodSurrogate(m);
assertTrue("["+j+"] name of Surrogate and method are the same", sur.getName().equals(m.getName()));
assertTrue("["+j+"] recreated method is still the same", sur.toRealInstance(tstCls.getName()).equals(m));
}
}
catch (Exception e) {
e.printStackTrace();
}
}
// create a fieldSurrogate and recreate the Field.
public void test0020_FieldSurrogate() {
try {
Field f;
FieldSurrogate sur;
Class type;
Field[] fArr = tstCls.getDeclaredFields();
for (int j=0; j < fArr.length; j++) {
f = fArr[j];
type = f.getType();
sur = new FieldSurrogate(f);
assertTrue("["+j+"] name of Surrogate and field are the same", sur.getName().equals(f.getName()));
assertTrue("["+j+"] recreated field is still the same", sur.toRealInstance(tstCls.getName()).equals(f));
}
}
catch (Exception e) {
e.printStackTrace();
}
}
// create a classSurrogate and recreate the Class.
public void test0030_ClassSurrogate() {
try {
Class c;
ClassSurrogate sur;
Class[] cArr = new Class[] {MyTestClass.class, MyExtension.class, SurrogateTest.class};
for (int j=0; j < cArr.length; j++) {
c = cArr[j];
sur = new ClassSurrogate(c);
assertTrue(c.getName()+": class names are equal", sur.getName().equals(c.getName()));
assertTrue(c.getName()+" class is still the same", sur.toRealInstance().equals(c));
}
}
catch (Exception e) {
e.printStackTrace();
}
}
// create JoinPointRequestSurrogates and recreate the JoinPointRequests.
public void test0040_JoinPointRequestSurrogate() {
try {
JoinPointManager jpm;
JoinPointRequest jpr;
JoinPointRequestSurrogate sur;
Method m; Field f; Class c;
jpm = ProseSystem.getAspectManager().getJoinPointManager();
QueryManager qm = new QueryManager(ProseSystem.getAspectManager());
Method[] mArr = tstCls.getDeclaredMethods();
for (int j=0; j < mArr.length; j++) {
m = mArr[j];
jpr = (MethodEntryRequest)jpm.createJoinPointRequest(MethodEntryJoinPoint.KIND,m);
sur = new JoinPointRequestSurrogate(jpr);
assertTrue("["+j+"] recreated MethodEntryRequest is still the same",
qm.reconstructRequest(sur).equals(jpr));
jpr = (MethodExitRequest)jpm.createJoinPointRequest(MethodExitJoinPoint.KIND,m);
sur = new JoinPointRequestSurrogate(jpr);
assertTrue("["+j+"] recreated MethodExitRequest is still the same",
qm.reconstructRequest(sur).equals(jpr));
}
Field[] fArr = tstCls.getDeclaredFields();
for (int j=0; j < fArr.length; j++) {
f = fArr[j];
jpr = (FieldAccessRequest)jpm.createJoinPointRequest(FieldAccessJoinPoint.KIND,f);
sur = new JoinPointRequestSurrogate(jpr);
assertTrue("["+j+"] recreated FieldAccessRequest is still the same",
qm.reconstructRequest(sur).equals(jpr));
jpr = (FieldModificationRequest)jpm.createJoinPointRequest(FieldModificationJoinPoint.KIND,f);
sur = new JoinPointRequestSurrogate(jpr);
assertTrue("["+j+"] recreated FieldModificationRequest is still the same",
qm.reconstructRequest(sur).equals(jpr));
}
Class[] cArr = new Class[] {Error.class, SystemStartupException.class, ArrayIndexOutOfBoundsException.class};
for (int j=0; j < cArr.length; j++)
{
c = cArr[j];
jpr = (ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,c);
sur = new JoinPointRequestSurrogate(jpr);
assertTrue("["+j+"] recreated ExceptionThrowRequest is still the same",
qm.reconstructRequest(sur).equals(jpr));
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public void test0050_equals() {
Method m; Field f; Class c; Aspect ext = new MyExtension();
Crosscut cr = new ThrowCut() {
public void THROW_ARGS() { }
public Class[] potentialCrosscutClasses()
{ Class[] result = { LinkageError.class,
IllegalStateException.class,
InstantiationError.class,
NullPointerException.class,
RuntimeException.class,
ClassFormatError.class,
UnknownError.class}
; return result; }
protected PointCutter pointCutter()
{ return ( (Exceptions.type("InstantiationError")) . OR (Exceptions.type("UnknownError")) );}
};
JoinPointManager jpm = ProseSystem.getAspectManager().getJoinPointManager();
try {
Class c1 = MyTestClass.class;
Class c2 = MyTestClass.class;
ClassSurrogate s1 = new ClassSurrogate(c1);
ClassSurrogate s2 = new ClassSurrogate(c2);
assertEquals("ClassSurrogate equals", s1, s2);
MethodSurrogate sur1, sur2;
Class[] paramTypes;
Method[] mArr = tstCls.getDeclaredMethods();
for (int j=0; j < mArr.length; j++) {
m = mArr[j];
paramTypes = m.getParameterTypes();
sur1 = new MethodSurrogate(m);
sur2 = new MethodSurrogate(m);
assertEquals("MethodSurrogate equals", sur1, sur2);
}
FieldSurrogate fsur1, fsur2;
Class type;
Field[] fArr = tstCls.getDeclaredFields();
for (int j=0; j < fArr.length; j++) {
f = fArr[j];
type = f.getType();
fsur1 = new FieldSurrogate(f);
fsur2 = new FieldSurrogate(f);
assertEquals("FieldSurrogate equals", fsur1, fsur2);
}
JoinPointRequest jpr;
JoinPointRequestSurrogate jsur1, jsur2;
mArr = tstCls.getDeclaredMethods();
for (int j=0; j < mArr.length; j++) {
m = mArr[j];
jpr = (MethodEntryRequest)jpm.createJoinPointRequest(MethodEntryJoinPoint.KIND,m);
jsur1 = new JoinPointRequestSurrogate(jpr);
jsur2 = new JoinPointRequestSurrogate(jpr);
assertEquals("JoinPointRequestSurrogate equals "+jsur1.getKind(), jsur1, jsur2);
assertEquals("Tupel equals"+jsur1.getKind(),
new Tuple(new AspectSurrogate(ext), new CrosscutSurrogate(new AspectSurrogate(ext),cr), jsur1),
new Tuple(new AspectSurrogate(ext), new CrosscutSurrogate(new AspectSurrogate(ext),cr), jsur2));
jpr = (MethodExitRequest)jpm.createJoinPointRequest(MethodExitJoinPoint.KIND,m);
jsur1 = new JoinPointRequestSurrogate(jpr);
jsur2 = new JoinPointRequestSurrogate(jpr);
assertEquals("JoinPointRequestSurrogate equals "+jsur1.getKind(), jsur1, jsur2);
}
fArr = tstCls.getDeclaredFields();
for (int j=0; j < fArr.length; j++) {
f = fArr[j];
jpr = (FieldAccessRequest)jpm.createJoinPointRequest(FieldAccessJoinPoint.KIND,f);
jsur1 = new JoinPointRequestSurrogate(jpr);
jsur2 = new JoinPointRequestSurrogate(jpr);
assertEquals("JoinPointRequestSurrogate equals "+jsur1.getKind(), jsur1, jsur2);
jpr = (FieldModificationRequest)jpm.createJoinPointRequest(FieldModificationJoinPoint.KIND,f);
jsur1 = new JoinPointRequestSurrogate(jpr);
jsur2 = new JoinPointRequestSurrogate(jpr);
assertEquals("JoinPointRequestSurrogate equals "+jsur1.getKind(), jsur1, jsur2);
}
Class[] cArr = new Class[] {Error.class, SystemStartupException.class, ArrayIndexOutOfBoundsException.class};
for (int j=0; j < cArr.length; j++) {
c = cArr[j];
jpr = (ExceptionThrowRequest)jpm.createJoinPointRequest(ExceptionJoinPoint.KIND,c);
jsur1 = new JoinPointRequestSurrogate(jpr);
jsur2 = new JoinPointRequestSurrogate(jpr);
assertEquals("JoinPointRequestSurrogate equals "+jsur1.getKind(), jsur1, jsur2);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* Test suite.
* @return test instance
*/
public static Test suite() {
return new TestSuite(SurrogateTest.class);
}
}