KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > project > libraries > LibraryManagerTest


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
20 package org.netbeans.api.project.libraries;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.beans.PropertyChangeSupport JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.net.MalformedURLException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Arrays JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.HashSet JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.Set JavaDoc;
36 import java.util.StringTokenizer JavaDoc;
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 /**
51  *
52  * @author Tomas Zezula
53  */

54 public class LibraryManagerTest extends NbTestCase {
55     
56     private TestLibraryProvider lp;
57     
58     private static final String JavaDoc LIBRARY_TYPE = "j2se"; //NOI18N
59
private static final String JavaDoc[] VOLUME_TYPES = new String JavaDoc[] {
60         "bin",
61         "src",
62         "doc"
63     };
64     
65     /** Creates a new instance of LibraryManagerTest */
66     public LibraryManagerTest (String JavaDoc testName) {
67         super (testName);
68     }
69     
70     protected void setUp() throws Exception JavaDoc {
71         super.setUp();
72         lp = new TestLibraryProvider ();
73         TestUtil.setLookup (Lookups.fixed(new Object JavaDoc[] {
74             lp
75         }));
76         registerLibraryTypeProvider();
77     }
78     
79     
80     private static void registerLibraryTypeProvider () throws Exception JavaDoc {
81         StringTokenizer JavaDoc tk = new StringTokenizer JavaDoc("org-netbeans-api-project-libraries/LibraryTypeProviders","/");
82         FileObject root = Repository.getDefault().getDefaultFileSystem().getRoot();
83         while (tk.hasMoreElements()) {
84             String JavaDoc 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 JavaDoc {
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 JavaDoc[] {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 JavaDoc[] {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 JavaDoc {
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 JavaDoc {
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 JavaDoc<LibraryImplementation> newLibs = new ArrayList JavaDoc<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 JavaDoc {
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 JavaDoc("file:/lib/libest1.so")));
167         impls[1].setContent("bin", Collections.singletonList(new URL JavaDoc("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 JavaDoc lc = libs[i].getContent("bin");
177             List JavaDoc 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 JavaDoc eventNames, String JavaDoc[] expectedName) {
189         assertEquals ("Events equals", Arrays.asList(expectedName), eventNames);
190     }
191     
192     
193     static class TestLibraryProvider implements WritableLibraryProvider {
194         
195         private PropertyChangeSupport JavaDoc support;
196         private List JavaDoc<LibraryImplementation> libraries;
197         
198         public TestLibraryProvider () {
199             this.support = new PropertyChangeSupport JavaDoc (this);
200             this.libraries = new ArrayList JavaDoc<LibraryImplementation>();
201         }
202         
203         public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
204             this.support.removePropertyChangeListener(listener);
205         }
206
207         public void addPropertyChangeListener(PropertyChangeListener JavaDoc 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 JavaDoc<LibraryImplementation>(Arrays.asList(libraries));
217             this.support.firePropertyChange(PROP_LIBRARIES,null,null);
218         }
219
220         public void addLibrary(LibraryImplementation library) throws IOException JavaDoc {
221             this.libraries.add (library);
222             this.support.firePropertyChange(PROP_LIBRARIES,null,null);
223         }
224
225         public void removeLibrary(LibraryImplementation library) throws IOException JavaDoc {
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 JavaDoc {
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 JavaDoc {
239         
240         private List JavaDoc<String JavaDoc> events = new ArrayList JavaDoc<String JavaDoc>();
241         
242         public void propertyChange(PropertyChangeEvent JavaDoc event) {
243             this.events.add(event.getPropertyName());
244         }
245         
246         public void reset () {
247             this.events.clear();
248         }
249         
250         public List JavaDoc getEventNames () {
251             return this.events;
252         }
253     }
254     
255     
256     public static class TestLibraryTypeProvider implements LibraryTypeProvider {
257             
258
259         public String JavaDoc getDisplayName() {
260             return LIBRARY_TYPE;
261         }
262
263         public String JavaDoc getLibraryType() {
264             return LIBRARY_TYPE;
265         }
266
267         public String JavaDoc[] 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 JavaDoc getCustomizer(String JavaDoc 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 JavaDoc<String JavaDoc> supportedTypes;
293         
294         private String JavaDoc name;
295         private String JavaDoc description;
296         private Map JavaDoc<String JavaDoc,List JavaDoc<URL JavaDoc>> contents;
297         private PropertyChangeSupport JavaDoc support;
298         
299         static {
300             supportedTypes = Collections.unmodifiableSet(new HashSet JavaDoc<String JavaDoc>(Arrays.asList(VOLUME_TYPES)));
301         }
302         
303         public TestLibraryImplementation () {
304             this.contents = new HashMap JavaDoc<String JavaDoc,List JavaDoc<URL JavaDoc>>();
305             this.support = new PropertyChangeSupport JavaDoc (this);
306         }
307         
308         public String JavaDoc getType() {
309             return LIBRARY_TYPE;
310         }
311         
312         public String JavaDoc getName() {
313             return this.name;
314         }
315         
316         public void setName(String JavaDoc name) {
317             this.name = name;
318             this.support.firePropertyChange(PROP_NAME, null, null);
319         }
320
321         public String JavaDoc getLocalizingBundle() {
322             return null;
323         }
324         
325         public void setLocalizingBundle(String JavaDoc resourceName) {
326         }
327         
328         public String JavaDoc getDescription() {
329             return this.description;
330         }
331
332         public void setDescription(String JavaDoc text) {
333             this.description = text;
334             this.support.firePropertyChange(PROP_DESCRIPTION, null, null);
335         }
336
337         public List JavaDoc<URL JavaDoc> getContent(String JavaDoc volumeType) throws IllegalArgumentException JavaDoc {
338             if (supportedTypes.contains(volumeType)) {
339                 List JavaDoc<URL JavaDoc> 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 JavaDoc ();
347             }
348         }
349         
350         public void setContent(String JavaDoc volumeType, List JavaDoc<URL JavaDoc> path) throws IllegalArgumentException JavaDoc {
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 JavaDoc ();
357             }
358         }
359
360         public void removePropertyChangeListener(PropertyChangeListener JavaDoc l) {
361             this.support.removePropertyChangeListener(l);
362         }
363
364         public void addPropertyChangeListener(PropertyChangeListener JavaDoc l) {
365             this.support.addPropertyChangeListener(l);
366         }
367     }
368     
369 }
370
Popular Tags