KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > filesystems > MIMESupportResolversTest


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 package org.openide.filesystems;
20
21 import java.net.URL JavaDoc;
22 import java.net.URLClassLoader JavaDoc;
23 import java.util.logging.Handler JavaDoc;
24 import java.util.logging.Level JavaDoc;
25 import java.util.logging.LogRecord JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27 import junit.framework.*;
28 import org.openide.ErrorManager;
29 import java.awt.Component JavaDoc;
30 import java.awt.Image JavaDoc;
31 import java.awt.MediaTracker JavaDoc;
32 import java.awt.Toolkit JavaDoc;
33 import java.awt.image.BufferedImage JavaDoc;
34 import java.awt.image.ImageObserver JavaDoc;
35 import java.lang.ref.*;
36 import java.util.*;
37
38 /**
39  *
40  * @author Jaroslav Tulach
41  */

42 public class MIMESupportResolversTest extends TestCase {
43     static {
44         System.setProperty("org.openide.util.Lookup", "org.openide.filesystems.MIMESupportResolversTest$Lkp");
45         Logger.getLogger("").addHandler(new ErrMgr());
46         Logger.getLogger("").setLevel(Level.ALL);
47     }
48     
49     
50     public MIMESupportResolversTest (String JavaDoc testName) {
51         super (testName);
52     }
53
54     protected void setUp () throws Exception JavaDoc {
55     }
56
57     protected void tearDown () throws Exception JavaDoc {
58     }
59
60     public static Test suite () {
61         TestSuite suite = new TestSuite(MIMESupportResolversTest.class);
62         return suite;
63     }
64     
65     
66     public void testWrongImplOfGetResolvers() throws Exception JavaDoc {
67         MIMEResolver[] all = MIMESupport.getResolvers();
68         assertTrue("Error manager race condition activated", ErrMgr.switchDone);
69         assertEquals("there is one", 1, all.length);
70         assertEquals("c1 the original one", Lkp.c1, all[0]);
71         
72         all = MIMESupport.getResolvers();
73         assertEquals("there is one", 1, all.length);
74         assertEquals("c2 the new one", Lkp.c2, all[0]);
75     }
76     
77     
78     
79     public static final class Lkp extends org.openide.util.lookup.AbstractLookup {
80         private ErrMgr err = new ErrMgr();
81         private org.openide.util.lookup.InstanceContent ic;
82         static MIMEResolver c1 = new MIMEResolver() {
83             public String JavaDoc findMIMEType(FileObject fo) {
84                 return null;
85             }
86             
87             public String JavaDoc toString() {
88                 return "C1";
89             }
90         };
91         static MIMEResolver c2 = new MIMEResolver() {
92             public String JavaDoc findMIMEType(FileObject fo) {
93                 return null;
94             }
95             public String JavaDoc toString() {
96                 return "C2";
97             }
98         };
99         
100         public Lkp () {
101             this (new org.openide.util.lookup.InstanceContent ());
102         }
103         
104         private Lkp (org.openide.util.lookup.InstanceContent ic) {
105             super (ic);
106             this.ic = ic;
107             
108             turn(c1);
109         }
110         
111         public void turn (MIMEResolver c) {
112             ArrayList<Object JavaDoc> l = new ArrayList<Object JavaDoc>();
113             l.add(err);
114             l.add(c);
115             ic.set (l, null);
116         }
117     }
118     
119     
120     private static class ErrMgr extends Handler JavaDoc {
121         public static boolean switchDone;
122
123         public ErrMgr() {
124             setLevel(Level.ALL);
125         }
126
127         public void publish(LogRecord JavaDoc r) {
128             String JavaDoc s = r.getMessage();
129
130             if (s.startsWith ("Resolvers computed")) {
131                 switchDone = true;
132                 Lkp lkp = (Lkp)org.openide.util.Lookup.getDefault ();
133                 lkp.turn (Lkp.c2);
134             }
135         }
136
137         public void flush() {
138         }
139
140         public void close() throws SecurityException JavaDoc {
141         }
142
143     }
144     
145 }
146
Popular Tags