Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.resources » [javadoc | source]
    1   // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/resources/PackageTest.java,v 1.18 2005/07/12 20:51:08 mstover1 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.MissingResourceException;
   29   import java.util.PropertyResourceBundle;
   30   
   31   import junit.framework.Test;
   32   import junit.framework.TestCase;
   33   import junit.framework.TestSuite;
   34   
   35   /*
   36    * Created on Nov 29, 2003
   37    * 
   38    * Test the composition of the properties files - properties files exist
   39    * (default, DE, NO, JA) - properties files don't have duplicate keys -
   40    * non-default properties files don't have any extra keys.
   41    * 
   42    * N.B. If there is a default resource, ResourceBundle does not detect missing
   43    * resources, i.e. the presence of messages.properties means that the
   44    * ResourceBundle for Locale "XYZ" would still be found, and have the same keys
   45    * as the default. This makes it not very useful for checking properties files.
   46    * 
   47    * This is why the tests use Class.getResourceAsStream() etc
   48    * 
   49    * The tests don't quite follow the normal JUnit test strategy of one test per
   50    * possible failure. This was done in order to make it easier to report exactly
   51    * why the tests failed.
   52    */
   53   
   54   /**
   55    * @version $Revision: 1.18 $ $Date: 2005/07/12 20:51:08 $
   56    */
   57   public class PackageTest extends TestCase {
   58   
   59   	// private static List defaultList = null;
   60   	private static PropertyResourceBundle defaultPRB;
   61   
   62   	// Read resource into ResourceBundle and store in List
   63   	private PropertyResourceBundle getRAS(String res) throws Exception {
   64   		InputStream ras = this.getClass().getResourceAsStream(res);
   65   		return new PropertyResourceBundle(ras);
   66   	}
   67   
   68   	private static Object[] DUMMY_PARAMS = new Object[] { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
   69   
   70   	// Read resource file saving the keys
   71   	private int readRF(String res, List l) throws Exception {
   72   		int fails = 0;
   73   		InputStream ras = this.getClass().getResourceAsStream(res);
   74   		BufferedReader fileReader = new BufferedReader(new InputStreamReader(ras));
   75   		String s;
   76   		while ((s = fileReader.readLine()) != null) {
   77   			if (s.length() > 0 && !s.startsWith("#") && !s.startsWith("!")) {
   78   				int equ = s.indexOf('=');
   79   				String key = s.substring(0, equ);
   80   				/*
   81   				 * JMeterUtils.getResString() converts space to _ and lowercases
   82   				 * the key, so make sure all keys pass the test
   83   				 */
   84   				if ((key.indexOf(' ') >= 0) || !key.toLowerCase().equals(key)) {
   85   					System.out.println("Invalid key for JMeterUtils " + key);
   86   					fails++;
   87   				}
   88   				String val = s.substring(equ + 1);
   89   				l.add(key); // Store the key
   90   				/*
   91   				 * Now check for invalid message format: if string contains {0}
   92   				 * and ' there may be a problem, so do a format with dummy
   93   				 * parameters and check if there is a { in the output. A bit
   94   				 * crude, but should be enough for now.
   95   				 */
   96   				if (val.indexOf("{0}") > 0 && val.indexOf("'") > 0) {
   97   					String m = java.text.MessageFormat.format(val, DUMMY_PARAMS);
   98   					if (m.indexOf("{") > 0) {
   99   						fails++;
  100   						System.out.println("Incorrect message format ? (input/output): ");
  101   						System.out.println(val);
  102   						System.out.println(m);
  103   					}
  104   				}
  105   
  106   			}
  107   		}
  108   		return fails;
  109   	}
  110   
  111   	// Helper method to construct resource name
  112   	private static String getResName(String lang) {
  113   		if (lang.length() == 0) {
  114   			return "messages.properties";
  115   		} else {
  116   			return "messages_" + lang + ".properties";
  117   		}
  118   	}
  119   
  120   	private void check(String resname) throws Exception {
  121   		check(resname, true);// check that there aren't any extra entries
  122   	}
  123   
  124   	/*
  125   	 * perform the checks on the resources
  126   	 * 
  127   	 */
  128   	private void check(String resname, boolean checkUnexpected) throws Exception {
  129   		ArrayList alf = new ArrayList(500);// holds keys from file
  130   		String res = getResName(resname);
  131   		subTestFailures += readRF(res, alf);
  132   		Collections.sort(alf);
  133   
  134   		// Look for duplicate keys in the file
  135   		String last = "";
  136   		for (int i = 0; i < alf.size(); i++) {
  137   			String curr = (String) alf.get(i);
  138   			if (curr.equals(last)) {
  139   				subTestFailures++;
  140   				System.out.println("\nDuplicate key =" + curr + " in " + res);
  141   			}
  142   			last = curr;
  143   		}
  144   
  145   		if (resname.length() == 0) // Must be the default resource file
  146   		{
  147   			defaultPRB = getRAS(res);
  148   		} else if (checkUnexpected) {
  149   			// Check all the keys are in the default props file
  150   			Enumeration enumr = getRAS(res).getKeys();
  151   			while (enumr.hasMoreElements()) {
  152   				String key = null;
  153   				try {
  154   					key = (String) enumr.nextElement();
  155   					defaultPRB.getString(key);
  156   				} catch (MissingResourceException e) {
  157   					subTestFailures++;
  158   					System.out.println("Locale: " + resname + " has unexpected key: " + key);
  159   				}
  160   			}
  161   		}
  162   
  163   		if (subTestFailures > 0) {
  164   			fail("One or more subtests failed");
  165   		}
  166   	}
  167   
  168   	/*
  169   	 * Use a suite to ensure that the default is done first
  170   	 */
  171   	public static Test suite() {
  172   		TestSuite ts = new TestSuite();
  173   		ts.addTest(new PackageTest("atestDefault"));
  174   		ts.addTest(new PackageTest("atestDE"));
  175   		ts.addTest(new PackageTest("atestNO"));
  176   		ts.addTest(new PackageTest("atestJA"));
  177   		ts.addTest(new PackageTest("atestzh_CN"));
  178   		ts.addTest(new PackageTest("atestzh_TW"));
  179   		ts.addTest(new PackageTest("atestFR"));
  180   		ts.addTest(new PackageTest("atestES"));
  181   		return ts;
  182   	}
  183   
  184   	private int subTestFailures;
  185   
  186   	public PackageTest(String string) {
  187   		super(string);
  188   		subTestFailures = 0;
  189   	}
  190   
  191   	public void atestDE() throws Exception {
  192   		check("de");
  193   	}
  194   
  195   	public void atestJA() throws Exception {
  196   		check("ja");
  197   	}
  198   
  199   	public void atestzh_CN() throws Exception {
  200   		check("zh_CN");
  201   	}
  202   
  203   	public void atestzh_TW() throws Exception {
  204   		check("zh_TW");
  205   	}
  206   
  207   	public void atestNO() throws Exception {
  208   		check("no");
  209   	}
  210   
  211   	public void atestFR() throws Exception {
  212   		check("fr");
  213   	}
  214   
  215   	public void atestES() throws Exception {
  216   		check("es");
  217   	}
  218   
  219   	public void atestDefault() throws Exception {
  220   		check("");
  221   	}
  222   }

Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.resources » [javadoc | source]