1   /*
2    * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
3    * 
4    * This software is open source. 
5    * See the bottom of this file for the licence.
6    * 
7    * $Id: TestMergeText.java,v 1.7 2004/06/25 08:03:47 maartenc Exp $
8    */
9   
10  package org.dom4j;
11  
12  import java.io.File;
13  import java.util.Iterator;
14  
15  import junit.framework.Test;
16  import junit.framework.TestSuite;
17  import junit.textui.TestRunner;
18  
19  import org.dom4j.io.SAXReader;
20  
21  /*** A test harness for SAXReader option setMergeAdjacentText(true)
22    *
23    * @author <a href="mailto:slehmann@novell.com">Steen Lehmann</a>
24    * @version $Revision: 1.7 $
25    */
26  public class TestMergeText extends AbstractTestCase {
27  
28      /*** Input XML file to read */
29      protected static String INPUT_XML_FILE = "xml/test/mergetext.xml";
30      
31      public static void main( String[] args ) {
32          TestRunner.run( suite() );
33      }
34      
35      public static Test suite() {
36          return new TestSuite( TestMergeText.class );
37      }
38      
39      public TestMergeText(String name) {
40          super(name);
41      }
42  
43      // Test case(s)
44      //-------------------------------------------------------------------------                    
45      public void testNoAdjacentText() throws Exception {
46          
47          // After reading using SAXReader with mergeAdjacentText true,
48          // no two Text objects should be adjacent to each other in the
49          // document.
50  
51          checkNoAdjacent(document.getRootElement());
52          log( "No adjacent Text nodes in " + document.asXML() );
53      }
54          
55      // Implementation methods
56      //-------------------------------------------------------------------------                    
57      protected void setUp() throws Exception {
58          SAXReader reader = new SAXReader();
59          reader.setMergeAdjacentText(true);
60          document = reader.read( new File( INPUT_XML_FILE ).toURL() );
61      }
62      
63      private void checkNoAdjacent(Element parent) {
64          // Check that no two Text nodes are adjacent in the parent's content
65          Node prev = null;
66          Iterator iter = parent.nodeIterator();
67              while (iter.hasNext()) {
68                  Node n = (Node)iter.next();
69                      if (n instanceof Text && (prev != null && prev instanceof Text)) {
70                          fail("Node: " + n + " is text and so is its "
71                              + "preceding sibling: " + prev);
72                      }
73                      else if (n instanceof Element) {
74                          checkNoAdjacent((Element)n);
75                      }
76                      
77                   prev = n;
78              }
79      }
80      
81  }
82  
83  
84  
85  
86  /*
87   * Redistribution and use of this software and associated documentation
88   * ("Software"), with or without modification, are permitted provided
89   * that the following conditions are met:
90   *
91   * 1. Redistributions of source code must retain copyright
92   *    statements and notices.  Redistributions must also contain a
93   *    copy of this document.
94   *
95   * 2. Redistributions in binary form must reproduce the
96   *    above copyright notice, this list of conditions and the
97   *    following disclaimer in the documentation and/or other
98   *    materials provided with the distribution.
99   *
100  * 3. The name "DOM4J" must not be used to endorse or promote
101  *    products derived from this Software without prior written
102  *    permission of MetaStuff, Ltd.  For written permission,
103  *    please contact dom4j-info@metastuff.com.
104  *
105  * 4. Products derived from this Software may not be called "DOM4J"
106  *    nor may "DOM4J" appear in their names without prior written
107  *    permission of MetaStuff, Ltd. DOM4J is a registered
108  *    trademark of MetaStuff, Ltd.
109  *
110  * 5. Due credit should be given to the DOM4J Project - 
111  *    http://www.dom4j.org
112  *
113  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
114  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
115  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
116  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
117  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
118  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
119  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
120  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
121  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
122  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
123  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
124  * OF THE POSSIBILITY OF SUCH DAMAGE.
125  *
126  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
127  *
128  * $Id: TestMergeText.java,v 1.7 2004/06/25 08:03:47 maartenc Exp $
129  */