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: package org.openide.filesystems;
042:
043: import java.net.URL;
044: import java.net.URLClassLoader;
045: import java.util.logging.Handler;
046: import java.util.logging.Level;
047: import java.util.logging.LogRecord;
048: import java.util.logging.Logger;
049: import junit.framework.*;
050: import org.openide.ErrorManager;
051: import java.awt.Component;
052: import java.awt.Image;
053: import java.awt.MediaTracker;
054: import java.awt.Toolkit;
055: import java.awt.image.BufferedImage;
056: import java.awt.image.ImageObserver;
057: import java.lang.ref.*;
058: import java.util.*;
059:
060: /**
061: *
062: * @author Jaroslav Tulach
063: */
064: public class MIMESupportResolversTest extends TestCase {
065: static {
066: System.setProperty("org.openide.util.Lookup",
067: "org.openide.filesystems.MIMESupportResolversTest$Lkp");
068: Logger.getLogger("").addHandler(new ErrMgr());
069: Logger.getLogger("").setLevel(Level.ALL);
070: }
071:
072: public MIMESupportResolversTest(String testName) {
073: super (testName);
074: }
075:
076: protected void setUp() throws Exception {
077: }
078:
079: protected void tearDown() throws Exception {
080: }
081:
082: public static Test suite() {
083: TestSuite suite = new TestSuite(MIMESupportResolversTest.class);
084: return suite;
085: }
086:
087: public void testWrongImplOfGetResolvers() throws Exception {
088: MIMEResolver[] all = MIMESupport.getResolvers();
089: assertTrue("Error manager race condition activated",
090: ErrMgr.switchDone);
091: assertEquals("there is one", 1, all.length);
092: assertEquals("c1 the original one", Lkp.c1, all[0]);
093:
094: all = MIMESupport.getResolvers();
095: assertEquals("there is one", 1, all.length);
096: assertEquals("c2 the new one", Lkp.c2, all[0]);
097: }
098:
099: public static final class Lkp extends
100: org.openide.util.lookup.AbstractLookup {
101: private ErrMgr err = new ErrMgr();
102: private org.openide.util.lookup.InstanceContent ic;
103: static MIMEResolver c1 = new MIMEResolver() {
104: public String findMIMEType(FileObject fo) {
105: return null;
106: }
107:
108: public String toString() {
109: return "C1";
110: }
111: };
112: static MIMEResolver c2 = new MIMEResolver() {
113: public String findMIMEType(FileObject fo) {
114: return null;
115: }
116:
117: public String toString() {
118: return "C2";
119: }
120: };
121:
122: public Lkp() {
123: this (new org.openide.util.lookup.InstanceContent());
124: }
125:
126: private Lkp(org.openide.util.lookup.InstanceContent ic) {
127: super (ic);
128: this .ic = ic;
129:
130: turn(c1);
131: }
132:
133: public void turn(MIMEResolver c) {
134: ArrayList<Object> l = new ArrayList<Object>();
135: l.add(err);
136: l.add(c);
137: ic.set(l, null);
138: }
139: }
140:
141: private static class ErrMgr extends Handler {
142: public static boolean switchDone;
143:
144: public ErrMgr() {
145: setLevel(Level.ALL);
146: }
147:
148: public void publish(LogRecord r) {
149: String s = r.getMessage();
150:
151: if (s.startsWith("Resolvers computed")) {
152: switchDone = true;
153: Lkp lkp = (Lkp) org.openide.util.Lookup.getDefault();
154: lkp.turn(Lkp.c2);
155: }
156: }
157:
158: public void flush() {
159: }
160:
161: public void close() throws SecurityException {
162: }
163:
164: }
165:
166: }
|