KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
23 import java.util.*;
24 import java.net.URL JavaDoc;
25 import org.xml.sax.*;
26 import org.openide.cookies.InstanceCookie;
27 import org.openide.loaders.*;
28 import org.openide.filesystems.*;
29 import org.openide.filesystems.FileSystem; // override java.io.FileSystem
30
import org.openide.util.*;
31 import org.openide.util.lookup.*;
32 import org.openide.xml.*;
33 import org.openide.*;
34 //import junit.framework.*;
35
import org.netbeans.junit.*;
36
37 public class MIMEResolverImplTest extends NbTestCase {
38     List resolvers;
39     FileObject root;
40            
41     public MIMEResolverImplTest(String JavaDoc testName) {
42         super(testName);
43     }
44     
45     protected void setUp() throws Exception JavaDoc {
46 /*
47         URL u = getClass().getProtectionDomain().getCodeSource().getLocation();
48         u = new URL(u, "org/netbeans/core/filesystems/code-fs.xml");
49 */

50         URL JavaDoc u = this.getClass().getResource ("code-fs.xml");
51         FileSystem fs = new XMLFileSystem(u);
52         
53         FileObject coderoot = fs.getRoot().getFileObject("root");
54         coderoot.refresh();
55         
56         FileObject fos[] = coderoot.getChildren();
57         resolvers = new ArrayList();
58         for (int i = 0; i<fos.length; i++) {
59             resolvers.add(createResolver(fos[i]));
60         }
61         
62 /*
63         u = getClass().getProtectionDomain().getCodeSource().getLocation();
64         u = new URL(u, "org/netbeans/core/filesystems/data-fs.xml");
65 */

66         u = this.getClass().getResource ("data-fs.xml");
67         fs = new XMLFileSystem(u);
68         
69         root = fs.getRoot().getFileObject("root");
70         root.refresh();
71         FileUtil.setMIMEType("txt2", "text/plain; charset=us-ascii");
72     }
73     
74     public static void main(java.lang.String JavaDoc[] args) {
75         junit.textui.TestRunner.run(suite());
76     }
77     
78     public static NbTest suite() {
79         NbTestSuite suite = new NbTestSuite(MIMEResolverImplTest.class);
80         
81         return suite;
82     }
83     
84     
85     private static MIMEResolver createResolver(FileObject fo) throws Exception JavaDoc {
86         if (fo == null) throw new NullPointerException JavaDoc();
87         return new MIMEResolverImpl.Impl(fo);
88     }
89
90     private String JavaDoc resolve(FileObject fo) {
91         Iterator it = resolvers.iterator();
92         while (it.hasNext()) {
93             MIMEResolver r = (MIMEResolver) it.next();
94             String JavaDoc s = r.findMIMEType(fo);
95             if (s != null) return s;
96         }
97         return null;
98     }
99     
100     public void testDeclarativeMIME() throws Exception JavaDoc {
101         
102         Object JavaDoc tl1 = new Object JavaDoc();
103         Object JavaDoc tl2 = new Object JavaDoc();
104         
105         TestThread t1 = new TestThread(tl1);
106         TestThread t2 = new TestThread(tl2);
107
108         // call resolver from two threads
109

110         t1.start();
111         t2.start();
112         Thread.currentThread().join(100);
113         synchronized (tl1) {tl1.notify();}
114         synchronized (tl2) {tl2.notify();}
115
116  
117         t1.join(5000);
118         t2.join(5000);
119         
120         if (t1.fail != null) fail(t1.fail);
121
122         if (t2.fail != null) fail(t2.fail);
123     }
124
125     private class TestThread extends Thread JavaDoc {
126         
127         Object JavaDoc lock;
128         String JavaDoc fail;
129         
130         private TestThread(Object JavaDoc lock) {
131             this.lock = lock;
132         }
133         
134         public void run() {
135             String JavaDoc s;
136             FileObject fo = null;
137
138             fo = root.getFileObject("test","txt2");
139             s = resolve(fo);
140             if ("mime.xml".equals(s) == false) fail = "mime rule failure: " + fo + " => " + s;
141                         
142             fo = root.getFileObject("test","elf");
143             s = resolve(fo);
144             if ("magic-mask.xml".equals(s) == false) fail = "magic-mask rule failure: " + fo + " => " + s;
145             
146             fo = root.getFileObject("test","exe");
147             s = resolve(fo);
148             if ("magic.xml".equals(s) == false) fail = "magic rule failure: " + fo + " => " + s;
149
150             fo = root.getFileObject("root","xml");
151             s = resolve(fo);
152             if ("root.xml".equals(s) == false) fail = "root rule failure" + fo + " => " + s;
153
154             fo = root.getFileObject("ns","xml");
155             s = resolve(fo);
156             if ("ns.xml".equals(s) == false) fail = "ns rule failure" + fo + " => " + s;
157
158             try {
159                 synchronized (lock) {
160                     lock.wait(5000); // switch threads here
161
}
162             } catch (Exception JavaDoc ex) {
163                 //
164
}
165             
166             fo = root.getFileObject("empty","dtd");
167             s = resolve(fo);
168             if (null != s) fail = "null rule failure" + fo + " => " + s;
169
170             fo = root.getFileObject("pid","xml");
171             s = resolve(fo);
172             if ("pid.xml".equals(s) == false) fail = "pid rule failure" + fo + " => " + s;
173                         
174         }
175     }
176     
177     /** See #15672.
178      * @author Jesse Glick
179      */

180     public void testParseFailures() {
181         assertEquals("build1.xml recognized as Ant script", "text/x-ant+xml", resolve(root.getFileObject("build1", "xml")));
182         assertEquals("bogus.xml not recognized as anything", null, resolve(root.getFileObject("bogus", "xml")));
183         assertEquals("build2.xml recognized as Ant script", "text/x-ant+xml", resolve(root.getFileObject("build2", "xml")));
184     }
185         
186 }
187
Popular Tags