KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.IOException JavaDoc;
21 import org.netbeans.junit.NbTestCase;
22 import org.openide.ErrorManager;
23 import org.openide.util.Lookup;
24 import org.openide.util.lookup.InstanceContent;
25
26 /** Simulating stackover flow from issue 68318
27  *
28  * @author Jaroslav Tulach
29  */

30 public class MIMESupport68318Test extends NbTestCase {
31     static {
32         System.setProperty("org.openide.util.Lookup", "org.openide.filesystems.MIMESupport68318Test$Lkp");
33     }
34
35     public MIMESupport68318Test(String JavaDoc testName) {
36         super(testName);
37     }
38
39     protected void setUp() throws Exception JavaDoc {
40         ErrorManager.getDefault().log("Just initialize the ErrorManager");
41     }
42
43     protected void tearDown() throws Exception JavaDoc {
44     }
45
46     public void testQueryMIMEFromInsideTheLookup() throws IOException JavaDoc {
47         Lkp l = (Lkp)Lookup.getDefault();
48         {
49             MIMEResolver[] result = MIMESupport.getResolvers();
50             assertEquals("One is there", 1, result.length);
51             assertEquals("It is c1", Lkp.c1, result[0]);
52
53             assertNotNull("Result computed", l.result);
54             assertEquals("But it has to be empty", 0, l.result.length);
55         }
56         
57         l.result = null;
58         l.ic.add(Lkp.c2);
59         
60         {
61             MIMEResolver[] result = MIMESupport.getResolvers();
62             assertEquals("Now two", 2, result.length);
63             assertEquals("It is c1", Lkp.c1, result[0]);
64             assertEquals("and c2", Lkp.c2, result[1]);
65
66             assertNotNull("Result in lookup computed", l.result);
67             assertEquals("And it contains the previous result", 1, l.result.length);
68             assertEquals("which is c1", Lkp.c1, l.result[0]);
69         }
70     }
71     
72     public static final class Lkp extends org.openide.util.lookup.AbstractLookup {
73         static MIMEResolver c1 = new MIMEResolver() {
74             public String JavaDoc findMIMEType(FileObject fo) {
75                 return null;
76             }
77             
78             public String JavaDoc toString() {
79                 return "C1";
80             }
81         };
82         static MIMEResolver c2 = new MIMEResolver() {
83             public String JavaDoc findMIMEType(FileObject fo) {
84                 return null;
85             }
86             
87             public String JavaDoc toString() {
88                 return "C2";
89             }
90         };
91         private MIMEResolver[] result;
92         
93         
94         public InstanceContent ic;
95         public Lkp () {
96             this (new InstanceContent ());
97         }
98         
99         private Lkp (InstanceContent ic) {
100             super (ic);
101             this.ic = ic;
102             
103             ic.add(c1);
104         }
105
106         protected void beforeLookup(org.openide.util.Lookup.Template template) {
107             if (template.getType() == MIMEResolver.class) {
108                 assertNull("First invocation to assign result", result);
109                 result = MIMESupport.getResolvers();
110             }
111         }
112
113         
114     }
115     
116 }
117
Popular Tags