51Testing软件测试论坛

标题: JMeter国际化资源解析代码片段之简单测试 [打印本页]

作者: liangjz    时间: 2008-8-3 23:49
标题: JMeter国际化资源解析代码片段之简单测试
Jmeter在国际化资源解析方面做得不错,适应不同的Locale去展现UI文字以及图片。通过修改属性文件jmeter.properties的language达到一套程序适应多国语言。

具体目录见:

图片:src\core\org\apache\jmeter\images
多语言文件:src\core\org\apache\jmeter\resources


核心的类是Locale和ResourceBundle。
具体的资源文件制作可以用UE或者专业的I18Nedit(可从sourceforge.net下载)

下面是自己组合源码来简单测试:

package org.apache.jmeter;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.Vector;

public class I18ResourcesTest {
       
        private static ResourceBundle resources;
        private static Locale locale;       
        //初始化locale
        //根据情况重新设置
        public static void initLocale(String language ) {
                String loc = language;  //appProperties.getProperty("language"); // $NON-NLS-1$
                if (loc != null) {
            String []parts = split(loc,"_",true);// $NON-NLS-1$
               if (parts.length==2) {
                setLocale(new Locale(parts[0], parts[1]));              
            } else {
                            setLocale(new Locale(loc, "")); // $NON-NLS-1$
            }
                } else {
                        setLocale(Locale.getDefault());
                }
        }
       
        public static void setLocale(Locale loc) {
                locale = loc;
                /*
                 * See bug 29920. getBundle() defaults to the property file for the
                 * default Locale before it defaults to the base property file, so we
                 * need to change the default Locale to ensure the base property file is
                 * found.
                 */
                Locale def = null;
                if (loc.getLanguage().equals(Locale.ENGLISH.getLanguage())) {
                        def = Locale.getDefault();
                        // Don't change locale from en_GB to en
                        if (!def.getLanguage().equals(Locale.ENGLISH.getLanguage())) {
                                Locale.setDefault(Locale.ENGLISH);
                        } else {
                                def = null; // no need to reset Locale
                        }
                }
                //获取资源文件
                //org.apache.jmeter.resources.messages是一个basename,存在org\apache\jmeter\resources\messages.properties
                //或者org\apache\jmeter\resources\messages_zh_CN.properties的属性文件
                //内容 key=value,如 monitor_legend_memory_per=Memory % (used/total)
               
                resources = ResourceBundle.getBundle("org.apache.jmeter.resources.messages", locale); // $NON-NLS-1$
                /*
                 * Reset Locale if necessary so other locales are properly handled
                 */
                if (def != null) {
                        Locale.setDefault(def);
                }
        }
        public  static String getResStringDefault(String key) {
                if (key == null) {
                        return null;
                }
                // Resource keys cannot contain spaces
                key = key.replace(' ', '_'); // $NON-NLS-1$ // $NON-NLS-2$
                key = key.toLowerCase(java.util.Locale.ENGLISH);
                String resString = null;
                try {
                        resString = resources.getString(key);
                } catch (MissingResourceException mre) {
                        resString ="";
                }
                return resString;
        }
        public static String[] split(String splittee, String splitChar,boolean truncate) {
                if (splittee == null || splitChar == null) {
                        return new String[0];
                }
        final String EMPTY_ELEMENT = "";
                int spot;
        final int splitLength = splitChar.length();
        final String adjacentSplit = splitChar + splitChar;
        final int adjacentSplitLength = adjacentSplit.length();
        if(truncate) {
            while ((spot = splittee.indexOf(adjacentSplit)) != -1) {
                            splittee = splittee.substring(0, spot + splitLength)
                                            + splittee.substring(spot + adjacentSplitLength, splittee.length());
                    }
            if(splittee.startsWith(splitChar)) {
                splittee = splittee.substring(splitLength);
            }
            if(splittee.endsWith(splitChar)) { // Remove trailing splitter
                splittee = splittee.substring(0,splittee.length()-splitLength);
            }
        }
                Vector returns = new Vector();
        final int length = splittee.length(); // This is the new length
                int start = 0;
                spot = 0;
        while (start < length && (spot = splittee.indexOf(splitChar, start)) > -1) {
                        if (spot > 0) {
                                returns.addElement(splittee.substring(start, spot));
                        }
            else
            {
                returns.addElement(EMPTY_ELEMENT);
            }
                        start = spot + splitLength;
                }
                if (start < length) {
                        returns.add(splittee.substring(start));
                } else if (spot == length - splitLength){// Found splitChar at end of line
            returns.addElement(EMPTY_ELEMENT);
        }
                String[] values = new String[returns.size()];
                returns.copyInto(values);
                return values;
        }
       
public static void main(String[] args) {       
         //String language="";
         //格式:语言_国家/地区
         String language="zh_CN"; //zh_CN,zh_TW;"",es
       
        initLocale(language);
        System.out.println(getResStringDefault("monitor_legend_memory_per"));
        System.out.println(getResStringDefault("monitor_legend_load"));
}
}
作者: 小刀    时间: 2008-8-5 09:11
你好,一直以来都是用Jmeter,可是发现周围使用Jmeter的还是很少,从你的博客上可以看到,你对jmeter颇有研究,希望可以从你那里获得一些帮助。
作者: liangjz    时间: 2008-8-5 11:44
过奖了

有时间多请多关注。

阿里巴巴QA架构组刚刚评估完JMeter,作为对Loadrunner的一个强有力的补充
作者: getfly    时间: 2008-8-5 17:58
标题: 谢谢,请高手帮忙!!!!!!
使用JMETER得WORKBENCH来录制脚本,为什么不能录制呢?
我是要登录到另外一台机器上去录制脚本
远程登录到  http://***.***.***.***:8088/***
我应该怎么设置WORKBENCH 和 INTERNER 的 TOOLS--CONNECTIONS--LAN呢?
!!!!!!!!!!!!!!!!!!
作者: getfly    时间: 2008-8-5 18:01
“具体目录见:

图片:src\core\org\apache\jmeter\images
多语言文件:src\core\org\apache\jmeter\resources


核心的类是Locale和ResourceBundle。
具体的资源文件制作可以用UE或者专业的I18Nedit(可从sourceforge.net下载)“

这个 是 什么 意思 ?
作者: liangjz    时间: 2008-8-6 14:21
回楼上

可以用badboy录制

你下载源代码看目录结构就明白
作者: getfly    时间: 2008-8-7 15:38
谢谢,我知道可以用那个工具去录制
可是我们这边让用workbench来录制,我是想问为什么录制不上呢?
作者: qaarchitech    时间: 2008-8-7 19:15
参见http://www.51testing.com/?170805 ... e_itemid_89855.html
作者: lijian422202    时间: 2008-9-23 15:43
特别喜欢看liangjz的帖子,狠为什么我不在杭州,很期望自己是阿里巴巴QA架构组的一员




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2