PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/runtime/manage/regress/com/sun/jbi/management/TestLocalStringKeys.java

https://bitbucket.org/rsaqc/openesb-core
Java | 199 lines | 107 code | 29 blank | 63 comment | 3 complexity | 6e16273b9823c2706b9dc6af425f1fc9 MD5 | raw file
  1. /*
  2. * BEGIN_HEADER - DO NOT EDIT
  3. *
  4. * The contents of this file are subject to the terms
  5. * of the Common Development and Distribution License
  6. * (the "License"). You may not use this file except
  7. * in compliance with the License.
  8. *
  9. * You can obtain a copy of the license at
  10. * https://open-esb.dev.java.net/public/CDDLv1.0.html.
  11. * See the License for the specific language governing
  12. * permissions and limitations under the License.
  13. *
  14. * When distributing Covered Code, include this CDDL
  15. * HEADER in each file and include the License file at
  16. * https://open-esb.dev.java.net/public/CDDLv1.0.html.
  17. * If applicable add the following below this CDDL HEADER,
  18. * with the fields enclosed by brackets "[]" replaced with
  19. * your own identifying information: Portions Copyright
  20. * [year] [name of copyright owner]
  21. */
  22. /*
  23. * @(#)TestLocalStringKeys.java
  24. * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
  25. *
  26. * END_HEADER - DO NOT EDIT
  27. */
  28. package com.sun.jbi.management;
  29. import com.sun.jbi.util.StringTranslator;
  30. import java.lang.reflect.Field;
  31. import java.text.MessageFormat;
  32. import java.util.Enumeration;
  33. import java.util.ResourceBundle;
  34. /**
  35. * Tests for the LocalStringKeys class.
  36. *
  37. * @author Sun Microsystems, Inc.
  38. */
  39. public class TestLocalStringKeys
  40. extends junit.framework.TestCase
  41. {
  42. /**
  43. * The constructor for this testcase, forwards the test name to
  44. * the jUnit TestCase base class.
  45. * @param aTestName String with the name of this test.
  46. */
  47. public TestLocalStringKeys(String aTestName)
  48. {
  49. super(aTestName);
  50. }
  51. /**
  52. * Test the message keys to make sure the class and the properties file
  53. * have the same set of keys.
  54. * @throws java.util.MissingResourceException when
  55. * ResourceBundle.getBundle fails
  56. */
  57. public void testKeys()
  58. throws java.util.MissingResourceException
  59. {
  60. System.out.println("TestLocalStringKeys.testKeys(): " +
  61. "Testing that LocalStringKeys interface and " +
  62. "LocalStrings.properties have " +
  63. "the same set of keys.");
  64. // Load resource bundle
  65. String packageName = this.getClass().getPackage().getName();
  66. String bundleName = packageName + "." +
  67. StringTranslator.RESOURCE_BUNDLE_NAME;
  68. ResourceBundle bundle = null;
  69. bundle = ResourceBundle.getBundle(bundleName);
  70. boolean mismatch = false;
  71. // Get a list of all the fields defined in the LocalStringKeys class
  72. Field[] keyFields = (LocalStringKeys.class).getDeclaredFields();
  73. Field keyField;
  74. int numFields = keyFields.length;
  75. // Get a list of all the keys defined in the resource bundle
  76. Enumeration rbKeys = bundle.getKeys();
  77. String rbKey = null;
  78. int numKeys = 0;
  79. // Verify that all the keys in the resource bundle are defined in the
  80. // fields in the LocalStringKeys class
  81. while (rbKeys.hasMoreElements())
  82. {
  83. rbKey = (String) rbKeys.nextElement();
  84. numKeys++;
  85. try
  86. {
  87. keyField = LocalStringKeys.class.getDeclaredField(rbKey);
  88. }
  89. catch ( NoSuchFieldException nsfEx )
  90. {
  91. System.out.println("Resource bundle key " + rbKey +
  92. " is not defined in LocalStringKeys");
  93. mismatch = true;
  94. }
  95. }
  96. // Verify that all the fields defined in the LocalStringKeys class
  97. // are defined as keys in the resource bundle
  98. int i = 0;
  99. String str;
  100. while ( i < numFields )
  101. {
  102. try
  103. {
  104. rbKey = keyFields[i].getName();
  105. str = bundle.getString(rbKey);
  106. }
  107. catch ( java.util.MissingResourceException mrEx )
  108. {
  109. System.out.println("LocalStringKeys field " + rbKey +
  110. " is not defined in the resource bundle");
  111. mismatch = true;
  112. }
  113. i++;
  114. }
  115. // Ensure that the bundle and the interface have the same number
  116. // of keys and they all match
  117. assertFalse("Resource bundle has " + numKeys + " keys; " +
  118. "LocalStringKeys interface has " + numFields + " fields",
  119. mismatch);
  120. }
  121. /**
  122. * Test the content of the properties file for syntax errors.
  123. * @throws java.util.MissingResourceException when
  124. * ResourceBundle.getBundle fails
  125. */
  126. public void testProperties()
  127. throws java.util.MissingResourceException
  128. {
  129. System.out.println("TestLocalStringKeys.testProperties(): " +
  130. "Testing that LocalStrings.properties has " +
  131. "no syntax errors.");
  132. // Load resource bundle
  133. String packageName = this.getClass().getPackage().getName();
  134. String bundleName = packageName + "." +
  135. StringTranslator.RESOURCE_BUNDLE_NAME;
  136. ResourceBundle bundle = null;
  137. bundle = ResourceBundle.getBundle(bundleName);
  138. boolean errors = false;
  139. // Get a list of all the keys defined in the resource bundle
  140. Enumeration rbKeys = bundle.getKeys();
  141. String rbKey = null;
  142. String rbMsg = null;
  143. String msg = null;
  144. int numBad = 0;
  145. // Check each property in the resource bundle for syntax errors.
  146. // Note: if a MissingResourceException is thrown here, something is
  147. // seriously wrong!
  148. while (rbKeys.hasMoreElements())
  149. {
  150. rbKey = (String) rbKeys.nextElement();
  151. try
  152. { rbMsg = bundle.getString(rbKey);
  153. msg = MessageFormat.format(rbMsg,
  154. "AAA", "BBB", "CCC", "DDD", "EEE", "FFF", "GGG", "HHH");
  155. }
  156. catch ( IllegalArgumentException iaEx )
  157. {
  158. System.out.println("---");
  159. System.out.println("Resource bundle property " + rbKey +
  160. " has invalid syntax: " + rbMsg);
  161. System.out.println("Error message: " + iaEx.getMessage());
  162. errors = true;
  163. ++numBad;
  164. }
  165. }
  166. // Ensure that there were no syntax errors.
  167. assertFalse("Resource bundle has " + numBad + " syntax errors.",
  168. errors);
  169. }
  170. }