KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > filesystems > FileEntityResolverTest


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.netbeans.core.filesystems;
21
22 import java.io.IOException JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.io.PrintStream JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.jar.Attributes JavaDoc;
27 import java.util.logging.Level JavaDoc;
28 import java.util.logging.Logger JavaDoc;
29 import javax.swing.event.ChangeEvent JavaDoc;
30 import javax.swing.event.ChangeListener JavaDoc;
31 import org.netbeans.core.LoaderPoolNode;
32 import org.netbeans.core.startup.ManifestSection;
33 import org.openide.ErrorManager;
34 import org.openide.filesystems.FileObject;
35 import org.openide.filesystems.FileUtil;
36 import org.openide.filesystems.LocalFileSystem;
37 import org.openide.filesystems.MIMEResolver;
38 import org.openide.filesystems.Repository;
39 import org.openide.loaders.DataLoaderPool;
40 import org.openide.loaders.DataObject;
41 import org.openide.loaders.DataObjectExistsException;
42 import org.openide.loaders.MultiDataObject;
43 import org.openide.loaders.UniFileLoader;
44 import org.openide.util.Lookup;
45 import org.openide.util.LookupEvent;
46 import org.openide.util.LookupListener;
47
48 /** Checking the behaviour of entity resolvers.
49  *
50  * @author Jaroslav Tulach
51  */

52 public class FileEntityResolverTest extends org.netbeans.core.LoggingTestCaseHid
53 implements LookupListener, ChangeListener JavaDoc {
54     private FileObject fo;
55     private Lenka loader;
56     private ManifestSection.LoaderSection ls;
57     private Lookup.Result mimeResolvers;
58     private int change;
59     private int poolChange;
60     private static ErrorManager err;
61     
62     public FileEntityResolverTest(String JavaDoc testName) {
63         super(testName);
64     }
65
66     protected void setUp() throws Exception JavaDoc {
67         clearWorkDir();
68         
69         err = ErrorManager.getDefault().getInstance("TEST-" + getName());
70         
71         DataLoaderPool.getDefault().addChangeListener(this);
72         
73         org.netbeans.core.startup.Main.getModuleSystem();
74         
75         Thread.sleep(2000);
76         
77         assertEquals("No change in pool during initialization of module system", 0, poolChange);
78         
79         LocalFileSystem lfs = new LocalFileSystem();
80         lfs.setRootDirectory(getWorkDir());
81         
82         fo = FileUtil.createData(lfs.getRoot(), "X.lenka");
83         
84         Attributes JavaDoc at = new Attributes JavaDoc();
85         at.putValue("OpenIDE-Module-Class", "Loader");
86         String JavaDoc name = Lenka.class.getName().replace('.', '/') + ".class";
87         ls = (ManifestSection.LoaderSection)ManifestSection.create(name, at, null);
88         LoaderPoolNode.add(ls);
89         
90         loader = (Lenka) Lenka.getLoader(Lenka.class);
91         
92         mimeResolvers = Lookup.getDefault().lookupResult(MIMEResolver.class);
93         mimeResolvers.addLookupListener(this);
94     }
95
96     protected void tearDown() throws Exception JavaDoc {
97         LoaderPoolNode.remove(loader);
98     }
99
100     public void testNewResolverShallInfluenceExistingDataObjects() throws Exception JavaDoc {
101         DataObject old = DataObject.find(fo);
102         if (old.getLoader() == loader) {
103             fail("The should be taken be default loader: " + old);
104         }
105         if (old.getClass() == MultiDataObject.class) {
106             fail("The should be taken be default loader: " + old);
107         }
108         
109         assertEquals("No changes in lookup yet", 0, change);
110         
111         err.log("starting to create the resolver");
112         FileObject res = FileUtil.createData(
113             Repository.getDefault().getDefaultFileSystem().getRoot(),
114             "Services/MIMEResolver/Lenkaresolver.xml"
115         );
116         err.log("file created: " + res);
117         org.openide.filesystems.FileLock l = res.lock();
118         OutputStream JavaDoc os = res.getOutputStream(l);
119         err.log("stream opened");
120         PrintStream JavaDoc ps = new PrintStream JavaDoc(os);
121         
122         ps.println("<?xml version='1.0' encoding='UTF-8'?>");
123         ps.println("<!DOCTYPE MIME-resolver PUBLIC '-//NetBeans//DTD MIME Resolver 1.0//EN' 'http://www.netbeans.org/dtds/mime-resolver-1_0.dtd'>");
124         ps.println("<MIME-resolver>");
125         ps.println(" <file>");
126         ps.println(" <ext name='lenka'/>");
127         ps.println(" <resolver mime='hodna/lenka'/>");
128         ps.println(" </file>");
129         ps.println("</MIME-resolver>");
130
131         err.log("Content written");
132         os.close();
133         err.log("Stream closed");
134         l.releaseLock();
135         err.log("releaseLock");
136         
137         err.log("Let's query the resolvers");
138         Collection JavaDoc isthere = mimeResolvers.allInstances();
139         err.log("What is the result: " + isthere);
140         assertEquals("resolver found", 1, change);
141         
142         err.log("Waiting till finished");
143         LoaderPoolNode.waitFinished();
144         err.log("Waiting done, querying the data object");
145         
146         err.log("Clear the mime type cache in org.openide.filesystems.MIMESupport: " + fo.getFileSystem().getRoot().getMIMEType());
147         
148         DataObject now = DataObject.find(fo);
149         
150         err.log("Object is here: " + now);
151         assertEquals("Loader updated to lenka (mimetype: " + fo.getMIMEType() + ")", loader, now.getLoader());
152     }
153
154     public void resultChanged(LookupEvent ev) {
155         Logger.global.log(Level.WARNING, null, new Exception JavaDoc("change in lookup"));
156         change++;
157     }
158
159     public void stateChanged(ChangeEvent JavaDoc e) {
160         poolChange++;
161     }
162     
163     public static final class Lenka extends UniFileLoader {
164         public Lenka() {
165             super(MultiDataObject.class.getName());
166         }
167         
168         protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException JavaDoc {
169             return new MultiDataObject(primaryFile, this);
170         }
171
172         protected void initialize() {
173             getExtensions().addMimeType("hodna/lenka");
174             super.initialize();
175         }
176
177         protected FileObject findPrimaryFile(FileObject fo) {
178             err.log("findPrimaryFile: " + fo + " with mime: " + fo.getMIMEType());
179             FileObject retValue;
180             retValue = super.findPrimaryFile(fo);
181             err.log("findPrimaryFile result: " + retValue);
182             return retValue;
183         }
184         
185     }
186 }
187
Popular Tags