001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TestXhtmlFormatter.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.cmf.format;
009:
010: import com.uwyn.rife.cmf.Content;
011: import com.uwyn.rife.cmf.MimeType;
012: import com.uwyn.rife.cmf.dam.exceptions.ContentManagerException;
013: import com.uwyn.rife.cmf.format.exceptions.InvalidContentDataTypeException;
014: import com.uwyn.rife.cmf.format.exceptions.UnreadableDataFormatException;
015: import com.uwyn.rife.cmf.loader.XhtmlContentLoader;
016: import com.uwyn.rife.cmf.transform.TextContentTransformer;
017: import com.uwyn.rife.tools.StringUtils;
018: import java.util.Map;
019: import junit.framework.TestCase;
020:
021: public class TestXhtmlFormatter extends TestCase {
022: public TestXhtmlFormatter(String name) {
023: super (name);
024: }
025:
026: public void testFormatBasic() throws Exception {
027: String data = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
028: + "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
029: + "<html><head><title>my title</title></head><body><p>some text \n"
030: + "<i>here</i> and <b>there</b></p></body></html>";
031: Content content = new Content(MimeType.APPLICATION_XHTML, data);
032: XhtmlFormatter formatter = new XhtmlFormatter();
033: String result = formatter.format(content, null);
034:
035: assertNotNull(result);
036:
037: assertEquals(data, result);
038: }
039:
040: public void testFormatInvalidDataType() throws Exception {
041: Content content = new Content(MimeType.APPLICATION_XHTML,
042: new Object());
043: XhtmlFormatter formatter = new XhtmlFormatter();
044: try {
045: formatter.format(content, null);
046: fail();
047: } catch (InvalidContentDataTypeException e) {
048: assertSame(String.class, e.getExpectedType());
049: assertSame(formatter, e.getFormatter());
050: assertSame(MimeType.APPLICATION_XHTML, e.getMimeType());
051: assertSame(Object.class, e.getReceivedType());
052: }
053: }
054:
055: public void testFormatCachedLoadedData() throws Exception {
056: String data = "<p>some text <i>here</i> and <b>there</b></p>";
057: Content content = new Content(MimeType.IMAGE_PNG, data);
058: String xhtml = new XhtmlContentLoader().load(data, true, null);
059: content.setCachedLoadedData(xhtml);
060:
061: XhtmlFormatter formatter = new XhtmlFormatter();
062: String result = formatter.format(content, null);
063:
064: assertNotNull(result);
065:
066: assertEquals(data, result);
067: }
068:
069: public void testFormatUnreadableData() throws Exception {
070: Content content = new Content(MimeType.APPLICATION_XHTML,
071: "<p>some text <i>here</b> and <b>there</i></blurp>");
072: XhtmlFormatter formatter = new XhtmlFormatter();
073: try {
074: formatter.format(content, null);
075: fail();
076: } catch (UnreadableDataFormatException e) {
077: assertSame(MimeType.APPLICATION_XHTML, e.getMimeType());
078: assertTrue(e.getErrors().size() > 0);
079: }
080: }
081:
082: public void testFormatTransformer() throws Exception {
083: String data = "<p>some text <i>here</i> and <b>there</b></p>";
084: Content content = new Content(MimeType.APPLICATION_XHTML, data)
085: .fragment(true);
086: XhtmlFormatter formatter = new XhtmlFormatter();
087: String result = formatter.format(content,
088: new XhtmlTransformer());
089:
090: assertNotNull(result);
091:
092: String transformed = "<p>some text <i>here</i> and <b>at home</b></p>";
093: assertEquals(transformed, result);
094: }
095:
096: static class XhtmlTransformer implements TextContentTransformer {
097: public String transform(String data,
098: Map<String, String> attributes)
099: throws ContentManagerException {
100: return StringUtils.replace(data, "there", "at home");
101: }
102: }
103: }
|