1 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 ; 36 37 41 public class ResettableTest extends NbTestCase { 42 43 private static final String MY_NULL = new String ("MY_NULL"); 44 45 public ResettableTest (String name) { 46 super (name); 47 } 48 49 public static void main(String [] args) { 50 TestRunner.run(new NbTestSuite(ResettableTest.class)); 51 } 52 53 protected void setUp () throws Exception { 54 Lookup.getDefault().lookup(ModuleInfo.class); 55 } 56 57 public void testResettable() throws Exception { 58 LocalFileSystem lfs = new LocalFileSystem(); 59 lfs.setRootDirectory(getWorkDir()); 60 61 URL 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 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 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 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 (), ctx.getObject("objectBindingOne", null)); 108 ctx.putObject("objectBindingOne", new String ("arigato")); 109 assertEquals("The value does not match", new String ("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 (), ctx.getObject("objectBindingOne", null)); 122 123 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 |