1 19 package org.netbeans.modules.xslt.model.impl; 20 21 import java.beans.PropertyChangeEvent ; 22 import java.beans.PropertyChangeListener ; 23 import java.io.IOException ; 24 import java.util.LinkedList ; 25 import java.util.List ; 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 42 public class SyncTest extends AbstractXslTestCase { 43 44 public String TEST_MODIFIED = "test_modified.xsl"; 45 46 public String TEST_MODIFIED_LITERAL_DELETE = "test_modified_literal_delete.xsl"; 47 48 public String TEST_MODIFIED_LITERAL = "test_modified_literal.xsl"; 49 50 public SyncTest( String testName ) { 51 super(testName); 52 } 53 54 public void setUp ( ) { 55 myListener = new Listener (); 56 } 57 public void FIXME_testStylesheetChildAdd() throws IOException { 59 Stylesheet stylesheet = getStyleSheet( TEST , true ); 60 61 List <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 <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 { 123 Stylesheet stylesheet = getStyleSheet( TEST , true); 124 125 Template template = stylesheet.getChildren( 126 Template.class).get(0); 127 List <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 <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 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 public void FIXME_testStylesheetLiteralAdd() throws IOException { 187 Stylesheet stylesheet = getStyleSheet( TEST , true); 188 189 Template template = stylesheet.getChildren( 190 Template.class).get(0); 191 List <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 <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 getListener() { 238 return myListener; 239 } 240 241 static class Listener implements PropertyChangeListener , ComponentListener{ 242 243 246 public void propertyChange( PropertyChangeEvent event ) { 247 myPropEvents.add( event ); 248 } 249 250 253 public void childrenAdded( ComponentEvent evt ) { 254 myAddedEvents.add(evt); 255 } 256 257 260 public void childrenDeleted( ComponentEvent evt ) { 261 myDeletedEvents.add(evt); 262 } 263 264 267 public void valueChanged( ComponentEvent evt ) { 268 myValueChangedEvents.add(evt); 269 } 270 271 List <PropertyChangeEvent > getPropertyChangeEvents(){ 272 return myPropEvents; 273 } 274 275 List <ComponentEvent> getAddedEvents() { 276 return myAddedEvents; 277 } 278 279 List <ComponentEvent> getDeletedEvents() { 280 return myDeletedEvents; 281 } 282 283 List <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 <PropertyChangeEvent > myPropEvents = 295 new LinkedList <PropertyChangeEvent >(); 296 297 private List <ComponentEvent> myAddedEvents = 298 new LinkedList <ComponentEvent>(); 299 300 private List <ComponentEvent> myDeletedEvents = 301 new LinkedList <ComponentEvent>(); 302 303 private List <ComponentEvent> myValueChangedEvents = 304 new LinkedList <ComponentEvent>(); 305 306 } 307 308 private Listener myListener; 309 310 } 311 | Popular Tags |