KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > registry > ConvertedObjectTest


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
20 package org.netbeans.core.registry;
21
22 import junit.textui.TestRunner;
23 import org.netbeans.api.registry.Context;
24 import org.netbeans.api.registry.fs.FileSystemContextFactory;
25 import org.netbeans.core.registry.convertors.TestBean;
26 import org.netbeans.core.registry.convertors.TestBeanConvertor;
27 import org.netbeans.junit.NbTestCase;
28 import org.netbeans.junit.NbTestSuite;
29 import org.netbeans.spi.registry.BasicContext;
30 import org.netbeans.spi.registry.SpiUtils;
31 import org.openide.filesystems.*;
32 import org.openide.xml.XMLUtil;
33 import org.w3c.dom.Document JavaDoc;
34 import org.w3c.dom.Element JavaDoc;
35 import org.xml.sax.InputSource JavaDoc;
36
37 import java.io.InputStream JavaDoc;
38 import java.net.URL JavaDoc;
39 import java.util.Arrays JavaDoc;
40 import java.util.Collections JavaDoc;
41 import java.util.List JavaDoc;
42
43 /**
44  * Test usage of Convertors API with object bindings.
45  * @author Jesse Glick
46  */

47 public class ConvertedObjectTest extends NbTestCase {
48     
49     public ConvertedObjectTest(String JavaDoc name) {
50         super(name);
51     }
52     
53     public static void main(String JavaDoc[] args) {
54         TestRunner.run(new NbTestSuite(ConvertedObjectTest.class));
55     }
56     
57     private Context c;
58     private FileSystem fs;
59     
60     protected void setUp() throws Exception JavaDoc {
61         super.setUp();
62         URL JavaDoc u = ConvertedObjectTest.class.getResource("data/object-layer.xml");
63         FileSystem xfs = new XMLFileSystem(u);
64         LocalFileSystem lfs = new LocalFileSystem();
65         clearWorkDir();
66         lfs.setRootDirectory(getWorkDir());
67         fs = new MultiFileSystem(new FileSystem[] {lfs, xfs});
68         BasicContext rootCtx = FileSystemContextFactory.createContext(fs.getRoot());
69         Context root = SpiUtils.createContext(rootCtx);
70         c = root.createSubcontext("test-context");
71     }
72     /*
73     protected void tearDown() throws Exception {
74     }
75      */

76
77     /** Check that read operations work. */
78     public void testRead() throws Exception JavaDoc {
79         TestBean t1 = (TestBean)c.getObject("binding1", null);
80         assertNotNull(t1);
81         assertEquals("val1", t1.getProp1());
82         assertEquals("val2", t1.getProp2());
83         TestBean t2 = (TestBean)c.getObject("binding2", null);
84         assertNotNull(t1);
85         assertEquals("val3", t2.getProp1());
86         assertEquals("val4", t2.getProp2());
87         List JavaDoc l = c.getOrderedNames();
88         Collections.sort(l);
89         assertEquals(Arrays.asList(new String JavaDoc[] {"binding1", "binding2"}), l);
90     }
91     
92     /** Check that writing new values works. */
93     public void testWriteNew() throws Exception JavaDoc {
94         TestBean t = new TestBean();
95         t.setProp1("gen1");
96         t.setProp2("gen2");
97         c.putObject("binding3", t);
98         FileObject fo = fs.findResource("test-context/binding3.xml");
99         assertNotNull(fo);
100         InputStream JavaDoc is = fo.getInputStream();
101         try {
102             Document JavaDoc d = XMLUtil.parse(new InputSource JavaDoc(is), false, true, null, null);
103             Element JavaDoc e = d.getDocumentElement();
104             assertEquals(TestBeanConvertor.NS, e.getNamespaceURI());
105             assertEquals("test-bean", e.getLocalName());
106             assertEquals("gen1", e.getAttribute("prop1"));
107             assertEquals("gen2", e.getAttribute("prop2"));
108         } finally {
109             is.close();
110         }
111     }
112     
113     /** Test that overwriting old values works too. */
114     public void testOverwrite() throws Exception JavaDoc {
115         TestBean t = (TestBean)c.getObject("binding1", null);
116         assertNotNull(t);
117         assertEquals("val1", t.getProp1());
118         assertEquals("val2", t.getProp2());
119         t.setProp1("val1-modified");
120         c.putObject("binding1", t);
121         assertEquals(t, c.getObject("binding1", null));
122         FileObject fo = fs.findResource("test-context/binding1.xml");
123         assertNotNull(fo);
124         InputStream JavaDoc is = fo.getInputStream();
125         try {
126             Document JavaDoc d = XMLUtil.parse(new InputSource JavaDoc(is), false, true, null, null);
127             Element JavaDoc e = d.getDocumentElement();
128             assertEquals(TestBeanConvertor.NS, e.getNamespaceURI());
129             assertEquals("test-bean", e.getLocalName());
130             assertEquals("val1-modified", e.getAttribute("prop1"));
131             assertEquals("val2", e.getAttribute("prop2"));
132         } finally {
133             is.close();
134         }
135     }
136     
137 }
138
Popular Tags