KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xslt > model > impl > XslComponentTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xslt.model.impl;
20
21 import java.util.List JavaDoc;
22
23 import org.netbeans.modules.xslt.model.LiteralResultElement;
24 import org.netbeans.modules.xslt.model.Stylesheet;
25 import org.netbeans.modules.xslt.model.Template;
26 import org.netbeans.modules.xslt.model.XslComponent;
27 import org.w3c.dom.Document JavaDoc;
28 import org.w3c.dom.Element JavaDoc;
29 import org.w3c.dom.Node JavaDoc;
30 import org.w3c.dom.NodeList JavaDoc;
31
32
33
34
35 /**
36  * @author ads
37  *
38  */

39 public class XslComponentTest extends AbstractXslTestCase {
40
41     public XslComponentTest( String JavaDoc testName ) {
42         super(testName);
43     }
44
45     public void testGetContent() {
46         Template template = getTemplate( 0 );
47         
48         assert template != null : "Couldn't found template child";
49         
50         Document JavaDoc doc = getDOMDocument( getModel() );
51         NodeList JavaDoc list = doc.getElementsByTagName( template.getPeer().getNodeName() );
52         
53         assert list.getLength() == 2;
54         
55         assert template.getContent().trim().equals( getContent( list.item( 0 )) );
56     }
57     
58     public void testSetContent() {
59         Template template = getTemplate( 0 );
60
61         List JavaDoc<XslComponent> childrenList = template.getChildren();
62         
63         getModel().startTransaction();
64         template.setContent( "a" );
65         getModel().endTransaction();
66         
67         Document JavaDoc doc = getDOMDocument( getModel() );
68         NodeList JavaDoc list = doc.getElementsByTagName( template.getPeer().getNodeName() );
69         
70         assert getContent( list.item( 0 ) ).equals( "a" );
71         
72         assert template.getContent().trim().equals( "a" );
73         
74         list = list.item( 0 ).getChildNodes();
75         
76         int count = 0;
77         for( int i=0 ; i<list.getLength(); i++ ) {
78             Node JavaDoc node = list.item( i );
79             if ( node instanceof Element ) {
80                 node.getNodeName().equals(
81                         childrenList.get( count ).getPeer().getNodeName());
82                 count++;
83             }
84         }
85     }
86     
87     public void testGetContentInEmpty() {
88         Template template = getTemplate( 1 );
89         
90         String JavaDoc content = template.getContent();
91         if ( content != null ) {
92             assert content.trim().length() == 0;
93         }
94     }
95     
96     public void testSetContentInEmpty() {
97         String JavaDoc text = "new";
98         Template template = getTemplate( 1 );
99         
100         getModel().startTransaction();
101         template.setContent( text );
102         getModel().endTransaction();
103         
104         template.getContent().trim().equals( text );
105         
106         Document JavaDoc doc = getDOMDocument( getModel() );
107         NodeList JavaDoc list = doc.getElementsByTagName( template.getPeer().getNodeName() );
108         
109         assert list.getLength() == 2;
110         
111         assert getContent( list.item( 1 )).equals( text );
112         
113         list = list.item( 1 ).getChildNodes();
114         for( int i=0 ; i<list.getLength(); i++ ) {
115             assert list.item( i ).getNodeType() == Node.TEXT_NODE;
116         }
117     }
118
119     public void testGetTrailingText() {
120         List JavaDoc<LiteralResultElement> children =
121             getTemplate( 0 ).getChildren( LiteralResultElement.class );
122         
123         LiteralResultElement element = children.get( 0 );
124         assert element!= null;
125         
126         String JavaDoc text = element.getTrailingText();
127         
128         checkTrailingText(text);
129         
130     }
131
132     public void testSetTrailingText() {
133         String JavaDoc text = "new_text";
134         
135         List JavaDoc<LiteralResultElement> children =
136             getTemplate( 0 ).getChildren( LiteralResultElement.class );
137         
138         LiteralResultElement element = children.get( 0 );
139         assert element!= null;
140         
141         getModel().startTransaction();
142         element.setTrailingText(text);
143         getModel().endTransaction();
144         
145         assert element.getTrailingText().trim().equals( text );
146         
147         checkTrailingText( text );
148     }
149     
150     private void checkTrailingText( String JavaDoc text ) {
151         Document JavaDoc doc = getDOMDocument( getModel() );
152         NodeList JavaDoc list = doc.getElementsByTagName(
153                 getTemplate( 0 ).getPeer().getNodeName() );
154         
155         Node JavaDoc node = list.item( 0 );
156         list = node.getChildNodes();
157         int count=0;
158         StringBuilder JavaDoc builder = new StringBuilder JavaDoc();
159         for( int i=0; i<list.getLength() ; i++ ) {
160             Node JavaDoc next = list.item( i );
161             if ( next instanceof Element ) {
162                 count++;
163             }
164             if ( count >1 &&
165                     ( next.getNodeType()== Node.TEXT_NODE
166                             || next.getNodeType()== Node.CDATA_SECTION_NODE ) )
167             {
168                 builder.append( next.getNodeValue() );
169             }
170         }
171         
172         assert count == 2; // we should have only two elements here
173

174         assert text.trim().equals( builder.toString().trim() );
175     }
176     
177     private Template getTemplate( int i ) {
178         Stylesheet stylesheet = getStyleSheet( TEST );
179         
180         List JavaDoc<Template> children = stylesheet.getChildren( Template.class );
181         Template template = children.get( i );
182         return template;
183     }
184
185     
186     private String JavaDoc getContent( Node JavaDoc node ) {
187         NodeList JavaDoc list = node.getChildNodes();
188         StringBuilder JavaDoc builder = new StringBuilder JavaDoc();
189         for( int i=0 ; i<list.getLength(); i++ ) {
190             Node JavaDoc next = list.item( i );
191             if ( next instanceof Element ) {
192                 break;
193             }
194             if ( next.getNodeType() == Node.TEXT_NODE || next.getNodeType() ==
195                 Node.CDATA_SECTION_NODE )
196             {
197                 builder.append( next.getNodeValue() );
198             }
199
200         }
201         return builder.toString().trim();
202     }
203 }
204
Popular Tags