KickJava   Java API By Example, From Geeks To Geeks.

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


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 /** Ensuring that getCookie really works.
36  *
37  * @author Jaroslav Tulach
38  */

39 public class XMLDataObjectGetCookieTest extends LoggingTestCaseHid
40 implements Node.Cookie {
41
42     private ErrorManager err;
43     
44     public XMLDataObjectGetCookieTest(String JavaDoc s) {
45         super(s);
46     }
47     protected void setUp() throws Exception JavaDoc {
48         clearWorkDir();
49         
50         err = ErrorManager.getDefault().getInstance("TEST-" + getName());
51         
52         registerIntoLookup(ENV);
53     }
54     
55     public void testGetTheLookupWhileWaitingBeforeAssigningIt() throws IOException JavaDoc {
56         doTest(
57             "THREAD:t2 MSG:parsedId set to NULL" +
58             "THREAD:t1 MSG:Has already been parsed.*" +
59             "THREAD:t1 MSG:Query for class org.openide.loaders.XMLDataObjectGetCookieTest"
60         );
61     }
62
63     public void testGetTheLookupWhileWaitingAfterParsing() throws IOException JavaDoc {
64         doTest(
65             "THREAD:t1 MSG:New id.*" +
66             "THREAD:t2 MSG:Going to read parseId.*"
67         );
68     }
69     
70     private void doTest(String JavaDoc switches) throws IOException JavaDoc {
71         FileObject res = FileUtil.createData(
72             Repository.getDefault().getDefaultFileSystem().getRoot(),
73             getName() + "/R.xml"
74         );
75         
76         err.log("file created: " + res);
77         org.openide.filesystems.FileLock l = res.lock();
78         OutputStream JavaDoc os = res.getOutputStream(l);
79         err.log("stream opened");
80         PrintStream JavaDoc ps = new PrintStream JavaDoc(os);
81         
82         ps.println("<?xml version='1.0' encoding='UTF-8'?>");
83         ps.println("<!DOCTYPE MIME-resolver PUBLIC '-//NetBeans//DTD MIME Resolver 1.0//EN' 'http://www.netbeans.org/dtds/mime-resolver-1_0.dtd'>");
84         ps.println("<MIME-resolver>");
85         ps.println(" <file>");
86         ps.println(" <ext name='lenka'/>");
87         ps.println(" <resolver mime='hodna/lenka'/>");
88         ps.println(" </file>");
89         ps.println("</MIME-resolver>");
90
91         err.log("Content written");
92         os.close();
93         err.log("Stream closed");
94         l.releaseLock();
95         err.log("releaseLock");
96     
97         
98         final DataObject obj = DataObject.find(res);
99         
100         class Run implements Runnable JavaDoc {
101             public EP cookie;
102             
103             public void run () {
104                 cookie = (EP) obj.getCookie(EP.class);
105             }
106         }
107         
108         Run r1 = new Run();
109         Run r2 = new Run();
110         
111         
112         registerSwitches(switches, 200);
113         
114         RequestProcessor.Task t1 = new RequestProcessor("t1").post(r1);
115         RequestProcessor.Task t2 = new RequestProcessor("t2").post(r2);
116         
117         t1.waitFinished();
118         t2.waitFinished();
119         
120         if (r1.cookie == null && r2.cookie == null) {
121             fail("Both cookies are null");
122         }
123         
124         assertEquals("First result is ok", ENV, r1.cookie);
125         assertEquals("Second result is ok", ENV, r2.cookie);
126     }
127     
128     
129     
130     private static Object JavaDoc ENV = new EP();
131         
132     private static final class EP implements Environment.Provider, Node.Cookie {
133         public Lookup getEnvironment(DataObject obj) {
134             assertEquals("Right object: ", XMLDataObject.class, obj.getClass());
135             XMLDataObject xml = (XMLDataObject)obj;
136             String JavaDoc id = null;
137             try {
138                 id = xml.getDocument().getDoctype().getPublicId();
139             } catch (IOException JavaDoc ex) {
140                 ex.printStackTrace();
141                 fail("No exception");
142             } catch (SAXException JavaDoc ex) {
143                 ex.printStackTrace();
144                 fail("No exception");
145             }
146             assertEquals("-//NetBeans//DTD MIME Resolver 1.0//EN", id);
147             return Lookups.singleton(this);
148         }
149     };
150 }
151
Popular Tags