All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jdom2.test.cases.output.AbstractTestOutputter Maven / Gradle / Ivy

Go to download

A complete, Java-based solution for accessing, manipulating, and outputting XML data

The newest version!
package org.jdom2.test.cases.output;

import static org.junit.Assert.assertEquals;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import org.jdom2.Attribute;
import org.jdom2.CDATA;
import org.jdom2.Comment;
import org.jdom2.Content;
import org.jdom2.DocType;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.EntityRef;
import org.jdom2.Namespace;
import org.jdom2.Parent;
import org.jdom2.ProcessingInstruction;
import org.jdom2.Text;
import org.jdom2.output.Format;
import org.jdom2.output.Format.TextMode;

@SuppressWarnings("javadoc")
public abstract class AbstractTestOutputter {
	
	protected static interface FormatSetup {
		public void setup(Format fmt);
	}
	
	
	private final boolean cr2xD;
	private final boolean pademptyelement;
	private final boolean forceexpand;
	private final boolean padpi;
	private final boolean usesrawxmlout;

	public AbstractTestOutputter(boolean cr2xD, boolean padpreempty, boolean padpi,
			boolean forceexpand, boolean usesrawxmlout) {
		this.cr2xD = cr2xD;
		this.pademptyelement = padpreempty;
		this.forceexpand = forceexpand;
		this.padpi = padpi;
		this.usesrawxmlout = usesrawxmlout;
	}
	
	protected final String expect(String expect) {
		if (cr2xD) {
			expect = expect.replaceAll("\r", "
");
		}
		if (forceexpand) {
			expect = expect.replaceAll("<(\\w+(:\\w+)?)\\s*/>", "<$1>");
			expect = expect.replaceAll("<(\\w+(:\\w+)?)\\s+(.+?)\"\\s*/>", "<$1 $3\">");
		}
		if (padpi) {
			expect = expect.replaceAll("(<\\?\\w+)(\\?>)", "$1 $2");
		}
		if (pademptyelement) {
			//expect = expect.replaceAll("\">", "\" >");
			//expect = expect.replaceAll("<(\\w+)>", "<$1 >");
			expect = expect.replaceAll("<(\\w+(:\\w+)?)/>", "<$1 />");
			expect = expect.replaceAll("<(\\w+(:\\w+)?\\s.+\")/>", "<$1 />");
		} else {
			//expect = expect.replaceAll("<(\\w+)\\s+>", "<$1>");
			expect = expect.replaceAll("<(\\w+)(\\s+.+?)?\\s+/>", "<$1$2/>");
			expect = expect.replaceAll("<(\\w+:\\w+)(\\s+.+?)?\\s+/>", "<$1$2/>");
		}
//		if (rawoutsideroot) {
//			// outside the root element will be raw-formatted.
//			StringBuilder sb = new StringBuilder(expect.length());
//			int gotstuff = 0;
//			boolean indoctype = false;
//			boolean gotroot = false;
//			int depth = 0;
//			char[] chars = expect.toCharArray();
//			int i = 0;
//			while (i < chars.length && Verifier.isXMLWhitespace(chars[i])) {
//				// skip initial whitespace.
//				i++;
//			}
//			for (; i < chars.length; i++) {
//				char c = chars[i];
//				sb.append(c);
//				if (!gotroot) {
//					if (c == '<') {
//						if (depth == 0) {
//							if (i < chars.length - 2) {
//								if (chars[i + 1] == '?') {
//									// PI or XML Declaration
//									gotstuff++;
//								} else if (chars[i + 1] == '!') {
//									// Comment of DOCTYPE
//									gotstuff++;
//									if (chars[i + 2] == 'D') {
//										// DOCTYPE
//										indoctype = true;
//									}
//								} else {
//									// root element
//									gotroot = true;
//								}
//							} else {
//								gotroot = true;
//							}
//						}
//						depth++;
//					} else if (c == '>') {
//						depth--;
//						if (depth == 0) {
//							if (indoctype) {
//								sb.append('\n');
//								indoctype = false;
//							}
//							while (i+1 < chars.length && Verifier.isXMLWhitespace(chars[i + 1])) {
//								// skip whitespace after top-level content.
//								i++;
//							}
//						}
//					}
//				}
//			}
//			while (Verifier.isXMLWhitespace(sb.charAt(sb.length() - 1))) {
//				// eliminate trailing whitespace.
//				sb.setLength(sb.length() - 1);
//			}
//			if (gotstuff > 1 || (gotroot && gotstuff > 0)) {
//				// there is multiple content stuff, need to trim the whitespace....
//				expect = sb.toString();
//			}
//		}
		return expect;
	}

	/**
	 * Return a string representing a {@link Document}. Uses an internal
	 * StringWriter.
	 * 

* Warning: a String is Unicode, which may not match the outputter's * specified encoding. * * @param doc * Document to format. * @return the input content formatted as an XML String. * @throws NullPointerException * if the specified content is null. */ public abstract String outputString(Format format, Document doc); /** * Return a string representing a {@link DocType}. *

* Warning: a String is Unicode, which may not match the outputter's * specified encoding. * * @param doctype * DocType to format. * @return the input content formatted as an XML String. * @throws NullPointerException * if the specified content is null. */ public abstract String outputString(Format format, DocType doctype); /** * Return a string representing an {@link Element}. *

* Warning: a String is Unicode, which may not match the outputter's * specified encoding. * * @param element * Element to format. * @return the input content formatted as an XML String. * @throws NullPointerException * if the specified content is null. */ public abstract String outputString(Format format, Element element); /** * Return a string representing a List of {@link Content} nodes.
* The list is assumed to contain legal JDOM nodes. If other content is * coerced on to the list it will cause ClassCastExceptions, and null List * members will cause NullPointerException. *

* Warning: a String is Unicode, which may not match the outputter's * specified encoding. * * @param list * List to format. * @return the input content formatted as an XML String. * @throws ClassCastException * if non-{@link Content} is forced in to the list * @throws NullPointerException * if the List is null or contains null members. */ public abstract String outputString(Format format, List list); /** * Return a string representing a {@link CDATA} node. *

* Warning: a String is Unicode, which may not match the outputter's * specified encoding. * * @param cdata * CDATA to format. * @return the input content formatted as an XML String. * @throws NullPointerException * if the specified content is null. */ public abstract String outputString(Format format, CDATA cdata); /** * Return a string representing a {@link Text} node. *

* Warning: a String is Unicode, which may not match the outputter's * specified encoding. * * @param text * Text to format. * @return the input content formatted as an XML String. * @throws NullPointerException * if the specified content is null. */ public abstract String outputString(Format format, Text text); /** * Return a string representing a {@link Comment}. *

* Warning: a String is Unicode, which may not match the outputter's * specified encoding. * * @param comment * Comment to format. * @return the input content formatted as an XML String. * @throws NullPointerException * if the specified content is null. */ public abstract String outputString(Format format, Comment comment); /** * Return a string representing a {@link ProcessingInstruction}. *

* Warning: a String is Unicode, which may not match the outputter's * specified encoding. * * @param pi * ProcessingInstruction to format. * @return the input content formatted as an XML String. * @throws NullPointerException * if the specified content is null. */ public abstract String outputString(Format format, ProcessingInstruction pi); /** * Return a string representing an {@link EntityRef}. *

* Warning: a String is Unicode, which may not match the outputter's * specified encoding. * * @param entity * EntityRef to format. * @return the input content formatted as an XML String. * @throws NullPointerException * if the specified content is null. */ public abstract String outputString(Format format, EntityRef entity); /** * This will handle printing out an {@link * Element}'s content only, not including its tag, and attributes. * This can be useful for printing the content of an element that contains * HTML, like "<description>JDOM is * <b>fun>!</description>". *

* Warning: a String is Unicode, which may not match the outputter's * specified encoding. * * @param element * Element to output. * @return the input content formatted as an XML String. * @throws NullPointerException * if the specified content is null. */ public abstract String outputElementContentString(Format format, Element element); protected static final Format fraw = Format.getRawFormat(); protected static final Format fcompact = Format.getCompactFormat(); protected static final Format fpretty = Format.getPrettyFormat(); protected static final Format ftso = Format.getPrettyFormat(); protected static final Format ftfw = Format.getPrettyFormat(); static { fraw.setLineSeparator("\n"); fcompact.setLineSeparator("\n"); fpretty.setLineSeparator("\n"); ftso.setLineSeparator("\n"); ftso.setSpecifiedAttributesOnly(true); ftfw.setLineSeparator("\n"); ftfw.setTextMode(TextMode.TRIM_FULL_WHITE); } @Test public void testTextEmpty() { Text content = new Text(""); assertEquals("", outputString(fraw, content)); assertEquals("", outputString(fcompact, content)); assertEquals("", outputString(fpretty, content)); assertEquals("", outputString(ftso, content)); assertEquals("", outputString(ftfw, content)); } @Test public void testTextWhitespace() { Text content = new Text(" \r \n \t "); assertEquals(expect(" \r \n \t "), outputString(fraw, content)); assertEquals("", outputString(fcompact, content)); assertEquals("", outputString(fpretty, content)); assertEquals("", outputString(ftso, content)); assertEquals("", outputString(ftfw, content)); } @Test public void testTextWithText() { Text content = new Text(" \r & \n \t "); assertEquals(expect(" \r & \n \t "), outputString(fraw, content)); assertEquals(expect("&"), outputString(fcompact, content)); assertEquals(expect("&"), outputString(fpretty, content)); assertEquals(expect("&"), outputString(ftso, content)); assertEquals(expect(" \r & \n \t "), outputString(ftfw, content)); } @Test public void testCDATAEmpty() { CDATA content = new CDATA(""); assertEquals("", outputString(fraw, content)); assertEquals("", outputString(fcompact, content)); assertEquals("", outputString(fpretty, content)); assertEquals("", outputString(ftso, content)); assertEquals("", outputString(ftfw, content)); } @Test public void testCDATAWhitespace() { CDATA content = new CDATA(" \r \n \t "); assertEquals("", outputString(fraw, content)); assertEquals("", outputString(fcompact, content)); assertEquals("", outputString(fpretty, content)); assertEquals("", outputString(ftso, content)); assertEquals("", outputString(ftfw, content)); } @Test public void testCDATAWithText() { CDATA content = new CDATA(" \r & \n \t "); assertEquals("", outputString(fraw, content)); assertEquals("", outputString(fcompact, content)); assertEquals("", outputString(fpretty, content)); assertEquals("", outputString(ftso, content)); assertEquals("", outputString(ftfw, content)); } @Test public void testEntityRef() { EntityRef content = new EntityRef("ref"); assertEquals("&ref;", outputString(fraw, content)); assertEquals("&ref;", outputString(fcompact, content)); assertEquals("&ref;", outputString(fpretty, content)); assertEquals("&ref;", outputString(ftso, content)); assertEquals("&ref;", outputString(ftfw, content)); } @Test public void testProcessingInstructionTargetOnly() { ProcessingInstruction content = new ProcessingInstruction("target"); assertEquals(expect(""), outputString(fraw, content)); assertEquals(expect(""), outputString(fcompact, content)); assertEquals(expect(""), outputString(fpretty, content)); assertEquals(expect(""), outputString(ftfw, content)); } @Test public void testProcessingInstructionTargetWithData() { ProcessingInstruction content = new ProcessingInstruction("target", "data"); assertEquals("", outputString(fraw, content)); assertEquals("", outputString(fcompact, content)); assertEquals("", outputString(fpretty, content)); assertEquals("", outputString(ftso, content)); assertEquals("", outputString(ftfw, content)); } @Test public void testComment() { Comment content = new Comment("comment"); assertEquals("", outputString(fraw, content)); assertEquals("", outputString(fcompact, content)); assertEquals("", outputString(fpretty, content)); assertEquals("", outputString(ftso, content)); assertEquals("", outputString(ftfw, content)); } @Test public void testDocTypeSimple() { DocType content = new DocType("root"); assertEquals("", outputString(fraw, content)); assertEquals("", outputString(fcompact, content)); assertEquals("", outputString(fpretty, content)); assertEquals("", outputString(ftso, content)); assertEquals("", outputString(ftfw, content)); } @Test public void testDocTypeSimpleISS() { DocType content = new DocType("root"); content.setInternalSubset(""); assertEquals("]>", outputString(fraw, content)); assertEquals("]>", outputString(fcompact, content)); assertEquals("]>", outputString(fpretty, content)); assertEquals("]>", outputString(ftso, content)); assertEquals("]>", outputString(ftfw, content)); } @Test public void testDocTypeSystemID() { DocType content = new DocType("root", "sysid"); assertEquals("", outputString(fraw, content)); assertEquals("", outputString(fcompact, content)); assertEquals("", outputString(fpretty, content)); assertEquals("", outputString(ftso, content)); assertEquals("", outputString(ftfw, content)); } @Test public void testDocTypeSystemIDISS() { DocType content = new DocType("root", "sysid"); content.setInternalSubset("internal"); assertEquals("", outputString(fraw, content)); assertEquals("", outputString(fcompact, content)); assertEquals("", outputString(fpretty, content)); assertEquals("", outputString(ftso, content)); assertEquals("", outputString(ftfw, content)); } @Test public void testDocTypePublicSystemID() { DocType content = new DocType("root", "pubid", "sysid"); assertEquals("", outputString(fraw, content)); assertEquals("", outputString(fcompact, content)); assertEquals("", outputString(fpretty, content)); assertEquals("", outputString(ftso, content)); assertEquals("", outputString(ftfw, content)); } @Test public void testDocTypePublicSystemIDISS() { DocType content = new DocType("root", "pubid", "sysid"); content.setInternalSubset("internal"); assertEquals("", outputString(fraw, content)); assertEquals("", outputString(fcompact, content)); assertEquals("", outputString(fpretty, content)); assertEquals("", outputString(ftso, content)); assertEquals("", outputString(ftfw, content)); } @Test public void testMultiWhiteText() { Element root = new Element("root"); root.addContent(new CDATA(" ")); root.addContent(new Text(" ")); root.addContent(new Text(" ")); root.addContent(new Text("")); root.addContent(new Text(" ")); root.addContent(new Text(" \n \n ")); root.addContent(new Text(" \t ")); root.addContent(new Text(" ")); assertEquals(expect(" \n \n \t "), outputString(fraw, root)); assertEquals(expect(""), outputString(fcompact, root)); assertEquals(expect(""), outputString(fpretty, root)); assertEquals(expect(""), outputString(ftso, root)); assertEquals(expect(""), outputString(ftfw, root)); } @Test public void testMultiText() { Element root = new Element("root"); root.addContent(new CDATA(" ")); root.addContent(new Text(" ")); root.addContent(new Text(" ")); root.addContent(new Text("")); root.addContent(new Text("X")); root.addContent(new Text(" \n \n ")); root.addContent(new Text(" \t ")); root.addContent(new Text(" ")); assertEquals(expect(" X \n \n \t "), outputString(fraw, root)); assertEquals(expect("X"), outputString(fcompact, root)); assertEquals(expect("X"), outputString(fpretty, root)); assertEquals(expect("X"), outputString(ftso, root)); assertEquals(expect(" X \n \n \t "), outputString(ftfw, root)); } @Test public void testDocumentSimple() { Document content = new Document(); assertEquals("\n", outputString(fraw, content)); assertEquals("\n", outputString(fcompact, content)); assertEquals("\n", outputString(fpretty, content)); assertEquals("\n", outputString(ftso, content)); assertEquals("\n", outputString(ftfw, content)); } @Test public void testDocumentDocType() { Document content = new Document(); content.setDocType(new DocType("root")); assertEquals("\n\n", outputString(fraw, content)); assertEquals("\n\n", outputString(fcompact, content)); assertEquals("\n\n", outputString(fpretty, content)); assertEquals("\n\n", outputString(ftso, content)); assertEquals("\n\n", outputString(ftfw, content)); } @Test public void testDocumentComment() { Document content = new Document(); content.addContent(new Comment("comment")); assertEquals("\n\n", outputString(fraw, content)); assertEquals("\n\n", outputString(fcompact, content)); assertEquals("\n\n", outputString(fpretty, content)); assertEquals("\n\n", outputString(ftso, content)); assertEquals("\n\n", outputString(ftfw, content)); } @Test public void testXXX() { Text content = new Text(""); assertEquals("", outputString(fraw, content)); assertEquals("", outputString(fcompact, content)); assertEquals("", outputString(fpretty, content)); assertEquals("", outputString(ftfw, content)); } @Test public void testOutputText() { checkOutput(new Text(" hello there "), " hello there ", "hello there", "hello there", "hello there", " hello there "); } @Test public void testOutputCDATA() { String indata = " hello there bozo ! "; String rawcdata = ""; String compdata = ""; String prettydata = ""; String trimdata = ""; checkOutput(new CDATA(indata), rawcdata, compdata, prettydata, prettydata, trimdata); } @Test public void testOutputComment() { String incomment = " hello there bozo ! "; String outcomment = ""; checkOutput(new Comment(incomment), outcomment, outcomment, outcomment, outcomment, outcomment); } @Test public void testOutputProcessingInstructionSimple() { ProcessingInstruction inpi = new ProcessingInstruction("jdomtest", ""); String outpi = ""; checkOutput(inpi, outpi, outpi, outpi, outpi, outpi); } @Test public void testOutputProcessingInstructionData() { String pi = " hello there "; ProcessingInstruction inpi = new ProcessingInstruction("jdomtest", pi); String outpi = ""; checkOutput(inpi, outpi, outpi, outpi, outpi, outpi); } @Test public void testOutputEntityRef() { checkOutput(new EntityRef("name", "publicID", "systemID"), "&name;", "&name;", "&name;", "&name;", "&name;"); } @Test public void testOutputElementSimple() { String txt = ""; checkOutput(new Element("root"), txt, txt, txt, txt, txt); } @Test public void testOutputElementAttribute() { String txt = ""; checkOutput(new Element("root").setAttribute("att", "val"), txt, txt, txt, txt, txt); } @Test public void testOutputElementAttributeNotSpecifiedA() { String txt = ""; final Element root = new Element("root"); final Attribute att = new Attribute("att", "val"); root.setAttribute(att); att.setSpecified(false); checkOutput(root, txt, txt, txt, "", txt); } @Test public void testOutputElementAttributeNotSpecifiedB() { String txt = ""; final Element root = new Element("root"); final Attribute atta = new Attribute("atta", "val"); final Attribute attb = new Attribute("attb", "attb"); root.setAttribute(atta); root.setAttribute(attb); atta.setSpecified(false); checkOutput(root, txt, txt, txt, "", txt); } @Test public void testOutputElementCDATA() { String txt = ""; Element root = new Element("root"); root.addContent(new CDATA("xx")); checkOutput(root, txt, txt, txt, txt, txt); } @Test public void testOutputElementExpandEmpty() { String txt = ""; FormatSetup setup = new FormatSetup() { @Override public void setup(Format fmt) { fmt.setExpandEmptyElements(true); } }; checkOutput(new Element("root"), setup, txt, txt, txt, txt, txt); } @Test public void testOutputElementPreserveSpace() { String txt = " abc "; Element root = new Element("root"); root.setAttribute("space", "preserve", Namespace.XML_NAMESPACE); root.addContent(" "); Element child = new Element("child"); child.setAttribute("space", "default", Namespace.XML_NAMESPACE); child.addContent("abc"); root.addContent(child); root.addContent(" "); checkOutput(root, txt, txt, txt, txt, txt); } @Test public void testOutputElementPreserveSpaceComplex() { // the purpose of this test is to ensure that the different // formatting values are used when going down one level, // back up, then in to 'preserve', back up, and then again // down in to normal (not preserve). // this is essentially a test of the FormatStack code.... Element tst = new Element("child"); Comment cmt = new Comment("comment"); tst.addContent(cmt); String spaced = " \n \n \n"; String compact = ""; String preserved = ""; Element root = new Element("root"); root.addContent(tst.clone()); root.addContent(tst.clone().setAttribute("space", "preserve", Namespace.XML_NAMESPACE)); root.addContent(tst.clone()); String rawcompact = "" + compact + preserved + compact + ""; String pretty = "\n" + spaced + " " + preserved + "\n" + spaced + ""; checkOutput(root, rawcompact, rawcompact, pretty, pretty, pretty); } @Test public void testOutputElementMultiText() { Element root = new Element("root"); root.addContent(new CDATA(" ")); root.addContent(new Text(" xx ")); root.addContent(new Text("yy")); root.addContent(new Text(" ")); root.addContent(new Text("zz")); root.addContent(new Text(" ww")); root.addContent(new EntityRef("amp")); root.addContent(new Text("vv")); root.addContent(new Text(" ")); checkOutput(root, " xx yy zz ww&vv ", "xx yy zz ww&vv", "xx yy zz ww&vv", "xx yy zz ww&vv", // This should be changed with issue #31. // The real value should have one additional // space at the beginning and two at the end // for now we leave the broken test here because it // helps with the coverage reports. // the next test is added to be a failing test. " xx yy zz ww&vv "); } @Test public void testOutputElementMultiAllWhite() { Element root = new Element("root"); root.addContent(new CDATA(" ")); root.addContent(new Text(" ")); root.addContent(new Text(" ")); root.addContent(new Text("")); root.addContent(new Text(" ")); root.addContent(new Text(" \n \n ")); root.addContent(new Text(" \t ")); root.addContent(new Text(" ")); checkOutput(root, " \n \n \t ", "", "", "", ""); } @Test public void testOutputElementMultiAllWhiteExpandEmpty() { Element root = new Element("root"); root.addContent(new CDATA(" ")); root.addContent(new Text(" ")); root.addContent(new Text(" ")); root.addContent(new Text("")); root.addContent(new Text(" ")); root.addContent(new Text(" \n \n ")); root.addContent(new Text(" \t ")); root.addContent(new Text(" ")); FormatSetup fs = new FormatSetup() { @Override public void setup(Format fmt) { fmt.setExpandEmptyElements(true); } }; checkOutput(root, fs, " \n \n \t ", "", "", "", ""); } @Test public void testOutputElementMultiMostWhiteExpandEmpty() { // this test has mixed content (text-type and not text type). // and, it has a multi-text-type at the end. Element root = new Element("root"); root.addContent(new CDATA(" ")); root.addContent(new Text(" ")); root.addContent(new Text(" ")); root.addContent(new Text("")); root.addContent(new Text(" ")); root.addContent(new Text(" \n \n ")); root.addContent(new Comment("Boo")); root.addContent(new Text(" \t ")); root.addContent(new Text(" ")); FormatSetup fs = new FormatSetup() { @Override public void setup(Format fmt) { fmt.setExpandEmptyElements(true); } }; checkOutput(root, fs, " \n \n \t ", "", "\n \n", "\n \n", "\n \n"); } @Test public void testOutputElementMixedMultiCDATA() { // this test has mixed content (text-type and not text type). // and, it has a multi-text-type at the end. Element root = new Element("root"); root.addContent(new Comment("Boo")); root.addContent(new Text(" ")); root.addContent(new CDATA("A")); FormatSetup fs = new FormatSetup() { @Override public void setup(Format fmt) { fmt.setExpandEmptyElements(true); } }; checkOutput(root, fs, " ", "", "\n \n \n", "\n \n \n", "\n \n \n"); } @Test public void testOutputElementMixedMultiEntityRef() { // this test has mixed content (text-type and not text type). // and, it has a multi-text-type at the end. Element root = new Element("root"); root.addContent(new Comment("Boo")); root.addContent(new Text(" ")); root.addContent(new EntityRef("aer")); FormatSetup fs = new FormatSetup() { @Override public void setup(Format fmt) { fmt.setExpandEmptyElements(true); } }; checkOutput(root, fs, " &aer;", "&aer;", "\n \n &aer;\n", "\n \n &aer;\n", "\n \n &aer;\n"); } @Test public void testOutputElementMixedMultiText() { // this test has mixed content (text-type and not text type). // and, it has a multi-text-type at the end. Element root = new Element("root"); root.addContent(new Comment("Boo")); root.addContent(new Text(" ")); root.addContent(new Text("txt")); FormatSetup fs = new FormatSetup() { @Override public void setup(Format fmt) { fmt.setExpandEmptyElements(true); } }; checkOutput(root, fs, " txt", "txt", "\n \n txt\n", "\n \n txt\n", "\n \n txt\n"); } @Test public void testOutputElementMixedMultiZeroText() { // this test has mixed content (text-type and not text type). // and, it has a multi-text-type at the end. Element root = new Element("root"); root.addContent(new Comment("Boo")); root.addContent(new Text("")); root.addContent(new Text(" ")); root.addContent(new Text("")); root.addContent(new Text("txt")); root.addContent(new Text("")); FormatSetup fs = new FormatSetup() { @Override public void setup(Format fmt) { fmt.setExpandEmptyElements(true); } }; checkOutput(root, fs, " txt", "txt", "\n \n txt\n", "\n \n txt\n", "\n \n txt\n"); } @Test public void testOutputElementInterleavedEmptyText() { // this is to test issue #72 // Compact format only prints first child. // and, it has a multi-text-type at the end. Element root = new Element("root"); root.addContent(new Text(" ")); root.addContent(new Comment("Boo")); root.addContent(new Text(" ")); root.addContent(new Element("child")); root.addContent(new Text(" ")); root.addContent(new ProcessingInstruction("pitarget")); root.addContent(new Text(" ")); checkOutput(root, " ", "", "\n \n \n \n", "\n \n \n \n", "\n \n \n \n"); } @Test public void testOutputElementMultiEntityLeftRight() { Element root = new Element("root"); root.addContent(new EntityRef("erl")); root.addContent(new Text(" ")); root.addContent(new Text(" ")); root.addContent(new EntityRef("err")); checkOutput(root, "&erl; &err;", "&erl; &err;", "&erl; &err;", "&erl; &err;", "&erl; &err;"); } @Test public void testOutputElementMultiTrimLeftRight() { Element root = new Element("root"); root.addContent(new Text(" tl ")); root.addContent(new Text(" mid ")); root.addContent(new Text(" tr ")); checkOutput(root, " tl mid tr ", "tl mid tr", "tl mid tr", "tl mid tr", " tl mid tr "); } @Test public void testOutputElementMultiCDATALeftRight() { Element root = new Element("root"); root.addContent(new CDATA(" tl ")); root.addContent(new Text(" mid ")); root.addContent(new CDATA(" tr ")); checkOutput(root, " mid ", " mid ", " mid ", " mid ", " mid "); } @Test public void testOutputElementNamespaces() { String txt = ""; Element emt = new Element("root", Namespace.getNamespace("ns", "myns")); Namespace ans = Namespace.getNamespace("ans", "attributens"); emt.setAttribute(new Attribute("att", "val", ans)); emt.addNamespaceDeclaration(Namespace.getNamespace("two", "two")); checkOutput(emt, txt, txt,txt, txt, txt); } @Test public void testOutputDocTypeSimple() { checkOutput(new DocType("root"), "", "", "", "", ""); } @Test public void testOutputDocTypeInternalSubset() { String dec = ""; DocType dt = new DocType("root"); dt.setInternalSubset("internal"); checkOutput(dt, dec, dec, dec, dec, dec); } @Test public void testOutputDocTypeSystem() { String dec = ""; checkOutput(new DocType("root", "systemID"), dec, dec, dec, dec, dec); } @Test public void testOutputDocTypePublic() { String dec = ""; checkOutput(new DocType("root", "publicID", null), dec, dec, dec, dec, dec); } @Test public void testOutputDocTypePublicSystem() { String dec = ""; checkOutput(new DocType("root", "publicID", "systemID"), dec, dec, dec, dec, dec); } @Test public void testOutputDocumentSimple() { Document doc = new Document(); doc.addContent(new Element("root")); String xmldec = ""; String rtdec = ""; checkOutput(doc, xmldec + "\n" + rtdec + "\n", xmldec + "\n" + rtdec + "\n", xmldec + "\n" + rtdec + "\n", xmldec + "\n" + rtdec + "\n", xmldec + "\n" + rtdec + "\n"); } @Test public void testOutputDocumentOmitEncoding() { Document doc = new Document(); doc.addContent(new Element("root")); String xmldec = ""; FormatSetup setup = new FormatSetup() { @Override public void setup(Format fmt) { fmt.setOmitEncoding(true); } }; String rtdec = ""; checkOutput(doc, setup, xmldec + "\n" + rtdec + "\n", xmldec + "\n" + rtdec + "\n", xmldec + "\n" + rtdec + "\n", xmldec + "\n" + rtdec + "\n", xmldec + "\n" + rtdec + "\n"); } @Test public void testOutputDocumentOmitDeclaration() { Document doc = new Document(); doc.addContent(new Element("root")); FormatSetup setup = new FormatSetup() { @Override public void setup(Format fmt) { fmt.setOmitDeclaration(true); } }; String rtdec = ""; checkOutput(doc, setup, rtdec + "\n", rtdec + "\n", rtdec + "\n", rtdec + "\n", rtdec + "\n"); } @Test public void testOutputDocumentFull() { DocType dt = new DocType("root"); Comment comment = new Comment("comment"); ProcessingInstruction pi = new ProcessingInstruction("jdomtest", ""); Element root = new Element("root"); Document doc = new Document(); doc.addContent(dt); doc.addContent(comment); doc.addContent(pi); doc.addContent(root); String xmldec = ""; String dtdec = ""; String commentdec = ""; String pidec = ""; String rtdec = ""; String lf = "\n"; String dlf = usesrawxmlout ? "" : lf; checkOutput(doc, xmldec + lf + dtdec + commentdec + pidec + rtdec + lf, xmldec + lf + dtdec + commentdec + pidec + rtdec + lf, xmldec + lf + dtdec + dlf + commentdec + dlf + pidec + dlf + rtdec + lf, xmldec + lf + dtdec + dlf + commentdec + dlf + pidec + dlf + rtdec + lf, xmldec + lf + dtdec + dlf + commentdec + dlf + pidec + dlf + rtdec + lf); } @Test public void testDeepNesting() { // need to get beyond 16 levels of XML. DocType dt = new DocType("root"); Element root = new Element("root"); Document doc = new Document(); doc.addContent(dt); doc.addContent(root); String xmldec = ""; String dtdec = ""; String lf = "\n"; StringBuilder raw = new StringBuilder(); raw.append(xmldec).append(lf).append(dtdec); StringBuilder pretty = new StringBuilder(); pretty.append(xmldec).append(lf).append(dtdec); if (!usesrawxmlout) { // most test systems use the XMLOutputter in raw mode to output // the results of the conversion. In Raw mode the XMLOutputter will // not make pretty content outside of the root element (but it will // put the XMLDeclaration on it's own line). // so, in the cases where the actual pretty format is used, we add // this newline after the DocType... pretty.append(lf); } raw.append(""); pretty.append(""); pretty.append(lf); final int depth = 40; int cnt = depth; Parent parent = root; StringBuilder indent = new StringBuilder(); while (--cnt > 0) { Element emt = new Element("emt"); parent.getContent().add(emt); parent = emt; raw.append(""); indent.append(" "); pretty.append(indent.toString()); pretty.append(""); pretty.append(lf); } parent.getContent().add(new Element("bottom")); raw.append(""); pretty.append(indent.toString()); pretty.append(" "); pretty.append(lf); cnt = depth; while (--cnt > 0) { raw.append(""); pretty.append(indent.toString()); pretty.append(""); indent.setLength(indent.length() - 2); pretty.append(lf); } raw.append(""); raw.append(lf); pretty.append(""); pretty.append(lf); checkOutput(doc, raw.toString(), raw.toString(), pretty.toString(), pretty.toString(), pretty.toString()); } @Test public void testOutputElementContent() { Element root = new Element("root"); root.addContent(new Element("child")); checkOutput(root, "outputElementContent", Element.class, null, "", "", "", "", ""); } @Test public void testOutputList() { List c = new ArrayList(); c.add(new Element("root")); checkOutput(c, "output", List.class, null, "", "", "", "", ""); } @Test public void testOutputEscapedMixedMultiText() { // this test has mixed content (text-type and not text type). // and, it has a multi-text-type at the end. Element root = new Element("root"); root.addContent(new Comment("Boo")); root.addContent(new Text(" xx ")); root.addContent(new Text("")); root.addContent(new Text(" xx ")); FormatSetup fs = new FormatSetup() { @Override public void setup(Format fmt) { fmt.setExpandEmptyElements(true); } }; checkOutput(root, fs, " xx <emb> xx ", "xx <emb> xx", "\n \n xx <emb> xx\n", "\n \n xx <emb> xx\n", "\n \n xx <emb> xx \n"); } protected void checkOutput(Object content, String raw, String compact, String pretty, String tso, String trimfw) { Class clazz = content.getClass(); checkOutput(content, "output", clazz, null, raw, compact, pretty, tso, trimfw); } protected void checkOutput(Object content, FormatSetup setup, String raw, String compact, String pretty, String tso, String trimfw) { Class clazz = content.getClass(); checkOutput(content, "output", clazz, setup, raw, compact, pretty, tso, trimfw); } /** * The following method will run the output data through each of the three base * formatters, raw, compact, and pretty. It will also run each of those * formatters as the outputString(content), output(content, OutputStream) * and output(content, Writer). * * The expectation is that the results of the three output forms (String, * OutputStream, and Writer) will be identical, and that it will match * the expected value for the appropriate formatter. * * @param content The content to output * @param methodprefix What the methods are called * @param clazz The class used as the parameter for the methods. * @param setup A callback mechanism to modify the formatters * @param raw What we expect the content to look like with the RAW format * @param compact What we expect the content to look like with the COMPACT format * @param pretty What we expect the content to look like with the PRETTY format * @param trimfw What we expect the content to look like with the TRIM_FULL_WHITE format */ protected void checkOutput(Object content, String methodprefix, Class clazz, FormatSetup setup, String raw, String compact, String pretty, String tso, String trimfw) { Method meth = getMyMethod(methodprefix + "String", Format.class, clazz); if (meth == null) { return; } String[] descn = new String[] {"Raw", "Compact", "Pretty", "PrettySpecifiedOnly", "TrimFullWhite"}; Format ftrimfw = Format.getPrettyFormat(); ftrimfw.setTextMode(TextMode.TRIM_FULL_WHITE); Format fattspec = Format.getPrettyFormat(); fattspec.setSpecifiedAttributesOnly(true); Format[] formats = new Format[] { getFormat(setup, Format.getRawFormat()), getFormat(setup, Format.getCompactFormat()), getFormat(setup, Format.getPrettyFormat()), getFormat(setup, fattspec), getFormat(setup, ftrimfw)}; String[] result = new String[] {raw, compact, pretty, tso, trimfw}; for (int i = 0; i < 5; i++) { String mstring; try { mstring = (String) meth.invoke(this, formats[i], content); } catch (Exception e) { e.printStackTrace(); throw new IllegalStateException(e); } String msg = "outputString Format " + descn[i]; assertEquals(msg, expect(result[i]), mstring); } } protected Format getFormat(FormatSetup setup, Format input) { if (setup == null) { input.setLineSeparator("\n"); return input; } input.setLineSeparator("\n"); setup.setup(input); return input; } private Method getMyMethod(String name, Class...classes) { try { return this.getClass().getMethod(name, classes); } catch (Exception e) { // ignore. System.out.println("Can't find " + name + " on " + this.getClass().getName() + ": " + e.getMessage()); } return null; } }