/** * Dumps the contents of the session attributes. * * @param obj the object to dump. * @return the dump of the given object. */ public static ObjectGraphNode dump(final Object obj) { ObjectGraphDump dump = new ObjectGraphDump(false, true); ObjectGraphNode root = new ObjectGraphNode(++dump.nodeCount, null, obj.getClass().getName(), obj); dump.visit(root); return root; }
/** * Visits all the elements of the given list. * * @param node the ObjectGraphNode containing the list. */ private void visitList(final ObjectGraphNode node) { int index = 0; for (Iterator i = ((List) node.getValue()).iterator(); i.hasNext();) { Object entry = i.next(); String entryType = entry == null ? Object.class.getName() : entry.getClass().getName(); ObjectGraphNode childNode = new ObjectGraphNode(++nodeCount, "[" + index++ + "]", entryType, entry); node.add(childNode); visit(childNode); } adjustOverhead(node); }
ObjectGraphNode keyNode = new ObjectGraphNode(++nodeCount, "key", key.getClass(). getName(), key); node.add(keyNode); visit(keyNode); } else { ObjectGraphNode keyNode = new ObjectGraphNode(++nodeCount, "key", Object.class. getName(), null); node.add(keyNode); ObjectGraphNode valueNode = new ObjectGraphNode(++nodeCount, "value", value. getClass().getName(), value); node.add(valueNode); visit(valueNode); } else { ObjectGraphNode valueNode = new ObjectGraphNode(++nodeCount, "value", Object.class. getName(), null); node.add(valueNode);
/** * Visits all the elements of the given array. * * @param node the ObjectGraphNode containing the array. */ private void visitArray(final ObjectGraphNode node) { if (node.getValue() instanceof Object[]) { Object[] array = (Object[]) node.getValue(); for (int i = 0; i < array.length; i++) { String entryType = array[i] == null ? Object.class.getName() : array[i].getClass(). getName(); ObjectGraphNode childNode = new ObjectGraphNode(++nodeCount, "[" + i + "]", entryType, array[i]); node.add(childNode); visit(childNode); } } else { ObjectGraphNode childNode = new ObjectGraphNode(++nodeCount, "[primitive array]", node. getValue().getClass().getName(), node.getValue()); node.add(childNode); } }
@Test public void testGetSize() { ObjectGraphNode node = new ObjectGraphNode(1, "test", int.class.getName(), 1); Assert.assertEquals("Incorrect size", 4, node.getSize()); node = new ObjectGraphNode(1, "test", long.class.getName(), 1); Assert.assertEquals("Incorrect size", 8, node.getSize()); node = new ObjectGraphNode(1, "test", Object.class.getName(), new Object()); int expectedSize = ObjectGraphNode.OBJECT_SHELL_SIZE + ObjectGraphNode.OBJREF_SIZE; Assert.assertEquals("Incorrect size", expectedSize, node.getSize()); }
@Test public void testIsPrimitive() { ObjectGraphNode node = new ObjectGraphNode(1, "test", String.class.getName(), "test"); Assert.assertFalse("Node should not be primitive", node.isPrimitive()); node = new ObjectGraphNode(1, "test", int.class.getName(), 123); Assert.assertTrue("Node should be is primitive", node.isPrimitive()); }
/** * Visits all the fields in the given complex object. * * @param node the ObjectGraphNode containing the object. */ private void visitComplexType(final ObjectGraphNode node) { Field[] fields = getAllInstanceFields(node.getValue()); for (int i = 0; i < fields.length; i++) { Object fieldValue = readField(fields[i], node.getValue()); String fieldType = fields[i].getType().getName(); ObjectGraphNode childNode = new ObjectGraphNode(++nodeCount, fields[i].getName(), fieldType, fieldValue); node.add(childNode); visit(childNode); } }
@Test public void testGetType() { ObjectGraphNode node = new ObjectGraphNode(1, "test", int.class.getName(), 1); Assert.assertEquals("Should retrieve type set", int.class.getName(), node.getType()); }
@Test public void testGetId() { int nodeId = 12345; ObjectGraphNode node = new ObjectGraphNode(nodeId, "test", String.class.getName(), "test"); Assert.assertEquals("should retrieve correct ID", nodeId, node.getId()); }
@Test public void testGetFieldName() { String fieldName = "ObjectGraphNode_Test.testGetFieldName.fieldName"; ObjectGraphNode node = new ObjectGraphNode(1, fieldName, String.class.getName(), "test"); Assert.assertEquals("should retrieve correct field name", fieldName, node.getFieldName()); }
@Test public void testSetRefNode() { ObjectGraphNode node = new ObjectGraphNode(1, "test", String.class.getName(), "test"); ObjectGraphNode refNode = new ObjectGraphNode(2, "test", String.class.getName(), "test"); node.setRefNode(refNode); Assert.assertSame("Should retrieve refNod set", refNode, node.getRefNode()); }
@Test public void testIsSimpleType() { ObjectGraphNode node = new ObjectGraphNode(1, "test", String.class.getName(), "test"); Assert.assertTrue("Node should be a simple type", node.isSimpleType()); node = new ObjectGraphNode(1, "test", Duplet.class.getName(), new Duplet<String, String>()); Assert.assertFalse("Node should not be a simple type", node.isSimpleType()); }
@Test public void testGetValue() { String fieldValue = "ObjectGraphNode_Test.testGetValue.fieldValue"; ObjectGraphNode node = new ObjectGraphNode(1, "test", String.class.getName(), fieldValue); Assert.assertSame("Should retrieve value set", fieldValue, node.getValue()); }
/** * Visits all the fields in the given complex object, noting differences. * * @param node the ObjectGraphNode containing the object. * @param otherInstance the other instance to compare to */ private void visitComplexTypeWithDiff(final ObjectGraphNode node, final Object otherInstance) { if (otherInstance == null) { // Nothing to compare against, just use the default visit visitComplexType(node); } else { Field[] fields = getAllInstanceFields(node.getValue()); for (int i = 0; i < fields.length; i++) { Object fieldValue = readField(fields[i], node.getValue()); Object otherValue = readField(fields[i], otherInstance); String fieldType = fields[i].getType().getName(); String nodeFieldName = fields[i].getName() + (Util.equals(fieldValue, otherValue) ? "" : "*"); ObjectGraphNode childNode = new ObjectGraphNode(++nodeCount, nodeFieldName, fieldType, fieldValue); node.add(childNode); visit(childNode); } } }
@Test public void testToXml() { WContainer component = new WContainer(); setActiveContext(createUIContext()); WLabel label = new WLabel(TEST_LABEL); component.add(label); final int nodeId = component.getIndexOfChild(label); final String fieldName = label.getId(); ObjectGraphNode node = new ObjectGraphNode(nodeId, fieldName, label.getClass().getName(), label); String xmlSummary = node.toXml(); Assert.assertTrue("should report correct ID", xmlSummary.indexOf("object id=\"0\"") != -1); Assert.assertTrue("should report correct field name", xmlSummary.indexOf( "field=\"" + fieldName + "\"") != -1); Assert.assertTrue("should report correct class name", xmlSummary.indexOf("type=\"com.github.bordertech.wcomponents.WLabel\"") != -1); Assert.assertTrue("should report correct size", xmlSummary.indexOf("size=\"12\"") != -1); } }
@Test public void testToFlatSummary() { WContainer component = new WContainer(); setActiveContext(createUIContext()); WLabel label = new WLabel(TEST_LABEL); component.add(label); final int nodeId = component.getIndexOfChild(label); final String fieldName = label.getId(); ObjectGraphNode node = new ObjectGraphNode(nodeId, fieldName, label.getClass().getName(), label); String flatSummary = node.toFlatSummary(); flatSummary = flatSummary.replaceAll("\n", ""); Assert.assertTrue("summary should start with size", flatSummary.startsWith( node.getSize() + " ")); Assert.assertTrue("summary should end with class name", flatSummary.endsWith(label. getClass().getName())); }