From 998099d11f6b6d5763a11101be8f2a866e068192 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Fri, 25 Nov 2005 08:42:22 +0000 Subject: [PATCH] fix extraneous whitespace, should fix svn-antlib tests git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@348913 13f79535-47bb-0310-9956-ffa450edef68 --- .../tools/ant/util/DOMElementWriter.java | 4 +- .../tools/ant/util/DOMElementWriterTest.java | 53 ++++++++++++++++++- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/main/org/apache/tools/ant/util/DOMElementWriter.java b/src/main/org/apache/tools/ant/util/DOMElementWriter.java index 4795e7523..69b5798b5 100644 --- a/src/main/org/apache/tools/ant/util/DOMElementWriter.java +++ b/src/main/org/apache/tools/ant/util/DOMElementWriter.java @@ -103,6 +103,7 @@ public class DOMElementWriter { // Write child elements and text NodeList children = element.getChildNodes(); boolean hasChildren = (children.getLength() > 0); + boolean hasChildElements = false; openElement(element, out, indent, indentWith, hasChildren); if (hasChildren) { @@ -112,6 +113,7 @@ public class DOMElementWriter { switch (child.getNodeType()) { case Node.ELEMENT_NODE: + hasChildElements = true; if (i == 0) { out.write(lSep); } @@ -154,7 +156,7 @@ public class DOMElementWriter { // Do nothing } } - closeElement(element, out, indent, indentWith, true); + closeElement(element, out, indent, indentWith, hasChildElements); } } diff --git a/src/testcases/org/apache/tools/ant/util/DOMElementWriterTest.java b/src/testcases/org/apache/tools/ant/util/DOMElementWriterTest.java index 72039347e..d5241bd25 100644 --- a/src/testcases/org/apache/tools/ant/util/DOMElementWriterTest.java +++ b/src/testcases/org/apache/tools/ant/util/DOMElementWriterTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2004 The Apache Software Foundation + * Copyright 2000-2005 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,9 +17,13 @@ package org.apache.tools.ant.util; +import java.io.IOException; +import java.io.StringWriter; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; +import org.w3c.dom.Document; +import org.w3c.dom.Element; /** * Tests for org.apache.tools.ant.util.DOMElementWriter. @@ -92,4 +96,51 @@ public class DOMElementWriterTest extends TestCase { assertEquals("A]]>B]]>C", w.encodedata("A]]>B]]>C")); } + + public void testNoAdditionalWhiteSpaceForText() throws IOException { + Document d = DOMUtils.newDocument(); + Element root = d.createElement("root"); + DOMUtils.appendTextElement(root, "textElement", "content"); + + StringWriter sw = new StringWriter(); + DOMElementWriter w = new DOMElementWriter(); + w.write(root, sw, 0, " "); + assertEquals("" + StringUtils.LINE_SEP + + " content" + + StringUtils.LINE_SEP + + "" + StringUtils.LINE_SEP, + sw.toString()); + } + + public void testNoAdditionalWhiteSpaceForCDATA() throws IOException { + Document d = DOMUtils.newDocument(); + Element root = d.createElement("root"); + DOMUtils.appendCDATAElement(root, "cdataElement", "content"); + + StringWriter sw = new StringWriter(); + DOMElementWriter w = new DOMElementWriter(); + w.write(root, sw, 0, " "); + assertEquals("" + StringUtils.LINE_SEP + + " " + + StringUtils.LINE_SEP + + "" + StringUtils.LINE_SEP, + sw.toString()); + } + + public void testNoAdditionalWhiteSpaceForEmptyElement() throws IOException { + Document d = DOMUtils.newDocument(); + Element root = d.createElement("root"); + DOMUtils.createChildElement(root, "emptyElement"); + + StringWriter sw = new StringWriter(); + DOMElementWriter w = new DOMElementWriter(); + w.write(root, sw, 0, " "); + assertEquals("" + StringUtils.LINE_SEP + // + " " + + " " + + StringUtils.LINE_SEP + + "" + StringUtils.LINE_SEP, + sw.toString()); + } + }