001: package com.opensymphony.module.sitemesh.html;
002:
003: import com.opensymphony.module.sitemesh.html.util.CharArray;
004: import com.opensymphony.module.sitemesh.html.rules.TagReplaceRule;
005:
006: import junit.framework.TestCase;
007:
008: import java.io.Reader;
009: import java.io.StringReader;
010: import java.io.Writer;
011: import java.io.StringWriter;
012: import java.io.IOException;
013:
014: public class HTMLProcessorTest extends TestCase {
015:
016: public void testCreatesStateTransitionEvent() throws IOException {
017: char[] input = "<a></a>".toCharArray();
018: HTMLProcessor htmlProcessor = new HTMLProcessor(input,
019: new CharArray(128));
020:
021: State defaultState = htmlProcessor.defaultState();
022:
023: final StringBuffer stateLog = new StringBuffer();
024:
025: defaultState.addListener(new StateChangeListener() {
026: public void stateFinished() {
027: stateLog.append("finished");
028: }
029: });
030:
031: htmlProcessor.process();
032: assertEquals("finished", stateLog.toString());
033: }
034:
035: public void testSupportsConventionalReaderAndWriter()
036: throws IOException {
037: Reader in = new StringReader(
038: "<hello><b id=\"something\">world</b></hello>");
039: Writer out = new StringWriter();
040:
041: HTMLProcessor processor = new HTMLProcessor(in, out);
042: processor.addRule(new TagReplaceRule("b", "strong"));
043:
044: processor.process();
045: assertEquals(
046: "<hello><strong id=\"something\">world</strong></hello>",
047: out.toString());
048: }
049:
050: public void testAllowsRulesToModifyAttributes() throws IOException {
051: Reader in = new StringReader(
052: "<hello><a href=\"modify-me\">world</a></hello>");
053: Writer out = new StringWriter();
054:
055: HTMLProcessor processor = new HTMLProcessor(in, out);
056: processor.addRule(new BasicRule("a") {
057: public void process(Tag tag) {
058: CustomTag customTag = new CustomTag(tag);
059: String href = customTag
060: .getAttributeValue("href", false);
061: if (href != null) {
062: href = href.toUpperCase();
063: customTag.setAttributeValue("href", true, href);
064: }
065: customTag.writeTo(currentBuffer());
066: }
067: });
068:
069: processor.process();
070: assertEquals("<hello><a href=\"MODIFY-ME\">world</a></hello>",
071: out.toString());
072: }
073:
074: public void testSupportsChainedFilteringOfTextContent()
075: throws IOException {
076: Reader in = new StringReader("<hello>world</hello>");
077: Writer out = new StringWriter();
078:
079: HTMLProcessor processor = new HTMLProcessor(in, out);
080: processor.addTextFilter(new TextFilter() {
081: public String filter(String text) {
082: return text.toUpperCase();
083: }
084: });
085: processor.addTextFilter(new TextFilter() {
086: public String filter(String text) {
087: return text.replaceAll("O", "o");
088: }
089: });
090:
091: processor.process();
092: assertEquals("<HELLo>WoRLD</HELLo>", out.toString());
093: }
094:
095: public void testSupportsTextFiltersForSpecificStates()
096: throws IOException {
097: Reader in = new StringReader(
098: "la la<br> la la <capitalism>laaaa<br> laaaa</capitalism> la la");
099: Writer out = new StringWriter();
100:
101: HTMLProcessor processor = new HTMLProcessor(in, out);
102:
103: State capsState = new State();
104: processor.addRule(new StateTransitionRule("capitalism",
105: capsState, true));
106:
107: capsState.addTextFilter(new TextFilter() {
108: public String filter(String text) {
109: return text.toUpperCase();
110: }
111: });
112:
113: processor.process();
114: assertEquals(
115: "la la<br> la la <capitalism>LAAAA<BR> LAAAA</capitalism> la la",
116: out.toString());
117: }
118:
119: public void testCanAddAttributesToCustomTag() throws IOException {
120: CharArray buffer = new CharArray(64);
121: String html = "<h1>Headline</h1>";
122: HTMLProcessor htmlProcessor = new HTMLProcessor(html
123: .toCharArray(), buffer);
124: htmlProcessor.addRule(new BasicRule() {
125: public boolean shouldProcess(String tag) {
126: return tag.equalsIgnoreCase("h1");
127: }
128:
129: public void process(Tag tag) {
130: if (tag.getType() == Tag.OPEN) {
131: CustomTag ctag = new CustomTag(tag);
132: ctag.addAttribute("class", "y");
133: assertEquals(1, ctag.getAttributeCount());
134: tag = ctag;
135: }
136: tag.writeTo(currentBuffer());
137: }
138: });
139: htmlProcessor.process();
140: assertEquals("<h1 class=\"y\">Headline</h1>", buffer.toString());
141: }
142: }
|