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.facade;
030:
031: import java.lang.reflect.Field;
032: import java.util.Enumeration;
033: import java.util.ResourceBundle;
034: import com.sun.jbi.management.system.StringTranslator;
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 testKeysGood()
058: throws java.util.MissingResourceException {
059: System.out.println("TestLocalStringKeys.testKeysGood(): "
060: + "Testing that LocalStringKeys interface and "
061: + "LocalStrings_<locale>.properties have "
062: + "the same set of keys.");
063:
064: // Load resource bundle
065:
066: String packageName = this .getClass().getPackage().getName();
067: String bundleName = packageName + "."
068: + StringTranslator.RESOURCE_BUNDLE_NAME;
069: ResourceBundle bundle = null;
070: bundle = ResourceBundle.getBundle(bundleName);
071:
072: boolean mismatch = false;
073:
074: // Get a list of all the fields defined in the LocalStringKeys class
075:
076: Field[] keyFields = (LocalStringKeys.class).getDeclaredFields();
077: Field keyField;
078: int numFields = keyFields.length;
079:
080: // Get a list of all the keys defined in the resource bundle
081:
082: Enumeration rbKeys = bundle.getKeys();
083: String rbKey = null;
084: int numKeys = 0;
085:
086: // Verify that all the keys in the resource bundle are defined in the
087: // fields in the LocalStringKeys class
088:
089: while (rbKeys.hasMoreElements()) {
090: rbKey = (String) rbKeys.nextElement();
091: numKeys++;
092: try {
093: keyField = LocalStringKeys.class
094: .getDeclaredField(rbKey);
095: } catch (NoSuchFieldException nsfEx) {
096: System.out.println("Resource bundle key " + rbKey
097: + " is not defined in LocalStringKeys");
098: mismatch = true;
099: }
100: }
101:
102: // Verify that all the fields defined in the LocalStringKeys class
103: // are defined as keys in the resource bundle
104:
105: int i = 0;
106: String str;
107: while (i < numFields) {
108: try {
109: rbKey = keyFields[i].getName();
110: str = bundle.getString(rbKey);
111: } catch (java.util.MissingResourceException mrEx) {
112: System.out.println("LocalStringKeys field " + rbKey
113: + " is not defined in the resource bundle");
114: mismatch = true;
115: }
116: i++;
117: }
118:
119: // Ensure that the bundle and the interface have the same number
120: // of keys and they all match
121:
122: assertFalse("Resource bundle has " + numKeys + " keys; "
123: + "LocalStringKeys interface has " + numFields
124: + " fields", mismatch);
125: }
126: }
|