1 package com.opensymphony.module.sitemesh.html.tokenizer;
2
3 import com.opensymphony.module.sitemesh.html.Tag;
4 import com.opensymphony.module.sitemesh.html.Text;
5 import junit.framework.Assert;
6
7 class MockTokenHandler implements TokenHandler {
8
9 private StringBuffer expected = new StringBuffer();
10 private StringBuffer actual = new StringBuffer();
11
12 public void expectText(String tag) {
13 expected.append(tag);
14 }
15
16 public void expectTag(int type, String tag) {
17 expectTag(type, tag, new String[0]);
18 }
19
20 public void expectTag(int type, String tag, String[] attributes) {
21 expected.append("{{TAG : ").append(tag);
22 for (int i = 0; i < attributes.length; i+=2) {
23 expected.append(' ').append(attributes[i]).append("=\"").append(attributes[i + 1]).append('"');
24 }
25 expected.append(' ').append(typeAsString(type)).append("}}");
26 }
27
28 public boolean shouldProcessTag(String name) {
29 return true;
30 }
31
32 public void tag(Tag tag) {
33 actual.append("{{TAG : ").append(tag.getName());
34 for (int i = 0; i < tag.getAttributeCount(); i++) {
35 actual.append(' ').append(tag.getAttributeName(i)).append("=\"").append(tag.getAttributeValue(i)).append('"');
36 }
37 actual.append(' ').append(typeAsString(tag.getType())).append("}}");
38 }
39
40 public void text(Text text) {
41 actual.append(text.getContents());
42 }
43
44 public void warning(String message, int line, int column) {
45 Assert.fail("Encountered error: " + message);
46 }
47
48 public void verify() {
49 Assert.assertEquals(expected.toString(), actual.toString());
50 }
51
52 private String typeAsString(int type) {
53 switch (type) {
54 case Tag.OPEN: return "*open*";
55 case Tag.CLOSE: return "*close*";
56 case Tag.EMPTY: return "*empty*";
57 default: return "*unknown*";
58 }
59 }
60
61 }