KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > settings > DOMConvertorTest


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 2002 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.spi.settings;
21
22 import java.io.IOException JavaDoc;
23 import java.util.logging.Level JavaDoc;
24 import java.util.logging.Logger JavaDoc;
25 import org.netbeans.junit.NbTestCase;
26 import org.openide.filesystems.FileObject;
27
28 import org.openide.filesystems.FileSystem;
29 import org.openide.filesystems.FileUtil;
30 import org.openide.loaders.DataFolder;
31 import org.openide.loaders.DataObject;
32 import org.openide.loaders.InstanceDataObject;
33 import org.openide.modules.ModuleInfo;
34 import org.openide.util.Lookup;
35 import org.w3c.dom.Element JavaDoc;
36 import org.w3c.dom.Node JavaDoc;
37 import org.w3c.dom.NodeList JavaDoc;
38
39 /**
40  *
41  * @author Jan Pokorsky
42  */

43 public class DOMConvertorTest extends NbTestCase {
44     FileSystem fs;
45     
46     /** Creates a new instance of EnvTest */
47     public DOMConvertorTest(String JavaDoc name) {
48         super(name);
49     }
50     
51     protected void setUp() throws Exception JavaDoc {
52         super.setUp();
53         
54         Lookup.getDefault().lookup(ModuleInfo.class);
55         fs = org.openide.filesystems.Repository.getDefault().getDefaultFileSystem();
56     }
57     
58     public void testCreateSetting() throws Exception JavaDoc {
59         try {
60         org.openide.filesystems.FileUtil.createFolder(fs.getRoot(), "testCreateSetting");
61         DataFolder folder = DataFolder.findFolder(fs.findResource("testCreateSetting"));
62         
63         ComposedSetting cs = new ComposedSetting();
64         cs.b1 = new java.awt.Button JavaDoc();
65         cs.b2 = cs.b1;
66         cs.cs = new ComposedSetting();
67         cs.cs.b1 = new java.awt.Button JavaDoc();
68         DataObject dobj = InstanceDataObject.create(folder, "testCreateSetting", cs, null);
69         
70         // test reading
71
FileObject fo = dobj.getPrimaryFile().copy(fs.getRoot(), dobj.getPrimaryFile().getName() + "_copy", "settings");
72         org.openide.cookies.InstanceCookie ic = (org.openide.cookies.InstanceCookie)
73             DataObject.find(fo).getCookie(org.openide.cookies.InstanceCookie.class);
74         assertNotNull("missing InstanceCookie", ic);
75         assertEquals(cs.getClass(), ic.instanceClass());
76         
77         try {
78             ComposedSetting cs2 = (ComposedSetting) ic.instanceCreate();
79             assertEquals(cs2.b1, cs2.b2);
80         } catch (IOException JavaDoc e) {
81             System.err.println("File contents:\n");
82             FileUtil.copy(fo.getInputStream(), System.err);
83             throw e;
84         }
85         } catch (Exception JavaDoc ex) {
86             Logger.global.log(Level.WARNING, null, ex);
87             throw ex;
88         }
89     }
90     
91     public static class ComposedSetting {
92         java.awt.Button JavaDoc b1;
93         java.awt.Button JavaDoc b2;
94         ComposedSetting cs;
95     }
96     
97     public static class ComposedSettingConvertor extends DOMConvertor {
98         private final static String JavaDoc PUBLIC_ID = "-//NetBeans org.netbeans.modules.settings.xtest//DTD ComposedSetting 1.0//EN"; // NOI18N
99
private final static String JavaDoc SYSTEM_ID = "nbres:/org/netbeans/modules/settings/convertors/data/composedsetting-1_0.dtd"; // NOI18N
100
private final static String JavaDoc ELM_COMPOSED_SETTING = "composedsetting"; // NOI18N
101

102         public ComposedSettingConvertor() {
103             super(PUBLIC_ID, SYSTEM_ID, ELM_COMPOSED_SETTING);
104         }
105         
106         protected Object JavaDoc readElement(org.w3c.dom.Element JavaDoc element) throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
107             if (!element.getTagName().equals(ELM_COMPOSED_SETTING)) {
108                 throw new IllegalArgumentException JavaDoc("required element: " +
109                     ELM_COMPOSED_SETTING + ", but was: " + element.getTagName());
110             }
111             
112             // test presence of context
113
Lookup l = findContext(element.getOwnerDocument());
114             if (l == null) throw new NullPointerException JavaDoc("missing context");
115             FileObject fo = (FileObject) l.lookup(FileObject.class);
116             if (fo == null) throw new NullPointerException JavaDoc("missing info about source");
117             
118             ComposedSetting cs = new ComposedSetting();
119             NodeList JavaDoc nl = element.getChildNodes();
120             for (int i = 0, len = nl.getLength(); i < len; i++) {
121                 Node JavaDoc n = nl.item(i);
122                 if (n instanceof Element JavaDoc) {
123                     Object JavaDoc obj = delegateRead((Element JavaDoc) n);
124                     if (obj instanceof java.awt.Button JavaDoc) {
125                         if (cs.b1 == null) {
126                             cs.b1 = (java.awt.Button JavaDoc) obj;
127                         } else {
128                             cs.b2 = (java.awt.Button JavaDoc) obj;
129                         }
130                     } else {
131                         cs.cs = (ComposedSetting) obj;
132                     }
133                 }
134             }
135             return cs;
136         }
137         
138         public void registerSaver(Object JavaDoc inst, Saver s) {
139         }
140         
141         public void unregisterSaver(Object JavaDoc inst, Saver s) {
142         }
143         
144         protected void writeElement(org.w3c.dom.Document JavaDoc doc, org.w3c.dom.Element JavaDoc el, Object JavaDoc inst) throws java.io.IOException JavaDoc, org.w3c.dom.DOMException JavaDoc {
145             if (!(inst instanceof ComposedSetting)) {
146                 throw new IllegalArgumentException JavaDoc("required: " + ComposedSetting.class.getName() + " but was: " + inst.getClass());
147             }
148             ComposedSetting cs = (ComposedSetting) inst;
149             // test CDATA wrapping
150
Element JavaDoc subel;
151             if (cs.b1 != null) {
152                 subel = delegateWrite(doc, cs.b1);
153                 el.appendChild(subel);
154             }
155             if (cs.b2 != null) {
156                 subel = delegateWrite(doc, cs.b2);
157                 el.appendChild(subel);
158             }
159
160             if (cs.cs != null) {
161                 subel = delegateWrite(doc, cs.cs);
162                 el.appendChild(subel);
163             }
164             
165             // test presence of context
166
Lookup l = findContext(doc);
167             if (l == null) throw new NullPointerException JavaDoc("missing context");
168             FileObject fo = (FileObject) l.lookup(FileObject.class);
169             if (fo == null) throw new NullPointerException JavaDoc("missing info about source");
170         }
171         
172     }
173 }
174
Popular Tags