KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.PropertyChangeEvent JavaDoc;
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.LinkedList JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.netbeans.modules.xml.xam.ComponentEvent;
28 import org.netbeans.modules.xml.xam.ComponentListener;
29 import org.netbeans.modules.xslt.model.AttributeSet;
30 import org.netbeans.modules.xslt.model.LiteralResultElement;
31 import org.netbeans.modules.xslt.model.SequenceElement;
32 import org.netbeans.modules.xslt.model.Stylesheet;
33 import org.netbeans.modules.xslt.model.StylesheetChild;
34 import org.netbeans.modules.xslt.model.Template;
35 import org.netbeans.modules.xslt.model.XslModel;
36
37
38 /**
39  * @author ads
40  *
41  */

42 public class SyncTest extends AbstractXslTestCase {
43     
44     public String JavaDoc TEST_MODIFIED = "test_modified.xsl";
45     
46     public String JavaDoc TEST_MODIFIED_LITERAL_DELETE = "test_modified_literal_delete.xsl";
47     
48     public String JavaDoc TEST_MODIFIED_LITERAL = "test_modified_literal.xsl";
49
50     public SyncTest( String JavaDoc testName ) {
51         super(testName);
52     }
53     
54     public void setUp ( ) {
55         myListener = new Listener JavaDoc();
56     }
57     //will put back after fix xam whitespace filter
58
public void FIXME_testStylesheetChildAdd() throws IOException JavaDoc {
59         Stylesheet stylesheet = getStyleSheet( TEST , true );
60         
61         List JavaDoc<StylesheetChild> oldList = stylesheet.getStylesheetChildren();
62         XslModel model = stylesheet.getModel();
63
64         model.addPropertyChangeListener( getListener() );
65         model.addComponentListener(getListener());
66         
67         Utils.setNewContentFromFile( model, TEST_MODIFIED );
68         
69         model.removePropertyChangeListener( getListener() );
70         model.removeComponentListener(getListener());
71         
72         List JavaDoc<StylesheetChild> list = stylesheet.getStylesheetChildren();
73         boolean nextShouldBeAttributeSet = false;
74         boolean found = false;
75         int count = 0;
76         for (StylesheetChild child : list) {
77             if ( child.getComponentType() == Template.class &&
78                     !nextShouldBeAttributeSet )
79             {
80                 assert child.getComponentType() ==
81                     oldList.get( count ).getComponentType(): "Expected "+
82                     oldList.get( count ).getComponentType()+" but got "+
83                     child.getComponentType();
84                 nextShouldBeAttributeSet = true;
85                 found = true;
86                 count++;
87             }
88             else if ( nextShouldBeAttributeSet && found ){
89                 assert child.getComponentType() == AttributeSet.class:
90                     "Expected AttributeSet class for child component "+
91                     "but got "+child.getComponentType();
92                 found = false;
93             }
94             else {
95                 assert child.getComponentType() ==
96                     oldList.get( count ).getComponentType(): "Expected "+
97                     oldList.get( count ).getComponentType()+" but got "+
98                     child.getComponentType();
99                 count++;
100             }
101         }
102         
103         assertEquals("Event count", 1, getListener().getPropertyChangeEvents().size());
104         assert getListener().getPropertyChangeEvents().get( 0 ).getPropertyName().
105             equals(Stylesheet.STYLESHEET_TOP_LEVEL_ELEMENTS): "Property name event" +
106                     " mismatch";
107         assert getListener().getPropertyChangeEvents().get( 0 ).getSource().
108             equals( stylesheet ) : "Property change source mismatch";
109         
110         assert getListener().getDeletedEvents().size() ==0 :
111             "No deleted events should occur";
112         assert getListener().getValueChangedEvents().size() ==0 :
113             "No value change events should occur";
114         assert getListener().getAddedEvents().size() ==1 : "There should be exactly " +
115                 "one add event";
116         assert getListener().getAddedEvents().get(0).getSource().equals(
117                 stylesheet) : "Source mismatch in added event";
118                 
119         getListener().clearEvents();
120     }
121     
122     public void testStylesheetLiteralDelete() throws IOException JavaDoc {
123         Stylesheet stylesheet = getStyleSheet( TEST , true);
124         
125         Template template = stylesheet.getChildren(
126                 Template.class).get(0);
127         List JavaDoc<SequenceElement> oldList = template.getSequenceChildren();
128         XslModel model = stylesheet.getModel();
129         model.addPropertyChangeListener( getListener() );
130         model.addComponentListener(getListener());
131         
132         Utils.setNewContentFromFile( model, TEST_MODIFIED_LITERAL_DELETE );
133         
134         
135         model.removePropertyChangeListener( getListener() );
136         model.removeComponentListener(getListener());
137
138         List JavaDoc<SequenceElement> list = template.getSequenceChildren();
139         int count = 0;
140         for (SequenceElement child : oldList) {
141             if ( !child.getComponentType().equals( LiteralResultElement.class )) {
142                 assert child.getComponentType().equals( list.get(count).
143                         getComponentType());
144                 count++;
145             }
146         }
147         assert oldList.size() == list.size()+1 :"Incorrect count of children " +
148                 "after deletion of element in source, should be " +
149                 (oldList.size()-1)+" but found :" +list.size();
150         
151         assert getListener().getPropertyChangeEvents().size()==2 :
152             "There should be exactly two property events , found : "+
153             getListener().getPropertyChangeEvents().size();
154         int i = 0;
155         for ( PropertyChangeEvent JavaDoc ev :getListener().getPropertyChangeEvents() ) {
156             System.out.println(ev.getPropertyName());
157             if ( ev.getPropertyName().equals( Template.TEXT_CONTENT_PROPERTY)) {
158                 i++;
159             }
160             else if ( ev.getPropertyName().equals( Template.SEQUENCE_ELEMENT) ) {
161                 i++;
162             }
163         }
164         assert i==2 : "There should be exactly two property events with" +
165                 " names " +Template.TEXT_CONTENT_PROPERTY +" and " +
166                 Template.SEQUENCE_ELEMENT +", but found "+i;
167         
168         assert getListener().getPropertyChangeEvents().get( 0 ).getSource().
169             equals( template ):"Source mismatch in property change events";
170         
171         assert getListener().getDeletedEvents().size() ==1 :"There should " +
172                 "be exactly one deleted event";
173         assert getListener().getValueChangedEvents().size() ==1: "There should be" +
174                 " exactly one value change event";
175         assert getListener().getValueChangedEvents().get(0).getSource().equals(
176                 template ): "Source mismatch in value changed event";
177         assert getListener().getAddedEvents().size() ==0 : "No added events " +
178                 "should occur";
179         assert getListener().getDeletedEvents().get(0).getSource().equals(
180                 template ) : "Source mismatch in deleted event";
181                 
182         getListener().clearEvents();
183     }
184     
185     //will put back after fix xam whitespace filter
186
public void FIXME_testStylesheetLiteralAdd() throws IOException JavaDoc {
187         Stylesheet stylesheet = getStyleSheet( TEST , true);
188         
189         Template template = stylesheet.getChildren(
190                 Template.class).get(0);
191         List JavaDoc<SequenceElement> oldList = template.getSequenceChildren();
192         XslModel model = stylesheet.getModel();
193         
194         model.addPropertyChangeListener( getListener() );
195         model.addComponentListener(getListener());
196         
197         Utils.setNewContentFromFile( model, TEST_MODIFIED_LITERAL );
198         
199         
200         model.removePropertyChangeListener( getListener() );
201         model.removeComponentListener(getListener());
202
203         List JavaDoc<SequenceElement> list = template.getSequenceChildren();
204         int count = 0;
205         boolean nextIsAdded = false;
206         for (SequenceElement child : oldList) {
207             if( nextIsAdded ) {
208                 nextIsAdded = false;
209                 count++;
210                 continue;
211             }
212             if ( child.getComponentType().equals( LiteralResultElement.class )
213                     && !nextIsAdded ) {
214                 nextIsAdded = true;
215             }
216             child.getComponentType().equals( list.get(count));
217             count++;
218         }
219         
220         assert getListener().getPropertyChangeEvents().size()==1:
221             "There should be exactly two property events , found : "+
222             getListener().getPropertyChangeEvents().size();
223         assert getListener().getPropertyChangeEvents().get( 0 ).getPropertyName().
224             equals( Template.SEQUENCE_ELEMENT): "Property name event mismatch";
225         assert getListener().getPropertyChangeEvents().get( 0 ).getSource().
226             equals( template ): "Source mismatch in property event";
227         
228         assert getListener().getDeletedEvents().size() ==0;
229         assert getListener().getValueChangedEvents().size() ==0;
230         assert getListener().getAddedEvents().size() ==1 :"There should " +
231             "be exactly one deleted event";
232         assert getListener().getAddedEvents().get(0).getSource().equals(
233                 template ) : "Source mismatch in added event";
234         getListener().clearEvents();
235     }
236     
237     private Listener JavaDoc getListener() {
238         return myListener;
239     }
240     
241     static class Listener implements PropertyChangeListener JavaDoc, ComponentListener{
242
243         /* (non-Javadoc)
244          * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
245          */

246         public void propertyChange( PropertyChangeEvent JavaDoc event ) {
247             myPropEvents.add( event );
248         }
249
250         /* (non-Javadoc)
251          * @see org.netbeans.modules.xml.xam.ComponentListener#childrenAdded(org.netbeans.modules.xml.xam.ComponentEvent)
252          */

253         public void childrenAdded( ComponentEvent evt ) {
254             myAddedEvents.add(evt);
255         }
256
257         /* (non-Javadoc)
258          * @see org.netbeans.modules.xml.xam.ComponentListener#childrenDeleted(org.netbeans.modules.xml.xam.ComponentEvent)
259          */

260         public void childrenDeleted( ComponentEvent evt ) {
261             myDeletedEvents.add(evt);
262         }
263
264         /* (non-Javadoc)
265          * @see org.netbeans.modules.xml.xam.ComponentListener#valueChanged(org.netbeans.modules.xml.xam.ComponentEvent)
266          */

267         public void valueChanged( ComponentEvent evt ) {
268             myValueChangedEvents.add(evt);
269         }
270         
271         List JavaDoc<PropertyChangeEvent JavaDoc> getPropertyChangeEvents(){
272             return myPropEvents;
273         }
274         
275         List JavaDoc<ComponentEvent> getAddedEvents() {
276             return myAddedEvents;
277         }
278         
279         List JavaDoc<ComponentEvent> getDeletedEvents() {
280             return myDeletedEvents;
281         }
282         
283         List JavaDoc<ComponentEvent> getValueChangedEvents() {
284             return myValueChangedEvents;
285         }
286         
287         void clearEvents() {
288             myPropEvents.clear();
289             myAddedEvents.clear();
290             myDeletedEvents.clear();
291             myValueChangedEvents.clear();
292         }
293
294         private List JavaDoc<PropertyChangeEvent JavaDoc> myPropEvents =
295                 new LinkedList JavaDoc<PropertyChangeEvent JavaDoc>();
296         
297         private List JavaDoc<ComponentEvent> myAddedEvents =
298             new LinkedList JavaDoc<ComponentEvent>();
299         
300         private List JavaDoc<ComponentEvent> myDeletedEvents =
301             new LinkedList JavaDoc<ComponentEvent>();
302         
303         private List JavaDoc<ComponentEvent> myValueChangedEvents =
304             new LinkedList JavaDoc<ComponentEvent>();
305         
306     }
307     
308     private Listener JavaDoc myListener;
309
310 }
311
Popular Tags