1 19 20 package org.netbeans.core.filesystems; 21 22 import java.io.BufferedReader ; 23 import java.io.File ; 24 import java.io.FileWriter ; 25 import java.io.IOException ; 26 import java.io.InputStreamReader ; 27 import java.io.PrintWriter ; 28 import java.net.MalformedURLException ; 29 import java.net.URL ; 30 import java.net.URLConnection ; 31 import java.util.StringTokenizer ; 32 import org.netbeans.core.startup.layers.NbinstURLMapper; 33 import org.netbeans.core.startup.layers.NbinstURLStreamHandlerFactory; 34 import org.netbeans.junit.MockServices; 35 import org.netbeans.junit.NbTestCase; 36 import org.netbeans.modules.masterfs.MasterURLMapper; 37 import org.openide.filesystems.FileObject; 38 import org.openide.filesystems.FileUtil; 39 import org.openide.filesystems.URLMapper; 40 import org.openide.modules.InstalledFileLocator; 41 import org.openide.util.Lookup; 42 43 public class NbinstURLMapperTest extends NbTestCase { 44 45 private static final String FILE_NAME = "test.txt"; private static final String FOLDER_NAME = "modules"; 48 private File testFile; 49 private int expectedLength; 50 51 public NbinstURLMapperTest (String testName) throws IOException { 52 super (testName); 53 } 54 55 protected void setUp() throws Exception { 56 super.setUp(); 57 58 MockServices.setServices( 59 TestInstalledFileLocator.class, 60 NbinstURLStreamHandlerFactory.class, 61 NbinstURLMapper.class, 62 MasterURLMapper.class); 63 64 org.netbeans.core.startup.Main.initializeURLFactory (); 65 66 File f = this.getWorkDir(); 67 this.clearWorkDir(); 68 Lookup.Result result = Lookup.getDefault().lookupResult(InstalledFileLocator.class); 69 boolean found = false; 70 for (java.util.Iterator it = result.allInstances().iterator(); it.hasNext();) { 71 Object locator = it.next(); 72 if (locator instanceof TestInstalledFileLocator) { 73 ((TestInstalledFileLocator)locator).setRoot(f); 74 found = true; 75 } 76 } 77 assertTrue("No TestInstalledFileLocator can be found in " + Lookup.getDefault(), found); 78 f = new File (f,FOLDER_NAME); 79 f.mkdir(); 80 f = new File (f,FILE_NAME); 81 f.createNewFile(); 82 testFile = f; 83 PrintWriter pw = null; 84 try { 85 pw = new PrintWriter (new FileWriter (f)); 86 pw.println(FILE_NAME); 87 } finally { 88 if (pw!=null) { 89 pw.close (); 90 } 91 } 92 this.expectedLength = (int) f.length(); 93 } 94 95 public void testFindFileObject () throws MalformedURLException , IOException { 96 URL url = new URL ("nbinst:///modules/test.txt"); FileObject fo = URLMapper.findFileObject (url); 98 assertNotNull ("The nbinst URL was not resolved.",fo); 99 assertEquals("URLMapper returned wrong file.",FileUtil.toFile(fo),testFile); 100 url = new URL ("nbinst://test-module/modules/test.txt"); 101 fo = URLMapper.findFileObject (url); 102 assertNotNull ("The nbinst URL was not resolved.",fo); 103 assertEquals("URLMapper returned wrong file.",FileUtil.toFile(fo),testFile); 104 url = new URL ("nbinst://foo-module/modules/test.txt"); 105 fo = URLMapper.findFileObject (url); 106 assertNull ("The nbinst URL was resolved.",fo); 107 } 108 109 public void testURLConnection() throws MalformedURLException , IOException { 110 URL url = new URL ("nbinst:///modules/test.txt"); URLConnection connection = url.openConnection(); 112 assertEquals ("URLConnection returned wrong content length.",connection.getContentLength(),expectedLength); 113 BufferedReader in = null; 114 try { 115 in = new BufferedReader ( new InputStreamReader (connection.getInputStream())); 116 String line = in.readLine(); 117 assertTrue("URLConnection returned invalid InputStream",line.equals(FILE_NAME)); 118 } finally { 119 if (in != null) { 120 in.close (); 121 } 122 } 123 } 124 125 public static class TestInstalledFileLocator extends InstalledFileLocator { 126 127 private File root; 128 129 public TestInstalledFileLocator() {} 130 131 public void setRoot (File root) { 132 this.root = root; 133 } 134 135 public File locate(String relativePath, String codeNameBase, boolean localized) { 136 assert relativePath != null; 137 if (root == null) { 138 return null; 139 } 140 if (codeNameBase!= null && !"test-module".equals(codeNameBase)) { 141 return null; 142 } 143 StringTokenizer tk = new StringTokenizer (relativePath,"/"); 144 File f = this.root; 145 while (tk.hasMoreTokens()) { 146 String part = tk.nextToken(); 147 f = new File (f,part); 148 if (!f.exists()) { 149 return null; 150 } 151 } 152 return f; 153 } 154 } 155 156 } 157 | Popular Tags |