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.filesystems;
043:
044: import java.io.*;
045: import java.util.*;
046: import java.net.URL;
047: import org.xml.sax.*;
048: import org.openide.loaders.*;
049: import org.openide.filesystems.*;
050: import org.openide.util.*;
051: import org.openide.util.lookup.*;
052: import org.openide.xml.*;
053: import org.openide.*;
054: import org.netbeans.junit.*;
055:
056: public class MIMEResolverImplTest extends NbTestCase {
057: List<MIMEResolver> resolvers;
058: FileObject root;
059:
060: public MIMEResolverImplTest(String testName) {
061: super (testName);
062: }
063:
064: @SuppressWarnings("deprecation")
065: protected void setUp() throws Exception {
066: /*
067: URL u = getClass().getProtectionDomain().getCodeSource().getLocation();
068: u = new URL(u, "org/netbeans/core/filesystems/code-fs.xml");
069: */
070: URL u = this .getClass().getResource("code-fs.xml");
071: FileSystem fs = new XMLFileSystem(u);
072:
073: FileObject coderoot = fs.getRoot().getFileObject("root");
074: coderoot.refresh();
075:
076: FileObject fos[] = coderoot.getChildren();
077: resolvers = new ArrayList<MIMEResolver>();
078: for (int i = 0; i < fos.length; i++) {
079: resolvers.add(createResolver(fos[i]));
080: }
081:
082: /*
083: u = getClass().getProtectionDomain().getCodeSource().getLocation();
084: u = new URL(u, "org/netbeans/core/filesystems/data-fs.xml");
085: */
086: u = this .getClass().getResource("data-fs.xml");
087: fs = new XMLFileSystem(u);
088:
089: root = fs.getRoot().getFileObject("root");
090: root.refresh();
091: FileUtil.setMIMEType("txt2", "text/plain; charset=us-ascii");
092: }
093:
094: public static void main(java.lang.String[] args) {
095: junit.textui.TestRunner.run(suite());
096: }
097:
098: public static NbTest suite() {
099: NbTestSuite suite = new NbTestSuite(MIMEResolverImplTest.class);
100:
101: return suite;
102: }
103:
104: private static MIMEResolver createResolver(FileObject fo)
105: throws Exception {
106: if (fo == null)
107: throw new NullPointerException();
108: return new MIMEResolverImpl.Impl(fo);
109: }
110:
111: private String resolve(FileObject fo) {
112: Iterator it = resolvers.iterator();
113: while (it.hasNext()) {
114: MIMEResolver r = (MIMEResolver) it.next();
115: String s = r.findMIMEType(fo);
116: if (s != null)
117: return s;
118: }
119: return null;
120: }
121:
122: public void testDeclarativeMIME() throws Exception {
123:
124: Object tl1 = new Object();
125: Object tl2 = new Object();
126:
127: TestThread t1 = new TestThread(tl1);
128: TestThread t2 = new TestThread(tl2);
129:
130: // call resolver from two threads
131:
132: t1.start();
133: t2.start();
134: Thread.currentThread().join(100);
135: synchronized (tl1) {
136: tl1.notify();
137: }
138: synchronized (tl2) {
139: tl2.notify();
140: }
141:
142: t1.join(5000);
143: t2.join(5000);
144:
145: if (t1.fail != null)
146: fail(t1.fail);
147:
148: if (t2.fail != null)
149: fail(t2.fail);
150: }
151:
152: private class TestThread extends Thread {
153:
154: Object lock;
155: String fail;
156:
157: private TestThread(Object lock) {
158: this .lock = lock;
159: }
160:
161: public void run() {
162: String s;
163: FileObject fo = null;
164:
165: fo = root.getFileObject("test", "txt2");
166: s = resolve(fo);
167: if ("mime.xml".equals(s) == false)
168: fail = "mime rule failure: " + fo + " => " + s;
169:
170: fo = root.getFileObject("test", "txt3");
171: s = resolve(fo);
172: if (s != null)
173: fail = "and-mime rule failure: " + fo + " => " + s;
174:
175: fo = root.getFileObject("test", "elf");
176: s = resolve(fo);
177: if ("magic-mask.xml".equals(s) == false)
178: fail = "magic-mask rule failure: " + fo + " => " + s;
179:
180: fo = root.getFileObject("test", "exe");
181: s = resolve(fo);
182: if ("magic.xml".equals(s) == false)
183: fail = "magic rule failure: " + fo + " => " + s;
184:
185: fo = root.getFileObject("root", "xml");
186: s = resolve(fo);
187: if ("root.xml".equals(s) == false)
188: fail = "root rule failure" + fo + " => " + s;
189:
190: fo = root.getFileObject("ns", "xml");
191: s = resolve(fo);
192: if ("ns.xml".equals(s) == false)
193: fail = "ns rule failure" + fo + " => " + s;
194:
195: try {
196: synchronized (lock) {
197: lock.wait(5000); // switch threads here
198: }
199: } catch (Exception ex) {
200: //
201: }
202:
203: fo = root.getFileObject("empty", "dtd");
204: s = resolve(fo);
205: if (null != s)
206: fail = "null rule failure" + fo + " => " + s;
207:
208: fo = root.getFileObject("pid", "xml");
209: s = resolve(fo);
210: if ("pid.xml".equals(s) == false)
211: fail = "pid rule failure" + fo + " => " + s;
212:
213: }
214: }
215:
216: /** See #15672.
217: * @author Jesse Glick
218: */
219: public void testParseFailures() {
220: assertEquals("build1.xml recognized as Ant script",
221: "text/x-ant+xml", resolve(root.getFileObject("build1",
222: "xml")));
223: assertEquals("bogus.xml not recognized as anything", null,
224: resolve(root.getFileObject("bogus", "xml")));
225: assertEquals("build2.xml recognized as Ant script",
226: "text/x-ant+xml", resolve(root.getFileObject("build2",
227: "xml")));
228: }
229:
230: }
|