KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > libraries > LibrariesStorageTest


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.modules.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.io.OutputStreamWriter JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28 import java.io.StringReader JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Arrays JavaDoc;
32 import java.util.Collections JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.HashSet JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.Map JavaDoc;
37 import java.util.Set JavaDoc;
38 import java.util.StringTokenizer JavaDoc;
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 JavaDoc;
53 import org.xml.sax.SAXException JavaDoc;
54
55
56
57 /**
58  *
59  * @author Tomas Zezula
60  */

61 public class LibrariesStorageTest extends NbTestCase {
62     
63     private FileObject storageFolder;
64     LibrariesStorage storage;
65     
66     public LibrariesStorageTest(String JavaDoc testName) {
67         super(testName);
68     }
69
70     protected void setUp() throws Exception JavaDoc {
71         super.setUp();
72         TestUtil.setLookup(new Object JavaDoc[] {
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 JavaDoc {
81         this.storage.getLibraries();
82         LibraryImplementation[] libs = this.storage.getLibraries();
83         assertEquals("Libraries count",1,libs.length);
84         assertLibEquals(libs, new String JavaDoc[] {"Library1"});
85         createLibraryDefinition(this.storageFolder,"Library2");
86         libs = this.storage.getLibraries();
87         assertEquals("Libraries count",2,libs.length);
88         assertLibEquals(libs, new String JavaDoc[] {"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 JavaDoc[] {"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 JavaDoc {
103         this.storage.getLibraries();
104         LibraryImplementation[] libs = this.storage.getLibraries();
105         assertEquals("Libraries count",1,libs.length);
106         assertLibEquals(libs, new String JavaDoc[] {"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 JavaDoc[] {"Library1","Library2"});
114         assertTrue (tlp.wasCreatedCalled());
115     }
116
117     public void testRemoveLibrary() throws Exception JavaDoc {
118         this.storage.getLibraries();
119         LibraryImplementation[] libs = this.storage.getLibraries();
120         assertEquals("Libraries count",1,libs.length);
121         assertLibEquals(libs, new String JavaDoc[] {"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 JavaDoc {
131         this.storage.getLibraries();
132         LibraryImplementation[] libs = this.storage.getLibraries();
133         assertEquals("Libraries count",1,libs.length);
134         assertLibEquals(libs, new String JavaDoc[] {"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 JavaDoc[] {"NewLibrary"});
143         assertTrue ("Library created called", tlp.wasCreatedCalled());
144     }
145     
146     private static void assertLibEquals (LibraryImplementation[] libs, String JavaDoc[] names) {
147         assertEquals("Libraries Equals (size)",names.length,libs.length);
148         Set JavaDoc<String JavaDoc> s = new HashSet JavaDoc<String JavaDoc>(Arrays.asList(names)); //Ordering is not important
149
for (LibraryImplementation lib : libs) {
150             String JavaDoc name = lib.getName();
151             assertTrue("Libraries Equals (unknown library "+name+")", s.remove(name));
152         }
153     }
154     
155     private static void registerLibraryTypeProvider () throws Exception JavaDoc {
156         StringTokenizer JavaDoc tk = new StringTokenizer JavaDoc("org-netbeans-api-project-libraries/LibraryTypeProviders","/");
157         FileObject root = Repository.getDefault().getDefaultFileSystem().getRoot();
158         while (tk.hasMoreElements()) {
159             String JavaDoc 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 // FileObject inst = root.createData("TestLibraryTypeProvider","instance");
168
// inst.setAttribute("newvalue","")
169
InstanceDataObject.create (DataFolder.findFolder(root),"TestLibraryTypeProvider",TestLibraryTypeProvider.class);
170         }
171     }
172     
173     private static void createLibraryDefinition (final FileObject storageFolder, final String JavaDoc libName) throws IOException JavaDoc {
174         storageFolder.getFileSystem().runAtomicAction(new FileSystem.AtomicAction() {
175             public void run () throws IOException JavaDoc {
176                 FileObject defFile = storageFolder.createData(libName,"xml");
177                 FileLock lock = null;
178                 PrintWriter JavaDoc out = null;
179                 try {
180                     lock = defFile.lock();
181                     out = new PrintWriter JavaDoc(new OutputStreamWriter JavaDoc(defFile.getOutputStream (lock),"UTF-8"));
182                     out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); //NOI18N
183
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 JavaDoc {
204         
205         private List JavaDoc<String JavaDoc> eventNames = new ArrayList JavaDoc<String JavaDoc>();
206         
207         public List JavaDoc<String JavaDoc> getEventNames () {
208             return this.eventNames;
209         }
210         
211         public void propertyChange(PropertyChangeEvent JavaDoc 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 JavaDoc 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 JavaDoc resolveEntity(String JavaDoc str, String JavaDoc str1) throws SAXException JavaDoc, IOException JavaDoc {
235             if ("-//NetBeans//DTD Library Declaration 1.0//EN".equals(str)) {
236                 InputSource JavaDoc in = new InputSource JavaDoc (new StringReader JavaDoc(DTD));
237                 return in;
238             }
239             else {
240                 return null;
241             }
242         }
243     }
244     
245     public static class TestLibraryTypeProvider implements LibraryTypeProvider, java.io.Serializable JavaDoc {
246         
247         static final String JavaDoc[] supportedTypes = new String JavaDoc[] {"bin","src"};
248         
249         static final String JavaDoc TYPE = "Test";
250         
251         private boolean createdCalled;
252         
253         private boolean deletedCalled;
254         
255         public java.beans.Customizer JavaDoc getCustomizer(String JavaDoc 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 JavaDoc[] getSupportedVolumeTypes() {
281             return supportedTypes;
282         }
283
284         public Lookup getLookup() {
285             return Lookup.EMPTY;
286         }
287
288         public String JavaDoc getLibraryType() {
289             return TYPE;
290         }
291
292         public String JavaDoc 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 JavaDoc name;
305         private String JavaDoc locBundle;
306         private String JavaDoc description;
307         private Map JavaDoc<String JavaDoc,List JavaDoc<URL JavaDoc>> contents;
308         private PropertyChangeSupport JavaDoc support;
309         
310         public TestLibrary () {
311             this.support = new PropertyChangeSupport JavaDoc (this);
312             this.contents = new HashMap JavaDoc<String JavaDoc,List JavaDoc<URL JavaDoc>>(2);
313         }
314         
315         public TestLibrary (String JavaDoc 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 JavaDoc getType() {
329             return TestLibraryTypeProvider.TYPE;
330         }
331         
332         public String JavaDoc getName () {
333             return this.name;
334         }
335         
336         public void setName(String JavaDoc name) {
337             this.name = name;
338             this.support.firePropertyChange(PROP_NAME,null,null);
339         }
340         
341         public String JavaDoc getLocalizingBundle() {
342             return this.locBundle;
343         }
344
345         public void setLocalizingBundle(String JavaDoc resourceName) {
346             this.locBundle = resourceName;
347             this.support.firePropertyChange("localizingBundle",null,null);
348         }
349         
350         public String JavaDoc getDescription() {
351             return this.description;
352         }
353
354         public void setDescription(String JavaDoc text) {
355             this.description = text;
356             this.support.firePropertyChange(PROP_DESCRIPTION,null,null);
357         }
358
359         public List JavaDoc<URL JavaDoc> getContent(String JavaDoc volumeType) throws IllegalArgumentException JavaDoc {
360             for (String JavaDoc t : TestLibraryTypeProvider.supportedTypes) {
361                 if (t.equals(volumeType)) {
362                     List JavaDoc<URL JavaDoc> l = this.contents.get(volumeType);
363                     if (l == null) {
364                         l = Collections.emptyList();
365                     }
366                     return l;
367                 }
368             }
369             throw new IllegalArgumentException JavaDoc ();
370         }
371
372         public void setContent(String JavaDoc volumeType, List JavaDoc<URL JavaDoc> path) throws IllegalArgumentException JavaDoc {
373             for (String JavaDoc t : TestLibraryTypeProvider.supportedTypes) {
374                 if (t.equals(volumeType)) {
375                     List JavaDoc<URL JavaDoc> l = this.contents.put(volumeType, path);
376                     this.support.firePropertyChange(PROP_CONTENT,null,null);
377                     return;
378                 }
379             }
380             throw new IllegalArgumentException JavaDoc ();
381         }
382         
383         public void addPropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
384             this.support.addPropertyChangeListener(l);
385         }
386         
387         public void removePropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
388             this.support.removePropertyChangeListener(l);
389         }
390     }
391     
392 }
393
Popular Tags