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

📄 testcmsxmlpage.java

📁 找了很久才找到到源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        page.addValue("anotherbody", Locale.FRENCH);

        names = page.getNames(Locale.FRENCH);
        assertEquals(3, names.size());
        assertTrue(names.contains("newbody"));
        assertTrue(names.contains("newbody2"));
        assertTrue(names.contains("anotherbody"));

        page.removeValue("body2", Locale.ENGLISH);
        names = page.getNames(Locale.ENGLISH);
        assertEquals(1, names.size());
        assertTrue(names.contains("body"));

        page.removeLocale(Locale.GERMAN);
        names = page.getNames(Locale.GERMAN);
        assertEquals(0, names.size());

        boolean success = false;
        try {
            page.addValue("body[0]", Locale.ENGLISH);
        } catch (CmsIllegalArgumentException e) {
            success = true;
        }
        if (!success) {
            throw new Exception("Multiple element name creation possible");
        }

        success = false;
        try {
            page.addValue("body[1]", Locale.ENGLISH);
        } catch (CmsIllegalArgumentException e) {
            success = true;
        }
        if (!success) {
            throw new Exception("Page element name creation with index [1] possible");
        }
    }

    /**
     * Tests acessing XML page values via locales.<p> 
     *
     * @throws Exception in case something goes wrong
     */
    public void testXmlPageLocaleAccess() throws Exception {

        // create a XML entity resolver
        CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(null);

        CmsXmlPage page;
        String content;

        content = CmsFileUtil.readFile("org/opencms/xml/page/xmlpage-2.xml", UTF8);
        page = CmsXmlPageFactory.unmarshal(content, UTF8, resolver);

        List locales;

        locales = page.getLocales("body");
        assertEquals(2, locales.size());
        assertTrue(locales.contains(Locale.ENGLISH));
        assertTrue(locales.contains(Locale.GERMAN));

        locales = page.getLocales("body2");
        assertEquals(1, locales.size());
        assertTrue(locales.contains(Locale.ENGLISH));
    }

    
    /**
     * Tests copying, moving and removing locales from a XML page.<p> 
     *
     * @throws Exception in case something goes wrong
     */
    public void testXmlPageLocaleCopyMoveRemove() throws Exception {

        // create a XML entity resolver
        CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(null);

        CmsXmlPage page;
        String content;

        content = CmsFileUtil.readFile("org/opencms/xml/page/xmlpage-2.xml", UTF8);
        page = CmsXmlPageFactory.unmarshal(content, UTF8, resolver);

        
        assertEquals(2, page.getLocales().size());
        assertTrue(page.hasLocale(Locale.ENGLISH));
        assertTrue(page.hasLocale(Locale.GERMAN));

        page.copyLocale(Locale.GERMAN, Locale.FRENCH);
        assertEquals(3, page.getLocales().size());
        assertTrue(page.hasLocale(Locale.ENGLISH));
        assertTrue(page.hasLocale(Locale.GERMAN));
        assertTrue(page.hasLocale(Locale.FRENCH));

        page.moveLocale(Locale.FRENCH, Locale.ITALIAN);
        assertEquals(3, page.getLocales().size());
        assertTrue(page.hasLocale(Locale.ENGLISH));
        assertTrue(page.hasLocale(Locale.GERMAN));
        assertTrue(page.hasLocale(Locale.ITALIAN));
        
        page.removeLocale(Locale.ITALIAN);
        assertEquals(2, page.getLocales().size());
        assertTrue(page.hasLocale(Locale.ENGLISH));
        assertTrue(page.hasLocale(Locale.GERMAN));
        
        page.removeLocale(Locale.ENGLISH);
        assertEquals(1, page.getLocales().size());
        assertTrue(page.hasLocale(Locale.GERMAN));
    }
    
    /**
     * Tests reading elements from the updated, final version of the XML page.<p> 
     * 
     * @throws Exception  in case something goes wrong
     */
    public void testXmlPageReadFinalVersion() throws Exception {

        // create a XML entity resolver
        CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(null);

        CmsXmlPage page;
        String content;

        // validate "final" xmlpage 1
        content = CmsFileUtil.readFile("org/opencms/xml/page/xmlpage-1.xml", UTF8);
        page = CmsXmlPageFactory.unmarshal(content, UTF8, resolver);
        assertTrue(page.hasValue("body", Locale.ENGLISH));
        CmsLinkTable table = page.getLinkTable("body", Locale.ENGLISH);
        assertTrue(table.getLink("link0").isInternal());
        assertEquals("English! Image <img src=\"/sites/default/folder1/image2.gif\" />", page.getStringValue(
            null,
            "body",
            Locale.ENGLISH));

        // validate "final" xmlpage 2
        content = CmsFileUtil.readFile("org/opencms/xml/page/xmlpage-2.xml", UTF8);
        page = CmsXmlPageFactory.unmarshal(content, UTF8, resolver);
        assertTrue(page.hasValue("body", Locale.ENGLISH));
        assertTrue(page.hasValue("body", Locale.GERMAN));
        assertTrue(page.hasValue("body2", Locale.ENGLISH));
        assertEquals("English! Image <img src=\"/sites/default/folder1/image2.gif\" />", page.getStringValue(
            null,
            "body",
            Locale.ENGLISH));
        assertEquals("English 2!", page.getStringValue(null, "body2", Locale.ENGLISH));
        assertEquals("Deutsch! Image <img src=\"/sites/default/folder1/image2.gif\" />", page.getStringValue(
            null,
            "body",
            Locale.GERMAN));
    }

    /**
     * Tests reading elements from the "old", pre 5.5.0 version of the XML page.<p>
     * 
     * @throws Exception in case something goes wrong
     */
    public void testXmlPageReadOldVersion() throws Exception {

        // create a XML entity resolver
        CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(null);

        CmsXmlPage page;
        String content;

        // validate "old" xmlpage 1
        content = CmsFileUtil.readFile("org/opencms/xml/page/xmlpage-old-1.xml", UTF8);
        page = CmsXmlPageFactory.unmarshal(content, UTF8, resolver);
        assertTrue(page.hasValue("body", Locale.ENGLISH));
        CmsLinkTable table = page.getLinkTable("body", Locale.ENGLISH);
        assertTrue(table.getLink("link0").isInternal());
        assertEquals("English! Image <img src=\"/sites/default/folder1/image2.gif\" />", page.getStringValue(
            null,
            "body",
            Locale.ENGLISH));

        // validate "old" xmlpage 2
        content = CmsFileUtil.readFile("org/opencms/xml/page/xmlpage-old-2.xml", UTF8);
        page = CmsXmlPageFactory.unmarshal(content, UTF8, resolver);
        assertTrue(page.hasValue("body", Locale.ENGLISH));
        assertTrue(page.hasValue("body", Locale.GERMAN));
        assertTrue(page.hasValue("body2", Locale.ENGLISH));
        assertEquals("English! Image <img src=\"/sites/default/folder1/image2.gif\" />", page.getStringValue(
            null,
            "body",
            Locale.ENGLISH));
        assertEquals("English 2!", page.getStringValue(null, "body2", Locale.ENGLISH));
        assertEquals("Deutsch! Image <img src=\"/sites/default/folder1/image2.gif\" />", page.getStringValue(
            null,
            "body",
            Locale.GERMAN));
    }

    /**
     * Tests accessing element names in the XML page.<p>
     * 
     * @throws Exception in case something goes wrong
     */
    public void testXmlPageRenameElement() throws Exception {

        // create a XML entity resolver for test case
        CmsXmlContentTypeManager.createTypeManagerForTestCases();
        CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(null);

        System.out.println("Testing renaming element in the XML page\n");

        // load stored XML page
        String pageStr = CmsFileUtil.readFile("org/opencms/xml/page/xmlpage-2.xml", UTF8);

        // create a new XML page with this content
        CmsXmlPage page = CmsXmlPageFactory.unmarshal(pageStr, UTF8, resolver);

        page.renameValue("body2", "bodyNEW", Locale.ENGLISH);
        page.validateXmlStructure(resolver);
        page.marshal();
        // check if page has the element 'body2NEW'
        assertTrue(page.hasValue("bodyNEW", Locale.ENGLISH));
        // check if page has the element 'body2'
        assertFalse(page.hasValue("body2", Locale.ENGLISH));
        System.out.println(page.toString());
    }

    /**
     * Tests writing elements to the updated, final version of the XML page.<p> 
     *
     * @throws Exception in case something goes wrong
     */
    public void testXmlPageWriteFinalVersion() throws Exception {

        // create a XML entity resolver
        CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(null);

        CmsXmlPage page;
        String content;

        // validate "final" xmlpage 1
        content = CmsFileUtil.readFile("org/opencms/xml/page/xmlpage-1.xml", UTF8);
        page = CmsXmlPageFactory.unmarshal(content, UTF8, resolver);
        page.addValue("body3", Locale.ENGLISH);
        page.setStringValue(null, "body3", Locale.ENGLISH, "English WRITTEN! Image <img src=\"/test/image.gif\" />");
        assertTrue(page.hasValue("body3", Locale.ENGLISH));
        CmsLinkTable table = page.getLinkTable("body3", Locale.ENGLISH);
        assertTrue(table.getLink("link0").isInternal());
        assertEquals("English WRITTEN! Image <img alt=\"\" src=\"/test/image.gif\" />", page.getStringValue(
            null,
            "body3",
            Locale.ENGLISH));
    }

    /**
     * Tests writing elements to the "old", pre 5.5.0 version of the XML page.<p>
     * 
     * @throws Exception in case something goes wrong
     */
    public void testXmlPageWriteOldVersion() throws Exception {

        // create a XML entity resolver
        CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(null);

        CmsXmlPage page;
        String content;

        // validate "old" xmlpage 1
        content = CmsFileUtil.readFile("org/opencms/xml/page/xmlpage-old-1.xml", UTF8);
        page = CmsXmlPageFactory.unmarshal(content, UTF8, resolver);
        page.addValue("body3", Locale.ENGLISH);
        page.setStringValue(null, "body3", Locale.ENGLISH, "English WRITTEN! Image <img src=\"/test/image.gif\" />");
        assertTrue(page.hasValue("body3", Locale.ENGLISH));
        CmsLinkTable table = page.getLinkTable("body3", Locale.ENGLISH);
        assertTrue(table.getLink("link0").isInternal());
        assertEquals("English WRITTEN! Image <img alt=\"\" src=\"/test/image.gif\" />", page.getStringValue(
            null,
            "body3",
            Locale.ENGLISH));
    }
}

⌨️ 快捷键说明

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