}
}
public void testReferences_getReferenceType() throws Exception {
JavaObject thisObj = getScenerioReference();
Iterator references = thisObj.getReferences().iterator();
while (references.hasNext()) {
Object next = references.next();
if (!(next instanceof JavaReference)) {
continue;
}
JavaReference ref = (JavaReference)next;
int type = ref.getReferenceType();
Object source = ref.getSource();
Object target = ref.getTarget();
switch (type) {
case JavaReference.REFERENCE_FIELD:
if (source instanceof JavaObject) {
JavaObject sourceObject = (JavaObject)source;
if (!(sourceObject.equals(thisObj))) {
fail("Source object for a field reference not equal to this object.");
}
if (target instanceof JavaObject) {
JavaObject targetObject = (JavaObject)target;
String objectsClassName = targetObject.getJavaClass().getName();
if (!(objectsClassName.endsWith("Reference") ||
objectsClassName.endsWith("Referent") ||
objectsClassName.endsWith("ArrayElement;") ||
objectsClassName.endsWith("ClassLoader"))) {
fail("Unexpected field reference of type: "+ objectsClassName);
}
if (objectsClassName.endsWith("ArrayElement;") && targetObject.isArray()) {
//found the array field.
Iterator refs = targetObject.getReferences().iterator();
boolean arrayElementFound = false;
while (refs.hasNext()) {
Object next2 = refs.next();
if (!(next2 instanceof JavaReference)) {
continue;
}
JavaReference ref2 = (JavaReference)next2;
int type2 = ref2.getReferenceType();
Object target2 = ref2.getTarget();
if (type2 == JavaReference.REFERENCE_ARRAY_ELEMENT) {
if (target2 instanceof JavaObject) {
JavaObject targetObject2 = (JavaObject)target2;
String objectsClassName2 = targetObject2.getJavaClass().getName();
if (objectsClassName2.endsWith("ArrayElement") && !targetObject2.isArray()) {
arrayElementFound = true;
}
} else {
fail("Target object of a REFERENCE_ARRAY_ELEMENT reference has the wrong type.");
}
}
}
assertTrue("Array element reference not found.", arrayElementFound);
}
} else {
fail("Target object of a REFERENCE_ARRAY_ELEMENT reference has the wrong type.");
}
} else {
fail("Source object of a field reference is not a valid JavaObject.");
}
break;
case JavaReference.REFERENCE_CLASS:
if (source instanceof JavaObject) {
JavaObject sourceObject = (JavaObject)source;
if (!(sourceObject.equals(thisObj))) {
fail("Source object for a CLASS reference not equal to this object.");
}
if (!(target instanceof JavaClass)) {
fail("Target object for a CLASS reference not a JavaClass object.");
}
JavaClass targetClass = (JavaClass)target;
assertTrue("Target class for the CLASS reference of this object different from expected.", targetClass.getName().endsWith("SetupJavaReferences"));
Iterator refs = targetClass.getReferences().iterator();
boolean classLoaderFound = false;
while (refs.hasNext()) {
Object next2 = refs.next();
if (!(next2 instanceof JavaReference)) {
continue;
}
JavaReference ref2 = (JavaReference)next2;
int type2 = ref2.getReferenceType();
Object target2 = ref2.getTarget();
if (type2 == JavaReference.REFERENCE_CLASS_LOADER) {
if (!(target2 instanceof JavaObject)) {
fail("Target object for a CLASSLOADER reference does not represent a JavaObject.");
}
JavaObject targetObject2 = (JavaObject)target2;
if (targetObject2.getClass().getName().endsWith("ClassLoader")) {
fail("Target object for a CLASSLOADER reference does not represent a ClassLoader.");
} else {
classLoaderFound = true;
}
}
}
assertTrue("ClassLoader reference not found.", classLoaderFound);
} else {
fail("Source object of a CLASS reference is not a valid JavaObject.");
}
break;
case JavaReference.REFERENCE_CLASS_LOADER:
if (source instanceof JavaObject) {
JavaObject sourceObject = (JavaObject)source;
if (!(sourceObject.equals(thisObj))) {
fail("Source object for a CLASSLOADER reference not equal to this object.");
}
if (!(target instanceof JavaClassLoader)) {
fail("Target object for a CLASSLOADER reference not a JavaClass object.");