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

📄 testcmsmessagebundles.java

📁 找了很久才找到到源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        // for locales other than ENGLISH
        if (!Locale.ENGLISH.equals(locale)) {
            // compare the additional key names and params with the ENGLISH ones
            ResourceBundle resBundle = messages.getResourceBundle();
            ResourceBundle enResBundle = bundle.getBundle().getResourceBundle();
            bundleKeys = enResBundle.getKeys();
            while (bundleKeys.hasMoreElements()) {
                String bundleKey = (String)bundleKeys.nextElement();
                if (!bundleKey.toUpperCase().equals(bundleKey)) {
                    // additional key found
                    String keyValue;
                    try {
                        // try to retrieve the key for the given locale
                        keyValue = resBundle.getString(bundleKey);
                    } catch (MissingResourceException e) {
                        errorMessages.add("Additional key '" + bundleKey + "' missing.");
                        continue;
                    }
                    String enKeyValue = enResBundle.getString(bundleKey);
                    boolean[] args = new boolean[10];
                    for (int j = 0; j < args.length; j++) {
                        String arg = "{" + j;
                        int pos = enKeyValue.indexOf(arg);
                        args[j] = pos >= 0;
                    }
                    for (int j = 0; j < args.length; j++) {
                        String arg = "{" + j;
                        int pos = keyValue.indexOf(arg);
                        if ((pos < 0) && (args[j])) {
                            // not in locale bundle but in master bundle
                            errorMessages.add("Additional message '"
                                + keyValue
                                + "' for key '"
                                + bundleKey
                                + "' misses argument {"
                                + j
                                + "} from master bundle.");
                        } else if ((pos >= 0) && (!args[j])) {
                            // in locale bundle but not in master bundle
                            errorMessages.add("Additional message '"
                                + keyValue
                                + "' for key '"
                                + bundleKey
                                + "' contains argument {"
                                + j
                                + "} not used in master bundle.");
                        }
                    }
                }
            }
        }

        if (!errorMessages.isEmpty()) {
            String msg = "Errors for bundle '" + bundle.getBundleName() + "' and Locale '" + locale + "':";
            Iterator it = errorMessages.iterator();
            while (it.hasNext()) {
                msg += "\n     - ";
                msg += it.next();
            }
            return msg + "\n";
        }
        // in case there was no error, return an emtpy String
        return "";
    }

    /**
     * Returns the resource bundles to be excluded from additional locales tests.<p>
     * 
     * @param locale the locale to get the excluded bundles for
     * 
     * @return the resource bundles to be excluded from additional locales tests
     */
    protected List getExcludedLocalizedBundles(Locale locale) {

        if (Locale.ENGLISH.equals(locale)) {
            return new ArrayList();
        }
        if (m_excludedBundles.get(locale) == null) {
            List excludedBundles;
            List notLocalized = getNotLocalizedBundles(locale);
            if (notLocalized == null) {
                excludedBundles = new ArrayList();
            } else {
                excludedBundles = new ArrayList(notLocalized);
            }
            for (int i = 0; i < getTestMessageBundles().length; i++) {
                I_CmsMessageBundle bundle = getTestMessageBundles()[i];
                if (excludedBundles.contains(bundle)) {
                    continue;
                }
                boolean exclude = true;
                // test if the bundle contains keys prefixed by GUI_ or RPT_
                Field[] fields = bundle.getClass().getDeclaredFields();
                for (int j = 0; j < fields.length; j++) {
                    Field field = fields[j];
                    if (field.getType().equals(String.class)) {
                        // check all String fields
                        String key = field.getName();
                        if (key.startsWith(KEY_PREFIX_ERR)
                            || key.startsWith(KEY_PREFIX_GUI)
                            || key.startsWith(KEY_PREFIX_RPT)) {
                            exclude = false;
                            break;
                        }
                    }
                }
                if (!exclude) {
                    continue;
                }
                // test if the bundle contains additional keys
                CmsMessages messages = new CmsMessages(bundle.getBundleName(), Locale.ENGLISH);
                Enumeration bundleKeys = messages.getResourceBundle().getKeys();
                while (bundleKeys.hasMoreElements()) {
                    String bundleKey = (String)bundleKeys.nextElement();
                    if (!bundleKey.toUpperCase().equals(bundleKey)) {
                        exclude = false;
                        break;
                    }
                }
                if (exclude) {
                    excludedBundles.add(bundle);
                }
            }
            m_excludedBundles.put(locale, excludedBundles);
        }
        return (List)m_excludedBundles.get(locale);
    }

    /**
     * Prepares the test for the given bundle and locale and
     * returns a message bundle that DOES NOT include the default keys.<p>
     * 
     * @param bundle the resource bundle to prepare
     * @param locale the locale to prepare the resource bundle for
     * 
     * @return a message bundle that DOES NOT include the default keys
     * 
     * @throws IOException if something goes wrong
     */
    protected CmsMessages getMessageBundle(I_CmsMessageBundle bundle, Locale locale) throws IOException {

        if (Locale.ENGLISH.equals(locale)) {
            return bundle.getBundle(locale);
        }
        String source = getMessageBundleSourceName(bundle, locale);
        String fileName = CmsStringUtil.substitute(bundle.getBundleName(), ".", "/")
            + "_"
            + locale.toString()
            + ".properties";
        String target = TARGET_FOLDER + fileName;
        CmsFileUtil.copy(source, target);
        return new CmsMessages(bundle.getBundleName() + "_" + locale.toString(), locale);
    }

    /**
     * Returns the file name of the source message bundle.<p>
     * 
     * @param bundle the resource bundle to get the file name for
     * @param locale the locale to get the file name for
     * 
     * @return the file name of the source message bundle
     */
    protected String getMessageBundleSourceName(I_CmsMessageBundle bundle, Locale locale) {

        if (Locale.ENGLISH.equals(locale)) {
            return bundle.getBundleName();
        }
        String fileName = CmsStringUtil.substitute(bundle.getBundleName(), ".", "/")
            + "_"
            + locale.toString()
            + ".properties";
        String source = SOURCE_FOLDER_PREFIX
            + locale.toString()
            + SOURCE_FOLDER_INFIX
            + locale.toString()
            + SOURCE_FOLDER_SUFFIX
            + fileName;

        // if file from the localized folder is not readable take the file from the original module
        if (!new File(source).canRead()) {
            source = getModuleMessagesBundleSourceName(bundle, locale);
        }
        return source;
    }

    /**
     * Returns the file name of the source message bundle from the module.<p>
     * 
     * @param bundle the resource bundle to get the file name for
     * @param locale the locale to get the file name for
     * 
     * @return the file name of the source message bundle of the module
     */
    protected String getModuleMessagesBundleSourceName(I_CmsMessageBundle bundle, Locale locale) {

        if (Locale.ENGLISH.equals(locale)) {
            return bundle.getBundleName();
        }
        // substitute the last occuring "." of the bundle name with "/" to build the correct filename
        String packageName = bundle.getBundleName().substring(0, bundle.getBundleName().lastIndexOf('.'));
        packageName += "/";
        String source = "modules/"
            + packageName
            + "resources/system/modules/"
            + packageName
            + "classes/"
            + CmsStringUtil.substitute(bundle.getBundleName(), ".", "/")
            + "_"
            + locale.toString()
            + ".properties";
        return source;
    }

    /**
     * Returns a list of bundles not to be localized.<p>
     * 
     * @param locale the locale to get the not localized bundles for 
     * 
     * @return a list of bundles not to be localized
     */
    protected abstract List getNotLocalizedBundles(Locale locale);

    /**
     * Template method that has to be overwritten to return the <code>I_CmsMessageBundle</code> 
     * instances that will be tested.<p> 
     * 
     * @return the <code>I_CmsMessageBundle</code> instances to test: these will be the 
     *         singleton instances of the <code>Messages</code> classes residing in every localized package. 
     */
    protected abstract I_CmsMessageBundle[] getTestMessageBundles();

    /**
     * Checks all OpenCms internal message bundles if the are correctly build.<p>
     * 
     * @param locale the locale to test
     * 
     * @throws Exception if the test fails
     */
    protected void messagesBundleConstantTest(Locale locale) throws Exception {

        // the default locale MUST be ENGLISH (this call will also set the default locale to ENGLISH if required)
        assertEquals(CmsLocaleManager.getDefaultLocale(), Locale.ENGLISH);

        StringBuffer errors = new StringBuffer();
        I_CmsMessageBundle[] bundles = getTestMessageBundles();
        for (int i = 0; i < bundles.length; i++) {
            I_CmsMessageBundle bundle = bundles[i];
            String tmp = bundle.getBundleName();
            tmp = doPreTestBundle(bundle, locale);
            errors.append(tmp);
            if (CmsStringUtil.isEmpty(tmp)) {
                if (!getExcludedLocalizedBundles(locale).contains(bundle)) {
                    errors.append(doTestBundle(bundle, locale));
                }
            }
        }
        if (errors.length() > 0) {
            fail("\n" + errors);
        }
    }
}

⌨️ 快捷键说明

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