001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.core.projects;
043:
044: import org.netbeans.junit.*;
045: import org.netbeans.Module;
046: import org.netbeans.ModuleManager;
047: import org.netbeans.core.startup.ModuleHistory;
048:
049: import org.openide.util.Mutex;
050: import org.openide.util.MutexException;
051: import org.openide.filesystems.FileObject;
052: import org.openide.filesystems.Repository;
053: import org.openide.nodes.Node;
054: import org.openide.loaders.DataObject;
055: import java.io.File;
056: import java.util.Collections;
057: import java.awt.Toolkit;
058: import java.awt.Image;
059: import java.net.URL;
060: import java.beans.BeanInfo;
061: import java.awt.image.PixelGrabber;
062: import java.awt.image.ImageObserver;
063: import org.netbeans.core.startup.MainLookup;
064: import org.openide.filesystems.FileSystem;
065: import org.openide.filesystems.FileUtil;
066:
067: /** Test operation of the SystemFileSystem.
068: * For now, just display attributes.
069: * @author Jesse Glick
070: */
071: public class SystemFileSystemTest extends NbTestCase {
072:
073: public SystemFileSystemTest(String name) {
074: super (name);
075: }
076:
077: private ModuleManager mgr;
078: private File satJar;
079: private Module satModule;
080:
081: protected void setUp() throws Exception {
082: mgr = org.netbeans.core.startup.Main.getModuleSystem()
083: .getManager();
084: org.netbeans.core.startup.Main.initializeURLFactory();
085: try {
086: mgr.mutex().readAccess(new Mutex.ExceptionAction() {
087: public Object run() throws Exception {
088: satJar = new File(SystemFileSystemTest.class
089: .getResource("data/sfs-attr-test.jar")
090: .getFile());
091: satModule = mgr.create(satJar, new ModuleHistory(
092: satJar.getAbsolutePath()), false, false,
093: false);
094: assertEquals(
095: "no problems installing sfs-attr-test.jar",
096: Collections.EMPTY_SET, satModule
097: .getProblems());
098: mgr.enable(satModule);
099: return null;
100: }
101: });
102: } catch (MutexException me) {
103: throw me.getException();
104: }
105: }
106:
107: protected void tearDown() throws Exception {
108: try {
109: mgr.mutex().readAccess(new Mutex.ExceptionAction() {
110: public Object run() throws Exception {
111: mgr.disable(satModule);
112: mgr.delete(satModule);
113: return null;
114: }
115: });
116: } catch (MutexException me) {
117: throw me.getException();
118: }
119: satModule = null;
120: satJar = null;
121: mgr = null;
122: }
123:
124: public void testLocalizingBundle() throws Exception {
125: FileObject bar = Repository.getDefault().getDefaultFileSystem()
126: .findResource("foo/bar.txt");
127: Node n = DataObject.find(bar).getNodeDelegate();
128: assertEquals("correct localized data object name",
129: "Localized Name", n.getDisplayName());
130: }
131:
132: public void testContentOfFileSystemIsInfluencedByLookup()
133: throws Exception {
134: FileSystem mem = FileUtil.createMemoryFileSystem();
135: String dir = "/yarda/own/file";
136: org.openide.filesystems.FileUtil.createFolder(mem.getRoot(),
137: dir);
138:
139: assertNull("File is not there yet", Repository.getDefault()
140: .getDefaultFileSystem().findResource(dir));
141: MainLookup.register(mem);
142: try {
143: assertNotNull("The file is there now", Repository
144: .getDefault().getDefaultFileSystem().findResource(
145: dir));
146: } finally {
147: MainLookup.unregister(mem);
148: }
149: assertNull("File is no longer there", Repository.getDefault()
150: .getDefaultFileSystem().findResource(dir));
151: }
152:
153: public void testIconFromURL() throws Exception {
154: FileObject bar = Repository.getDefault().getDefaultFileSystem()
155: .findResource("foo/bar.txt");
156: Node n = DataObject.find(bar).getNodeDelegate();
157: Image reference = Toolkit.getDefaultToolkit().createImage(
158: new URL("jar:" + satJar.toURL()
159: + "!/sfs_attr_test/main.gif"));
160: Image tested = n.getIcon(BeanInfo.ICON_COLOR_16x16);
161: int h1 = imageHash("main.gif", reference, 16, 16);
162: int h2 = imageHash("bar.txt icon", tested, 16, 16);
163: assertEquals("correct icon", h1, h2);
164: }
165:
166: /** @see "#18832" */
167: public void testIconFromImageMethod() throws Exception {
168: FileObject baz = Repository.getDefault().getDefaultFileSystem()
169: .findResource("foo/baz.txt");
170: Node n = DataObject.find(baz).getNodeDelegate();
171: Image reference = Toolkit.getDefaultToolkit().createImage(
172: new URL("jar:" + satJar.toURL()
173: + "!/sfs_attr_test/main-plus-badge.gif"));
174: Image tested = n.getIcon(BeanInfo.ICON_COLOR_16x16);
175: int h1 = imageHash("main-plus-badge.gif", reference, 16, 16);
176: int h2 = imageHash("baz.txt icon", tested, 16, 16);
177: assertEquals("correct icon", h1, h2);
178: }
179:
180: private static int imageHash(String name, Image img, int w, int h)
181: throws InterruptedException {
182: int[] pixels = new int[w * h];
183: PixelGrabber pix = new PixelGrabber(img, 0, 0, w, h, pixels, 0,
184: w);
185: pix.grabPixels();
186: assertEquals(0, pix.getStatus() & ImageObserver.ABORT);
187: if (false) {
188: // Debugging.
189: System.out.println("Pixels of " + name + ":");
190: for (int y = 0; y < h; y++) {
191: for (int x = 0; x < w; x++) {
192: if (x == 0) {
193: System.out.print('\t');
194: } else {
195: System.out.print(' ');
196: }
197: int p = pixels[y * w + x];
198: String hex = Integer.toHexString(p);
199: while (hex.length() < 8) {
200: hex = "0" + hex;
201: }
202: System.out.print(hex);
203: if (x == w - 1) {
204: System.out.print('\n');
205: }
206: }
207: }
208: }
209: int hash = 0;
210: for (int i = 0; i < pixels.length; i++) {
211: hash += 172881;
212: int p = pixels[i];
213: if ((p & 0xff000000) == 0) {
214: // Transparent; normalize.
215: p = 0;
216: }
217: hash ^= p;
218: }
219: return hash;
220: }
221:
222: }
|