1 package tests.org.w3c.dom;
2 
3 import org.w3c.dom.Node;
4 import org.w3c.dom.Document;
5 import org.w3c.dom.DocumentFragment;
6 import org.w3c.dom.Text;
7 
8 import javax.xml.parsers.DocumentBuilder;
9 
10 /**
11  * Create a document fragment with two adjacent text nodes, normalize and see if
12  * the text nodes were combined.
13  *
14  * @author Curt Arnold
15  * @see <a
16  *      href="http://www.w3.org/TR/DOM-Level-2-Core/core#ID-F68D095">http://www.w3.org/TR/DOM-Level-2-Core/core#ID-F68D095</a>
17  * @see <a
18  *      href="http://www.w3.org/TR/DOM-Level-2-Core/core#ID-B63ED1A3">http://www.w3.org/TR/DOM-Level-2-Core/core#ID-B63ED1A3</a>
19  */
20 public final class HCNodeDocumentFragmentNormalize extends DOMTestCase {
21 
22     DOMDocumentBuilderFactory factory;
23 
24     DocumentBuilder builder;
25 
setUp()26     protected void setUp() throws Exception {
27         super.setUp();
28         try {
29             factory = new DOMDocumentBuilderFactory(DOMDocumentBuilderFactory
30                     .getConfiguration1());
31             builder = factory.getBuilder();
32         } catch (Exception e) {
33             fail("Unexpected exception" + e.getMessage());
34         }
35     }
36 
tearDown()37     protected void tearDown() throws Exception {
38         factory = null;
39         builder = null;
40         super.tearDown();
41     }
42 
43     /**
44      * Runs the test case.
45      *
46      * @throws Throwable
47      *             Any uncaught exception causes test to fail
48      */
testNodeDocumentFragmentNormalize1()49     public void testNodeDocumentFragmentNormalize1() throws Throwable {
50         Document doc;
51         DocumentFragment docFragment;
52         String nodeValue;
53         Text txtNode;
54         Node retval;
55 
56         doc = (Document) load("hc_staff", builder);
57         docFragment = doc.createDocumentFragment();
58         txtNode = doc.createTextNode("foo");
59         retval = docFragment.appendChild(txtNode);
60         txtNode = doc.createTextNode("bar");
61         retval = docFragment.appendChild(txtNode);
62         docFragment.normalize();
63         txtNode = (Text) docFragment.getFirstChild();
64         nodeValue = txtNode.getNodeValue();
65         assertEquals("normalizedNodeValue", "foobar", nodeValue);
66         retval = txtNode.getNextSibling();
67         assertNull("singleChild", retval);
68     }
testNodeDocumentFragmentNormalize2()69     public void testNodeDocumentFragmentNormalize2() throws Throwable {
70         Document doc;
71         DocumentFragment docFragment;
72         Text txtNode;
73 
74         doc = (Document) load("hc_staff", builder);
75         docFragment = doc.createDocumentFragment();
76         txtNode = doc.createTextNode("");
77         docFragment.appendChild(txtNode);
78         docFragment.normalize();
79         txtNode = (Text) docFragment.getFirstChild();
80         assertNull("noChild", txtNode);
81     }
82 }
83