KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > dom4j > TestMergeText


1 /*
2  * Copyright 2001 (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.1 2003/07/07 10:30:29 per_nyfelt Exp $
8  */

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 JavaDoc;
21 import java.io.FileNotFoundException JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 /** A test harness for SAXReader option setMergeAdjacentText(true)
25   *
26   * @author <a HREF="mailto:slehmann@novell.com">Steen Lehmann</a>
27   * @version $Revision: 1.1 $
28   */

29 public class TestMergeText extends AbstractTestCase {
30
31     /** Input XML file to read */
32     protected static String JavaDoc INPUT_XML_FILE = "xml/test/mergeText.xml";
33
34     public static void main( String JavaDoc[] args ) {
35         TestRunner.run( suite() );
36     }
37
38     public static Test suite() {
39         return new TestSuite( TestMergeText.class );
40     }
41
42     public TestMergeText(String JavaDoc name) {
43         super(name);
44     }
45
46     // Test case(s)
47
//-------------------------------------------------------------------------
48
public void testNoAdjacentText() throws Exception JavaDoc {
49
50         // After reading using SAXReader with mergeAdjacentText true,
51
// no two Text objects should be adjacent to each other in the
52
// document.
53

54         checkNoAdjacent(document.getRootElement());
55         log( "No adjacent Text nodes in " + document.asXML() );
56     }
57
58     // Implementation methods
59
//-------------------------------------------------------------------------
60
protected void setUp() throws Exception JavaDoc {
61         SAXReader reader = new SAXReader();
62         reader.setMergeAdjacentText(true);
63         File JavaDoc file = new File JavaDoc( INPUT_XML_FILE );
64         if (!file.exists()) {
65             throw new FileNotFoundException JavaDoc("Xml file does not exist at " + file.getAbsolutePath());
66         }
67         document = reader.read(file);
68     }
69
70     private void checkNoAdjacent(Element parent) {
71         // Check that no two Text nodes are adjacent in the parent's content
72
Node prev = null;
73         Iterator JavaDoc 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 /*
94  * Redistribution and use of this software and associated documentation
95  * ("Software"), with or without modification, are permitted provided
96  * that the following conditions are met:
97  *
98  * 1. Redistributions of source code must retain copyright
99  * statements and notices. Redistributions must also contain a
100  * copy of this document.
101  *
102  * 2. Redistributions in binary form must reproduce the
103  * above copyright notice, this list of conditions and the
104  * following disclaimer in the documentation and/or other
105  * materials provided with the distribution.
106  *
107  * 3. The name "DOM4J" must not be used to endorse or promote
108  * products derived from this Software without prior written
109  * permission of MetaStuff, Ltd. For written permission,
110  * please contact dom4j-info@metastuff.com.
111  *
112  * 4. Products derived from this Software may not be called "DOM4J"
113  * nor may "DOM4J" appear in their names without prior written
114  * permission of MetaStuff, Ltd. DOM4J is a registered
115  * trademark of MetaStuff, Ltd.
116  *
117  * 5. Due credit should be given to the DOM4J Project
118  * (http://dom4j.org/).
119  *
120  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
121  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
122  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
123  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
124  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
125  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
126  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
127  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
128  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
129  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
130  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
131  * OF THE POSSIBILITY OF SUCH DAMAGE.
132  *
133  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
134  *
135  * $Id: TestMergeText.java,v 1.1 2003/07/07 10:30:29 per_nyfelt Exp $
136  */

137
Popular Tags