jdomwritertest.java
来自「JiBX是一个为Java提供的XML数据绑定框架。它可以和现存的类一起运行」· Java 代码 · 共 400 行 · 第 1/2 页
JAVA
400 行
} /** * Test invalid constructor usages. */ public void testInvalidConstructorUsage() throws Exception { JDOMWriter jdomWriter = null; // invalid: empty and xml namespace have to be defined try { jdomWriter = new JDOMWriter(new String[0]); fail("expected ArrayIndexOutOfBoundsException"); } catch(ArrayIndexOutOfBoundsException e) {} try { jdomWriter = new JDOMWriter(new String[1]); fail("expected ArrayIndexOutOfBoundsException"); } catch(ArrayIndexOutOfBoundsException e) {} // invalid: Document is null try { jdomWriter = new JDOMWriter(STD_NAMESPACES, (Document) null); fail("expected NullPointerException"); } catch(NullPointerException e) {} // invalid: Element is null try { jdomWriter = new JDOMWriter(STD_NAMESPACES, (Element) null); fail("expected NullPointerException"); } catch(NullPointerException e) {} // invalid: Element has no parent Document try { jdomWriter = new JDOMWriter(STD_NAMESPACES, new Element("element")); fail("expected NullPointerException"); } catch(NullPointerException e) {} assertNull(jdomWriter); } /** * Test adding and removing extension namespaces. */ public void testExtensionNamespaces() throws Exception { final JDOMWriter jdomWriter = new JDOMWriter(STD_NAMESPACES); String[][] ns; ns = jdomWriter.getExtensionNamespaces(); assertNull(ns); jdomWriter.pushExtensionNamespaces(new String[] { "urn:myNs1", "urn:myNs2" }); ns = jdomWriter.getExtensionNamespaces(); assertEquals(1, ns.length); assertEquals("urn:myNs1", ns[0][0]); assertEquals("urn:myNs2", ns[0][1]); jdomWriter.pushExtensionNamespaces(new String[] { "urn:myNs3" }); ns = jdomWriter.getExtensionNamespaces(); assertEquals(2, ns.length); assertEquals(2, ns[0].length); assertEquals(1, ns[1].length); assertEquals("urn:myNs1", ns[0][0]); assertEquals("urn:myNs2", ns[0][1]); assertEquals("urn:myNs3", ns[1][0]); jdomWriter.popExtensionNamespaces(); ns = jdomWriter.getExtensionNamespaces(); assertEquals(1, ns.length); assertEquals(2, ns[0].length); assertEquals("urn:myNs1", ns[0][0]); assertEquals("urn:myNs2", ns[0][1]); jdomWriter.popExtensionNamespaces(); ns = jdomWriter.getExtensionNamespaces(); assertNull(ns); } /** * Test changing namespace prefixes. */ public void testNamespacePrefixes() throws Exception { JDOMWriter jdomWriter = new JDOMWriter(new String[] { EMPTY_NAMESPACE, XML_NAMESPACE, "urn:myNs1", "urn:myNs2" }); assertEquals("", jdomWriter.getNamespaceUri(0)); assertEquals("http://www.w3.org/XML/1998/namespace", jdomWriter.getNamespaceUri(1)); assertEquals("urn:myNs1", jdomWriter.getNamespaceUri(2)); assertEquals("urn:myNs2", jdomWriter.getNamespaceUri(3)); assertEquals("", jdomWriter.getNamespacePrefix(0)); assertEquals("xml", jdomWriter.getNamespacePrefix(1)); assertNull("unexpected prefix defined", jdomWriter.getNamespacePrefix(2)); assertNull("unexpected prefix defined", jdomWriter.getNamespacePrefix(3)); jdomWriter.startTagNamespaces(0, "element1", new int[] {2, 3}, new String[] {"myns1", "myns2"}); jdomWriter.closeStartTag(); assertEquals("myns1", jdomWriter.getNamespacePrefix(2)); assertEquals("myns2", jdomWriter.getNamespacePrefix(3)); jdomWriter.startTagNamespaces(0, "element2", new int[] {2}, new String[] {"prefix"}); assertEquals("prefix", jdomWriter.getNamespacePrefix(2)); assertEquals("myns2", jdomWriter.getNamespacePrefix(3)); jdomWriter.closeEmptyTag(); assertEquals("myns1", jdomWriter.getNamespacePrefix(2)); assertEquals("myns2", jdomWriter.getNamespacePrefix(3)); jdomWriter.endTag(0, "element1"); assertNull("unexpected prefix defined", jdomWriter.getNamespacePrefix(2)); assertNull("unexpected prefix defined", jdomWriter.getNamespacePrefix(3)); } /** * Helper method to read a file to a <code>Document</code>. * * @param filename the name of a XML file to read * @return a Document based on the given file */ private static Document readExpectedOutput(String filename) throws IOException, JDOMException { final SAXBuilder builder = new SAXBuilder(); final InputStream input = new FileInputStream(filename); return builder.build(input); } /** * Helper method to format a <code>Document</code> to String. * * @param document the Document to format * @return a String containing XML */ private static String formatDocument(Document document) throws IOException { final XMLOutputter outputter = new XMLOutputter(); final StringWriter writer = new StringWriter(); outputter.output(document, writer); return writer.getBuffer().toString(); } /** * Helper method to marshal an Object to a Document. * * @param root the Object to marshal * @return the Document representation */ private static Document marshalActualDocument(Object root) throws JiBXException { final IBindingFactory bfact = BindingDirectory.getFactory(root.getClass()); final IMarshallingContext mctx = bfact.createMarshallingContext(); final JDOMWriter jdomWriter = new JDOMWriter(bfact.getNamespaces()); return marshalDocument(root, mctx, jdomWriter); } private static Document marshalActualDocument(Object root, Document document) throws JiBXException { final IBindingFactory bfact = BindingDirectory.getFactory(root.getClass()); final IMarshallingContext mctx = bfact.createMarshallingContext(); final JDOMWriter jdomWriter = new JDOMWriter(bfact.getNamespaces(), document); return marshalDocument(root, mctx, jdomWriter); } private static Document marshalActualDocument(Object root, Element element) throws JiBXException { final IBindingFactory bfact = BindingDirectory.getFactory(root.getClass()); final IMarshallingContext mctx = bfact.createMarshallingContext(); final JDOMWriter jdomWriter = new JDOMWriter(bfact.getNamespaces(), element); return marshalDocument(root, mctx, jdomWriter); } private static Document marshalDocument(Object root, IMarshallingContext mctx, JDOMWriter jdomWriter) throws JiBXException { mctx.setXmlWriter(jdomWriter); mctx.marshalDocument(root); return jdomWriter.getDocument(); } /** * Helper method to save the actual output to temp.xml * * @param actualOutput the output generated by marshalling */ private static void saveActualOutput(String actualOutput) throws IOException { File fout = new File("temp.xml"); fout.delete(); FileOutputStream fos = new FileOutputStream(fout); fos.write(actualOutput.getBytes()); fos.close(); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?