KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > registry > enabledisabletest > CompatibilityTest


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.enabledisabletest;
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.TestMFS;
26 import org.netbeans.junit.NbTestCase;
27 import org.netbeans.junit.NbTestSuite;
28 import org.netbeans.spi.registry.SpiUtils;
29 import org.openide.cookies.InstanceCookie;
30 import org.openide.filesystems.FileObject;
31 import org.openide.filesystems.FileSystem;
32 import org.openide.filesystems.LocalFileSystem;
33 import org.openide.filesystems.XMLFileSystem;
34 import org.openide.loaders.DataObject;
35 import org.openide.loaders.FolderLookup;
36 import org.openide.util.Lookup;
37 import org.xml.sax.SAXException JavaDoc;
38
39 import java.beans.PropertyVetoException JavaDoc;
40 import java.io.IOException JavaDoc;
41 import java.net.URL JavaDoc;
42
43 public class CompatibilityTest extends NbTestCase {
44     private static final String JavaDoc LAYER = "data/compatible_layer.xml";
45     private FileSystem mfs;
46
47     private static final String JavaDoc DOC_NAME = "module";
48     private static Context docElementCtx;
49     
50     private static Exception JavaDoc moduleInitExc;
51
52     static {
53         try {
54             ModuleUtils.DEFAULT.install();
55             ModuleUtils.DEFAULT.enableBookModule(true);
56             ModuleUtils.DEFAULT.enableCDModule(true);
57         } catch (Exception JavaDoc e) {
58             moduleInitExc = e;
59         }
60     }
61     
62     public CompatibilityTest(String JavaDoc s) {
63         super(s);
64     }
65     public static void main(String JavaDoc[] args) {
66         TestRunner.run(new NbTestSuite(CompatibilityTest.class));
67     }
68
69     public void testMethodValue1 () throws Exception JavaDoc {
70         getAndTestObject("methodvalue1");
71     }
72
73     public void testMethodValue2 () throws Exception JavaDoc {
74         getAndTestObject("methodvalue2");
75         
76     }
77
78     public void testMethodValue3 () throws Exception JavaDoc {
79         Object JavaDoc book = getAndTestObject("methodvalue3");
80         
81         assertTrue("Author should be Tom. See: " + book.toString(),book.toString().indexOf("Tom") > 0);
82         assertTrue("Title should be Sorry.See: " + book.toString(),book.toString().indexOf("Sorry") > 0);
83     }
84     
85     public void testNewValue1 () throws Exception JavaDoc {
86         getAndTestObject("newvalue1");
87     }
88
89     public void testBookTest () throws Exception JavaDoc {
90         getAndTestObject("bookTest");
91     }
92
93     
94     private Object JavaDoc getAndTestObject(final String JavaDoc bindingName) throws Exception JavaDoc {
95         if (moduleInitExc != null)
96             throw moduleInitExc;
97         Object JavaDoc retVal = getContext().getSubcontext(bindingName).getObject(bindingName, null);
98         assertNotNull(retVal);
99
100         FileObject fileObject = getFileObject(bindingName);
101         DataObject dataObject = DataObject.find(fileObject);
102         assertNotNull(dataObject);
103
104         InstanceCookie cookie = (InstanceCookie)dataObject.getCookie(InstanceCookie.class);
105         assertNotNull(cookie);
106         
107         Object JavaDoc instance = cookie.instanceCreate();
108         assertNotNull(instance);
109
110         Lookup lkp = new FolderLookup(dataObject.getFolder()).getLookup();
111         Object JavaDoc lookupItem = lkp.lookup(instance.getClass());
112         assertNotNull(lookupItem);
113         assertTrue(lookupItem == instance);
114         
115         //TODO: uncomment - partly tests InstanceDataObject, Lookup, Registry coexistence
116
//assertTrue(instance == retVal);
117
//assertTrue(lookupItem == retVal);
118

119         return retVal;
120     }
121
122
123     private Context getContext() throws PropertyVetoException JavaDoc, IOException JavaDoc, SAXException JavaDoc {
124         if (docElementCtx == null) {
125             docElementCtx = SpiUtils.createContext(FileSystemContextFactory.createContext(getFileSystem().getRoot()));
126             docElementCtx = docElementCtx.getSubcontext(DOC_NAME);
127         }
128
129         return docElementCtx;
130     }
131
132     private FileSystem getFileSystem() throws PropertyVetoException JavaDoc, IOException JavaDoc, SAXException JavaDoc {
133         if (mfs == null) {
134             LocalFileSystem lfs = new LocalFileSystem();
135             lfs.setRootDirectory(getWorkDir());
136                 
137             URL JavaDoc u1 = getClass().getResource(LAYER);
138                        
139             FileSystem xfs1 = new XMLFileSystem( u1 );
140             mfs = new TestMFS( new FileSystem[] { lfs, xfs1 } );
141         }
142         return mfs;
143     }
144
145     
146     private FileObject getFileObject(final String JavaDoc bindingName) throws PropertyVetoException JavaDoc, IOException JavaDoc, SAXException JavaDoc {
147         final String JavaDoc absolutePath = getContext().getAbsoluteContextName() + "/" + bindingName + "/" + bindingName;
148         final String JavaDoc [] extensions = new String JavaDoc [] {"settings", "instance", "xml"};
149
150         FileObject fo = null;
151         for (int i = 0; i < extensions.length; i++) {
152             String JavaDoc name = absolutePath + "." + extensions[i];
153             fo = getFileSystem().findResource(name);
154             if (fo != null) break;
155         }
156         
157         assertNotNull(fo);
158         return fo;
159     }
160 }
161
Popular Tags