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.util;
030:
031: import java.lang.reflect.Field;
032: import java.util.Enumeration;
033: import java.util.ResourceBundle;
034:
035: /**
036: * Tests for the LocalStringKeys class.
037: *
038: * @author Sun Microsystems, Inc.
039: */
040: public class TestLocalStringKeys extends junit.framework.TestCase {
041: /**
042: * The constructor for this testcase, forwards the test name to
043: * the jUnit TestCase base class.
044: * @param aTestName String with the name of this test.
045: */
046: public TestLocalStringKeys(String aTestName) {
047: super (aTestName);
048: }
049:
050: /**
051: * Test the message keys to make sure the class and the properties file
052: * have the same set of keys.
053: * @throws java.util.MissingResourceException when
054: * ResourceBundle.getBundle fails
055: */
056: public void testKeysGood()
057: throws java.util.MissingResourceException {
058: System.out.println("TestLocalStringKeys.testKeysGood(): "
059: + "Testing that LocalStringKeys interface and "
060: + "LocalStrings_<locale>.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: }
|