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.framework;
030:
031: import java.lang.reflect.Field;
032: import java.text.MessageFormat;
033: import java.util.Enumeration;
034: import java.util.ResourceBundle;
035:
036: /**
037: * Tests for the LocalStringKeys class.
038: *
039: * @author Sun Microsystems, Inc.
040: */
041: public class TestLocalStringKeys extends junit.framework.TestCase {
042: /**
043: * The constructor for this testcase, forwards the test name to
044: * the jUnit TestCase base class.
045: * @param aTestName String with the name of this test.
046: */
047: public TestLocalStringKeys(String aTestName) {
048: super (aTestName);
049: }
050:
051: /**
052: * Test the message keys to make sure the class and the properties file
053: * have the same set of keys.
054: * @throws java.util.MissingResourceException when
055: * ResourceBundle.getBundle fails
056: */
057: public void testKeys() throws java.util.MissingResourceException {
058: System.out.println("TestLocalStringKeys.testKeys(): "
059: + "Testing that LocalStringKeys interface and "
060: + "LocalStrings.properties have "
061: + "the same set of keys.");
062:
063: // Load resource bundle
064:
065: String packageName = this .getClass().getPackage().getName();
066: String bundleName = packageName + "."
067: + StringTranslator.RESOURCE_BUNDLE_NAME;
068: ResourceBundle bundle = null;
069: bundle = ResourceBundle.getBundle(bundleName);
070:
071: boolean mismatch = false;
072:
073: // Get a list of all the fields defined in the LocalStringKeys class
074:
075: Field[] keyFields = (LocalStringKeys.class).getDeclaredFields();
076: Field keyField;
077: int numFields = keyFields.length;
078:
079: // Get a list of all the keys defined in the resource bundle
080:
081: Enumeration rbKeys = bundle.getKeys();
082: String rbKey = null;
083: int numKeys = 0;
084:
085: // Verify that all the keys in the resource bundle are defined in the
086: // fields in the LocalStringKeys class
087:
088: while (rbKeys.hasMoreElements()) {
089: rbKey = (String) rbKeys.nextElement();
090: numKeys++;
091: try {
092: keyField = LocalStringKeys.class
093: .getDeclaredField(rbKey);
094: } catch (NoSuchFieldException nsfEx) {
095: System.out.println("Resource bundle key " + rbKey
096: + " is not defined in LocalStringKeys");
097: mismatch = true;
098: }
099: }
100:
101: // Verify that all the fields defined in the LocalStringKeys class
102: // are defined as keys in the resource bundle
103:
104: int i = 0;
105: String str;
106: while (i < numFields) {
107: try {
108: rbKey = keyFields[i].getName();
109: str = bundle.getString(rbKey);
110: } catch (java.util.MissingResourceException mrEx) {
111: System.out.println("LocalStringKeys field " + rbKey
112: + " is not defined in the resource bundle");
113: mismatch = true;
114: }
115: i++;
116: }
117:
118: // Ensure that the bundle and the interface have the same number
119: // of keys and they all match
120:
121: assertFalse("Resource bundle has " + numKeys + " keys; "
122: + "LocalStringKeys interface has " + numFields
123: + " fields", mismatch);
124: }
125:
126: /**
127: * Test the content of the properties file for syntax errors.
128: * @throws java.util.MissingResourceException when
129: * ResourceBundle.getBundle fails
130: */
131: public void testProperties()
132: throws java.util.MissingResourceException {
133: System.out.println("TestLocalStringKeys.testProperties(): "
134: + "Testing that LocalStrings.properties has "
135: + "no syntax errors.");
136:
137: // Load resource bundle
138:
139: String packageName = this .getClass().getPackage().getName();
140: String bundleName = packageName + "."
141: + StringTranslator.RESOURCE_BUNDLE_NAME;
142: ResourceBundle bundle = null;
143: bundle = ResourceBundle.getBundle(bundleName);
144:
145: boolean errors = false;
146:
147: // Get a list of all the keys defined in the resource bundle
148:
149: Enumeration rbKeys = bundle.getKeys();
150:
151: String rbKey = null;
152: String rbMsg = null;
153: String msg = null;
154: int numBad = 0;
155:
156: // Check each property in the resource bundle for syntax errors.
157: // Note: if a MissingResourceException is thrown here, something is
158: // seriously wrong!
159:
160: while (rbKeys.hasMoreElements()) {
161: rbKey = (String) rbKeys.nextElement();
162: try {
163: rbMsg = bundle.getString(rbKey);
164: msg = MessageFormat.format(rbMsg, "AAA", "BBB", "CCC",
165: "DDD", "EEE", "FFF", "GGG", "HHH");
166: } catch (IllegalArgumentException iaEx) {
167: System.out.println("---");
168: System.out.println("Resource bundle property " + rbKey
169: + " has invalid syntax: " + rbMsg);
170: System.out.println("Error message: "
171: + iaEx.getMessage());
172: errors = true;
173: ++numBad;
174: }
175: }
176:
177: // Ensure that there were no syntax errors.
178:
179: assertFalse(
180: "Resource bundle has " + numBad + " syntax errors.",
181: errors);
182: }
183: }
|