KickJava   Java API By Example, From Geeks To Geeks.

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


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 XMLDataObjectGetNoCookieTest extends LoggingTestCaseHid
40 implements Node.Cookie {
41
42     private ErrorManager err;
43     
44     public XMLDataObjectGetNoCookieTest(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 testNoDTD() throws IOException JavaDoc {
56         FileObject res = FileUtil.createData(
57             Repository.getDefault().getDefaultFileSystem().getRoot(),
58             getName() + "/R.xml"
59         );
60         
61         err.log("file created: " + res);
62         org.openide.filesystems.FileLock l = res.lock();
63         OutputStream JavaDoc os = res.getOutputStream(l);
64         err.log("stream opened");
65         PrintStream JavaDoc ps = new PrintStream JavaDoc(os);
66         
67         ps.println("<?xml version='1.0' encoding='UTF-8'?>");
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         EP cookie = (EP) obj.getCookie(EP.class);
85
86         assertEquals("But cookie is returned correctly", ENV, cookie);
87     }
88     
89     
90     private static Object JavaDoc ENV = new EP();
91         
92     private static final class EP implements Environment.Provider, Node.Cookie {
93         public Lookup getEnvironment(DataObject obj) {
94             assertEquals("Right object: ", XMLDataObject.class, obj.getClass());
95             XMLDataObject xml = (XMLDataObject)obj;
96             String JavaDoc id = null;
97             try {
98                 id = xml.getDocument().getDoctype().getPublicId();
99             } catch (IOException JavaDoc ex) {
100                 ex.printStackTrace();
101                 fail("No exception");
102             } catch (SAXException JavaDoc ex) {
103                 ex.printStackTrace();
104                 fail("No exception");
105             }
106             assertEquals("No DTD means no ID", null, id);
107             return Lookups.singleton(this);
108         }
109     };
110 }
111
Popular Tags