KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
27    Note that this test requires resources for testing. These resources
28    are construct4ed from the two files P1 & P2 located in the current
29    directory. If these file names are changed then the corresponding
30    names in this submodules build.xml file should be changed also
31 */

32 import junit.framework.*;
33 import java.util.Properties JavaDoc;
34 import java.util.HashSet JavaDoc;
35 import java.util.Set JavaDoc;
36 import junit.textui.TestRunner;
37 import java.util.ResourceBundle JavaDoc;
38 import java.util.Enumeration JavaDoc;
39 import java.util.List JavaDoc;
40 import java.util.ArrayList JavaDoc;
41 /**
42  *
43  * @author <a HREF="mailto:toby.h.ferguson@sun.com">Toby H Ferguson</a>
44  * @version $Revision: 1.4 $
45  */

46
47 /**
48    Execute these tests using gmake (and Ant) by:
49    cd <framework>
50    gmake ANT_TARGETS=LocalStringsManagerFactoryTest
51 */

52
53 public class LocalStringsManagerFactoryTest extends TestCase {
54       // I used these to experiment with different names/types for the ResourceBundles
55
private static final String JavaDoc P1_PROP = "P1";
56     private static final String JavaDoc P2_PROP = "P2";
57     private static final String JavaDoc PACKAGE = "com.sun.enterprise.cli.framework";
58
59     public void testConstructor(){
60         final LocalStringsManagerFactory lsmf = new LocalStringsManagerFactory();
61     }
62     public void testSetInstance() throws Exception JavaDoc {
63         final LocalStringsManager lsm = new LocalStringsManager(PACKAGE, P1_PROP);
64         LocalStringsManagerFactory.setInstance(PACKAGE, lsm);
65         assertEquals(lsm, LocalStringsManagerFactory.getLocalStringsManager(PACKAGE, P1_PROP));
66     }
67     
68       // requirement: Property values unique to each resource can be retrieved
69
// try to show that unique values in each resource are present
70
public void testMultiplePropertyUniqueValuesMissing() throws Exception JavaDoc{
71         Set JavaDoc hs = new HashSet JavaDoc();
72         hs.add(p1);
73         hs.add(p2);
74         LocalStringsManagerFactory.setCommandLocalStringsManagerProperties(hs.iterator());
75         LocalStringsManager lsm = LocalStringsManagerFactory.getCommandLocalStringsManager();
76         assertEquals("expected b to be from P1", "P1.b", lsm.getString("b"));
77         assertEquals("expected c to be from P2", "P2.c", lsm.getString("c"));
78     }
79     
80   
81       // Requirement: First reference to a property is returned first
82
// Try to show that second property is the one returned.
83
public void testMultiplePropertyLookupOverride() throws Exception JavaDoc{
84         List JavaDoc hs = new ArrayList JavaDoc();
85         hs.add(p1);
86         hs.add(p2);
87
88         LocalStringsManagerFactory.setCommandLocalStringsManagerProperties(hs.iterator());
89         LocalStringsManager lsm = LocalStringsManagerFactory.getCommandLocalStringsManager();
90         assertEquals("expected a to be overridden and come from P1", "P1.a", lsm.getString("a"));
91     }
92     
93       // try to find that a single property file cannot be properly
94
// handled using the iterator initialization
95
public void testP1Lookup() throws Exception JavaDoc {
96         Set JavaDoc hs = new HashSet JavaDoc();
97         hs.add(p1);
98         LocalStringsManagerFactory.setCommandLocalStringsManagerProperties(hs.iterator());
99         LocalStringsManager lsm = LocalStringsManagerFactory.getCommandLocalStringsManager();
100         assertEquals("P1.a", lsm.getString("a"));
101         assertEquals("P1.b", lsm.getString("b"));
102     }
103
104     public void testP2Lookup() throws Exception JavaDoc {
105         Set JavaDoc hs = new HashSet JavaDoc();
106         hs.add(p2);
107         LocalStringsManagerFactory.setCommandLocalStringsManagerProperties(hs.iterator());
108         LocalStringsManager lsm = LocalStringsManagerFactory.getCommandLocalStringsManager();
109         assertEquals("P2.a", lsm.getString("a"));
110         assertEquals("P2.c", lsm.getString("c"));
111     
112     }
113
114     public LocalStringsManagerFactoryTest(String JavaDoc name){
115         super(name);
116     }
117
118     Properties JavaDoc p1;
119     Properties JavaDoc p2;
120   
121     protected void setUp() {
122         p1 = new Properties JavaDoc();
123         p2 = new Properties JavaDoc();
124         initProperty(p1, P1_PROP);
125         initProperty(p2, P2_PROP);
126     }
127   
128     void initProperty(Properties JavaDoc prop, String JavaDoc file){
129         prop.setProperty("base-package", PACKAGE);
130         prop.setProperty("property-file-name", file);
131     }
132   
133
134     protected void tearDown() {
135     }
136
137     private void nyi(){
138         fail("Not Yet Implemented");
139     }
140
141     public static Test suite(){
142         TestSuite suite = new TestSuite(LocalStringsManagerFactoryTest.class);
143         return suite;
144     }
145
146     public static void main(String JavaDoc args[]) throws Exception JavaDoc {
147         final TestRunner runner= new TestRunner();
148         final TestResult result = runner.doRun(LocalStringsManagerFactoryTest.suite(), false);
149         System.exit(result.errorCount() + result.failureCount());
150     }
151 }
152
153
Popular Tags