KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > loaders > XMLDataObjectLifeLock68934Test


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.loaders;
20
21 import java.io.IOException JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.io.PrintStream JavaDoc;
24 import org.openide.ErrorManager;
25 import org.openide.filesystems.FileObject;
26 import org.openide.filesystems.FileUtil;
27 import org.openide.filesystems.Repository;
28 import org.openide.loaders.Environment.Provider;
29 import org.openide.nodes.Node;
30 import org.openide.util.Lookup;
31 import org.openide.util.RequestProcessor;
32 import org.openide.util.lookup.Lookups;
33 import org.xml.sax.SAXException JavaDoc;
34
35 /** There was a problem for objects queried twice from inside the XML parsing and
36  * lookup preparation.
37  *
38  * @author Jaroslav Tulach
39  */

40 public class XMLDataObjectLifeLock68934Test extends LoggingTestCaseHid
41 implements Node.Cookie {
42
43     private ErrorManager err;
44     
45     public XMLDataObjectLifeLock68934Test(String JavaDoc s) {
46         super(s);
47     }
48     protected void setUp() throws Exception JavaDoc {
49         clearWorkDir();
50         
51         err = ErrorManager.getDefault().getInstance("TEST-" + getName());
52     }
53     
54     public void testQueryFromInsideTheQuery() throws IOException JavaDoc {
55         FileObject res = FileUtil.createData(
56             Repository.getDefault().getDefaultFileSystem().getRoot(),
57             getName() + "/R.xml"
58         );
59         
60         err.log("file created: " + res);
61         org.openide.filesystems.FileLock l = res.lock();
62         OutputStream JavaDoc os = res.getOutputStream(l);
63         err.log("stream opened");
64         PrintStream JavaDoc ps = new PrintStream JavaDoc(os);
65         
66         ps.println("<?xml version='1.0' encoding='UTF-8'?>");
67         ps.println("<!DOCTYPE MIME-resolver PUBLIC '-//NetBeans//DTD MIME Resolver 1.0//EN' 'http://www.netbeans.org/dtds/mime-resolver-1_0.dtd'>");
68         ps.println("<MIME-resolver>");
69         ps.println(" <file>");
70         ps.println(" <ext name='lenka'/>");
71         ps.println(" <resolver mime='hodna/lenka'/>");
72         ps.println(" </file>");
73         ps.println("</MIME-resolver>");
74
75         err.log("Content written");
76         os.close();
77         err.log("Stream closed");
78         l.releaseLock();
79         err.log("releaseLock");
80     
81         
82         final DataObject obj = DataObject.find(res);
83         
84         class EP implements Environment.Provider, Node.Cookie {
85             public EP query = this;
86             
87             public Lookup getEnvironment(DataObject obj) {
88                 
89                 if (query == this) {
90                     query = null;
91                     query = (EP)obj.getCookie(EP.class);
92                 
93                     assertEquals("Right object: ", XMLDataObject.class, obj.getClass());
94                     XMLDataObject xml = (XMLDataObject)obj;
95                     String JavaDoc id = null;
96                     try {
97                         id = xml.getDocument().getDoctype().getPublicId();
98                     } catch (IOException JavaDoc ex) {
99                         ex.printStackTrace();
100                         fail("No exception");
101                     } catch (SAXException JavaDoc ex) {
102                         ex.printStackTrace();
103                         fail("No exception");
104                     }
105                     assertEquals("-//NetBeans//DTD MIME Resolver 1.0//EN", id);
106                 }
107                 
108                 
109                 return Lookups.singleton(this);
110             }
111         };
112         
113         EP environmentProvider = new EP();
114         registerIntoLookup(environmentProvider);
115         
116
117         EP mine = (EP)obj.getCookie(EP.class);
118         
119         assertEquals("provider is objects cookie", environmentProvider, mine);
120         assertEquals("query inside works as well", environmentProvider, environmentProvider.query);
121     }
122     
123         
124 }
125
Popular Tags