All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opencms.util.TestCmsHtmlConverter Maven / Gradle / Ivy

Go to download

OpenCms is an enterprise-ready, easy to use website content management system based on Java and XML technology. Offering a complete set of features, OpenCms helps content managers worldwide to create and maintain beautiful websites fast and efficiently.

There is a newer version: 18.0
Show newest version
/*
 * This library is part of OpenCms -
 * the Open Source Content Management System
 *
 * Copyright (c) Alkacon Software GmbH & Co. KG (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 & Co. KG, 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 org.opencms.test.OpenCmsTestCase;
import org.opencms.test.OpenCmsTestProperties;

import java.io.File;

import junit.extensions.TestSetup;
import junit.framework.Test;
import junit.framework.TestSuite;

/**
 */
public class TestCmsHtmlConverter extends OpenCmsTestCase {

    private static final String SIMPLE_HTML = "

Test

This is a test

some content

last line

Some pre
\r\n More pre\r\n
Final line."; // some test Strings private static final String STRING_1 = "Test: äöüÄÖÜß"; private static final String STRING_1_UTF8_RESULT = "Test: \u00e4\u00f6\u00fc\u00c4\u00d6\u00dc\u00df"; private static final String STRING_2 = "Test: äöüÄÖÜ߀"; private static final String STRING_2_UTF8_RESULT = "Test: \u00e4\u00f6\u00fc\u00c4\u00d6\u00dc\u00df\u20ac"; /** * Default JUnit constructor.

* * @param arg0 JUnit parameters */ public TestCmsHtmlConverter(String arg0) { super(arg0); } /** * Test suite for this test class.

* * @return the test suite */ public static Test suite() { OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH); TestSuite suite = new TestSuite(); suite.setName(TestCmsHtmlConverter.class.getName()); suite.addTest(new TestCmsHtmlConverter("testISO")); suite.addTest(new TestCmsHtmlConverter("testUTF8")); suite.addTest(new TestCmsHtmlConverter("testPrettyPrint")); suite.addTest(new TestCmsHtmlConverter("testRemoveWordTags")); suite.addTest(new TestCmsHtmlConverter("testHrefWhitespaceIssue")); TestSetup wrapper = new TestSetup(suite) { @Override protected void setUp() { setupOpenCms("simpletest", "/"); } @Override protected void tearDown() { removeOpenCms(); } }; return wrapper; } /** * Tests an issue with white space insertion after href tags.

* * Tidy sometimes inserts unwanted, visible whitespace in a href tag because of * the formatting used.

* * @throws Exception in case the test fails */ public void testHrefWhitespaceIssue() throws Exception { System.out.println("Testing href whitespace issue"); CmsHtmlConverter converter = new CmsHtmlConverter(CmsEncoder.ENCODING_UTF_8, CmsHtmlConverter.PARAM_XHTML); String input = CmsFileUtil.readFile("org/opencms/util/testConverter_03.html", CmsEncoder.ENCODING_ISO_8859_1); // the input has the right (that is no) white-spacing between the tags assertContains(input, ")."); String output = converter.convertToString(input); System.out.println("----------------"); System.out.println(output); System.out.println("----------------"); // PARAM_XHTML will cause closing tags on new lines assertContains(output, "" + System.getProperty("line.separator") + ")."); } /** * Tests conversion of ISO-encoded entities.

* * @throws Exception in case the test fails */ public void testISO() throws Exception { System.out.println("Testing US-ASCII conversion"); CmsHtmlConverter converter = new CmsHtmlConverter(CmsEncoder.ENCODING_US_ASCII, CmsHtmlConverter.PARAM_WORD); String convertedHtml1 = converter.convertToString(STRING_1); String convertedHtml2 = converter.convertToString(STRING_2); assertEquals(STRING_1, convertedHtml1); assertEquals(STRING_2, convertedHtml2); } /** * Tests if simple pretty printing works.

* * @throws Exception in case the test fails */ public void testPrettyPrint() throws Exception { System.out.println("Testing HTML pretty printing"); CmsHtmlConverter converter = new CmsHtmlConverter( CmsEncoder.ENCODING_ISO_8859_1, CmsHtmlConverter.PARAM_ENABLED); String result = converter.convertToString(SIMPLE_HTML); System.out.println("----------------"); System.out.println(result); System.out.println("----------------"); } /** * Tests if all word tags are removed.

* * @throws Exception in case the test fails */ public void testRemoveWordTags() throws Exception { System.out.println("Testing Word conversion"); CmsHtmlConverter converter = new CmsHtmlConverter( CmsEncoder.ENCODING_ISO_8859_1, CmsHtmlConverter.PARAM_WORD + ";" + CmsHtmlConverter.PARAM_XHTML); // read a file and convert it File inputfile = new File(getTestDataPath("test2.html")); byte[] htmlInput = CmsFileUtil.readFile(inputfile); String outputContent = converter.convertToString(htmlInput); System.out.println(outputContent); // now check if all word specific tags are removed assertContainsNot(outputContent, ""); assertContainsNot(outputContent, " * * @throws Exception in case the test fails */ public void testUTF8() throws Exception { System.out.println("Testing UTF-8 conversion"); CmsHtmlConverter converter = new CmsHtmlConverter(CmsEncoder.ENCODING_UTF_8, CmsHtmlConverter.PARAM_WORD); String convertedHtml1 = converter.convertToString(STRING_1); String convertedHtml2 = converter.convertToString(STRING_2); assertEquals(STRING_1_UTF8_RESULT, convertedHtml1); assertEquals(STRING_2_UTF8_RESULT, convertedHtml2); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy