001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jmeter.testbeans.gui;
018:
019: import java.beans.BeanInfo;
020: import java.beans.IntrospectionException;
021: import java.beans.Introspector;
022: import java.beans.PropertyDescriptor;
023: import java.util.Enumeration;
024: import java.util.Iterator;
025: import java.util.Locale;
026: import java.util.ResourceBundle;
027:
028: import org.apache.jmeter.junit.JMeterTestCase;
029: import org.apache.jmeter.testbeans.TestBean;
030: import org.apache.jmeter.testelement.TestElement;
031: import org.apache.jmeter.util.JMeterUtils;
032: import org.apache.jorphan.logging.LoggingManager;
033: import org.apache.jorphan.reflect.ClassFinder;
034: import org.apache.log.Logger;
035:
036: import junit.framework.Test; // import junit.framework.TestCase;
037: import junit.framework.TestSuite;
038:
039: /*
040: * Find all beans out there and check their resource property files: - Check
041: * that non-default property files don't have any extra keys. - Check all
042: * necessary properties are defined at least in the default property file,
043: * except for beans whose name contains "Experimental" or "Alpha". TODO: - Check
044: * property files don't have duplicate keys (is this important)
045: *
046: * @version $Revision: 493796 $ updated on $Date: 2007-01-07 18:31:05 +0000 (Sun, 07 Jan 2007) $
047: */
048: public class PackageTest extends JMeterTestCase {
049: private static Logger log = LoggingManager.getLoggerForClass();
050:
051: private ResourceBundle defaultBundle;
052:
053: private Class testBeanClass;
054:
055: // ResourceBundle i18nEdit=
056: // ResourceBundle.getBundle("org.apache.jmeter.resources.i18nedit");
057: private static final Locale defaultLocale = new Locale("en", ""); // i18nEdit.getString("locale.default");
058:
059: // TODO: find a clean way to get these from i18nedit.properties
060:
061: private static final Locale[] locales = new Locale[] {
062: // new Locale("de"), // No resources yet
063: new Locale("ja", ""),
064: // new Locale("no",""), // No resources yet
065: // new Locale("fr",""), // No resources yet
066: // new Locale("zh","CN"), //No resources yet
067: new Locale("zh", "TW") };
068:
069: private Locale testLocale;
070:
071: private PackageTest(Class testBeanClass, Locale locale,
072: ResourceBundle defaultBundle) {
073: super (testBeanClass.getName() + " - " + locale.getLanguage()
074: + " - " + locale.getCountry());
075: this .testBeanClass = testBeanClass;
076: this .testLocale = locale;
077: this .defaultBundle = defaultBundle;
078: }
079:
080: BeanInfo beanInfo;
081:
082: ResourceBundle bundle;
083:
084: public void setUp() {
085: JMeterUtils.setLocale(testLocale);
086: Introspector.flushFromCaches(testBeanClass);
087: try {
088: beanInfo = Introspector.getBeanInfo(testBeanClass);
089: bundle = (ResourceBundle) beanInfo
090: .getBeanDescriptor()
091: .getValue(GenericTestBeanCustomizer.RESOURCE_BUNDLE);
092: } catch (IntrospectionException e) {
093: log.error("Can't get beanInfo for "
094: + testBeanClass.getName(), e);
095: throw new Error(e.toString()); // Programming error. Don't
096: // continue.
097: }
098: if (bundle == null)
099: throw new Error("This can't happen!");
100: }
101:
102: public void tearDown() {
103: JMeterUtils.setLocale(Locale.getDefault());
104: }
105:
106: public void runTest() {
107: if (bundle == defaultBundle)
108: checkAllNecessaryKeysPresent();
109: else
110: checkNoInventedKeys();
111: }
112:
113: public void checkNoInventedKeys() {
114: // Check that all keys in the bundle are also in the default bundle:
115: for (Enumeration keys = bundle.getKeys(); keys
116: .hasMoreElements();) {
117: String key = (String) keys.nextElement();
118: defaultBundle.getString(key);
119: // Will throw MissingResourceException if key is not there.
120: }
121: }
122:
123: public void checkAllNecessaryKeysPresent() {
124: // Check that all necessary keys are there:
125:
126: // displayName is always mandatory:
127: String dn = defaultBundle.getString("displayName").toUpperCase(
128: Locale.ENGLISH);
129:
130: // Skip the rest of this test for alpha/experimental beans:
131: if (dn.indexOf("(ALPHA") != -1
132: || dn.indexOf("(EXPERIMENTAL") != -1)
133: return;
134:
135: // Check for property- and group-related texts:
136: PropertyDescriptor[] descriptors = beanInfo
137: .getPropertyDescriptors();
138: for (int i = 0; i < descriptors.length; i++) {
139: // Skip non-editable properties, that is:
140: // Ignore hidden, read-only, and write-only properties
141: if (descriptors[i].isHidden()
142: || descriptors[i].getReadMethod() == null
143: || descriptors[i].getWriteMethod() == null)
144: continue;
145: // Ignore TestElement properties which don't have an explicit
146: // editor:
147: if (TestElement.class.isAssignableFrom(descriptors[i]
148: .getPropertyType())
149: && descriptors[i].getPropertyEditorClass() == null)
150: continue;
151: // Done -- we're working with an editable property.
152:
153: String name = descriptors[i].getName();
154:
155: bundle.getString(name + ".displayName");
156: // bundle.getString(name+".shortDescription"); NOT MANDATORY
157:
158: String group = (String) descriptors[i]
159: .getValue(GenericTestBeanCustomizer.GROUP);
160: if (group != null)
161: bundle.getString(group + ".displayName");
162: }
163: }
164:
165: public static Test suite() throws Exception {
166: TestSuite suite = new TestSuite("Bean Resource Test Suite");
167:
168: Iterator iter = ClassFinder.findClassesThatExtend(
169: JMeterUtils.getSearchPaths(),
170: new Class[] { TestBean.class }).iterator();
171:
172: while (iter.hasNext()) {
173: String className = (String) iter.next();
174: Class testBeanClass = Class.forName(className);
175: JMeterUtils.setLocale(defaultLocale);
176: ResourceBundle defaultBundle;
177: try {
178: defaultBundle = (ResourceBundle) Introspector
179: .getBeanInfo(testBeanClass)
180: .getBeanDescriptor()
181: .getValue(
182: GenericTestBeanCustomizer.RESOURCE_BUNDLE);
183: } catch (IntrospectionException e) {
184: log.error("Can't get beanInfo for "
185: + testBeanClass.getName(), e);
186: throw new Error(e.toString()); // Programming error. Don't
187: // continue.
188: }
189:
190: if (defaultBundle == null) {
191: if (className.startsWith("org.apache.jmeter.examples.")) {
192: log
193: .warn("No default bundle found for "
194: + className);
195: continue;
196: }
197: throw new Error("No default bundle for class "
198: + className);
199: }
200:
201: suite.addTest(new PackageTest(testBeanClass, defaultLocale,
202: defaultBundle));
203:
204: for (int i = 0; i < locales.length; i++) {
205: suite.addTest(new PackageTest(testBeanClass,
206: locales[i], defaultBundle));
207: }
208: }
209:
210: return suite;
211: }
212: }
|