KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > filesystems > NbinstURLMapperTest


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.filesystems;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileWriter JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStreamReader JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28 import java.net.MalformedURLException JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.net.URLConnection JavaDoc;
31 import java.util.StringTokenizer JavaDoc;
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 JavaDoc FILE_NAME = "test.txt"; //NOI18N
46
private static final String JavaDoc FOLDER_NAME = "modules"; //NOI18N
47

48     private File JavaDoc testFile;
49     private int expectedLength;
50
51     public NbinstURLMapperTest (String JavaDoc testName) throws IOException JavaDoc {
52         super (testName);
53     }
54
55     protected void setUp() throws Exception JavaDoc {
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 JavaDoc f = this.getWorkDir();
67         this.clearWorkDir();
68         Lookup.Result result = Lookup.getDefault().lookupResult(InstalledFileLocator.class);
69         boolean found = false;
70         for (java.util.Iterator JavaDoc it = result.allInstances().iterator(); it.hasNext();) {
71             Object JavaDoc 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 JavaDoc (f,FOLDER_NAME);
79         f.mkdir();
80         f = new File JavaDoc (f,FILE_NAME);
81         f.createNewFile();
82         testFile = f;
83         PrintWriter JavaDoc pw = null;
84         try {
85             pw = new PrintWriter JavaDoc(new FileWriter JavaDoc(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 JavaDoc, IOException JavaDoc {
96         URL JavaDoc url = new URL JavaDoc ("nbinst:///modules/test.txt"); //NOI18N
97
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 JavaDoc ("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 JavaDoc ("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 JavaDoc, IOException JavaDoc {
110         URL JavaDoc url = new URL JavaDoc ("nbinst:///modules/test.txt"); //NOI18N
111
URLConnection JavaDoc connection = url.openConnection();
112         assertEquals ("URLConnection returned wrong content length.",connection.getContentLength(),expectedLength);
113         BufferedReader JavaDoc in = null;
114         try {
115             in = new BufferedReader JavaDoc ( new InputStreamReader JavaDoc (connection.getInputStream()));
116             String JavaDoc 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 JavaDoc root;
128
129         public TestInstalledFileLocator() {}
130
131         public void setRoot (File JavaDoc root) {
132             this.root = root;
133         }
134
135         public File JavaDoc locate(String JavaDoc relativePath, String JavaDoc 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 JavaDoc tk = new StringTokenizer JavaDoc(relativePath,"/");
144             File JavaDoc f = this.root;
145             while (tk.hasMoreTokens()) {
146                 String JavaDoc part = tk.nextToken();
147                 f = new File JavaDoc (f,part);
148                 if (!f.exists()) {
149                     return null;
150                 }
151             }
152             return f;
153         }
154     }
155
156 }
157
Popular Tags