1 19 20 package org.netbeans.modules.project.libraries; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.beans.PropertyChangeSupport ; 25 import java.io.IOException ; 26 import java.io.OutputStreamWriter ; 27 import java.io.PrintWriter ; 28 import java.io.StringReader ; 29 import java.net.URL ; 30 import java.util.ArrayList ; 31 import java.util.Arrays ; 32 import java.util.Collections ; 33 import java.util.HashMap ; 34 import java.util.HashSet ; 35 import java.util.List ; 36 import java.util.Map ; 37 import java.util.Set ; 38 import java.util.StringTokenizer ; 39 import org.netbeans.api.project.TestUtil; 40 import org.netbeans.junit.NbTestCase; 41 import org.netbeans.spi.project.libraries.LibraryImplementation; 42 import org.netbeans.spi.project.libraries.LibraryProvider; 43 import org.netbeans.spi.project.libraries.LibraryTypeProvider; 44 import org.openide.filesystems.FileLock; 45 import org.openide.filesystems.FileObject; 46 import org.openide.filesystems.FileSystem; 47 import org.openide.filesystems.Repository; 48 import org.openide.loaders.DataFolder; 49 import org.openide.loaders.InstanceDataObject; 50 import org.openide.util.Lookup; 51 import org.openide.xml.EntityCatalog; 52 import org.xml.sax.InputSource ; 53 import org.xml.sax.SAXException ; 54 55 56 57 61 public class LibrariesStorageTest extends NbTestCase { 62 63 private FileObject storageFolder; 64 LibrariesStorage storage; 65 66 public LibrariesStorageTest(String testName) { 67 super(testName); 68 } 69 70 protected void setUp() throws Exception { 71 super.setUp(); 72 TestUtil.setLookup(new Object [] { 73 new TestEntityCatalog()}); 74 this.registerLibraryTypeProvider(); 75 this.storageFolder = TestUtil.makeScratchDir(this); 76 this.createLibraryDefinition(this.storageFolder,"Library1"); 77 this.storage = new LibrariesStorage (this.storageFolder); 78 } 79 80 public void testGetLibraries() throws Exception { 81 this.storage.getLibraries(); 82 LibraryImplementation[] libs = this.storage.getLibraries(); 83 assertEquals("Libraries count",1,libs.length); 84 assertLibEquals(libs, new String [] {"Library1"}); 85 createLibraryDefinition(this.storageFolder,"Library2"); 86 libs = this.storage.getLibraries(); 87 assertEquals("Libraries count",2,libs.length); 88 assertLibEquals(libs, new String [] {"Library1", "Library2"}); 89 TestListener l = new TestListener (); 90 this.storage.addPropertyChangeListener(l); 91 TestLibraryTypeProvider tlp = (TestLibraryTypeProvider) LibraryTypeRegistry.getDefault().getLibraryTypeProvider (TestLibraryTypeProvider.TYPE); 92 tlp.reset(); 93 createLibraryDefinition(this.storageFolder,"Library3"); 94 libs = this.storage.getLibraries(); 95 assertEquals("Libraries count",3,libs.length); 96 assertLibEquals(libs, new String [] {"Library1", "Library2", "Library3"}); 97 assertEquals("Event count",1,l.getEventNames().size()); 98 assertEquals("Event names",LibraryProvider.PROP_LIBRARIES,l.getEventNames().get(0)); 99 assertTrue("Library created called",tlp.wasCreatedCalled()); 100 } 101 102 public void testAddLibrary() throws Exception { 103 this.storage.getLibraries(); 104 LibraryImplementation[] libs = this.storage.getLibraries(); 105 assertEquals("Libraries count",1,libs.length); 106 assertLibEquals(libs, new String [] {"Library1"}); 107 TestLibraryTypeProvider tlp = (TestLibraryTypeProvider) LibraryTypeRegistry.getDefault().getLibraryTypeProvider (TestLibraryTypeProvider.TYPE); 108 tlp.reset(); 109 LibraryImplementation impl = new TestLibrary("Library2"); 110 this.storage.addLibrary(impl); 111 libs = this.storage.getLibraries(); 112 assertEquals("Libraries count",2,libs.length); 113 assertLibEquals(libs, new String [] {"Library1","Library2"}); 114 assertTrue (tlp.wasCreatedCalled()); 115 } 116 117 public void testRemoveLibrary() throws Exception { 118 this.storage.getLibraries(); 119 LibraryImplementation[] libs = this.storage.getLibraries(); 120 assertEquals("Libraries count",1,libs.length); 121 assertLibEquals(libs, new String [] {"Library1"}); 122 TestLibraryTypeProvider tlp = (TestLibraryTypeProvider) LibraryTypeRegistry.getDefault().getLibraryTypeProvider (TestLibraryTypeProvider.TYPE); 123 tlp.reset(); 124 this.storage.removeLibrary(libs[0]); 125 libs = this.storage.getLibraries(); 126 assertEquals("Libraries count",0,libs.length); 127 assertTrue ("Library deleted called", tlp.wasDeletedCalled()); 128 } 129 130 public void testUpdateLibrary() throws Exception { 131 this.storage.getLibraries(); 132 LibraryImplementation[] libs = this.storage.getLibraries(); 133 assertEquals("Libraries count",1,libs.length); 134 assertLibEquals(libs, new String [] {"Library1"}); 135 TestLibraryTypeProvider tlp = (TestLibraryTypeProvider) LibraryTypeRegistry.getDefault().getLibraryTypeProvider (TestLibraryTypeProvider.TYPE); 136 tlp.reset(); 137 LibraryImplementation newLib = new TestLibrary ((TestLibrary)libs[0]); 138 newLib.setName ("NewLibrary"); 139 this.storage.updateLibrary(libs[0],newLib); 140 libs = this.storage.getLibraries(); 141 assertEquals("Libraries count",1,libs.length); 142 assertLibEquals(libs, new String [] {"NewLibrary"}); 143 assertTrue ("Library created called", tlp.wasCreatedCalled()); 144 } 145 146 private static void assertLibEquals (LibraryImplementation[] libs, String [] names) { 147 assertEquals("Libraries Equals (size)",names.length,libs.length); 148 Set <String > s = new HashSet <String >(Arrays.asList(names)); for (LibraryImplementation lib : libs) { 150 String name = lib.getName(); 151 assertTrue("Libraries Equals (unknown library "+name+")", s.remove(name)); 152 } 153 } 154 155 private static void registerLibraryTypeProvider () throws Exception { 156 StringTokenizer tk = new StringTokenizer ("org-netbeans-api-project-libraries/LibraryTypeProviders","/"); 157 FileObject root = Repository.getDefault().getDefaultFileSystem().getRoot(); 158 while (tk.hasMoreElements()) { 159 String pathElement = tk.nextToken(); 160 FileObject tmp = root.getFileObject(pathElement); 161 if (tmp == null) { 162 tmp = root.createFolder(pathElement); 163 } 164 root = tmp; 165 } 166 if (root.getChildren().length == 0) { 167 InstanceDataObject.create (DataFolder.findFolder(root),"TestLibraryTypeProvider",TestLibraryTypeProvider.class); 170 } 171 } 172 173 private static void createLibraryDefinition (final FileObject storageFolder, final String libName) throws IOException { 174 storageFolder.getFileSystem().runAtomicAction(new FileSystem.AtomicAction() { 175 public void run () throws IOException { 176 FileObject defFile = storageFolder.createData(libName,"xml"); 177 FileLock lock = null; 178 PrintWriter out = null; 179 try { 180 lock = defFile.lock(); 181 out = new PrintWriter (new OutputStreamWriter (defFile.getOutputStream (lock),"UTF-8")); 182 out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); out.println("<!DOCTYPE library PUBLIC \"-//NetBeans//DTD Library Declaration 1.0//EN\" \"http://www.netbeans.org/dtds/library-declaration-1_0.dtd\">"); 184 out.println("<library version=\"1.0\">"); 185 out.println("\t<name>"+libName+"</name>"); 186 out.println("\t<type>"+TestLibraryTypeProvider.TYPE+"</type>"); 187 for (int i = 0; i < TestLibraryTypeProvider.supportedTypes.length; i++) { 188 out.println("\t<volume>"); 189 out.println ("\t\t<type>"+TestLibraryTypeProvider.supportedTypes[i]+"</type>"); 190 out.println("\t</volume>"); 191 } 192 out.println("</library>"); 193 } finally { 194 if (out != null) 195 out.close(); 196 if (lock != null) 197 lock.releaseLock(); 198 } 199 } 200 }); 201 } 202 203 private static class TestListener implements PropertyChangeListener { 204 205 private List <String > eventNames = new ArrayList <String >(); 206 207 public List <String > getEventNames () { 208 return this.eventNames; 209 } 210 211 public void propertyChange(PropertyChangeEvent propertyChangeEvent) { 212 this.eventNames.add (propertyChangeEvent.getPropertyName()); 213 } 214 215 public void reset () { 216 this.eventNames.clear(); 217 } 218 219 } 220 221 222 private static class TestEntityCatalog extends EntityCatalog { 223 224 private static final String DTD = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 225 "<!ELEMENT library (name, type, description?, localizing-bundle?, volume*) >\n" + 226 "<!ATTLIST library version CDATA #FIXED \"1.0\" >\n" + 227 "<!ELEMENT name (#PCDATA) >\n" + 228 "<!ELEMENT description (#PCDATA) >\n" + 229 "<!ELEMENT localizing-bundle (#PCDATA)>\n" + 230 "<!ELEMENT volume (type, resource*) >\n" + 231 "<!ELEMENT type (#PCDATA) >\n" + 232 "<!ELEMENT resource (#PCDATA) >\n"; 233 234 public InputSource resolveEntity(String str, String str1) throws SAXException , IOException { 235 if ("-//NetBeans//DTD Library Declaration 1.0//EN".equals(str)) { 236 InputSource in = new InputSource (new StringReader (DTD)); 237 return in; 238 } 239 else { 240 return null; 241 } 242 } 243 } 244 245 public static class TestLibraryTypeProvider implements LibraryTypeProvider, java.io.Serializable { 246 247 static final String [] supportedTypes = new String [] {"bin","src"}; 248 249 static final String TYPE = "Test"; 250 251 private boolean createdCalled; 252 253 private boolean deletedCalled; 254 255 public java.beans.Customizer getCustomizer(String volumeType) { 256 return null; 257 } 258 259 public void libraryDeleted(LibraryImplementation libraryImpl) { 260 this.deletedCalled = true; 261 } 262 263 public void libraryCreated(LibraryImplementation libraryImpl) { 264 this.createdCalled = true; 265 } 266 267 public void reset () { 268 this.createdCalled = false; 269 this.deletedCalled = false; 270 } 271 272 public boolean wasCreatedCalled () { 273 return this.createdCalled; 274 } 275 276 public boolean wasDeletedCalled () { 277 return this.deletedCalled; 278 } 279 280 public String [] getSupportedVolumeTypes() { 281 return supportedTypes; 282 } 283 284 public Lookup getLookup() { 285 return Lookup.EMPTY; 286 } 287 288 public String getLibraryType() { 289 return TYPE; 290 } 291 292 public String getDisplayName() { 293 return "Test Library Type"; 294 } 295 296 public LibraryImplementation createLibrary() { 297 return new TestLibrary (); 298 } 299 300 } 301 302 private static class TestLibrary implements LibraryImplementation { 303 304 private String name; 305 private String locBundle; 306 private String description; 307 private Map <String ,List <URL >> contents; 308 private PropertyChangeSupport support; 309 310 public TestLibrary () { 311 this.support = new PropertyChangeSupport (this); 312 this.contents = new HashMap <String ,List <URL >>(2); 313 } 314 315 public TestLibrary (String name) { 316 this (); 317 this.name = name; 318 } 319 320 public TestLibrary (TestLibrary lib) { 321 this (); 322 this.name = lib.name; 323 this.locBundle = lib.locBundle; 324 this.description = lib.description; 325 this.contents = lib.contents; 326 } 327 328 public String getType() { 329 return TestLibraryTypeProvider.TYPE; 330 } 331 332 public String getName () { 333 return this.name; 334 } 335 336 public void setName(String name) { 337 this.name = name; 338 this.support.firePropertyChange(PROP_NAME,null,null); 339 } 340 341 public String getLocalizingBundle() { 342 return this.locBundle; 343 } 344 345 public void setLocalizingBundle(String resourceName) { 346 this.locBundle = resourceName; 347 this.support.firePropertyChange("localizingBundle",null,null); 348 } 349 350 public String getDescription() { 351 return this.description; 352 } 353 354 public void setDescription(String text) { 355 this.description = text; 356 this.support.firePropertyChange(PROP_DESCRIPTION,null,null); 357 } 358 359 public List <URL > getContent(String volumeType) throws IllegalArgumentException { 360 for (String t : TestLibraryTypeProvider.supportedTypes) { 361 if (t.equals(volumeType)) { 362 List <URL > l = this.contents.get(volumeType); 363 if (l == null) { 364 l = Collections.emptyList(); 365 } 366 return l; 367 } 368 } 369 throw new IllegalArgumentException (); 370 } 371 372 public void setContent(String volumeType, List <URL > path) throws IllegalArgumentException { 373 for (String t : TestLibraryTypeProvider.supportedTypes) { 374 if (t.equals(volumeType)) { 375 List <URL > l = this.contents.put(volumeType, path); 376 this.support.firePropertyChange(PROP_CONTENT,null,null); 377 return; 378 } 379 } 380 throw new IllegalArgumentException (); 381 } 382 383 public void addPropertyChangeListener(java.beans.PropertyChangeListener l) { 384 this.support.addPropertyChangeListener(l); 385 } 386 387 public void removePropertyChangeListener(java.beans.PropertyChangeListener l) { 388 this.support.removePropertyChangeListener(l); 389 } 390 } 391 392 } 393 | Popular Tags |