KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > loaders > DefaultSettingsContextTest


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.openide.loaders;
21
22 import javax.naming.Binding JavaDoc;
23 import javax.naming.Context JavaDoc;
24 import javax.naming.InvalidNameException JavaDoc;
25 import javax.naming.Name JavaDoc;
26 import javax.naming.NameAlreadyBoundException JavaDoc;
27 import javax.naming.NamingEnumeration JavaDoc;
28 import junit.textui.TestRunner;
29 import org.netbeans.junit.*;
30 import org.openide.filesystems.FileObject;
31 import org.openide.filesystems.FileSystem;
32
33 /**
34  *
35  * @author Jan Pokorsky
36  */

37 public final class DefaultSettingsContextTest extends NbTestCase {
38
39     private FileSystem lfs;
40     private DataObject dobj;
41     
42     /** Creates a new instance of DefaultSettingsContextTest */
43     public DefaultSettingsContextTest(String JavaDoc name) {
44         super(name);
45     }
46     
47     protected void setUp() throws java.lang.Exception JavaDoc {
48         super.setUp();
49         clearWorkDir();
50         
51         String JavaDoc fsstruct [] = new String JavaDoc [] {
52             "AA/a.test"
53         };
54         
55         TestUtilHid.destroyLocalFileSystem(getName());
56         lfs = TestUtilHid.createLocalFileSystem(getWorkDir(), fsstruct);
57         
58         FileObject fo = lfs.findResource("AA/a.test");
59         assertNotNull("file not found", fo);
60         dobj = DataObject.find(fo);
61         
62     }
63     
64     public void testOperations() throws Exception JavaDoc {
65         Context JavaDoc ctx = new DefaultSettingsContext(dobj);
66         String JavaDoc val = "attrVal";
67         String JavaDoc name = "attrName";
68         
69         // bind
70
ctx.bind(name, val);
71         assertEquals("lookup bound", val, ctx.lookup(name));
72         assertEquals("lookupLink", val, ctx.lookupLink(name));
73         
74         NameAlreadyBoundException JavaDoc nabex = null;
75         try {
76             ctx.bind(name, val);
77         } catch (NameAlreadyBoundException JavaDoc ex) {
78             nabex = ex;
79         }
80         assertNotNull("bind with the same name", nabex);
81         
82         // rebind
83
String JavaDoc newVal = "attrNewVal";
84         String JavaDoc newName = "attrNewName";
85         ctx.rebind(name, val);
86         assertEquals("lookup orig", val, ctx.lookup(name));
87         ctx.rebind(name, newVal);
88         assertEquals("lookup rebound", newVal, ctx.lookup(name));
89         ctx.rebind(name, val);
90         assertEquals("lookup rebound", val, ctx.lookup(name));
91         
92         // rename
93
ctx.rename(name, newName);
94         assertEquals("lookup old name", null, ctx.lookup(name));
95         assertEquals("lookup renamed", val, ctx.lookup(newName));
96         
97         ctx.bind(name, val);
98         assertEquals("lookup bound", val, ctx.lookup(name));
99         nabex = null;
100         try {
101             ctx.rename(newName, name);
102         } catch (NameAlreadyBoundException JavaDoc ex) {
103             nabex = ex;
104         }
105         assertNotNull("rename to existing name", nabex);
106         
107         // unbind
108
ctx.unbind(newName);
109         assertEquals("lookup unbound", null, ctx.lookup(newName));
110     }
111     
112     public void testListing() throws Exception JavaDoc {
113         Context JavaDoc ctx = new DefaultSettingsContext(dobj);
114         String JavaDoc[] names = new String JavaDoc[] {"attrName1", "attrName2", "attrName3"};
115         String JavaDoc[] vals = new String JavaDoc[] {"attrVal1", "attrVal2", "attrVal3"};
116         
117         NamingEnumeration JavaDoc en = ctx.listBindings(".");
118         assertNotNull(en);
119         assertTrue("listBindings is not empty", !en.hasMore());
120         
121         en = ctx.list(".");
122         assertNotNull(en);
123         assertTrue("list is not empty", !en.hasMore());
124         
125         for (int i = 0; i < names.length; i++) {
126             ctx.bind(names[i], vals[i]);
127         }
128         
129         en = ctx.listBindings(".");
130         assertNotNull(en);
131         java.util.List JavaDoc namesLst = java.util.Arrays.asList(names);
132         java.util.Set JavaDoc binds = new java.util.HashSet JavaDoc();
133         for (int i = 0; i < names.length; i++) {
134             assertTrue("expected attr: " + names[i], en.hasMore());
135             Binding JavaDoc bi = (Binding JavaDoc) en.next();
136             int index = namesLst.indexOf(bi.getName());
137             assertTrue("wrong name: " + bi.getName(), index >= 0);
138             assertEquals("wrong value", vals[index], bi.getObject());
139             binds.add(bi.getName());
140         }
141         assertEquals("wrong set of bindings: " + binds, names.length, binds.size());
142         
143         InvalidNameException JavaDoc iex = null;
144         try {
145             ctx.listBindings("../name");
146         } catch (InvalidNameException JavaDoc ex) {
147             iex = ex;
148         }
149         assertNotNull("name was not invalidated: ../name", iex);
150         
151     }
152     
153     public void testParse() throws Exception JavaDoc {
154         DefaultSettingsContext ctx = new DefaultSettingsContext(dobj);
155         
156         javax.naming.Name JavaDoc n = ctx.parse("attrName");
157         assertNotNull(n);
158         assertEquals("size", 1, n.size());
159         String JavaDoc name = getRelativeName(ctx, n);
160         assertEquals("size", "attrName", name);
161         
162         n = ctx.parse("././attrName");
163         assertNotNull(n);
164         assertEquals("size", 3, n.size());
165         name = getRelativeName(ctx, n);
166         assertEquals("size", "attrName", name);
167         
168         n = ctx.parse("../attrName");
169         assertNotNull(n);
170         assertEquals("size", 2, n.size());
171         InvalidNameException JavaDoc iex = null;
172         try {
173             name = getRelativeName(ctx, n);
174         } catch (InvalidNameException JavaDoc ex) {
175             iex = ex;
176         }
177         assertNotNull("name was not invalidated: ../attrName", iex);
178     }
179     
180     public void testEnvironmentFind() {
181         Context JavaDoc ctx = Environment.findSettingsContext(dobj);
182         assertNotNull("missing default impl", ctx);
183         assertEquals("unknown impl", DefaultSettingsContext.class, ctx.getClass());
184     }
185     
186     private String JavaDoc getRelativeName(DefaultSettingsContext ctx, Name JavaDoc name) throws Exception JavaDoc {
187         java.lang.reflect.Method JavaDoc m = ctx.getClass().getDeclaredMethod(
188             "getRelativeName", new Class JavaDoc[] {Name JavaDoc.class});
189         try {
190             m.setAccessible(true);
191             return (String JavaDoc) m.invoke(ctx, new Object JavaDoc[] {name});
192         } catch (java.lang.reflect.InvocationTargetException JavaDoc ite) {
193             throw (Exception JavaDoc) ite.getTargetException();
194         }
195     }
196     
197 }
198
Popular Tags