KickJava   Java API By Example, From Geeks To Geeks.

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


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.openide.filesystems;
21
22 import java.io.IOException JavaDoc;
23 import java.lang.ref.WeakReference JavaDoc;
24 import org.netbeans.junit.NbTestCase;
25 import org.openide.util.Lookup;
26 import org.openide.util.lookup.AbstractLookup;
27 import org.openide.util.lookup.InstanceContent;
28 import org.openide.util.lookup.ProxyLookup;
29
30 /**
31  *
32  * @author Jaroslav Tulach, Radek Matous
33  */

34 public class MIMESupportTest extends NbTestCase {
35     private TestLookup lookup;
36     public MIMESupportTest(String JavaDoc testName) {
37         super(testName);
38     }
39
40     static {
41         System.setProperty("org.openide.util.Lookup", MIMESupportTest.TestLookup.class.getName());
42         assertEquals(MIMESupportTest.TestLookup.class, Lookup.getDefault().getClass());
43         
44     }
45     
46     protected void setUp() throws Exception JavaDoc {
47         lookup = (MIMESupportTest.TestLookup)Lookup.getDefault();
48         lookup.init();
49     }
50
51     public void testFindMIMETypeCanBeGarbageCollected() throws IOException JavaDoc {
52         FileObject fo = FileUtil.createData(FileUtil.createMemoryFileSystem().getRoot(), "Ahoj.bla");
53         
54         String JavaDoc expResult = "content/unknown";
55         String JavaDoc result = FileUtil.getMIMEType(fo);
56         assertEquals("some content found", expResult, result);
57         
58         WeakReference JavaDoc<FileObject> r = new WeakReference JavaDoc<FileObject>(fo);
59         fo = null;
60         assertGC("Can be GCed", r);
61     }
62     
63     public void testBehaviourWhemLookupResultIsChanging() throws Exception JavaDoc {
64         MIMESupportTest.TestResolver testR = new MIMESupportTest.TestResolver("a/a");
65         assertTrue(Lookup.getDefault().lookupAll(MIMEResolver.class).isEmpty());
66         
67         FileObject fo = FileUtil.createData(FileUtil.createMemoryFileSystem().getRoot(), "mysterious.lenka");
68         
69         assertEquals("content/unknown",fo.getMIMEType());
70         
71         lookup.setLookups(testR);
72         assertTrue(Lookup.getDefault().lookupAll(MIMEResolver.class).contains(testR));
73         assertEquals(testR.getMime(),fo.getMIMEType());
74         
75         testR = new MIMESupportTest.TestResolver("b/b");
76         lookup.setLookups(testR);
77         assertTrue(Lookup.getDefault().lookupAll(MIMEResolver.class).contains(testR));
78         assertEquals(testR.getMime(),fo.getMIMEType());
79     }
80
81     private class TestResolver extends MIMEResolver {
82         private String JavaDoc mime;
83         private TestResolver(String JavaDoc mime) {
84             this.mime = mime;
85         }
86         
87         public String JavaDoc findMIMEType(FileObject fo) {
88             return mime;
89         }
90         
91         private String JavaDoc getMime() {
92             return mime;
93         }
94     }
95     
96     public static class TestLookup extends ProxyLookup {
97         public TestLookup() {
98             super();
99             init();
100         }
101         
102         private void init() {
103             setLookups(new Lookup[] {});
104         }
105         
106         private void setLookups(Object JavaDoc instance) {
107             setLookups(new Lookup[] {getInstanceLookup(instance)});
108         }
109         
110         private Lookup getInstanceLookup(final Object JavaDoc instance) {
111             InstanceContent instanceContent = new InstanceContent();
112             instanceContent.add(instance);
113             Lookup instanceLookup = new AbstractLookup(instanceContent);
114             return instanceLookup;
115         }
116     }
117     
118 }
119
Popular Tags