KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > testbeans > gui > PackageTest


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/testbeans/gui/PackageTest.java,v 1.8.2.1 2005/01/09 14:15:53 sebb Exp $
2
/*
3  * Copyright 2003-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.jmeter.testbeans.gui;
18
19 import java.beans.BeanInfo JavaDoc;
20 import java.beans.IntrospectionException JavaDoc;
21 import java.beans.Introspector JavaDoc;
22 import java.beans.PropertyDescriptor JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Locale JavaDoc;
26 import java.util.ResourceBundle JavaDoc;
27
28 import org.apache.jmeter.junit.JMeterTestCase;
29 import org.apache.jmeter.testbeans.TestBean;
30 import org.apache.jmeter.testelement.TestElement;
31 import org.apache.jmeter.util.JMeterUtils;
32 import org.apache.jorphan.logging.LoggingManager;
33 import org.apache.jorphan.reflect.ClassFinder;
34 import org.apache.log.Logger;
35
36 import junit.framework.Test;
37 //import junit.framework.TestCase;
38
import junit.framework.TestSuite;
39
40 /*
41  * Find all beans out there and check their resource property files:
42  * - Check that non-default property files don't have any extra keys.
43  * - Check all necessary properties are defined at least in the default property file,
44  * except for beans whose name contains "Experimental" or "Alpha".
45  * TODO: - Check property files don't have duplicate keys (is this important)
46  *
47  * @version $Revision: 1.8.2.1 $ updated on $Date: 2005/01/09 14:15:53 $
48  */

49 public class PackageTest extends JMeterTestCase
50 {
51     private static Logger log = LoggingManager.getLoggerForClass();
52
53     private ResourceBundle JavaDoc defaultBundle;
54
55     private Class JavaDoc testBeanClass;
56
57     private String JavaDoc language;
58
59     private PackageTest(
60         Class JavaDoc testBeanClass, String JavaDoc language, ResourceBundle JavaDoc defaultBundle)
61     {
62         super(testBeanClass.getName()+" - "+language);
63         this.testBeanClass= testBeanClass;
64         this.language= language;
65         this.defaultBundle= defaultBundle;
66     }
67     
68     BeanInfo JavaDoc beanInfo;
69     ResourceBundle JavaDoc bundle;
70
71     public void setUp()
72     {
73         JMeterUtils.setLocale(new Locale JavaDoc(language,""));
74         try
75         {
76             beanInfo= Introspector.getBeanInfo(testBeanClass);
77             bundle= (ResourceBundle JavaDoc) beanInfo
78                 .getBeanDescriptor()
79                 .getValue(GenericTestBeanCustomizer.RESOURCE_BUNDLE);
80         }
81         catch (IntrospectionException JavaDoc e)
82         {
83             log.error("Can't get beanInfo for "+testBeanClass.getName(),
84                 e);
85             throw new Error JavaDoc(e.toString()); // Programming error. Don't continue.
86
}
87         if (bundle == null) throw new Error JavaDoc("This can't happen!");
88     }
89     
90     public void tearDown()
91     {
92         JMeterUtils.setLocale(Locale.getDefault());
93     }
94     
95     public void runTest()
96     {
97         if (bundle == defaultBundle) checkAllNecessaryKeysPresent();
98         else checkNoInventedKeys();
99     }
100
101     public void checkNoInventedKeys()
102     {
103         // Check that all keys in the bundle are also in the default bundle:
104
for (Enumeration JavaDoc keys= bundle.getKeys(); keys.hasMoreElements(); )
105         {
106             String JavaDoc key= (String JavaDoc)keys.nextElement();
107             defaultBundle.getString(key);
108                 // Will throw MissingResourceException if key is not there.
109
}
110     }
111     
112     public void checkAllNecessaryKeysPresent()
113     {
114         // Check that all necessary keys are there:
115

116         // displayName is always mandatory:
117
String JavaDoc dn= defaultBundle.getString("displayName").toUpperCase(Locale.ENGLISH);
118
119         // Skip the rest of this test for alpha/experimental beans:
120
if (dn.indexOf("(ALPHA") != -1
121             || dn.indexOf("(EXPERIMENTAL") != -1) return;
122
123         // Check for property- and group-related texts:
124
PropertyDescriptor JavaDoc[] descriptors= beanInfo.getPropertyDescriptors();
125         for (int i=0; i<descriptors.length; i++)
126         {
127             // Skip non-editable properties, that is:
128
// Ignore hidden, read-only, and write-only properties
129
if (descriptors[i].isHidden()
130                 || descriptors[i].getReadMethod() == null
131                 || descriptors[i].getWriteMethod() == null) continue;
132             // Ignore TestElement properties which don't have an explicit editor:
133
if (TestElement.class.isAssignableFrom(
134                     descriptors[i].getPropertyType())
135                 && descriptors[i].getPropertyEditorClass() == null) continue;
136             // Done -- we're working with an editable property.
137

138             String JavaDoc name= descriptors[i].getName();
139                 
140             bundle.getString(name+".displayName");
141             //bundle.getString(name+".shortDescription"); NOT MANDATORY
142

143             String JavaDoc group= (String JavaDoc)descriptors[i]
144                     .getValue(GenericTestBeanCustomizer.GROUP);
145             if (group != null) bundle.getString(group+".displayName");
146         }
147     }
148
149     public static Test suite() throws Exception JavaDoc
150     {
151         TestSuite suite = new TestSuite("Bean Resource Test Suite");
152
153         //ResourceBundle i18nEdit= ResourceBundle.getBundle("org.apache.jmeter.resources.i18nedit");
154
String JavaDoc[] languages= new String JavaDoc[] { "de", "ja", "no" };
155         String JavaDoc defaultLanguage= "en"; //i18nEdit.getString("locale.default");
156
// TODO: find a clean way to get these from i18nedit.properties
157

158         Iterator JavaDoc iter =
159             ClassFinder
160                 .findClassesThatExtend(
161                     JMeterUtils.getSearchPaths(),
162                     new Class JavaDoc[] { TestBean.class })
163                 .iterator();
164
165         while (iter.hasNext())
166         {
167             String JavaDoc className = (String JavaDoc)iter.next();
168             Class JavaDoc testBeanClass= Class.forName(className);
169             JMeterUtils.setLocale(new Locale JavaDoc(defaultLanguage,""));
170             ResourceBundle JavaDoc defaultBundle;
171             try
172             {
173                 defaultBundle= (ResourceBundle JavaDoc)
174                     Introspector.getBeanInfo(testBeanClass)
175                     .getBeanDescriptor()
176                     .getValue(GenericTestBeanCustomizer.RESOURCE_BUNDLE);
177             }
178             catch (IntrospectionException JavaDoc e)
179             {
180                 log.error("Can't get beanInfo for "+testBeanClass.getName(),
181                     e);
182                 throw new Error JavaDoc(e.toString()); // Programming error. Don't continue.
183
}
184
185             if (defaultBundle == null)
186             {
187                 if (className.startsWith("org.apache.jmeter.examples."))
188                 {
189                     log.warn("No default bundle found for "+className);
190                     continue;
191                 }
192                 throw new Error JavaDoc("No default bundle for class " + className);
193             }
194
195             suite.addTest(new PackageTest(testBeanClass, defaultLanguage, defaultBundle));
196
197             for (int i=0; i<languages.length; i++)
198             {
199                 suite.addTest(new PackageTest(testBeanClass, languages[i], defaultBundle));
200             }
201         }
202
203         return suite;
204     }
205 }
206
Popular Tags