KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > resources > PackageTest


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/resources/PackageTest.java,v 1.8.2.5 2005/01/09 15:03:10 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 */

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 /*
37  * Created on Nov 29, 2003
38  *
39  * Test the composition of the properties files
40  * - properties files exist (default, DE, NO, JA)
41  * - properties files don't have duplicate keys
42  * - non-default properties files don't have any extra keys.
43  *
44  * N.B. If there is a default resource, ResourceBundle does not detect missing resources,
45  * i.e. the presence of messages.properties means that the ResourceBundle for Locale "XYZ"
46  * would still be found, and have the same keys as the default. This makes it not very
47  * useful for checking properties files.
48  *
49  * This is why the tests use Class.getResourceAsStream() etc
50  *
51  * The tests don't quite follow the normal JUnit test strategy of one test
52  * per possible failure. This was done in order to make it easier to report
53  * exactly why the tests failed.
54  */

55
56 /**
57  * @version $Revision: 1.8.2.5 $ $Date: 2005/01/09 15:03:10 $
58  */

59 public class PackageTest extends TestCase
60 {
61
62     //private static List defaultList = null;
63
private static PropertyResourceBundle defaultPRB;
64     
65     // Read resource into ResourceBundle and store in List
66
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     // Read resource file saving the keys
73
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('='))); // Store the key
84
/*
85                  * Now check for invalid message format:
86                  * if string contains {0} and ' there may be a problem,
87                  * so do a format with dummy parameters and check if there
88                  * is a { in the output.
89                  * A bit crude, but should be enough for now.
90                  */

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     // Helper method to construct resource name
108
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);// check that there aren't any extra entries
119
}
120     /*
121      * perform the checks on the resources
122      *
123      */

124     private void check(String resname, boolean checkUnexpected) throws Exception
125     {
126         ArrayList alf = new ArrayList(500);// holds keys from file
127
String res = getResName(resname);
128         subTestFailures += readRF(res,alf);
129         Collections.sort(alf);
130         
131         // Look for duplicate keys in the file
132
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) // Must be the default resource file
143
{
144             defaultPRB = getRAS(res);
145         }
146         else if (checkUnexpected)
147         {
148             // Check all the keys are in the default props file
149
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     /*
172      * Use a suite to ensure that the default is done first
173      */

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);//Don't report unexpected rows (for 2.0 branch only!)
213
}
214     public void atestDefault() throws Exception
215     {
216         check("");
217     }
218 }
219
Popular Tags