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

📄 testcmsstringutil.java

📁 找了很久才找到到源代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/test/org/opencms/util/TestCmsStringUtil.java,v $
 * Date   : $Date: 2007-08-23 12:11:53 $
 * Version: $Revision: 1.17 $
 *
 * 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.util;

import org.opencms.i18n.CmsEncoder;

import java.util.Arrays;
import java.util.List;

import junit.framework.TestCase;

/** 
 * Test cases for {@link org.opencms.util.CmsStringUtil}.<p>
 * 
 * @author Andreas Zahner 
 * @author Achim Westermann 
 * 
 * @version $Revision: 1.17 $
 */
public class TestCmsStringUtil extends TestCase {

    /**
     * Default JUnit constructor.<p>
     * 
     * @param arg0 JUnit parameters
     */
    public TestCmsStringUtil(String arg0) {

        super(arg0);
    }

    /**
     * Tests content replacement during import.<p>
     */
    public void testCmsContentReplacement() {

        String content, result, context, search, replace;

        content = "<html><body>\n"
            + "See <a href=\"http://www.opencms.org/opencms/opencms/opencms/index.html\">\n"
            + "http://www.opencms.org/opencms/opencms/opencms/index.html</a>\n"
            + "or <a href=\"/opencms/opencms/opencms/index.html\">\n"
            + "/opencms/opencms/opencms/index.html</a>\n"
            + "<img src=\"/opencms/opencms/system/galleries/pics/test/test.gif\">\n"
            + "<img src=\"http://www.othersite.org/opencms/opencms/system/galleries/pics/test/test.gif\">\n"
            + "Some URL in the Text: http://www.thirdsite.org/opencms/opencms/some/url.html.\n"
            + "Another URL in the Text: /opencms/opencms/some/url.html.\n"
            + "</body></html>\n";

        result = "<html><body>\n"
            + "See <a href=\"http://www.opencms.org/opencms/opencms/opencms/index.html\">\n"
            + "http://www.opencms.org/opencms/opencms/opencms/index.html</a>\n"
            + "or <a href=\""
            + CmsStringUtil.MACRO_OPENCMS_CONTEXT
            + "/opencms/index.html\">\n"
            + CmsStringUtil.MACRO_OPENCMS_CONTEXT
            + "/opencms/index.html</a>\n"
            + "<img src=\""
            + CmsStringUtil.MACRO_OPENCMS_CONTEXT
            + "/system/galleries/pics/test/test.gif\">\n"
            + "<img src=\"http://www.othersite.org/opencms/opencms/system/galleries/pics/test/test.gif\">\n"
            + "Some URL in the Text: http://www.thirdsite.org/opencms/opencms/some/url.html.\n"
            + "Another URL in the Text: "
            + CmsStringUtil.MACRO_OPENCMS_CONTEXT
            + "/some/url.html.\n"
            + "</body></html>\n";

        context = "/opencms/opencms/";

        // search = "([>\"']\\s*)" + context;
        search = "([^\\w/])" + context;
        replace = "$1" + CmsStringUtil.escapePattern(CmsStringUtil.MACRO_OPENCMS_CONTEXT) + "/";

        String test = CmsStringUtil.substitutePerl(content, search, replace, "g");

        System.err.println(this.getClass().getName() + ".testCmsContentReplacement():");
        System.err.println(test);
        assertEquals(test, result);

        test = CmsStringUtil.substituteContextPath(content, context);
        assertEquals(test, result);
    }

    /**
     * Combined tests.<p>
     */
    public void testCombined() {

        String test;
        String content = "<p>A paragraph with text...<img src=\"/opencms/opencms/empty.gif\"></p>\n<a href=\"/opencms/opencms/test.jpg\">";
        String search = "/opencms/opencms/";
        String replace = "${path}";
        test = CmsStringUtil.substitute(content, search, replace);
        assertEquals(
            test,
            "<p>A paragraph with text...<img src=\"${path}empty.gif\"></p>\n<a href=\"${path}test.jpg\">");
        test = CmsStringUtil.substitute(test, replace, search);
        assertEquals(
            test,
            "<p>A paragraph with text...<img src=\"/opencms/opencms/empty.gif\"></p>\n<a href=\"/opencms/opencms/test.jpg\">");
    }

    /**
     * Tests for complext import patterns.<p>
     */
    public void testComplexPatternForImport() {

        String content = "<cms:link>/pics/test.gif</cms:link> <img src=\"/pics/test.gif\"> script = '/pics/test.gif' <cms:link> /pics/othertest.gif </cms:link>\n"
            + "<cms:link>/mymodule/pics/test.gif</cms:link> <img src=\"/mymodule/pics/test.gif\"> script = '/mymodule/pics/test.gif' <cms:link> /mymodule/system/galleries/pics/othertest.gif </cms:link>";
        String search = "([>\"']\\s*)/pics/";
        String replace = "$1/system/galleries/pics/";
        String test = CmsStringUtil.substitutePerl(content, search, replace, "g");
        assertEquals(
            test,
            "<cms:link>/system/galleries/pics/test.gif</cms:link> <img src=\"/system/galleries/pics/test.gif\"> script = '/system/galleries/pics/test.gif' <cms:link> /system/galleries/pics/othertest.gif </cms:link>\n"
                + "<cms:link>/mymodule/pics/test.gif</cms:link> <img src=\"/mymodule/pics/test.gif\"> script = '/mymodule/pics/test.gif' <cms:link> /mymodule/system/galleries/pics/othertest.gif </cms:link>");
    }

    /**
     * Tests for the escape patterns.<p>
     */
    public void testEscapePattern() {

        String test;
        test = CmsStringUtil.escapePattern("/opencms/opencms");
        assertEquals(test, "\\/opencms\\/opencms");
        test = CmsStringUtil.escapePattern("/opencms/$");
        assertEquals(test, "\\/opencms\\/\\$");
    }

    /**
     * Tests the body tag extraction.<p> 
     */
    public void testExtractHtmlBody() {

        String content, result;
        String innerContent = "This is body content in the body\n<h1>A headline</h1>\nSome text in the body\n";

        content = "<html><body>" + innerContent + "</body></html>";
        result = CmsStringUtil.extractHtmlBody(content);
        assertEquals(result, innerContent);

        content = "<html><body style='css' background-color:#ffffff>" + innerContent + "</body></html>";
        result = CmsStringUtil.extractHtmlBody(content);
        assertEquals(result, innerContent);

        content = "<html>\n<title>Test</title>\n<body style='css' background-color:#ffffff>"
            + innerContent
            + "</body>\n</html>";
        result = CmsStringUtil.extractHtmlBody(content);
        assertEquals(result, innerContent);

        content = "<html>< body style='css' background-color:#ffffff>" + innerContent + "</ BODY>";
        result = CmsStringUtil.extractHtmlBody(content);
        assertEquals(result, innerContent);

        content = "<BODY>" + innerContent + "</boDY></html></body><body>somemoretext</BODY>";
        result = CmsStringUtil.extractHtmlBody(content);
        assertEquals(result, innerContent);

        content = innerContent + "</boDY></html>";
        result = CmsStringUtil.extractHtmlBody(content);
        assertEquals(result, innerContent);

        content = "<html><BODY>" + innerContent;
        result = CmsStringUtil.extractHtmlBody(content);
        assertEquals(result, innerContent);

        content = innerContent;
        result = CmsStringUtil.extractHtmlBody(content);
        assertEquals(result, innerContent);
    }

    /**
     * Tests the xml encoding extraction.<p>
     */
    public void testExtractXmlEncoding() {

        String xml, result;

        xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
            + "<!DOCTYPE opencms SYSTEM"
            + "\"http://www.opencms.org/dtd/6.0/opencms-importexport.dtd\">\n"
            + "<opencms/>";

        result = CmsStringUtil.extractXmlEncoding(xml);
        assertEquals(result, CmsEncoder.ENCODING_UTF_8);

        xml = "<?xml version=\"1.0\" encoding='ISO-8859-1'?>\n" + "<opencms/>";

        result = CmsStringUtil.extractXmlEncoding(xml);
        assertEquals(result, "ISO-8859-1");
    }

    /**
     * Tests for the resource name formatting.<p>
     */
    public void testFormatResourceName() {

        String test;
        test = "/xmlcontentdemo/list.jsp";
        assertEquals("/.../list.jsp", CmsStringUtil.formatResourceName(test, 10));
        test = "/xmlcontentdemo/list.jsp";
        assertEquals("/xmlcontentdemo/list.jsp", CmsStringUtil.formatResourceName(test, 25));
        test = "/averylongresourcename.jsp";
        assertEquals("/averylongresourcename.jsp", CmsStringUtil.formatResourceName(test, 25));
        test = "/folder1/folder2/averylongresourcename.jsp";

⌨️ 快捷键说明

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