KickJava   Java API By Example, From Geeks To Geeks.

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


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.Key;
24 import org.netbeans.modules.xslt.model.Output;
25 import org.netbeans.modules.xslt.model.Param;
26 import org.netbeans.modules.xslt.model.Stylesheet;
27 import org.netbeans.modules.xslt.model.StylesheetChild;
28 import org.netbeans.modules.xslt.model.Template;
29 import org.netbeans.modules.xslt.model.Variable;
30 import org.w3c.dom.Document JavaDoc;
31 import org.w3c.dom.Element JavaDoc;
32 import org.w3c.dom.Node JavaDoc;
33 import org.w3c.dom.NodeList JavaDoc;
34
35
36 /**
37  * @author ads
38  *
39  */

40 public class StyleheetTest extends AbstractXslTestCase {
41     
42     public StyleheetTest( String JavaDoc testName ) {
43         super(testName);
44     }
45     
46     public void testGetStylesheetChildren() {
47         Stylesheet stylesheet = getStyleSheet( TEST );
48         
49         List JavaDoc<StylesheetChild> list = stylesheet.getStylesheetChildren();
50         
51         assert list.size() == 7 ;
52         
53         assert list.get( 0 ).getComponentType() == Param.class;
54         assert list.get( 1 ).getComponentType() == Output.class;
55         assert list.get( 2 ).getComponentType() == Variable.class;
56         assert list.get( 3 ).getComponentType() == Template.class;
57         assert list.get( 4 ).getComponentType() == Template.class;
58         assert list.get( 5 ).getComponentType() == Variable.class;
59         assert list.get( 6 ).getComponentType() == Param.class;
60     }
61     
62     public void testAppendStylesheetChild() {
63         Stylesheet stylesheet = getStyleSheet( TEST );
64         
65         Template template = getModel().getFactory().createTemplate();
66         
67         List JavaDoc<String JavaDoc> children = getChildrenTags(
68                 stylesheet.getStylesheetChildren() );
69         
70         getModel().startTransaction();
71         stylesheet.appendStylesheetChild( template );
72         getModel().endTransaction();
73         
74         Document JavaDoc doc = getDOMDocument( getModel() );
75         
76         Element JavaDoc root = doc.getDocumentElement();
77         NodeList JavaDoc list = root.getChildNodes();
78         
79         int count = 0;
80         assert list.getLength() > children.size();
81         for( int i = 0 ; i< list.getLength(); i++ ) {
82             Node JavaDoc next = list.item( i );
83             if ( count == children.size() && next instanceof Element JavaDoc) {
84                 assert next.getNodeName().endsWith(
85                         template.getPeer().getNodeName() ): "Found "+
86                         next.getNodeName()+" element, but expected :"+
87                         template.getPeer().getNodeName();
88                     
89                 break;
90             }
91             if ( next instanceof Element JavaDoc &&
92                     !next.getNodeName().endsWith("import")) // NOI18N
93
{
94                 assert next.getNodeName().equals( children.get( count )):
95                 "Found "+next.getNodeName()+" element, but expected : "+
96                 children.get( i );
97                 count++;
98             }
99         }
100     }
101     
102     public void testRemoveStylesheetChild() {
103         Stylesheet stylesheet = getStyleSheet( TEST );
104         
105         List JavaDoc<StylesheetChild> children = stylesheet.getStylesheetChildren();
106         List JavaDoc<String JavaDoc> childrenNames = getChildrenTags( children );
107         
108         getModel().startTransaction();
109         stylesheet.removeStylesheetChild( children.get( 0 ));
110         getModel().endTransaction();
111         
112         childrenNames.remove( 0 );
113         
114         Document JavaDoc doc = getDOMDocument( getModel() );
115         
116         Element JavaDoc root = doc.getDocumentElement();
117         NodeList JavaDoc list = root.getChildNodes();
118         
119         int count = 0;
120         for( int i = 0 ; i< list.getLength(); i++ ) {
121             Node JavaDoc next = list.item( i );
122             if ( next instanceof Element JavaDoc &&
123                     !next.getNodeName().endsWith("import")) // NOI18N
124
{
125                 assert next.getNodeName().equals( childrenNames.get( count )):
126                     "Found "+next.getNodeName()+" element, but expected : "+
127                     childrenNames.get( count );
128                 count++;
129             }
130         }
131     }
132     
133     public void testAddStylesheetChild() {
134         Stylesheet stylesheet = getStyleSheet( TEST );
135         
136         List JavaDoc<StylesheetChild> children = stylesheet.getStylesheetChildren();
137         List JavaDoc<String JavaDoc> childrenNames = getChildrenTags( children );
138         
139         Key key = getModel().getFactory().createKey();
140         
141         int position = 1;
142         getModel().startTransaction();
143         stylesheet.addStylesheetChild( key, position );
144         getModel().endTransaction();
145         
146         Document JavaDoc doc = getDOMDocument( getModel() );
147         
148         Element JavaDoc root = doc.getDocumentElement();
149         NodeList JavaDoc list = root.getChildNodes();
150         
151         int count = 0;
152         boolean flag = true;
153         for( int i = 0 ; i< list.getLength(); i++ ) {
154             Node JavaDoc next = list.item( i );
155             if ( count == position && next instanceof Element JavaDoc && flag ) {
156                 assert next.getNodeName().endsWith( key.getPeer().getNodeName() ):
157                     "Found "+
158                     next.getNodeName()+" element, but expected :"+
159                     key.getPeer().getNodeName();
160                     flag = false;
161             }
162             else if ( next instanceof Element JavaDoc &&
163                     !next.getNodeName().endsWith("import")) // NOI18N
164
{
165                 assert next.getNodeName().equals( childrenNames.get( count )):
166                     "Found "+next.getNodeName()+" element, but expected : "+
167                     childrenNames.get( count );
168                 count++;
169             }
170         }
171     }
172     
173     // TODO : need to test import access.
174

175 }
176
Popular Tags