KickJava   Java API By Example, From Geeks To Geeks.

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


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.junit.NbTestCase;
25 import org.netbeans.junit.NbTestSuite;
26 import org.netbeans.spi.registry.BasicContext;
27 import org.netbeans.spi.registry.SpiUtils;
28 import org.openide.filesystems.FileSystem;
29 import org.openide.filesystems.LocalFileSystem;
30 import org.openide.filesystems.XMLFileSystem;
31 import org.openide.modules.ModuleInfo;
32 import org.openide.util.Lookup;
33
34 import javax.swing.*;
35 import java.net.URL JavaDoc;
36
37 /**
38  *
39  * @author David Konecny
40  */

41 public class ResettableTest extends NbTestCase {
42     
43     private static final String JavaDoc MY_NULL = new String JavaDoc("MY_NULL");
44
45     public ResettableTest (String JavaDoc name) {
46         super (name);
47     }
48     
49     public static void main(String JavaDoc[] args) {
50         TestRunner.run(new NbTestSuite(ResettableTest.class));
51     }
52     
53     protected void setUp () throws Exception JavaDoc {
54         Lookup.getDefault().lookup(ModuleInfo.class);
55     }
56     
57     public void testResettable() throws Exception JavaDoc {
58         LocalFileSystem lfs = new LocalFileSystem();
59         lfs.setRootDirectory(getWorkDir());
60
61         URL JavaDoc u1 = getClass().getResource("data/layer_defaults.xml");
62                
63         FileSystem xfs1 = new XMLFileSystem( u1 );
64         FileSystem mfs = new TestMFS( new FileSystem[] { lfs, xfs1 } );
65     
66         BasicContext root = new ResettableContextImpl(mfs.getRoot(), null, xfs1, lfs);
67         Context rootContext = SpiUtils.createContext(root);
68
69         
70         Context ctx = rootContext.createSubcontext("ah/oy");
71         
72         String JavaDoc bindingName = "myB";
73         ctx.putInt(bindingName, 123);
74         assertTrue("The binding has default value", ctx.hasDefault(bindingName) == false);
75         assertTrue("The binding is not modified", ctx.isModified(bindingName) == true);
76         ctx.revert(bindingName);
77         assertTrue("The value was not reverted", ctx.getInt(bindingName, 99) == 99);
78         
79         bindingName = "myBBB";
80         ctx.putObject(bindingName, new JLabel("arigato"));
81         assertTrue("The binding has default value", ctx.hasDefault(bindingName) == false);
82         assertTrue("The binding is not modified", ctx.isModified(bindingName) == true);
83         ctx.revert(bindingName);
84         assertTrue("The value was not reverted", ctx.getObject(bindingName, null) == null);
85         
86         // make context dirty
87
ctx.setAttribute(null, "smthA", "sayonara");
88         ctx.createSubcontext("smthC");
89         ctx.putInt("smthB", 123);
90         
91         assertTrue("The context has default value", ctx.hasDefault(null) == false);
92         assertTrue("The context is not modified", ctx.isModified(null) == true);
93         ctx.revert(null);
94         assertTrue("The context was not reverted - "+ctx.getBindingNames()+ctx.getSubcontextNames()+
95             ctx.getAttributeNames(null), (ctx.getBindingNames().size() == 0) &&
96             (ctx.getAttributeNames(null).size() == 1 && ctx.getAttributeNames(null).iterator().next().equals("default.context.sorting"))
97             && (ctx.getSubcontextNames().size() == 0));
98         
99         rootContext.destroySubcontext("ah");
100         
101         ctx = rootContext.getSubcontext("contextZ");
102         
103         // test object binding
104
assertTrue("The binding does not have default value", ctx.hasDefault("objectBindingOne") == true);
105         assertTrue("The binding is modified", ctx.isModified("objectBindingOne") == false);
106         
107         assertEquals("The value does not match", new String JavaDoc(), ctx.getObject("objectBindingOne", null));
108         ctx.putObject("objectBindingOne", new String JavaDoc("arigato"));
109         assertEquals("The value does not match", new String JavaDoc("arigato"), ctx.getObject("objectBindingOne", null));
110         assertTrue("The binding does not have default value", ctx.hasDefault("objectBindingOne") == true);
111         assertTrue("The binding is not modified", ctx.isModified("objectBindingOne") == true);
112
113         ctx.putObject("objectBindingOne", null);
114         assertTrue("The binding does not have default value", ctx.hasDefault("objectBindingOne") == true);
115         assertTrue("The binding is not modified", ctx.isModified("objectBindingOne") == true);
116         assertEquals("The value does not match", null, ctx.getObject("objectBindingOne", null));
117         
118         ctx.revert("objectBindingOne");
119         assertTrue("The binding does not have default value", ctx.hasDefault("objectBindingOne") == true);
120         assertTrue("The binding is not modified", ctx.isModified("objectBindingOne") == false);
121         assertEquals("The value does not match", new String JavaDoc(), ctx.getObject("objectBindingOne", null));
122         
123         // test primitive binding
124
assertTrue("The binding does not have default value", ctx.hasDefault("primitiveBindingOne") == true);
125         assertTrue("The binding is modified", ctx.isModified("primitiveBindingOne") == false);
126         
127         assertEquals("The value does not match", "primitiveBindingOneValue", ctx.getString("primitiveBindingOne", "nono"));
128         ctx.putString("primitiveBindingOne", "new value");
129         assertEquals("The value does not match", "new value", ctx.getString("primitiveBindingOne", "nono"));
130         assertTrue("The binding does not have default value", ctx.hasDefault("primitiveBindingOne") == true);
131         assertTrue("The binding is not modified", ctx.isModified("primitiveBindingOne") == true);
132
133         ctx.putObject("primitiveBindingOne", null);
134         assertTrue("The binding does not have default value", ctx.hasDefault("primitiveBindingOne") == true);
135         assertTrue("The binding is not modified", ctx.isModified("primitiveBindingOne") == true);
136         assertEquals("The value does not match", null, ctx.getString("primitiveBindingOne", null));
137         
138         ctx.revert("primitiveBindingOne");
139         assertTrue("The binding does not have default value", ctx.hasDefault("primitiveBindingOne") == true);
140         assertTrue("The binding is not modified", ctx.isModified("primitiveBindingOne") == false);
141         assertEquals("The value does not match", "primitiveBindingOneValue", ctx.getString("primitiveBindingOne", "nono"));
142         
143     }
144     
145 }
146
Popular Tags