1 19 20 package org.netbeans.api.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.net.MalformedURLException ; 27 import java.net.URL ; 28 import java.util.ArrayList ; 29 import java.util.Arrays ; 30 import java.util.Collections ; 31 import java.util.HashMap ; 32 import java.util.HashSet ; 33 import java.util.List ; 34 import java.util.Map ; 35 import java.util.Set ; 36 import java.util.StringTokenizer ; 37 import org.netbeans.api.project.TestUtil; 38 import org.netbeans.junit.NbTestCase; 39 import org.netbeans.modules.project.libraries.WritableLibraryProvider; 40 import org.netbeans.spi.project.libraries.LibraryFactory; 41 import org.netbeans.spi.project.libraries.LibraryImplementation; 42 import org.netbeans.spi.project.libraries.LibraryTypeProvider; 43 import org.netbeans.spi.project.libraries.support.LibrariesSupport; 44 import org.openide.filesystems.FileObject; 45 import org.openide.filesystems.Repository; 46 import org.openide.loaders.DataFolder; 47 import org.openide.loaders.InstanceDataObject; 48 import org.openide.util.lookup.Lookups; 49 import org.openide.util.lookup.Lookups; 50 54 public class LibraryManagerTest extends NbTestCase { 55 56 private TestLibraryProvider lp; 57 58 private static final String LIBRARY_TYPE = "j2se"; private static final String [] VOLUME_TYPES = new String [] { 60 "bin", 61 "src", 62 "doc" 63 }; 64 65 66 public LibraryManagerTest (String testName) { 67 super (testName); 68 } 69 70 protected void setUp() throws Exception { 71 super.setUp(); 72 lp = new TestLibraryProvider (); 73 TestUtil.setLookup (Lookups.fixed(new Object [] { 74 lp 75 })); 76 registerLibraryTypeProvider(); 77 } 78 79 80 private static void registerLibraryTypeProvider () throws Exception { 81 StringTokenizer tk = new StringTokenizer ("org-netbeans-api-project-libraries/LibraryTypeProviders","/"); 82 FileObject root = Repository.getDefault().getDefaultFileSystem().getRoot(); 83 while (tk.hasMoreElements()) { 84 String pathElement = tk.nextToken(); 85 FileObject tmp = root.getFileObject(pathElement); 86 if (tmp == null) { 87 tmp = root.createFolder(pathElement); 88 } 89 root = tmp; 90 } 91 if (root.getChildren().length == 0) { 92 InstanceDataObject.create (DataFolder.findFolder(root),"TestLibraryTypeProvider",TestLibraryTypeProvider.class); 93 } 94 } 95 96 public void testGetLibraries () throws Exception { 97 LibraryManager lm = LibraryManager.getDefault(); 98 TestListener tl = new TestListener (); 99 lm.addPropertyChangeListener(tl); 100 Library[] libs = lm.getLibraries(); 101 assertEquals ("Libraries count", 0, libs.length); 102 LibraryImplementation[] impls = createTestLibs (); 103 lp.setLibraries(impls); 104 libs = lm.getLibraries(); 105 assertEventsEquals(tl.getEventNames(), new String [] {LibraryManager.PROP_LIBRARIES}); 106 tl.reset(); 107 assertEquals ("Libraries count", 2, libs.length); 108 assertLibsEquals (libs, impls); 109 lp.setLibraries(new LibraryImplementation[0]); 110 assertEventsEquals(tl.getEventNames(), new String [] {LibraryManager.PROP_LIBRARIES}); 111 tl.reset(); 112 libs = lm.getLibraries(); 113 assertEquals ("Libraries count", 0, libs.length); 114 } 115 116 public void testGetLibrary () throws Exception { 117 LibraryImplementation[] impls = createTestLibs (); 118 lp.setLibraries(impls); 119 LibraryManager lm = LibraryManager.getDefault(); 120 Library[] libs = lm.getLibraries(); 121 assertEquals ("Libraries count", 2, libs.length); 122 assertLibsEquals (libs, impls); 123 Library lib = lm.getLibrary("Library1"); 124 assertNotNull ("Existing library", lib); 125 assertLibsEquals(new Library[] {lib}, new LibraryImplementation[] {impls[0]}); 126 lib = lm.getLibrary("Library2"); 127 assertNotNull ("Existing library", lib); 128 assertLibsEquals(new Library[] {lib}, new LibraryImplementation[] {impls[1]}); 129 lib = lm.getLibrary("Library3"); 130 assertNull ("Nonexisting library", lib); 131 } 132 133 public void testAddRemoveLibrary () throws Exception { 134 final LibraryImplementation[] impls = createTestLibs(); 135 lp.setLibraries(impls); 136 final LibraryManager lm = LibraryManager.getDefault(); 137 Library[] libs = lm.getLibraries(); 138 assertEquals ("Libraries count", 2, libs.length); 139 assertLibsEquals (libs, impls); 140 final LibraryTypeProvider provider = LibrariesSupport.getLibraryTypeProvider(LIBRARY_TYPE); 141 assertNotNull (provider); 142 final LibraryImplementation newLibImplementation = provider.createLibrary(); 143 newLibImplementation.setName("NewLib"); 144 final Library newLibrary = LibraryFactory.createLibrary(newLibImplementation); 145 lm.addLibrary(newLibrary); 146 libs = lm.getLibraries(); 147 assertEquals ("Libraries count", 3, libs.length); 148 List <LibraryImplementation> newLibs = new ArrayList <LibraryImplementation>(Arrays.asList(impls)); 149 newLibs.add (newLibImplementation); 150 assertLibsEquals(libs, newLibs.toArray(new LibraryImplementation[newLibs.size()])); 151 lm.removeLibrary(newLibrary); 152 libs = lm.getLibraries(); 153 assertEquals("Libraries count",2,libs.length); 154 assertLibsEquals (libs, impls); 155 } 156 157 static LibraryImplementation[] createTestLibs () throws MalformedURLException { 158 LibraryImplementation[] impls = new LibraryImplementation[] { 159 new TestLibraryImplementation (), 160 new TestLibraryImplementation () 161 }; 162 impls[0].setName ("Library1"); 163 impls[1].setName ("Library2"); 164 impls[0].setDescription("Library1 description"); 165 impls[1].setDescription("Library2 description"); 166 impls[0].setContent("bin", Collections.singletonList( new URL ("file:/lib/libest1.so"))); 167 impls[1].setContent("bin", Collections.singletonList(new URL ("file:/lib/libest2.so"))); 168 return impls; 169 } 170 171 static void assertLibsEquals (Library[] libs, LibraryImplementation[] impls) { 172 assertEquals ("libs equals (size)", impls.length, libs.length); 173 for (int i=0; i< libs.length; i++) { 174 assertEquals ("libs equals (name)", impls[i].getName(), libs[i].getName()); 175 assertEquals ("libs equals (description)", impls[i].getDescription(), libs[i].getDescription()); 176 List lc = libs[i].getContent("bin"); 177 List ic = impls[i].getContent("bin"); 178 assertEquals ("libs equals (content bin)", ic, lc); 179 lc = libs[i].getContent("src"); 180 ic = impls[i].getContent("src"); 181 assertEquals ("libs equals (content src)", ic, lc); 182 lc = libs[i].getContent("doc"); 183 ic = impls[i].getContent("doc"); 184 assertEquals ("libs equals (content doc)", ic, lc); 185 } 186 } 187 188 static void assertEventsEquals (List eventNames, String [] expectedName) { 189 assertEquals ("Events equals", Arrays.asList(expectedName), eventNames); 190 } 191 192 193 static class TestLibraryProvider implements WritableLibraryProvider { 194 195 private PropertyChangeSupport support; 196 private List <LibraryImplementation> libraries; 197 198 public TestLibraryProvider () { 199 this.support = new PropertyChangeSupport (this); 200 this.libraries = new ArrayList <LibraryImplementation>(); 201 } 202 203 public void removePropertyChangeListener(PropertyChangeListener listener) { 204 this.support.removePropertyChangeListener(listener); 205 } 206 207 public void addPropertyChangeListener(PropertyChangeListener listener) { 208 this.support.addPropertyChangeListener(listener); 209 } 210 211 public LibraryImplementation[] getLibraries() { 212 return this.libraries.toArray(new LibraryImplementation[libraries.size()]); 213 } 214 215 public void setLibraries (LibraryImplementation[] libraries) { 216 this.libraries = new ArrayList <LibraryImplementation>(Arrays.asList(libraries)); 217 this.support.firePropertyChange(PROP_LIBRARIES,null,null); 218 } 219 220 public void addLibrary(LibraryImplementation library) throws IOException { 221 this.libraries.add (library); 222 this.support.firePropertyChange(PROP_LIBRARIES,null,null); 223 } 224 225 public void removeLibrary(LibraryImplementation library) throws IOException { 226 this.libraries.remove (library); 227 this.support.firePropertyChange(PROP_LIBRARIES,null,null); 228 } 229 230 public void updateLibrary(LibraryImplementation oldLibrary, LibraryImplementation newLibrary) throws IOException { 231 this.libraries.remove(oldLibrary); 232 this.libraries.add (newLibrary); 233 this.support.firePropertyChange(PROP_LIBRARIES,null,null); 234 } 235 236 } 237 238 static class TestListener implements PropertyChangeListener { 239 240 private List <String > events = new ArrayList <String >(); 241 242 public void propertyChange(PropertyChangeEvent event) { 243 this.events.add(event.getPropertyName()); 244 } 245 246 public void reset () { 247 this.events.clear(); 248 } 249 250 public List getEventNames () { 251 return this.events; 252 } 253 } 254 255 256 public static class TestLibraryTypeProvider implements LibraryTypeProvider { 257 258 259 public String getDisplayName() { 260 return LIBRARY_TYPE; 261 } 262 263 public String getLibraryType() { 264 return LIBRARY_TYPE; 265 } 266 267 public String [] getSupportedVolumeTypes() { 268 return VOLUME_TYPES; 269 } 270 271 public LibraryImplementation createLibrary() { 272 return LibrariesSupport.createLibraryImplementation(LIBRARY_TYPE, VOLUME_TYPES); 273 } 274 275 public void libraryDeleted(LibraryImplementation library) { 276 } 277 278 public void libraryCreated(LibraryImplementation library) { 279 } 280 281 public java.beans.Customizer getCustomizer(String volumeType) { 282 return null; 283 } 284 285 public org.openide.util.Lookup getLookup() { 286 return null; 287 } 288 } 289 290 static class TestLibraryImplementation implements LibraryImplementation { 291 292 private static final Set <String > supportedTypes; 293 294 private String name; 295 private String description; 296 private Map <String ,List <URL >> contents; 297 private PropertyChangeSupport support; 298 299 static { 300 supportedTypes = Collections.unmodifiableSet(new HashSet <String >(Arrays.asList(VOLUME_TYPES))); 301 } 302 303 public TestLibraryImplementation () { 304 this.contents = new HashMap <String ,List <URL >>(); 305 this.support = new PropertyChangeSupport (this); 306 } 307 308 public String getType() { 309 return LIBRARY_TYPE; 310 } 311 312 public String getName() { 313 return this.name; 314 } 315 316 public void setName(String name) { 317 this.name = name; 318 this.support.firePropertyChange(PROP_NAME, null, null); 319 } 320 321 public String getLocalizingBundle() { 322 return null; 323 } 324 325 public void setLocalizingBundle(String resourceName) { 326 } 327 328 public String getDescription() { 329 return this.description; 330 } 331 332 public void setDescription(String text) { 333 this.description = text; 334 this.support.firePropertyChange(PROP_DESCRIPTION, null, null); 335 } 336 337 public List <URL > getContent(String volumeType) throws IllegalArgumentException { 338 if (supportedTypes.contains(volumeType)) { 339 List <URL > l = contents.get(volumeType); 340 if (l == null) { 341 l = Collections.emptyList(); 342 } 343 return Collections.unmodifiableList(l); 344 } 345 else { 346 throw new IllegalArgumentException (); 347 } 348 } 349 350 public void setContent(String volumeType, List <URL > path) throws IllegalArgumentException { 351 if (supportedTypes.contains(volumeType)) { 352 this.contents.put (volumeType, path); 353 this.support.firePropertyChange(PROP_CONTENT, null, null); 354 } 355 else { 356 throw new IllegalArgumentException (); 357 } 358 } 359 360 public void removePropertyChangeListener(PropertyChangeListener l) { 361 this.support.removePropertyChangeListener(l); 362 } 363 364 public void addPropertyChangeListener(PropertyChangeListener l) { 365 this.support.addPropertyChangeListener(l); 366 } 367 } 368 369 } 370 | Popular Tags |