assertNull(references);
}
public void testNestedMultipleReferences()
{
ArrayRegistry registry = ArrayRegistry.getInstance();
int[] arr = new int[] {1, 2, 3};
ClassForReference obj = new ClassForReference();
Object[][][][] fieldA = new Object[][][][] {null, null, new Object[][][] {null, new Object[][] {null, null, new Object[] {null, null, null, arr}}}}; //2, 1, 2, 3
obj.fieldA = fieldA;
Object[][][] staticA = new Object[][][] {new Object[][][] {null, new Object[][] {null, null, new Object[]{null, null, arr}}},null}; //0, 1, 2, 2
ClassForReference.staticA = staticA;
final String FIELDA = "fieldA2123";
final String STATICA = "staticA0122";
HashSet elements = new HashSet();
elements.add(FIELDA);
elements.add(STATICA);
List<ArrayReference> references = registry.getArrayOwners(arr);
assertEquals(2, references.size());
for (ArrayReference ref : references)
{
Object owner = ref.getRootObject();
assertNotNull(ref.getNestedArrayIndices());
StringBuffer elem = new StringBuffer(ref.getRootField());
List<Integer> indices = ref.getNestedArrayIndices();
for (Integer index : indices)
{
elem.append(index);
}
System.out.println("----> elem " + elem.toString());
elements.remove(elem.toString());
if (elem.toString().equals(STATICA))
{
assertEquals(ClassForReference.class, owner);
}
else if (elem.toString().equals(FIELDA))
{
assertEquals(obj, owner);
}
}
assertTrue("Did not find all references " + elements, elements.size() == 0);
fieldA[2][1][2][3] = null;
references = registry.getArrayOwners(arr);
assertEquals(1, references.size());
ArrayReference reference = references.get(0);
assertEquals(ClassForReference.class, reference.getRootObject());
assertEquals("staticA", reference.getRootField());
assertEquals(4, reference.getNestedArrayIndices().size());
assertEquals(new Integer(0), reference.getNestedArrayIndices().get(0));
assertEquals(new Integer(1), reference.getNestedArrayIndices().get(1));
assertEquals(new Integer(2), reference.getNestedArrayIndices().get(2));
assertEquals(new Integer(2), reference.getNestedArrayIndices().get(3));
((Object[])staticA[0][1][2])[2] = null;
references = registry.getArrayOwners(arr);
assertNull(references);
}