1 18 19 package org.apache.jmeter.resources; 20 21 import java.io.BufferedReader; 22 import java.io.InputStream; 23 import java.io.InputStreamReader; 24 import java.util.ArrayList; 25 import java.util.Collections; 26 import java.util.Enumeration; 27 import java.util.List; 28 import java.util.Locale; 29 import java.util.MissingResourceException; 30 import java.util.PropertyResourceBundle; 31 32 import junit.framework.Test; 33 import junit.framework.TestCase; 34 import junit.framework.TestSuite; 35 36 55 56 59 public class PackageTest extends TestCase 60 { 61 62 private static PropertyResourceBundle defaultPRB; 64 65 private PropertyResourceBundle getRAS(String res) throws Exception{ 67 InputStream ras = this.getClass().getResourceAsStream(res); 68 return new PropertyResourceBundle(ras); 69 } 70 71 private static Object [] DUMMY_PARAMS = new Object[]{"1","2","3","4","5","6","7","8","9"}; 72 private int readRF(String res, List l) throws Exception 74 { 75 int fails =0 ; 76 InputStream ras = this.getClass().getResourceAsStream(res); 77 BufferedReader fileReader = 78 new BufferedReader(new InputStreamReader(ras)); 79 String s; 80 while((s=fileReader.readLine())!=null) 81 { 82 if (s.length() > 0 && !s.startsWith("#")) { 83 l.add(s.substring(0,s.indexOf('='))); 91 if (s.indexOf("{0}") > 0 && s.indexOf("'") > 0) 92 { 93 String m = java.text.MessageFormat.format(s,DUMMY_PARAMS); 94 if (m.indexOf("{") > 0) { 95 fails++; 96 System.out.println("Incorrect message format ? (input/output): "); 97 System.out.println(s); 98 System.out.println(m); 99 } 100 } 101 102 } 103 } 104 return fails; 105 } 106 107 private static String getResName(String lang){ 109 if (lang.length()==0){ 110 return "messages.properties"; 111 } else { 112 return "messages_"+lang.toLowerCase(Locale.ENGLISH)+".properties"; 113 } 114 } 115 116 private void check(String resname) throws Exception 117 { 118 check(resname, true); } 120 124 private void check(String resname, boolean checkUnexpected) throws Exception 125 { 126 ArrayList alf = new ArrayList(500); String res = getResName(resname); 128 subTestFailures += readRF(res,alf); 129 Collections.sort(alf); 130 131 String last=""; 133 for (int i=0;i<alf.size();i++){ 134 String curr = (String) alf.get(i); 135 if (curr.equals(last)){ 136 subTestFailures++; 137 System.out.println("\nDuplicate key ="+curr+" in "+res); 138 } 139 last=curr; 140 } 141 142 if (resname.length()==0) { 144 defaultPRB = getRAS(res); 145 } 146 else if (checkUnexpected) 147 { 148 Enumeration enum = getRAS(res).getKeys(); 150 while(enum.hasMoreElements()) 151 { 152 String key = null; 153 try 154 { 155 key = (String)enum.nextElement(); 156 defaultPRB.getString(key); 157 } 158 catch (MissingResourceException e) 159 { 160 subTestFailures++; 161 System.out.println("Locale: "+resname+" has unexpected key: "+ key); 162 } 163 } 164 } 165 166 if (subTestFailures > 0) { 167 fail("One or more subtests failed"); 168 } 169 } 170 171 174 public static Test suite(){ 175 TestSuite ts=new TestSuite(); 176 ts.addTest(new PackageTest("atestDefault")); 177 ts.addTest(new PackageTest("atestDE")); 178 ts.addTest(new PackageTest("atestNO")); 179 ts.addTest(new PackageTest("atestJA")); 180 ts.addTest(new PackageTest("atestCN")); 181 ts.addTest(new PackageTest("atestFR")); 182 return ts; 183 } 184 185 private int subTestFailures; 186 187 public PackageTest(String string) 188 { 189 super(string); 190 subTestFailures=0; 191 } 192 193 public void atestDE() throws Exception 194 { 195 check("DE"); 196 } 197 198 public void atestJA() throws Exception 199 { 200 check("JA"); 201 } 202 public void atestCN() throws Exception 203 { 204 check("CN"); 205 } 206 public void atestNO() throws Exception 207 { 208 check("NO"); 209 } 210 public void atestFR() throws Exception 211 { 212 check("FR",false); } 214 public void atestDefault() throws Exception 215 { 216 check(""); 217 } 218 } 219 | Popular Tags |