1 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 ; 34 import java.util.Collections ; 35 import java.awt.Toolkit ; 36 import java.awt.Image ; 37 import java.net.URL ; 38 import java.beans.BeanInfo ; 39 import java.awt.image.PixelGrabber ; 40 import java.awt.image.ImageObserver ; 41 import org.netbeans.core.startup.MainLookup; 42 import org.openide.filesystems.FileSystem; 43 import org.openide.filesystems.FileUtil; 44 45 49 public class SystemFileSystemTest extends NbTestCase { 50 51 public SystemFileSystemTest(String name) { 52 super(name); 53 } 54 55 private ModuleManager mgr; 56 private File satJar; 57 private Module satModule; 58 protected void setUp() throws Exception { 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 run() throws Exception { 64 satJar = new File (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 { 76 try { 77 mgr.mutex().readAccess(new Mutex.ExceptionAction() { 78 public Object run() throws Exception { 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 { 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 { 99 FileSystem mem = FileUtil.createMemoryFileSystem(); 100 String 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 { 114 FileObject bar = Repository.getDefault().getDefaultFileSystem().findResource("foo/bar.txt"); 115 Node n = DataObject.find(bar).getNodeDelegate(); 116 Image reference = Toolkit.getDefaultToolkit().createImage(new URL ("jar:" + satJar.toURL() + "!/sfs_attr_test/main.gif")); 117 Image 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 124 public void testIconFromImageMethod() throws Exception { 125 FileObject baz = Repository.getDefault().getDefaultFileSystem().findResource("foo/baz.txt"); 126 Node n = DataObject.find(baz).getNodeDelegate(); 127 Image reference = Toolkit.getDefaultToolkit().createImage(new URL ("jar:" + satJar.toURL() + "!/sfs_attr_test/main-plus-badge.gif")); 128 Image 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 name, Image img, int w, int h) throws InterruptedException { 135 int[] pixels = new int[w * h]; 136 PixelGrabber pix = new PixelGrabber (img, 0, 0, w, h, pixels, 0, w); 137 pix.grabPixels(); 138 assertEquals(0, pix.getStatus() & ImageObserver.ABORT); 139 if (false) { 140 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 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 p = 0; 168 } 169 hash ^= p; 170 } 171 return hash; 172 } 173 174 } 175 | Popular Tags |