1 9 10 package test.dom4j; 11 12 import junit.framework.Test; 13 import junit.framework.TestSuite; 14 import junit.textui.TestRunner; 15 import org.dom4j.Element; 16 import org.dom4j.Node; 17 import org.dom4j.Text; 18 import org.dom4j.io.SAXReader; 19 20 import java.io.File ; 21 import java.io.FileNotFoundException ; 22 import java.util.Iterator ; 23 24 29 public class TestMergeText extends AbstractTestCase { 30 31 32 protected static String INPUT_XML_FILE = "xml/test/mergeText.xml"; 33 34 public static void main( String [] args ) { 35 TestRunner.run( suite() ); 36 } 37 38 public static Test suite() { 39 return new TestSuite( TestMergeText.class ); 40 } 41 42 public TestMergeText(String name) { 43 super(name); 44 } 45 46 public void testNoAdjacentText() throws Exception { 49 50 54 checkNoAdjacent(document.getRootElement()); 55 log( "No adjacent Text nodes in " + document.asXML() ); 56 } 57 58 protected void setUp() throws Exception { 61 SAXReader reader = new SAXReader(); 62 reader.setMergeAdjacentText(true); 63 File file = new File ( INPUT_XML_FILE ); 64 if (!file.exists()) { 65 throw new FileNotFoundException ("Xml file does not exist at " + file.getAbsolutePath()); 66 } 67 document = reader.read(file); 68 } 69 70 private void checkNoAdjacent(Element parent) { 71 Node prev = null; 73 Iterator iter = parent.nodeIterator(); 74 while (iter.hasNext()) { 75 Node n = (Node)iter.next(); 76 if (n instanceof Text && (prev != null && prev instanceof Text)) { 77 fail("Node: " + n + " is text and so is its " 78 + "preceding sibling: " + prev); 79 } 80 else if (n instanceof Element) { 81 checkNoAdjacent((Element)n); 82 } 83 84 prev = n; 85 } 86 } 87 88 } 89 90 91 92 93 137 | Popular Tags |