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:
042: package org.netbeans.core.filesystems;
043:
044: import java.io.IOException;
045: import java.io.OutputStream;
046: import java.io.PrintStream;
047: import java.lang.ref.Reference;
048: import java.lang.ref.WeakReference;
049: import java.util.Collection;
050: import java.util.jar.Attributes;
051: import java.util.logging.Level;
052: import java.util.logging.Logger;
053: import javax.swing.event.ChangeEvent;
054: import javax.swing.event.ChangeListener;
055: import org.netbeans.core.LoaderPoolNode;
056: import org.netbeans.core.startup.ManifestSection;
057: import org.openide.ErrorManager;
058: import org.openide.cookies.OpenCookie;
059: import org.openide.filesystems.FileObject;
060: import org.openide.filesystems.FileUtil;
061: import org.openide.filesystems.LocalFileSystem;
062: import org.openide.filesystems.MIMEResolver;
063: import org.openide.filesystems.Repository;
064: import org.openide.loaders.DataLoaderPool;
065: import org.openide.loaders.DataObject;
066: import org.openide.loaders.DataObjectExistsException;
067: import org.openide.loaders.MultiDataObject;
068: import org.openide.loaders.UniFileLoader;
069: import org.openide.util.Lookup;
070: import org.openide.util.LookupEvent;
071: import org.openide.util.LookupListener;
072:
073: /** Checking the behaviour of entity resolvers.
074: *
075: * @author Jaroslav Tulach
076: */
077: public class FileEntityResolverTest extends
078: org.netbeans.core.LoggingTestCaseHid implements LookupListener,
079: ChangeListener {
080: private FileObject fo;
081: private Lenka loader;
082: private ManifestSection.LoaderSection ls;
083: private Lookup.Result mimeResolvers;
084: private int change;
085: private int poolChange;
086: private static ErrorManager err;
087:
088: public FileEntityResolverTest(String testName) {
089: super (testName);
090: }
091:
092: protected void setUp() throws Exception {
093: clearWorkDir();
094:
095: err = ErrorManager.getDefault()
096: .getInstance("TEST-" + getName());
097:
098: DataLoaderPool.getDefault().addChangeListener(this );
099:
100: org.netbeans.core.startup.Main.getModuleSystem();
101:
102: Thread.sleep(2000);
103:
104: assertEquals(
105: "No change in pool during initialization of module system",
106: 0, poolChange);
107:
108: LocalFileSystem lfs = new LocalFileSystem();
109: lfs.setRootDirectory(getWorkDir());
110:
111: fo = FileUtil.createData(lfs.getRoot(), "X.lenka");
112:
113: Attributes at = new Attributes();
114: at.putValue("OpenIDE-Module-Class", "Loader");
115: String name = Lenka.class.getName().replace('.', '/')
116: + ".class";
117: ls = (ManifestSection.LoaderSection) ManifestSection.create(
118: name, at, null);
119: LoaderPoolNode.add(ls);
120:
121: loader = (Lenka) Lenka.getLoader(Lenka.class);
122:
123: mimeResolvers = Lookup.getDefault().lookupResult(
124: MIMEResolver.class);
125: mimeResolvers.addLookupListener(this );
126: }
127:
128: protected void tearDown() throws Exception {
129: LoaderPoolNode.remove(loader);
130: }
131:
132: public void testNewResolverShallInfluenceExistingDataObjects()
133: throws Exception {
134: DataObject old = DataObject.find(fo);
135: if (old.getLoader() == loader) {
136: fail("The should be taken be default loader: " + old);
137: }
138: if (old.getClass() == MultiDataObject.class) {
139: fail("The should be taken be default loader: " + old);
140: }
141:
142: assertEquals("No changes in lookup yet", 0, change);
143:
144: err.log("starting to create the resolver");
145: FileObject res = FileUtil.createData(Repository.getDefault()
146: .getDefaultFileSystem().getRoot(),
147: "Services/MIMEResolver/Lenkaresolver.xml");
148: err.log("file created: " + res);
149: org.openide.filesystems.FileLock l = res.lock();
150: OutputStream os = res.getOutputStream(l);
151: err.log("stream opened");
152: PrintStream ps = new PrintStream(os);
153:
154: ps.println("<?xml version='1.0' encoding='UTF-8'?>");
155: ps
156: .println("<!DOCTYPE MIME-resolver PUBLIC '-//NetBeans//DTD MIME Resolver 1.0//EN' 'http://www.netbeans.org/dtds/mime-resolver-1_0.dtd'>");
157: ps.println("<MIME-resolver>");
158: ps.println(" <file>");
159: ps.println(" <ext name='lenka'/>");
160: ps.println(" <resolver mime='hodna/lenka'/>");
161: ps.println(" </file>");
162: ps.println("</MIME-resolver>");
163:
164: err.log("Content written");
165: os.close();
166: err.log("Stream closed");
167: l.releaseLock();
168: err.log("releaseLock");
169:
170: err.log("Let's query the resolvers");
171: Collection isthere = mimeResolvers.allInstances();
172: err.log("What is the result: " + isthere);
173: assertEquals("resolver found", 1, change);
174:
175: err.log("Waiting till finished");
176: LoaderPoolNode.waitFinished();
177: err.log("Waiting done, querying the data object");
178:
179: err
180: .log("Clear the mime type cache in org.openide.filesystems.MIMESupport: "
181: + fo.getFileSystem().getRoot().getMIMEType());
182:
183: DataObject now = DataObject.find(fo);
184:
185: err.log("Object is here: " + now);
186: assertEquals("Loader updated to lenka (mimetype: "
187: + fo.getMIMEType() + ")", loader, now.getLoader());
188:
189: {
190: DataObject xml = DataObject.find(res).copy(now.getFolder());
191:
192: Reference<Object> ref = new WeakReference<Object>(xml);
193: xml = null;
194: assertGC("And the copied XML object can disapper", ref);
195:
196: }
197:
198: {
199: Reference<Object> ref = new WeakReference<Object>(now);
200: now = null;
201: assertGC("And the object can disapper", ref);
202: }
203: }
204:
205: public void resultChanged(LookupEvent ev) {
206: Logger.global.log(Level.WARNING, null, new Exception(
207: "change in lookup"));
208: change++;
209: }
210:
211: public void stateChanged(ChangeEvent e) {
212: poolChange++;
213: }
214:
215: public static final class Lenka extends UniFileLoader {
216: public Lenka() {
217: super (MultiDataObject.class.getName());
218: }
219:
220: protected MultiDataObject createMultiObject(
221: FileObject primaryFile)
222: throws DataObjectExistsException, IOException {
223: return new MultiDataObject(primaryFile, this );
224: }
225:
226: protected void initialize() {
227: getExtensions().addMimeType("hodna/lenka");
228: super .initialize();
229: }
230:
231: protected FileObject findPrimaryFile(FileObject fo) {
232: err.log("findPrimaryFile: " + fo + " with mime: "
233: + fo.getMIMEType());
234: FileObject retValue;
235: retValue = super .findPrimaryFile(fo);
236: err.log("findPrimaryFile result: " + retValue);
237: return retValue;
238: }
239:
240: }
241: }
|