001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)TestLocalStringKeys.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.management;
030:
031: import com.sun.jbi.util.StringTranslator;
032:
033: import java.lang.reflect.Field;
034: import java.text.MessageFormat;
035: import java.util.Enumeration;
036: import java.util.ResourceBundle;
037:
038: /**
039: * Tests for the LocalStringKeys class.
040: *
041: * @author Sun Microsystems, Inc.
042: */
043: public class TestLocalStringKeys extends junit.framework.TestCase {
044: /**
045: * The constructor for this testcase, forwards the test name to
046: * the jUnit TestCase base class.
047: * @param aTestName String with the name of this test.
048: */
049: public TestLocalStringKeys(String aTestName) {
050: super (aTestName);
051: }
052:
053: /**
054: * Test the message keys to make sure the class and the properties file
055: * have the same set of keys.
056: * @throws java.util.MissingResourceException when
057: * ResourceBundle.getBundle fails
058: */
059: public void testKeys() throws java.util.MissingResourceException {
060: System.out.println("TestLocalStringKeys.testKeys(): "
061: + "Testing that LocalStringKeys interface and "
062: + "LocalStrings.properties have "
063: + "the same set of keys.");
064:
065: // Load resource bundle
066:
067: String packageName = this .getClass().getPackage().getName();
068: String bundleName = packageName + "."
069: + StringTranslator.RESOURCE_BUNDLE_NAME;
070: ResourceBundle bundle = null;
071: bundle = ResourceBundle.getBundle(bundleName);
072:
073: boolean mismatch = false;
074:
075: // Get a list of all the fields defined in the LocalStringKeys class
076:
077: Field[] keyFields = (LocalStringKeys.class).getDeclaredFields();
078: Field keyField;
079: int numFields = keyFields.length;
080:
081: // Get a list of all the keys defined in the resource bundle
082:
083: Enumeration rbKeys = bundle.getKeys();
084: String rbKey = null;
085: int numKeys = 0;
086:
087: // Verify that all the keys in the resource bundle are defined in the
088: // fields in the LocalStringKeys class
089:
090: while (rbKeys.hasMoreElements()) {
091: rbKey = (String) rbKeys.nextElement();
092: numKeys++;
093: try {
094: keyField = LocalStringKeys.class
095: .getDeclaredField(rbKey);
096: } catch (NoSuchFieldException nsfEx) {
097: System.out.println("Resource bundle key " + rbKey
098: + " is not defined in LocalStringKeys");
099: mismatch = true;
100: }
101: }
102:
103: // Verify that all the fields defined in the LocalStringKeys class
104: // are defined as keys in the resource bundle
105:
106: int i = 0;
107: String str;
108: while (i < numFields) {
109: try {
110: rbKey = keyFields[i].getName();
111: str = bundle.getString(rbKey);
112: } catch (java.util.MissingResourceException mrEx) {
113: System.out.println("LocalStringKeys field " + rbKey
114: + " is not defined in the resource bundle");
115: mismatch = true;
116: }
117: i++;
118: }
119:
120: // Ensure that the bundle and the interface have the same number
121: // of keys and they all match
122:
123: assertFalse("Resource bundle has " + numKeys + " keys; "
124: + "LocalStringKeys interface has " + numFields
125: + " fields", mismatch);
126: }
127:
128: /**
129: * Test the content of the properties file for syntax errors.
130: * @throws java.util.MissingResourceException when
131: * ResourceBundle.getBundle fails
132: */
133: public void testProperties()
134: throws java.util.MissingResourceException {
135: System.out.println("TestLocalStringKeys.testProperties(): "
136: + "Testing that LocalStrings.properties has "
137: + "no syntax errors.");
138:
139: // Load resource bundle
140:
141: String packageName = this .getClass().getPackage().getName();
142: String bundleName = packageName + "."
143: + StringTranslator.RESOURCE_BUNDLE_NAME;
144: ResourceBundle bundle = null;
145: bundle = ResourceBundle.getBundle(bundleName);
146:
147: boolean errors = false;
148:
149: // Get a list of all the keys defined in the resource bundle
150:
151: Enumeration rbKeys = bundle.getKeys();
152:
153: String rbKey = null;
154: String rbMsg = null;
155: String msg = null;
156: int numBad = 0;
157:
158: // Check each property in the resource bundle for syntax errors.
159: // Note: if a MissingResourceException is thrown here, something is
160: // seriously wrong!
161:
162: while (rbKeys.hasMoreElements()) {
163: rbKey = (String) rbKeys.nextElement();
164: try {
165: rbMsg = bundle.getString(rbKey);
166: msg = MessageFormat.format(rbMsg, "AAA", "BBB", "CCC",
167: "DDD", "EEE", "FFF", "GGG", "HHH");
168: } catch (IllegalArgumentException iaEx) {
169: System.out.println("---");
170: System.out.println("Resource bundle property " + rbKey
171: + " has invalid syntax: " + rbMsg);
172: System.out.println("Error message: "
173: + iaEx.getMessage());
174: errors = true;
175: ++numBad;
176: }
177: }
178:
179: // Ensure that there were no syntax errors.
180:
181: assertFalse(
182: "Resource bundle has " + numBad + " syntax errors.",
183: errors);
184: }
185: }
|