KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > cli > framework > GlobalsManagerTest


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.cli.framework;
25
26 import junit.framework.*;
27 import java.util.ResourceBundle JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Locale JavaDoc;
31
32 /**
33  *
34  * @author <a HREF="mailto:toby.h.ferguson@sun.com">Toby H Ferguson</a>
35  * @version $Revision: 1.5 $
36  */

37
38 public class GlobalsManagerTest extends TestCase {
39       // For test purposes there're two properties files, one in this
40
// directory (and package) and one in the directory above. The
41
// one in this directory is for the framework package, the one
42
// in the directory above is for the command package.
43
private static final String JavaDoc getCommandPackageName(){
44         return "com.sun.enterprise.cli";
45     }
46     
47     public void testCommandStringLocalization() throws Exception JavaDoc {
48         final ICommandEnvironment env = new Env();
49         final GlobalsManager gm = new GlobalsManager(env);
50         GlobalsManager.setInstance(gm);
51         GlobalsManager.setBasePackage(getCommandPackageName());
52         GlobalsManager.setPropertyFile("CommandPropertyFile");
53         assertEquals("command property", GlobalsManager.getString("a"));
54         assertEquals("command property", GlobalsManager.getString("a", (Object JavaDoc []) null));
55         GlobalsManager.setBasePackage((String JavaDoc) null);
56     }
57         
58     public void testCommandStringLocalizationNoBundle() {
59         final ICommandEnvironment env = new Env();
60         final GlobalsManager gm = new GlobalsManager(env);
61         GlobalsManager.setInstance(gm);
62         try {
63             assertEquals("", GlobalsManager.getString("a"));
64             fail("Expected error indicating no base package could be found");
65         }
66         catch (CommandException ce){
67 // assertEquals("Can't find bundle for base name null.CommandPropertyFile, locale en_US", ce.getMessage());
68
assertEquals("Can't find bundle for base name null.CommandPropertyFile, locale "+Locale.getDefault(), ce.getMessage());
69             assertNull(ce.getCause());
70         }
71     }
72
73     public void testFrameworkStringLocalization() throws Exception JavaDoc {
74         final ICommandEnvironment env = new Env();
75         final GlobalsManager gm = new GlobalsManager(env);
76         GlobalsManager.setInstance(gm);
77         assertEquals("framework property", GlobalsManager.getFrameworkString("a", (Object JavaDoc []) null));
78         assertEquals("framework property", GlobalsManager.getFrameworkString("a"));
79     }
80     
81     
82         
83     public void testEnvironment(){
84         final ICommandEnvironment env = new Env();
85         final GlobalsManager gm = new GlobalsManager(env);
86         final ICommandEnvironment env2 = new Env();
87         gm.setGlobalsEnv(env2);
88         assertEquals(env2, gm.getGlobalsEnv());
89     }
90     
91     public void testSingletonMethods(){
92         final ICommandEnvironment env = new Env();
93         final GlobalsManager gm = new GlobalsManager(env);
94         GlobalsManager.setInstance(gm);
95         assertEquals(gm, GlobalsManager.getInstance());
96     }
97     
98     public void testGetSetRemove() {
99         final ICommandEnvironment env = new Env();
100         final GlobalsManager gm = new GlobalsManager(env);
101         gm.setOption("key", "value");
102         assertEquals("value", gm.getOption("key"));
103         gm.removeOption("key");
104         assertNull(gm.getOption("key"));
105     }
106     
107         
108     public void testSimpleConstructionAndObservers() {
109         final ICommandEnvironment env = new Env();
110         final GlobalsManager gm = new GlobalsManager(env);
111         assertEquals(env, gm.getGlobalsEnv());
112     }
113
114     public GlobalsManagerTest(String JavaDoc name){
115         super(name);
116     }
117
118     protected void setUp() {
119     }
120
121     protected void tearDown() {
122     }
123
124     private void nyi(){
125         fail("Not Yet Implemented");
126     }
127
128     public static void main(String JavaDoc args[]){
129         if (args.length == 0){
130             junit.textui.TestRunner.run(GlobalsManagerTest.class);
131         } else {
132             junit.textui.TestRunner.run(makeSuite(args));
133         }
134     }
135     private static TestSuite makeSuite(String JavaDoc args[]){
136         final TestSuite ts = new TestSuite();
137         for (int i = 0; i < args.length; i++){
138             ts.addTest(new GlobalsManagerTest(args[i]));
139         }
140         return ts;
141     }
142
143     private class Env implements ICommandEnvironment
144     {
145         HashMap JavaDoc h = new HashMap JavaDoc();
146         
147         public void setEnvironment(String JavaDoc name, String JavaDoc value){
148             h.put(name, value);
149         }
150         
151         public Object JavaDoc removeEnvironment( String JavaDoc name ){
152             final Object JavaDoc result = h.get(name);
153             h.remove(name);
154             return result;
155         }
156         
157         public HashMap JavaDoc getEnvironments(){
158             return new HashMap JavaDoc(h);
159         }
160         
161         public Object JavaDoc getEnvironmentValue(String JavaDoc key){
162             return h.get(key);
163         }
164         
165         public String JavaDoc toString(){
166             return h.toString();
167         }
168         
169         public int getNumEnvironments(){
170             return h.size();
171         }
172     }
173     
174 }
175
Popular Tags