1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.jsieve.util;
20
21 import org.jmock.Mock;
22 import org.jmock.MockObjectTestCase;
23
24 public class SieveToXmlTest extends MockObjectTestCase {
25
26 SieveToXml subject;
27 Mock mockOut;
28 SieveHandler instance;
29
30 protected void setUp() throws Exception {
31 super.setUp();
32 subject = new SieveToXml();
33 mockOut = mock(SieveToXml.Out.class);
34 instance = subject.build((SieveToXml.Out) mockOut.proxy());
35 }
36
37 protected void tearDown() throws Exception {
38 super.tearDown();
39 }
40
41 private void assertElementIsOutput(final String commandName, final String elementName) throws HaltTraversalException {
42 mockOut.expects(once()).method("openElement").with(eq(elementName),
43 eq(SieveToXml.DEFAULT_NAMESPACE), eq(SieveToXml.DEFAULT_PREFIX)).id("1");
44 mockOut.expects(once()).method("attribute").with(eq(SieveToXml.DEFAULT_NAME_ATTRIBUTE), eq(SieveToXml.DEFAULT_NAMESPACE),
45 eq(SieveToXml.DEFAULT_PREFIX), eq(commandName)).id("2");
46 mockOut.expects(once()).method("closeElement").after("2");
47 assertBuilderIsReturned(instance.startCommand(commandName));
48 assertBuilderIsReturned(instance.endCommand(commandName));
49 }
50
51 private void assertBuilderIsReturned(SieveHandler handler) {
52 assertEquals("Builder pattern so instance should be returned", instance, handler);
53 }
54
55 public void testBuildIsNotNull() {
56 assertNotNull(instance);
57 }
58
59 public void testStartScriptShouldBeIgnored() throws Exception {
60 assertBuilderIsReturned(instance.startScript());
61 }
62
63 public void testEndScriptShouldBeIgnored() throws Exception {
64 assertBuilderIsReturned(instance.endScript());
65 }
66
67 public void testStartBlockShouldBeIgnored() throws Exception {
68 assertBuilderIsReturned(instance.startBlock());
69 }
70
71 public void testEndBlockShouldBeIgnored() throws Exception {
72 assertBuilderIsReturned(instance.endBlock());
73 }
74
75 public void testStartCommandsShouldBeIgnored() throws Exception {
76 assertBuilderIsReturned(instance.startCommands());
77 }
78
79 public void testEndCommandsShouldBeIgnored() throws Exception {
80 assertBuilderIsReturned(instance.endCommands());
81 }
82
83 public void testControlCommandShouldOutputElement() throws Exception {
84 assertElementIsOutput("if", SieveToXml.DEFAULT_NAME_CONTROL_COMMAND);
85 }
86
87 public void testActionCommand() throws Exception {
88 assertElementIsOutput("other", SieveToXml.DEFAULT_NAME_ACTION_COMMAND);
89 }
90
91 public void testStringArgument() throws Exception {
92 final String stringArgument = "A String Tag";
93 mockOut.expects(once()).method("openElement").with(eq(SieveToXml.DEFAULT_NAME_STRING),
94 eq(SieveToXml.DEFAULT_NAMESPACE), eq(SieveToXml.DEFAULT_PREFIX)).id("1");
95 mockOut.expects(once()).method("content").with(eq(stringArgument)).after("1").id("2");
96 mockOut.expects(once()).method("closeElement").after("2");
97 assertBuilderIsReturned(instance.listMember(stringArgument));
98 }
99
100 public void testNumericArgument()throws Exception {
101 int number = 42;
102 mockOut.expects(once()).method("openElement").with(eq(SieveToXml.DEFAULT_NAME_NUM),
103 eq(SieveToXml.DEFAULT_NAMESPACE), eq(SieveToXml.DEFAULT_PREFIX)).id("1");
104 mockOut.expects(once()).method("content").with(eq(Integer.toString(number))).after("1").id("2");
105 mockOut.expects(once()).method("closeElement").after("2");
106 assertBuilderIsReturned(instance.argument(number));
107 }
108
109 public void testTagArgument() throws Exception {
110 String tag = "A Tag";
111 mockOut.expects(once()).method("openElement").with(eq(SieveToXml.DEFAULT_NAME_TAG),
112 eq(SieveToXml.DEFAULT_NAMESPACE), eq(SieveToXml.DEFAULT_PREFIX)).id("1");
113 mockOut.expects(once()).method("content").with(eq(tag)).after("1").id("2");
114 mockOut.expects(once()).method("closeElement").after("2");
115 assertBuilderIsReturned(instance.argument(tag));
116 }
117
118 public void testStartTestList() throws Exception {
119 mockOut.expects(once()).method("openElement").with(eq(SieveToXml.DEFAULT_NAME_LIST),
120 eq(SieveToXml.DEFAULT_NAMESPACE), eq(SieveToXml.DEFAULT_PREFIX));
121 assertBuilderIsReturned(instance.startTestList());
122 }
123
124 public void testEndTestList() throws Exception {
125 mockOut.expects(once()).method("closeElement");
126 assertBuilderIsReturned(instance.endTestList());
127 }
128
129 public void testTest() throws Exception {
130 final String testName = "is";
131 mockOut.expects(once()).method("openElement").with(eq(SieveToXml.DEFAULT_NAME_TEST),
132 eq(SieveToXml.DEFAULT_NAMESPACE), eq(SieveToXml.DEFAULT_PREFIX)).id("1");
133 mockOut.expects(once()).method("attribute").with(eq(SieveToXml.DEFAULT_NAME_ATTRIBUTE), eq(SieveToXml.DEFAULT_NAMESPACE),
134 eq(SieveToXml.DEFAULT_PREFIX), eq(testName)).id("2");
135 mockOut.expects(once()).method("closeElement").after("2");
136 assertBuilderIsReturned(instance.startTest(testName));
137 assertBuilderIsReturned(instance.endTest(testName));
138 }
139
140 }