1 17 package org.apache.jmeter.testbeans.gui; 18 19 import java.beans.BeanInfo ; 20 import java.beans.IntrospectionException ; 21 import java.beans.Introspector ; 22 import java.beans.PropertyDescriptor ; 23 import java.util.Enumeration ; 24 import java.util.Iterator ; 25 import java.util.Locale ; 26 import java.util.ResourceBundle ; 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.TestSuite; 39 40 49 public class PackageTest extends JMeterTestCase 50 { 51 private static Logger log = LoggingManager.getLoggerForClass(); 52 53 private ResourceBundle defaultBundle; 54 55 private Class testBeanClass; 56 57 private String language; 58 59 private PackageTest( 60 Class testBeanClass, String language, ResourceBundle defaultBundle) 61 { 62 super(testBeanClass.getName()+" - "+language); 63 this.testBeanClass= testBeanClass; 64 this.language= language; 65 this.defaultBundle= defaultBundle; 66 } 67 68 BeanInfo beanInfo; 69 ResourceBundle bundle; 70 71 public void setUp() 72 { 73 JMeterUtils.setLocale(new Locale (language,"")); 74 try 75 { 76 beanInfo= Introspector.getBeanInfo(testBeanClass); 77 bundle= (ResourceBundle ) beanInfo 78 .getBeanDescriptor() 79 .getValue(GenericTestBeanCustomizer.RESOURCE_BUNDLE); 80 } 81 catch (IntrospectionException e) 82 { 83 log.error("Can't get beanInfo for "+testBeanClass.getName(), 84 e); 85 throw new Error (e.toString()); } 87 if (bundle == null) throw new Error ("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 for (Enumeration keys= bundle.getKeys(); keys.hasMoreElements(); ) 105 { 106 String key= (String )keys.nextElement(); 107 defaultBundle.getString(key); 108 } 110 } 111 112 public void checkAllNecessaryKeysPresent() 113 { 114 116 String dn= defaultBundle.getString("displayName").toUpperCase(Locale.ENGLISH); 118 119 if (dn.indexOf("(ALPHA") != -1 121 || dn.indexOf("(EXPERIMENTAL") != -1) return; 122 123 PropertyDescriptor [] descriptors= beanInfo.getPropertyDescriptors(); 125 for (int i=0; i<descriptors.length; i++) 126 { 127 if (descriptors[i].isHidden() 130 || descriptors[i].getReadMethod() == null 131 || descriptors[i].getWriteMethod() == null) continue; 132 if (TestElement.class.isAssignableFrom( 134 descriptors[i].getPropertyType()) 135 && descriptors[i].getPropertyEditorClass() == null) continue; 136 138 String name= descriptors[i].getName(); 139 140 bundle.getString(name+".displayName"); 141 143 String group= (String )descriptors[i] 144 .getValue(GenericTestBeanCustomizer.GROUP); 145 if (group != null) bundle.getString(group+".displayName"); 146 } 147 } 148 149 public static Test suite() throws Exception 150 { 151 TestSuite suite = new TestSuite("Bean Resource Test Suite"); 152 153 String [] languages= new String [] { "de", "ja", "no" }; 155 String defaultLanguage= "en"; 158 Iterator iter = 159 ClassFinder 160 .findClassesThatExtend( 161 JMeterUtils.getSearchPaths(), 162 new Class [] { TestBean.class }) 163 .iterator(); 164 165 while (iter.hasNext()) 166 { 167 String className = (String )iter.next(); 168 Class testBeanClass= Class.forName(className); 169 JMeterUtils.setLocale(new Locale (defaultLanguage,"")); 170 ResourceBundle defaultBundle; 171 try 172 { 173 defaultBundle= (ResourceBundle ) 174 Introspector.getBeanInfo(testBeanClass) 175 .getBeanDescriptor() 176 .getValue(GenericTestBeanCustomizer.RESOURCE_BUNDLE); 177 } 178 catch (IntrospectionException e) 179 { 180 log.error("Can't get beanInfo for "+testBeanClass.getName(), 181 e); 182 throw new Error (e.toString()); } 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 ("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 |