KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > transformation > XAbstractDOMTransformerTestCase


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.cocoon.transformation;
18
19 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
20
21 import junit.framework.TestCase;
22
23 import org.apache.cocoon.xml.AttributesImpl;
24 import org.apache.cocoon.xml.dom.DOMBuilder;
25 import org.w3c.dom.Document JavaDoc;
26 import org.w3c.dom.Element JavaDoc;
27 import org.w3c.dom.NodeList JavaDoc;
28 import org.xml.sax.Attributes JavaDoc;
29
30 /**
31  * A simple testcase for AbstractDOMTransformer.
32  *
33  * @version CVS $Id: XAbstractDOMTransformerTestCase.java 30932 2004-07-29 17:35:38Z vgritsenko $
34  */

35 public class XAbstractDOMTransformerTestCase extends TestCase {
36
37     /**
38      * Constructor.
39      * @param name
40      */

41     public XAbstractDOMTransformerTestCase(String JavaDoc name) {
42         super(name);
43     }
44
45     /**
46      * Test if sending two consecutive "characters" events to the transformer
47      * doesn't lose one of them (cfr. bug #26219).
48      */

49     public void testJoiningCharacters() throws Exception JavaDoc {
50         /*
51          * Simple transformer that produces a document with a root with a single
52          * text node whose value is given by the concatenation of the values
53          * of the children of the root element of the original document.
54          */

55         AbstractDOMTransformer adt = new AbstractDOMTransformer() {
56             protected Document JavaDoc transform(Document JavaDoc doc) {
57                 try {
58                     Document JavaDoc newdoc = DocumentBuilderFactory
59                             .newInstance().newDocumentBuilder().newDocument();
60                     Element JavaDoc root = newdoc.createElement("out");
61                     newdoc.appendChild(root);
62                     NodeList JavaDoc children = doc.getDocumentElement().getChildNodes();
63                     StringBuffer JavaDoc value = new StringBuffer JavaDoc();
64                     for (int i = 0 ; i < children.getLength() ; ++i) {
65                         value.append(children.item(i).getNodeValue());
66                     }
67                     root.appendChild(newdoc.createTextNode(value.toString()));
68                     return newdoc;
69                 } catch (Exception JavaDoc e) {
70                     e.printStackTrace();
71                     return null;
72                 }
73             }
74         };
75         DOMBuilder builder = new DOMBuilder();
76         adt.setConsumer(builder);
77         Attributes JavaDoc attrs = new AttributesImpl();
78         char c1[] = "ABC".toCharArray();
79         char c2[] = "DEF".toCharArray();
80         adt.startDocument();
81         adt.startElement("", "in", "in", attrs);
82         adt.characters(c1, 0, 3);
83         adt.characters(c2, 0, 3);
84         adt.endElement("", "in", "in");
85         adt.endDocument();
86         assertEquals("Content of root element not what expected", "ABCDEF",
87                 builder.getDocument().getDocumentElement().getFirstChild().getNodeValue());
88     }
89 }
90
Popular Tags