public void testInspection() { String inspect = mInspector.inspect(null, "org.metawidget.inspector.xml.XmlInspectorTest$SubFoo"); internalTestInspection(XmlUtils.documentFromString(inspect)); Element domInspect = mInspector.inspectAsDom(null, "org.metawidget.inspector.xml.XmlInspectorTest$SubFoo"); assertEquals(inspect, XmlUtils.nodeToString(domInspect, false)); internalTestInspection(domInspect.getOwnerDocument()); }
public void testAnnotations() { SwingAppFrameworkInspector inspector = new SwingAppFrameworkInspector(); Document document = XmlUtils.documentFromString(inspector.inspect(null, Foo.class.getName())); assertEquals("inspection-result", document.getFirstChild().getNodeName()); // Entity Element entity = (Element) document.getDocumentElement().getFirstChild(); assertEquals(ENTITY, entity.getNodeName()); assertEquals(Foo.class.getName(), entity.getAttribute(TYPE)); assertFalse(entity.hasAttribute(NAME)); // Actions Element action = (Element) entity.getFirstChild(); assertEquals(ACTION, action.getNodeName()); assertEquals("doBar", action.getAttribute(NAME)); assertEquals("barLabel", action.getAttribute(LABEL)); assertEquals(action.getAttributes().getLength(), 2); assertEquals(entity.getChildNodes().getLength(), 1); }
public void testCollectionWithManyColumns() throws Exception { HtmlMetawidgetTag metawidgetTag = new HtmlMetawidgetTag(); metawidgetTag.setPageContext(new DummyPageContext()); Document document = XmlUtils.documentFromString( "<inspection-result><entity><property name=\"column1\"/><property name=\"column2\"/><property name=\"column3\"/><property name=\"column4\"/><property name=\"column5\"/><property name=\"column6\"/></entity></inspection-result>"); Element entity = (Element) document.getDocumentElement().getFirstChild(); final List<String> columnsAdded = CollectionUtils.newArrayList(); DisplayTagWidgetBuilder widgetBuilder = new DisplayTagWidgetBuilder() { @Override protected boolean addColumnTag( TableTag tableTag, Map<String, String> tableAttributes, Map<String, String> columnAttributes, MetawidgetTag innerMetawidgetTag) { columnsAdded.add(innerMetawidgetTag.getLabelString(columnAttributes)); return true; } }; TableTag tableTag = new TableTag(); Map<String, String> attributes = CollectionUtils.newHashMap(); widgetBuilder.addColumnTags(tableTag, attributes, entity.getChildNodes(), metawidgetTag); assertEquals(5, columnsAdded.size()); }
public void testTld() throws Exception { InputStream in = ClassLoader.getSystemResourceAsStream("META-INF/metawidget-faces.tld"); ByteArrayOutputStream out = new ByteArrayOutputStream(); IOUtils.streamBetween(in, out); Document document = XmlUtils.documentFromString(out.toString()); Element root = document.getDocumentElement(); assertEquals(root.getNodeName(), "taglib"); // org.metawidget.faces.taglib.html.HtmlMetawidgetTag Element tag = XmlUtils.getChildNamed(root, "tag"); Element tagclass = XmlUtils.getChildNamed(tag, "tagclass"); assertEquals(tagclass.getTextContent(), "org.metawidget.faces.taglib.html.HtmlMetawidgetTag"); Element bodycontent = XmlUtils.getSiblingNamed(tagclass, "bodycontent"); assertEquals(bodycontent.getTextContent(), "JSP"); // org.metawidget.faces.taglib.StubTag tag = XmlUtils.getSiblingNamed(tag, "tag"); tagclass = XmlUtils.getChildNamed(tag, "tagclass"); assertEquals(tagclass.getTextContent(), "org.metawidget.faces.taglib.StubTag"); bodycontent = XmlUtils.getSiblingNamed(tagclass, "bodycontent"); assertEquals(bodycontent.getTextContent(), "JSP"); }
public void testTraverseViaParent() { Document document = XmlUtils.documentFromString( mInspector.inspect( null, "org.metawidget.inspector.xml.XmlInspectorTest$SubFoo", "bar")); assertEquals("inspection-result", document.getFirstChild().getNodeName()); Element entity = (Element) document.getDocumentElement().getFirstChild(); assertEquals(ENTITY, entity.getNodeName()); assertEquals("Bar", entity.getAttribute(TYPE)); assertEquals("bar", entity.getAttribute(NAME)); assertEquals("true", entity.getAttribute(REQUIRED)); // Properties Element property = (Element) entity.getFirstChild(); assertEquals(PROPERTY, property.getNodeName()); assertEquals("baz", property.getAttribute(NAME)); property = (Element) property.getNextSibling(); assertEquals(ACTION, property.getNodeName()); assertEquals("doAction", property.getAttribute(NAME)); assertEquals(entity.getChildNodes().getLength(), 2); // No extends support XmlInspector inspector = new XmlInspector( new XmlInspectorConfig().setInputStream(new ByteArrayInputStream(mXml.getBytes()))) { @Override protected String getExtendsAttribute() { return null; } }; assertEquals( null, XmlUtils.documentFromString( inspector.inspect( null, "org.metawidget.inspector.xml.XmlInspectorTest$SubFoo", "bar"))); }
public void testTraversal() { String json = "{ properties: { \"path1\": { \"properties\": { \"foo\": { \"fooProp\": \"Foo\" }}, \"bar\": 42 }, \"path2\": { \"baz\": true }}}"; JsonSchemaInspector inspector = new JsonSchemaInspector( new JsonInspectorConfig().setInputStream(new ByteArrayInputStream(json.getBytes()))); // Path1 Document document = XmlUtils.documentFromString(inspector.inspect(null, "fooObject", "path1")); assertEquals("inspection-result", document.getFirstChild().getNodeName()); Element entity = (Element) document.getDocumentElement().getFirstChild(); assertEquals(ENTITY, entity.getNodeName()); assertEquals("path1", entity.getAttribute(NAME)); assertEquals("fooObject", entity.getAttribute(TYPE)); assertEquals("42", entity.getAttribute("bar")); assertEquals(entity.getAttributes().getLength(), 3); Element property = (Element) entity.getFirstChild(); assertEquals(PROPERTY, property.getNodeName()); assertEquals("foo", property.getAttribute(NAME)); assertEquals("Foo", property.getAttribute("fooProp")); assertEquals(property.getAttributes().getLength(), 2); assertEquals(property.getNextSibling(), null); // Path2 document = XmlUtils.documentFromString(inspector.inspect(null, "fooObject", "path2")); assertEquals("inspection-result", document.getFirstChild().getNodeName()); entity = (Element) document.getDocumentElement().getFirstChild(); assertEquals(ENTITY, entity.getNodeName()); assertEquals("path2", entity.getAttribute(NAME)); assertEquals("fooObject", entity.getAttribute(TYPE)); assertEquals("true", entity.getAttribute("baz")); assertEquals(entity.getAttributes().getLength(), 3); assertEquals(entity.getFirstChild(), null); // Bad path assertEquals( null, XmlUtils.documentFromString(inspector.inspect(null, "fooObject", "badPath"))); }
public void testLookup() { JspAnnotationInspector inspector = new JspAnnotationInspector(); Document document = XmlUtils.documentFromString(inspector.inspect(new Foo(), Foo.class.getName())); assertEquals("inspection-result", document.getFirstChild().getNodeName()); // Entity Element entity = (Element) document.getDocumentElement().getFirstChild(); assertEquals(ENTITY, entity.getNodeName()); assertEquals(Foo.class.getName(), entity.getAttribute(TYPE)); assertFalse(entity.hasAttribute(NAME)); // Properties Element property = XmlUtils.getChildWithAttributeValue(entity, NAME, "object1"); assertEquals(PROPERTY, property.getNodeName()); assertEquals("${foo.bar}", property.getAttribute(JSP_LOOKUP)); assertEquals(2, property.getAttributes().getLength()); assertEquals(1, entity.getChildNodes().getLength()); }
/** * Parses the given XML and determines what namespaces it already declares. These are later * removed from the list of namespaces that Metawidget introduces. */ protected Map<String, String> parseNamespaces(final String template) { Map<String, String> namespaces = CollectionUtils.newHashMap(); Document document = XmlUtils.documentFromString(template); Element element = document.getDocumentElement(); NamedNodeMap attributes = element.getAttributes(); for (int loop = 0, length = attributes.getLength(); loop < length; loop++) { org.w3c.dom.Node node = attributes.item(loop); String nodeName = node.getNodeName(); int indexOf = nodeName.indexOf(XMLNS_PREFIX); if (indexOf == -1) { continue; } namespaces.put(nodeName.substring(indexOf + XMLNS_PREFIX.length()), node.getNodeValue()); } return namespaces; }
public void testNormalUse() { String json = "{ \"foo\": \"Foo\", properties: { \"bar\": { \"barProp\": 42, \"baz\": true }, \"abc\": { \"arrayProp\": [ 1, 2, \"3,4\" ] }}}"; JsonSchemaInspector inspector = new JsonSchemaInspector( new JsonInspectorConfig().setInputStream(new ByteArrayInputStream(json.getBytes()))); Document document = XmlUtils.documentFromString(inspector.inspect(null, "fooObject")); assertEquals("inspection-result", document.getFirstChild().getNodeName()); // Entity Element entity = (Element) document.getDocumentElement().getFirstChild(); assertEquals(ENTITY, entity.getNodeName()); assertFalse(entity.hasAttribute(NAME)); assertEquals("fooObject", entity.getAttribute(TYPE)); assertEquals("Foo", entity.getAttribute("foo")); assertEquals(entity.getAttributes().getLength(), 2); // Properties Element property = (Element) entity.getFirstChild(); assertEquals(PROPERTY, property.getNodeName()); assertEquals("bar", property.getAttribute(NAME)); assertEquals("42", property.getAttribute("barProp")); assertEquals("true", property.getAttribute("baz")); assertEquals(property.getAttributes().getLength(), 3); property = (Element) property.getNextSibling(); assertEquals(PROPERTY, property.getNodeName()); assertEquals("abc", property.getAttribute(NAME)); assertEquals("1,2,3\\,4", property.getAttribute("arrayProp")); assertEquals(property.getAttributes().getLength(), 2); assertEquals(property.getNextSibling(), null); }
@Override public Element processInspectionResultAsDom( Element inspectionResult, M metawidget, Object toInspect, String type, String... names) { try { // Start a new document // // (Android 1.1 did not cope well with shuffling the nodes of an existing document) Document newDocument = XmlUtils.newDocument(); Element newInspectionResult = newDocument.createElementNS(NAMESPACE, ROOT); XmlUtils.setMapAsAttributes( newInspectionResult, XmlUtils.getAttributesAsMap(inspectionResult)); newDocument.appendChild(newInspectionResult); Element entity = XmlUtils.getFirstChildElement(inspectionResult); Element newEntity = newDocument.createElementNS(NAMESPACE, ENTITY); XmlUtils.setMapAsAttributes(newEntity, XmlUtils.getAttributesAsMap(entity)); newInspectionResult.appendChild(newEntity); // Record all traits (ie. properties/actions) that have comes-after Map<Element, String[]> traitsWithComesAfter = new LinkedHashMap<Element, String[]>(); Element trait = XmlUtils.getFirstChildElement(entity); while (trait != null) { // (if no comes-after, move them across to the new document) if (hasComesAfter(trait, metawidget)) { traitsWithComesAfter.put(trait, ArrayUtils.fromString(getComesAfter(trait, metawidget))); } else { newEntity.appendChild(XmlUtils.importElement(newDocument, trait)); } trait = XmlUtils.getNextSiblingElement(trait); } // Next, sort the traits // // Note: this has high big-O complexity. We could plug-in something smarter for large // numbers of traits. int infiniteLoop = traitsWithComesAfter.size(); infiniteLoop *= infiniteLoop; while (!traitsWithComesAfter.isEmpty()) { // Infinite loop? Explain why infiniteLoop--; if (infiniteLoop < 0) { List<String> comesAfterNames = CollectionUtils.newArrayList(); for (Map.Entry<Element, String[]> entry : traitsWithComesAfter.entrySet()) { String value; if (entry.getValue().length == 0) { value = "at the end"; } else { value = "after " + ArrayUtils.toString(entry.getValue(), " and "); } comesAfterNames.add(entry.getKey().getAttribute(NAME) + " comes " + value); } // (sort for unit tests) Collections.sort(comesAfterNames); throw InspectionResultProcessorException.newException( "Infinite loop detected when sorting " + COMES_AFTER + ": " + CollectionUtils.toString(comesAfterNames, ", but ")); } // For each entry in the Map... outer: for (Iterator<Map.Entry<Element, String[]>> i = traitsWithComesAfter.entrySet().iterator(); i.hasNext(); ) { Map.Entry<Element, String[]> entry = i.next(); Element traitWithComesAfter = entry.getKey(); String[] comesAfter = entry.getValue(); // ...if it 'comesAfter everything', make sure there are only // other 'comesAfter everything's left... if (comesAfter.length == 0) { for (String[] traitWithComesAfterExisting : traitsWithComesAfter.values()) { if (traitWithComesAfterExisting.length > 0) { continue outer; } } newEntity.appendChild(XmlUtils.importElement(newDocument, traitWithComesAfter)); } // ...or, if it 'comesAfter' something, make sure none of those // somethings are left... else { String name = traitWithComesAfter.getAttribute(NAME); for (String comeAfter : comesAfter) { if (name.equals(comeAfter)) { throw InspectionResultProcessorException.newException( "'" + comeAfter + "' " + COMES_AFTER + " itself"); } for (Element traitExisting : traitsWithComesAfter.keySet()) { if (comeAfter.equals(traitExisting.getAttribute(NAME))) { continue outer; } } } // Insert it at the earliest point. This seems most 'natural' Element newTrait = XmlUtils.getFirstChildElement(newEntity); Element insertBefore = newTrait; while (newTrait != null) { if (ArrayUtils.contains(comesAfter, newTrait.getAttribute(NAME))) { newTrait = XmlUtils.getNextSiblingElement(newTrait); insertBefore = newTrait; continue; } newTrait = XmlUtils.getNextSiblingElement(newTrait); } if (insertBefore == null) { newEntity.appendChild(XmlUtils.importElement(newDocument, traitWithComesAfter)); } else { newEntity.insertBefore( XmlUtils.importElement(newDocument, traitWithComesAfter), insertBefore); } } i.remove(); } } return newInspectionResult; } catch (Exception e) { throw InspectionResultProcessorException.newException(e); } }
public void testNestedProperties() { String xml = "<?xml version=\"1.0\"?>"; xml += "<inspection-result xmlns=\"http://www.metawidget.org/inspection-result\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.metawidget.org/inspection-result ../../inspector/inspection-result-1.0.xsd\" version=\"1.0\">"; xml += "<entity type=\"Foo\">"; xml += "<property name=\"a\"/>"; xml += "<property name=\"b\"/>"; xml += "<property name=\"c\">"; xml += "\t<property name=\"c1\"/>"; xml += "\t<property name=\"c2\"/>"; xml += "\t<property name=\"c3\" type=\"C3-Type\">"; xml += "\t\t<property name=\"c31\"/>"; xml += "\t\t<property name=\"c32\"/>"; xml += "\t</property>"; xml += "</property>"; xml += "</entity>"; xml += "</inspection-result>"; // Without 'type' attribute Inspector inspector = new XmlInspector( new XmlInspectorConfig().setInputStream(new ByteArrayInputStream(xml.getBytes()))); Document document = XmlUtils.documentFromString(inspector.inspect(null, "Foo", "c")); Element entity = (Element) document.getDocumentElement().getFirstChild(); assertEquals(ENTITY, entity.getNodeName()); assertEquals("c", entity.getAttribute(NAME)); assertEquals("", entity.getAttribute(TYPE)); assertEquals(2, entity.getAttributes().getLength()); Element property = (Element) entity.getFirstChild(); assertEquals(PROPERTY, property.getNodeName()); assertEquals("c1", property.getAttribute(NAME)); assertEquals(1, property.getAttributes().getLength()); property = (Element) property.getNextSibling(); assertEquals(PROPERTY, property.getNodeName()); assertEquals("c2", property.getAttribute(NAME)); assertEquals(1, property.getAttributes().getLength()); property = (Element) property.getNextSibling(); assertEquals(PROPERTY, property.getNodeName()); assertEquals("c3", property.getAttribute(NAME)); assertEquals("C3-Type", property.getAttribute(TYPE)); assertEquals(2, property.getAttributes().getLength()); assertEquals(3, entity.getChildNodes().getLength()); // With 'type' attribute inspector = new XmlInspector( new XmlInspectorConfig().setInputStream(new ByteArrayInputStream(xml.getBytes()))); document = XmlUtils.documentFromString(inspector.inspect(null, "Foo", "c", "c3")); entity = (Element) document.getDocumentElement().getFirstChild(); assertEquals(ENTITY, entity.getNodeName()); assertEquals("c3", entity.getAttribute(NAME)); assertEquals("C3-Type", entity.getAttribute(TYPE)); assertEquals(2, entity.getAttributes().getLength()); property = (Element) entity.getFirstChild(); assertEquals(PROPERTY, property.getNodeName()); assertEquals("c31", property.getAttribute(NAME)); assertEquals(1, property.getAttributes().getLength()); property = (Element) property.getNextSibling(); assertEquals(PROPERTY, property.getNodeName()); assertEquals("c32", property.getAttribute(NAME)); assertEquals(1, property.getAttributes().getLength()); assertEquals(2, entity.getChildNodes().getLength()); }
public void testTraversalToNullTopLevelElement() { String xml = "<?xml version=\"1.0\"?>"; xml += "<inspection-result xmlns=\"http://www.metawidget.org/inspection-result\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.metawidget.org/inspection-result ../../inspector/inspection-result-1.0.xsd\" version=\"1.0\">"; xml += "<entity type=\"Foo\">"; xml += "<property name=\"bar\" type=\"Bar\"/>"; xml += "<property name=\"baz\" type=\"Baz\"/>"; xml += "</entity>"; xml += "<entity type=\"Baz\">"; xml += "<property name=\"abc\" type=\"Abc\"/>"; xml += "</entity>"; xml += "<entity type=\"Abc\">"; xml += "<property name=\"def\" type=\"Def\"/>"; xml += "</entity>"; xml += "</inspection-result>"; // Top level XmlInspectorConfig config = new XmlInspectorConfig(); mInspector = new XmlInspector(config.setInputStream(new ByteArrayInputStream(xml.getBytes()))); Document document = XmlUtils.documentFromString(mInspector.inspect(null, "Foo")); Element entity = (Element) document.getDocumentElement().getFirstChild(); assertEquals(ENTITY, entity.getNodeName()); assertEquals("Foo", entity.getAttribute(TYPE)); assertEquals(1, entity.getAttributes().getLength()); Element property = (Element) entity.getFirstChild(); assertEquals(PROPERTY, property.getNodeName()); assertEquals("bar", property.getAttribute(NAME)); assertEquals("Bar", property.getAttribute(TYPE)); assertEquals(2, property.getAttributes().getLength()); property = (Element) property.getNextSibling(); assertEquals(PROPERTY, property.getNodeName()); assertEquals("baz", property.getAttribute(NAME)); assertEquals("Baz", property.getAttribute(TYPE)); assertEquals(2, property.getAttributes().getLength()); assertEquals(2, entity.getChildNodes().getLength()); // Missing type document = XmlUtils.documentFromString(mInspector.inspect(null, "Foo", "bar")); entity = (Element) document.getDocumentElement().getFirstChild(); assertEquals(ENTITY, entity.getNodeName()); assertEquals("bar", entity.getAttribute(NAME)); assertEquals("Bar", entity.getAttribute(TYPE)); assertEquals(2, entity.getAttributes().getLength()); assertEquals(0, entity.getChildNodes().getLength()); // Missing type after traverse from top document = XmlUtils.documentFromString(mInspector.inspect(null, "Foo", "baz")); entity = (Element) document.getDocumentElement().getFirstChild(); assertEquals(ENTITY, entity.getNodeName()); assertEquals("baz", entity.getAttribute(NAME)); assertEquals("Baz", entity.getAttribute(TYPE)); assertEquals(2, entity.getAttributes().getLength()); assertEquals(1, entity.getChildNodes().getLength()); mInspector = new XmlInspector(config.setInputStream(new ByteArrayInputStream(xml.getBytes()))) { @Override protected Element traverseFromTopLevelTypeToNamedChildren(Element topLevel) { return null; } }; document = XmlUtils.documentFromString(mInspector.inspect(null, "Foo")); assertEquals(null, document); document = XmlUtils.documentFromString(mInspector.inspect(null, "Foo", "baz")); assertEquals(null, document); // Missing type after traverse from top (one level down) mInspector = new XmlInspector(config.setInputStream(new ByteArrayInputStream(xml.getBytes()))); document = XmlUtils.documentFromString(mInspector.inspect(null, "Foo", "baz", "abc")); entity = (Element) document.getDocumentElement().getFirstChild(); assertEquals(ENTITY, entity.getNodeName()); assertEquals("abc", entity.getAttribute(NAME)); assertEquals("Abc", entity.getAttribute(TYPE)); assertEquals(2, entity.getAttributes().getLength()); assertEquals(1, entity.getChildNodes().getLength()); mInspector = new XmlInspector(config.setInputStream(new ByteArrayInputStream(xml.getBytes()))) { @Override protected Element traverseFromTopLevelTypeToNamedChildren(Element topLevel) { if ("Abc".equals(topLevel.getAttribute(TYPE))) { return null; } return topLevel; } }; document = XmlUtils.documentFromString(mInspector.inspect(null, "Foo", "baz", "abc")); entity = (Element) document.getDocumentElement().getFirstChild(); assertEquals(ENTITY, entity.getNodeName()); assertEquals("abc", entity.getAttribute(NAME)); assertEquals("Abc", entity.getAttribute(TYPE)); assertEquals(2, entity.getAttributes().getLength()); assertEquals(0, entity.getChildNodes().getLength()); }
public void testTraverseAgainstObjectImpliesType() { String xml = "<?xml version=\"1.0\"?>"; xml += "<inspection-result xmlns=\"http://www.metawidget.org/inspection-result\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.metawidget.org/inspection-result ../../inspector/inspection-result-1.0.xsd\" version=\"1.0\">"; xml += "<entity type=\"" + TraverseAgainstObjectFoo.class.getName() + "\">"; xml += "<property name=\"toTraverse\"/>"; xml += "</entity>"; xml += "</inspection-result>"; TraverseAgainstObjectFoo traverseAgainstObjectFoo = new TraverseAgainstObjectFoo(); traverseAgainstObjectFoo.toTraverse = new TraverseAgainstObjectFoo(); // Top level XmlInspectorConfig config = new XmlInspectorConfig() .setRestrictAgainstObject( new JavaBeanPropertyStyle( new JavaBeanPropertyStyleConfig().setSupportPublicFields(true))); mInspector = new XmlInspector(config.setInputStream(new ByteArrayInputStream(xml.getBytes()))); Document document = XmlUtils.documentFromString( mInspector.inspect(traverseAgainstObjectFoo, TraverseAgainstObjectFoo.class.getName())); // Entity Element entity = (Element) document.getDocumentElement().getFirstChild(); assertEquals(ENTITY, entity.getNodeName()); assertEquals(TraverseAgainstObjectFoo.class.getName(), entity.getAttribute(TYPE)); assertEquals(1, entity.getAttributes().getLength()); // Property Element property = (Element) entity.getFirstChild(); assertEquals(PROPERTY, property.getNodeName()); assertEquals("toTraverse", property.getAttribute(NAME)); // (shouldn't need @type on properties, as XmlUtils.combineElements uses the @name) assertTrue(!property.hasAttribute(TYPE)); assertEquals(1, property.getAttributes().getLength()); assertEquals(1, entity.getChildNodes().getLength()); // Sub-level document = XmlUtils.documentFromString( mInspector.inspect( traverseAgainstObjectFoo, TraverseAgainstObjectFoo.class.getName(), "toTraverse")); // Entity entity = (Element) document.getDocumentElement().getFirstChild(); assertEquals(ENTITY, entity.getNodeName()); assertEquals(Object.class.getName(), entity.getAttribute(TYPE)); assertEquals("toTraverse", entity.getAttribute(NAME)); assertEquals(2, entity.getAttributes().getLength()); // Property property = (Element) entity.getFirstChild(); assertEquals(PROPERTY, property.getNodeName()); assertEquals("toTraverse", property.getAttribute(NAME)); assertTrue(!property.hasAttribute(TYPE)); assertEquals(1, property.getAttributes().getLength()); assertEquals(1, entity.getChildNodes().getLength()); }
public void testTraverseAgainstObject() { String xml = "<?xml version=\"1.0\"?>"; xml += "<inspection-result xmlns=\"http://www.metawidget.org/inspection-result\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.metawidget.org/inspection-result ../../inspector/inspection-result-1.0.xsd\" version=\"1.0\">"; xml += "<entity type=\"" + RestrictAgainstObjectFoo.class.getName() + "\">"; xml += "<property name=\"xmlBar\" type=\"int\"/>"; xml += "</entity>"; xml += "</inspection-result>"; TraverseAgainstObjectFoo traverseAgainstObjectFoo = new TraverseAgainstObjectFoo(); traverseAgainstObjectFoo.toTraverse = new RestrictAgainstObjectFoo(); // Without traverseAgainstObject mInspector = new XmlInspector( new XmlInspectorConfig().setInputStream(new ByteArrayInputStream(xml.getBytes()))); assertEquals( null, mInspector.inspect( traverseAgainstObjectFoo, TraverseAgainstObjectFoo.class.getName(), "toTraverse")); // With traverseAgainstObject XmlInspectorConfig config = new XmlInspectorConfig() .setRestrictAgainstObject( new JavaBeanPropertyStyle( new JavaBeanPropertyStyleConfig().setSupportPublicFields(true))); mInspector = new XmlInspector(config.setInputStream(new ByteArrayInputStream(xml.getBytes()))); Document document = XmlUtils.documentFromString( mInspector.inspect( traverseAgainstObjectFoo, TraverseAgainstObjectFoo.class.getName(), "toTraverse")); // Entity Element entity = (Element) document.getDocumentElement().getFirstChild(); assertEquals(ENTITY, entity.getNodeName()); // (should be the parent property type, so as to align the XML with other Inspectors) assertEquals(Object.class.getName(), entity.getAttribute(TYPE)); assertFalse(entity.hasAttribute(NAME)); // Properties Element property = (Element) entity.getFirstChild(); assertEquals(PROPERTY, property.getNodeName()); assertEquals("xmlBar", property.getAttribute(NAME)); assertEquals("int", property.getAttribute(TYPE)); assertEquals(property.getAttributes().getLength(), 2); assertEquals(entity.getChildNodes().getLength(), 1); // Traverse to a subtype traverseAgainstObjectFoo.toTraverse = new SubRestrictAgainstObjectFoo(); mInspector = new XmlInspector(config.setInputStream(new ByteArrayInputStream(xml.getBytes()))); document = XmlUtils.documentFromString( mInspector.inspect( traverseAgainstObjectFoo, TraverseAgainstObjectFoo.class.getName(), "toTraverse")); // Entity entity = (Element) document.getDocumentElement().getFirstChild(); assertEquals(ENTITY, entity.getNodeName()); // (should be the parent property type, so as to align the XML with other Inspectors) assertEquals(Object.class.getName(), entity.getAttribute(TYPE)); assertFalse(entity.hasAttribute(NAME)); // Properties property = (Element) entity.getFirstChild(); assertEquals(PROPERTY, property.getNodeName()); assertEquals("xmlBar", property.getAttribute(NAME)); assertEquals("int", property.getAttribute(TYPE)); assertEquals(property.getAttributes().getLength(), 2); assertEquals(entity.getChildNodes().getLength(), 1); }
public void testInferInheritanceHierarchy() { String xml = "<?xml version=\"1.0\"?>"; xml += "<inspection-result xmlns=\"http://www.metawidget.org/inspection-result\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.metawidget.org/inspection-result ../../inspector/inspection-result-1.0.xsd\" version=\"1.0\">"; xml += "<entity type=\"" + RestrictAgainstObjectFoo.class.getName() + "\">"; xml += "<property name=\"xmlBar\" type=\"int\"/>"; xml += "</entity>"; xml += "<entity type=\"org.metawidget.inspector.xml.XmlInspectorTest$SubSubRestrictAgainstObjectFoo\" extends=\"org.metawidget.inspector.xml.XmlInspectorTest$SubRestrictAgainstObjectFoo\">"; xml += "<property name=\"xmlSubSubBar\" type=\"boolean\"/>"; xml += "</entity>"; xml += "</inspection-result>"; // Without inferInheritanceHierarchy mInspector = new XmlInspector( new XmlInspectorConfig().setInputStream(new ByteArrayInputStream(xml.getBytes()))); assertEquals(null, mInspector.inspect(null, SubRestrictAgainstObjectFoo.class.getName())); Document document = XmlUtils.documentFromString( mInspector.inspect( null, "org.metawidget.inspector.xml.XmlInspectorTest$SubSubRestrictAgainstObjectFoo")); Element entity = (Element) document.getDocumentElement().getFirstChild(); assertEquals(ENTITY, entity.getNodeName()); assertEquals( "org.metawidget.inspector.xml.XmlInspectorTest$SubSubRestrictAgainstObjectFoo", entity.getAttribute(TYPE)); assertFalse(entity.hasAttribute(NAME)); // Properties Element property = (Element) entity.getFirstChild(); assertEquals(PROPERTY, property.getNodeName()); assertEquals("xmlSubSubBar", property.getAttribute(NAME)); assertEquals("boolean", property.getAttribute(TYPE)); assertEquals(property.getAttributes().getLength(), 2); assertEquals(entity.getChildNodes().getLength(), 1); // With inferInheritanceHierarchy // Against a missing top-level entity mInspector = new XmlInspector( new XmlInspectorConfig() .setInferInheritanceHierarchy(true) .setInputStream(new ByteArrayInputStream(xml.getBytes()))); document = XmlUtils.documentFromString( mInspector.inspect(null, SubRestrictAgainstObjectFoo.class.getName())); // Entity entity = (Element) document.getDocumentElement().getFirstChild(); assertEquals(ENTITY, entity.getNodeName()); // (should be the top-level type, so as to align the XML with other Inspectors) assertEquals(SubRestrictAgainstObjectFoo.class.getName(), entity.getAttribute(TYPE)); assertFalse(entity.hasAttribute(NAME)); // Properties property = (Element) entity.getFirstChild(); assertEquals(PROPERTY, property.getNodeName()); assertEquals("xmlBar", property.getAttribute(NAME)); assertEquals("int", property.getAttribute(TYPE)); assertEquals(property.getAttributes().getLength(), 2); assertEquals(entity.getChildNodes().getLength(), 1); // Against a missing middle-level entity document = XmlUtils.documentFromString( mInspector.inspect( null, "org.metawidget.inspector.xml.XmlInspectorTest$SubSubRestrictAgainstObjectFoo")); // Entity entity = (Element) document.getDocumentElement().getFirstChild(); assertEquals(ENTITY, entity.getNodeName()); // (should be the top-level type, so as to align the XML with other Inspectors) assertEquals( "org.metawidget.inspector.xml.XmlInspectorTest$SubSubRestrictAgainstObjectFoo", entity.getAttribute(TYPE)); assertFalse(entity.hasAttribute(NAME)); // Properties property = (Element) entity.getFirstChild(); assertEquals(PROPERTY, property.getNodeName()); assertEquals("xmlBar", property.getAttribute(NAME)); assertEquals("int", property.getAttribute(TYPE)); assertEquals(property.getAttributes().getLength(), 2); property = (Element) property.getNextSibling(); assertEquals(PROPERTY, property.getNodeName()); assertEquals("xmlSubSubBar", property.getAttribute(NAME)); assertEquals("boolean", property.getAttribute(TYPE)); assertEquals(property.getAttributes().getLength(), 2); assertEquals(entity.getChildNodes().getLength(), 2); // Against a fake entity assertEquals(null, XmlUtils.documentFromString(mInspector.inspect(null, "Fake Entity"))); }