⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testcmsmessagebundles.java

📁 找了很久才找到到源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/test/org/opencms/i18n/TestCmsMessageBundles.java,v $
 * Date   : $Date: 2007-08-13 16:30:12 $
 * Version: $Revision: 1.16 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Management System
 *
 * Copyright (c) 2002 - 2007 Alkacon Software GmbH (http://www.alkacon.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * For further information about Alkacon Software GmbH, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project website: http://www.opencms.org
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.opencms.i18n;

import org.opencms.util.CmsFileUtil;
import org.opencms.util.CmsStringUtil;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import junit.framework.TestCase;

/**
 * Tests for the CmsMessageBundles.<p>
 * 
 * @author Alexander Kandzior 
 * 
 * @version $Revision: 1.16 $
 * 
 * @since 6.0.0
 */
public abstract class TestCmsMessageBundles extends TestCase {

    /** Prefix for the error messages in the bundles. */
    private static final String KEY_PREFIX_ERR = "ERR_";

    /** Prefix for the gui messages in the bundles. */
    private static final String KEY_PREFIX_GUI = "GUI_";

    /** Prefix for the initialization messages in the bundles. */
    private static final String KEY_PREFIX_INIT = "INIT_";

    /** Prefix for the log messages in the bundles. */
    private static final String KEY_PREFIX_LOG = "LOG_";

    /** Prefix for the report messages in the bundles. */
    private static final String KEY_PREFIX_RPT = "RPT_";

    /** The source folder to copy the resource bundles from. */
    private static final String SOURCE_FOLDER_INFIX = "/resources/system/workplace/locales/";

    /** The source folder to copy the resource bundles from. */
    private static final String SOURCE_FOLDER_PREFIX = "modules/org.opencms.locale.";

    /** The source folder to copy the resource bundles from. */
    private static final String SOURCE_FOLDER_SUFFIX = "/messages/";

    /** The taget folder to copy the resource bundles to. */
    private static final String TARGET_FOLDER = "bin/";

    /** Cache the resource bundle to exclude from additional locales tests. */
    private Map m_excludedBundles = new HashMap();

    /**
     * Checks all message bundles for the DE locale.<p>
     * 
     * @throws Exception if the test fails
     */
    public void testLocale_DE_MessagesBundles() throws Exception {

        messagesBundleConstantTest(Locale.GERMAN);
    }

    /**
     * Checks all message bundles for the EN locale.<p>
     * 
     * @throws Exception if the test fails
     */
    public void testLocale_EN_MessagesBundles() throws Exception {

        messagesBundleConstantTest(Locale.ENGLISH);
    }

    /**
     * Performs some key and language independent tests.<p>
     * 
     * @param bundle the bundle to test
     * @param locale the locale to test
     * 
     * @return a description of all errors found
     */
    protected String doPreTestBundle(I_CmsMessageBundle bundle, Locale locale) {

        String className = bundle.getClass().getName();
        if (!className.endsWith(".Messages")) {
            return "Bundle '" + className + "' is not a 'Messages' class.\n";
        }
        if (!className.toLowerCase().equals(bundle.getBundleName())) {
            return "Bundle '" + bundle.getBundleName() + "' has not the form: packagename.messages.\n";
        }
        if (!bundle.getBundleName().endsWith(".messages")) {
            return "The Message bundle '"
                + bundle.getBundleName()
                + "' does not ends with: '.messages'. \n "
                + "Change the constant literal ('private static final String BUNDLE_NAME')\n";
        }
        String fileName = getMessageBundleSourceName(bundle, locale);
        if (!Locale.ENGLISH.equals(locale)) {
            if (!(new File(fileName)).canRead()) {
                return "Bundle '" + className + "' has no input for locale '" + locale + "'.\n";
            }
        }
        // in case of no errors, return the emtpy String 
        return "";
    }

    /**
     * Tests an individual message bundle.<p>
     * 
     * @param bundle the bundle to test
     * @param locale the locale to test
     * 
     * @return a description of all errors found
     * 
     * @throws Exception if the test fails
     */
    protected String doTestBundle(I_CmsMessageBundle bundle, Locale locale) throws Exception {

        List keys = new ArrayList();

        System.out.println("\nValidating all keys in bundle " + bundle.getBundleName() + " for locale " + locale + ":");

        CmsMessages messages = getMessageBundle(bundle, locale);

        List errorMessages = new ArrayList();
        // use reflection on all member constants
        Field[] fields = bundle.getClass().getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            if (field.getType().equals(String.class)) {
                // check all String fields
                String key = field.getName();

                String value;
                try {
                    value = (String)field.get(bundle);
                } catch (IllegalAccessException e) {
                    continue;
                }

                System.out.println("Validating key '" + key + "': '" + messages.key(key) + "'");

                // ensure the name id identical to the value
                if (!key.equals(value)) {
                    errorMessages.add("Key '" + key + "' has bad value '" + value + "'.");
                }
                // check if key exists in bundle for constant
                String message = messages.key(key);
                boolean isPresent = !CmsMessages.isUnknownKey(message);
                boolean testKeyValue = false;
                if (Locale.ENGLISH.equals(locale)) {
                    testKeyValue = true;
                } else {
                    boolean isAdditionalKey = !key.toUpperCase().equals(key);
                    testKeyValue = (isAdditionalKey || key.startsWith(KEY_PREFIX_ERR) || key.startsWith(KEY_PREFIX_GUI) || key.startsWith(KEY_PREFIX_RPT));
                }
                if (testKeyValue && !isPresent) {
                    errorMessages.add("No message for '" + key + "' in bundle.");
                }

                // ensure key has the form
                // "{ERR|LOG|INIT|GUI|RPT}_KEYNAME_{0-9}";
                if (key.length() < 7) {
                    errorMessages.add("Key '" + key + "' is too short (length must be at least 7).");
                }
                if (!key.equals(key.toUpperCase())) {
                    errorMessages.add("Key '" + key + "' must be all upper case.");
                }
                if ((key.charAt(key.length() - 2) != '_')
                    || (!key.startsWith(KEY_PREFIX_ERR)
                        && !key.startsWith(KEY_PREFIX_GUI)
                        && !key.startsWith(KEY_PREFIX_INIT)
                        && !key.startsWith(KEY_PREFIX_LOG) && !key.startsWith(KEY_PREFIX_RPT))) {
                    errorMessages.add("Key '" + key + "' must have the form {ERR|LOG|INIT|GUI|RPT}_KEYNAME_{0-9}.");
                }
                int argCount = Integer.valueOf(key.substring(key.length() - 1)).intValue();

                if (testKeyValue && isPresent) {
                    for (int j = 0; j < argCount; j++) {
                        String arg = "{" + j;
                        int pos = message.indexOf(arg);
                        if (pos < 0) {
                            errorMessages.add("Message '"
                                + message
                                + "' for key '"
                                + key
                                + "' misses argument {"
                                + j
                                + "}.");
                        }
                    }
                    for (int j = argCount; j < 10; j++) {
                        String arg = "{" + j;
                        int pos = message.indexOf(arg);
                        if (pos >= 0) {
                            errorMessages.add("Message '"
                                + message
                                + "' for key '"
                                + key
                                + "' contains unused argument {"
                                + j
                                + "}.");
                        }
                    }
                }
                // store this key for later check against all properties in the bundle
                keys.add(key);
            }
        }

        Enumeration bundleKeys = messages.getResourceBundle().getKeys();
        while (bundleKeys.hasMoreElements()) {
            String bundleKey = (String)bundleKeys.nextElement();
            if (bundleKey.toUpperCase().equals(bundleKey)) {
                // only check keys which are all upper case
                if (!keys.contains(bundleKey)) {
                    errorMessages.add("Bundle contains unreferenced message '" + bundleKey + "'.");
                }
            } else {
                System.out.println("Additional key '" + bundleKey + "' in bundle.");
            }
        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -