KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > projects > SystemFileSystemTest


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.core.projects;
21
22 import org.netbeans.junit.*;
23 import org.netbeans.Module;
24 import org.netbeans.ModuleManager;
25 import org.netbeans.core.startup.ModuleHistory;
26
27 import org.openide.util.Mutex;
28 import org.openide.util.MutexException;
29 import org.openide.filesystems.FileObject;
30 import org.openide.filesystems.Repository;
31 import org.openide.nodes.Node;
32 import org.openide.loaders.DataObject;
33 import java.io.File JavaDoc;
34 import java.util.Collections JavaDoc;
35 import java.awt.Toolkit JavaDoc;
36 import java.awt.Image JavaDoc;
37 import java.net.URL JavaDoc;
38 import java.beans.BeanInfo JavaDoc;
39 import java.awt.image.PixelGrabber JavaDoc;
40 import java.awt.image.ImageObserver JavaDoc;
41 import org.netbeans.core.startup.MainLookup;
42 import org.openide.filesystems.FileSystem;
43 import org.openide.filesystems.FileUtil;
44
45 /** Test operation of the SystemFileSystem.
46  * For now, just display attributes.
47  * @author Jesse Glick
48  */

49 public class SystemFileSystemTest extends NbTestCase {
50     
51     public SystemFileSystemTest(String JavaDoc name) {
52         super(name);
53     }
54     
55     private ModuleManager mgr;
56     private File JavaDoc satJar;
57     private Module satModule;
58     protected void setUp() throws Exception JavaDoc {
59         mgr = org.netbeans.core.startup.Main.getModuleSystem().getManager();
60         org.netbeans.core.startup.Main.initializeURLFactory ();
61         try {
62             mgr.mutex().readAccess(new Mutex.ExceptionAction() {
63                 public Object JavaDoc run() throws Exception JavaDoc {
64                     satJar = new File JavaDoc(SystemFileSystemTest.class.getResource("data/sfs-attr-test.jar").getFile());
65                     satModule = mgr.create(satJar, new ModuleHistory(satJar.getAbsolutePath()), false, false, false);
66                     assertEquals("no problems installing sfs-attr-test.jar", Collections.EMPTY_SET, satModule.getProblems());
67                     mgr.enable(satModule);
68                     return null;
69                 }
70             });
71         } catch (MutexException me) {
72             throw me.getException();
73         }
74     }
75     protected void tearDown() throws Exception JavaDoc {
76         try {
77             mgr.mutex().readAccess(new Mutex.ExceptionAction() {
78                 public Object JavaDoc run() throws Exception JavaDoc {
79                     mgr.disable(satModule);
80                     mgr.delete(satModule);
81                     return null;
82                 }
83             });
84         } catch (MutexException me) {
85             throw me.getException();
86         }
87         satModule = null;
88         satJar = null;
89         mgr = null;
90     }
91     
92     public void testLocalizingBundle() throws Exception JavaDoc {
93         FileObject bar = Repository.getDefault().getDefaultFileSystem().findResource("foo/bar.txt");
94         Node n = DataObject.find(bar).getNodeDelegate();
95         assertEquals("correct localized data object name", "Localized Name", n.getDisplayName());
96     }
97     
98     public void testContentOfFileSystemIsInfluencedByLookup () throws Exception JavaDoc {
99         FileSystem mem = FileUtil.createMemoryFileSystem();
100         String JavaDoc dir = "/yarda/own/file";
101         org.openide.filesystems.FileUtil.createFolder (mem.getRoot (), dir);
102         
103         assertNull ("File is not there yet", Repository.getDefault ().getDefaultFileSystem ().findResource (dir));
104         MainLookup.register (mem);
105         try {
106             assertNotNull ("The file is there now", Repository.getDefault ().getDefaultFileSystem ().findResource (dir));
107         } finally {
108             MainLookup.unregister (mem);
109         }
110         assertNull ("File is no longer there", Repository.getDefault ().getDefaultFileSystem ().findResource (dir));
111     }
112     
113     public void testIconFromURL() throws Exception JavaDoc {
114         FileObject bar = Repository.getDefault().getDefaultFileSystem().findResource("foo/bar.txt");
115         Node n = DataObject.find(bar).getNodeDelegate();
116         Image JavaDoc reference = Toolkit.getDefaultToolkit().createImage(new URL JavaDoc("jar:" + satJar.toURL() + "!/sfs_attr_test/main.gif"));
117         Image JavaDoc tested = n.getIcon(BeanInfo.ICON_COLOR_16x16);
118         int h1 = imageHash("main.gif", reference, 16, 16);
119         int h2 = imageHash("bar.txt icon", tested, 16, 16);
120         assertEquals("correct icon", h1, h2);
121     }
122     
123     /** @see "#18832" */
124     public void testIconFromImageMethod() throws Exception JavaDoc {
125         FileObject baz = Repository.getDefault().getDefaultFileSystem().findResource("foo/baz.txt");
126         Node n = DataObject.find(baz).getNodeDelegate();
127         Image JavaDoc reference = Toolkit.getDefaultToolkit().createImage(new URL JavaDoc("jar:" + satJar.toURL() + "!/sfs_attr_test/main-plus-badge.gif"));
128         Image JavaDoc tested = n.getIcon(BeanInfo.ICON_COLOR_16x16);
129         int h1 = imageHash("main-plus-badge.gif", reference, 16, 16);
130         int h2 = imageHash("baz.txt icon", tested, 16, 16);
131         assertEquals("correct icon", h1, h2);
132     }
133     
134     private static int imageHash(String JavaDoc name, Image JavaDoc img, int w, int h) throws InterruptedException JavaDoc {
135         int[] pixels = new int[w * h];
136         PixelGrabber JavaDoc pix = new PixelGrabber JavaDoc(img, 0, 0, w, h, pixels, 0, w);
137         pix.grabPixels();
138         assertEquals(0, pix.getStatus() & ImageObserver.ABORT);
139         if (false) {
140             // Debugging.
141
System.out.println("Pixels of " + name + ":");
142             for (int y = 0; y < h; y++) {
143                 for (int x = 0; x < w; x++) {
144                     if (x == 0) {
145                         System.out.print('\t');
146                     } else {
147                         System.out.print(' ');
148                     }
149                     int p = pixels[y * w + x];
150                     String JavaDoc hex = Integer.toHexString(p);
151                     while (hex.length() < 8) {
152                         hex = "0" + hex;
153                     }
154                     System.out.print(hex);
155                     if (x == w - 1) {
156                         System.out.print('\n');
157                     }
158                 }
159             }
160         }
161         int hash = 0;
162         for (int i = 0; i < pixels.length; i++) {
163             hash += 172881;
164             int p = pixels[i];
165             if ((p & 0xff000000) == 0) {
166                 // Transparent; normalize.
167
p = 0;
168             }
169             hash ^= p;
170         }
171         return hash;
172     }
173     
174 }
175
Popular Tags