01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package org.apache.cocoon.transformation;
19:
20: import javax.xml.parsers.DocumentBuilderFactory;
21:
22: import junit.framework.TestCase;
23:
24: import org.apache.cocoon.xml.AttributesImpl;
25: import org.apache.cocoon.xml.dom.DOMBuilder;
26: import org.w3c.dom.Document;
27: import org.w3c.dom.Element;
28: import org.w3c.dom.NodeList;
29: import org.xml.sax.Attributes;
30:
31: /**
32: * A simple testcase for AbstractDOMTransformer.
33: *
34: * @version CVS $Id: XAbstractDOMTransformerTestCase.java 433543 2006-08-22 06:22:54Z crossley $
35: */
36: public class XAbstractDOMTransformerTestCase extends TestCase {
37:
38: /**
39: * Constructor.
40: * @param name
41: */
42: public XAbstractDOMTransformerTestCase(String name) {
43: super (name);
44: }
45:
46: /**
47: * Test if sending two consecutive "characters" events to the transformer
48: * doesn't lose one of them (cfr. bug #26219).
49: */
50: public void testJoiningCharacters() throws Exception {
51: /*
52: * Simple transformer that produces a document with a root with a single
53: * text node whose value is given by the concatenation of the values
54: * of the children of the root element of the original document.
55: */
56: AbstractDOMTransformer adt = new AbstractDOMTransformer() {
57: protected Document transform(Document doc) {
58: try {
59: Document newdoc = DocumentBuilderFactory
60: .newInstance().newDocumentBuilder()
61: .newDocument();
62: Element root = newdoc.createElement("out");
63: newdoc.appendChild(root);
64: NodeList children = doc.getDocumentElement()
65: .getChildNodes();
66: StringBuffer value = new StringBuffer();
67: for (int i = 0; i < children.getLength(); ++i) {
68: value.append(children.item(i).getNodeValue());
69: }
70: root.appendChild(newdoc.createTextNode(value
71: .toString()));
72: return newdoc;
73: } catch (Exception e) {
74: e.printStackTrace();
75: return null;
76: }
77: }
78: };
79: DOMBuilder builder = new DOMBuilder();
80: adt.setConsumer(builder);
81: Attributes attrs = new AttributesImpl();
82: char c1[] = "ABC".toCharArray();
83: char c2[] = "DEF".toCharArray();
84: adt.startDocument();
85: adt.startElement("", "in", "in", attrs);
86: adt.characters(c1, 0, 3);
87: adt.characters(c2, 0, 3);
88: adt.endElement("", "in", "in");
89: adt.endDocument();
90: assertEquals("Content of root element not what expected",
91: "ABCDEF", builder.getDocument().getDocumentElement()
92: .getFirstChild().getNodeValue());
93: }
94: }
|