1
|
|
#region Copyright
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
#endregion
|
18
|
|
|
19
|
|
using System;
|
20
|
|
using System.Reflection;
|
21
|
|
using System.IO;
|
22
|
|
using System.Text;
|
23
|
|
using NUnit.Framework;
|
24
|
|
using Seasar.Framework.Xml;
|
25
|
|
using Seasar.Framework.Util;
|
26
|
|
|
27
|
|
namespace Seasar.Tests.Framework.Xml
|
28
|
|
{
|
29
|
|
|
30
|
|
|
31
|
|
|
32
|
|
[TestFixture]
|
33
|
|
public class XmlHandlerTest
|
34
|
|
{
|
35
|
|
private const string XML_FILE_NAME =
|
36
|
|
"Seasar.Tests.Framework.Xml.test1.xml";
|
37
|
|
|
38
|
|
private TagHandlerRule rule_;
|
39
|
|
private Assembly asm_;
|
40
|
|
|
41
|
7
|
[SetUp]
|
42
|
|
public void SetUp()
|
43
|
|
{
|
44
|
7
|
rule_ = new TagHandlerRule();
|
45
|
7
|
asm_ = Assembly.GetExecutingAssembly();
|
46
|
|
}
|
47
|
|
|
48
|
1
|
[Test]
|
49
|
|
public void TestStart()
|
50
|
|
{
|
51
|
1
|
rule_["/tag1"] = new TagHandler1();
|
52
|
1
|
XmlHandler handler = new XmlHandler(rule_);
|
53
|
1
|
XmlHandlerParser parser = new XmlHandlerParser(handler);
|
54
|
1
|
StreamReader stream = null;
|
55
|
1
|
try
|
56
|
|
{
|
57
|
1
|
stream = ResourceUtil.GetResourceAsStreamReader(XML_FILE_NAME,asm_);
|
58
|
1
|
Assert.AreEqual("aaa", parser.Parse(stream));
|
59
|
|
}
|
60
|
|
finally
|
61
|
|
{
|
62
|
1
|
if(stream != null) stream.Close();
|
63
|
|
}
|
64
|
|
}
|
65
|
|
|
66
|
|
public class TagHandler1 : TagHandler
|
67
|
|
{
|
68
|
1
|
public override void Start(TagHandlerContext ctx,
|
69
|
|
IAttributes attributes)
|
70
|
|
{
|
71
|
1
|
ctx.Push(attributes["attr1"]);
|
72
|
|
}
|
73
|
|
}
|
74
|
|
|
75
|
1
|
[Test]
|
76
|
|
public void TestAppendBody()
|
77
|
|
{
|
78
|
1
|
StringBuilder buf = new StringBuilder();
|
79
|
1
|
TagHandler2 th2 = new TagHandler2();
|
80
|
1
|
th2.Buf = buf;
|
81
|
1
|
rule_["/tag1"] = th2;
|
82
|
1
|
XmlHandler handler = new XmlHandler(rule_);
|
83
|
1
|
XmlHandlerParser parser = new XmlHandlerParser(handler);
|
84
|
1
|
StreamReader stream = null;
|
85
|
1
|
try
|
86
|
|
{
|
87
|
1
|
stream = ResourceUtil.GetResourceAsStreamReader(XML_FILE_NAME,asm_);
|
88
|
1
|
parser.Parse(stream);
|
89
|
|
}
|
90
|
|
finally
|
91
|
|
{
|
92
|
1
|
if(stream != null) stream.Close();
|
93
|
|
}
|
94
|
1
|
Console.WriteLine(buf);
|
95
|
1
|
Assert.AreEqual("[111 222][333]", buf.ToString());
|
96
|
|
}
|
97
|
|
|
98
|
|
public class TagHandler2 : TagHandler
|
99
|
|
{
|
100
|
|
private StringBuilder buf_;
|
101
|
|
|
102
|
5
|
public override void AppendBody(TagHandlerContext context, string body)
|
103
|
|
{
|
104
|
5
|
buf_.Append("[" + body + "]");
|
105
|
|
}
|
106
|
|
|
107
|
|
public StringBuilder Buf
|
108
|
|
{
|
109
|
3
|
set { buf_ = value; }
|
110
|
|
}
|
111
|
|
}
|
112
|
|
|
113
|
1
|
[Test]
|
114
|
|
public void TestAppendBody2()
|
115
|
|
{
|
116
|
1
|
StringBuilder buf = new StringBuilder();
|
117
|
1
|
TagHandler2 th2 = new TagHandler2();
|
118
|
1
|
th2.Buf = buf;
|
119
|
1
|
rule_["tag1"] = th2;
|
120
|
1
|
XmlHandler handler = new XmlHandler(rule_);
|
121
|
1
|
XmlHandlerParser parser = new XmlHandlerParser(handler);
|
122
|
1
|
StreamReader stream = null;
|
123
|
1
|
try
|
124
|
|
{
|
125
|
1
|
stream = ResourceUtil.GetResourceAsStreamReader(XML_FILE_NAME,asm_);
|
126
|
1
|
parser.Parse(stream);
|
127
|
|
}
|
128
|
|
finally
|
129
|
|
{
|
130
|
1
|
if(stream != null) stream.Close();
|
131
|
|
}
|
132
|
1
|
Console.WriteLine(buf);
|
133
|
1
|
Assert.AreEqual("[111 222][333]", buf.ToString());
|
134
|
|
}
|
135
|
|
|
136
|
1
|
[Test]
|
137
|
|
public void TestAppendBody3()
|
138
|
|
{
|
139
|
1
|
StringBuilder buf = new StringBuilder();
|
140
|
1
|
TagHandler2 th2 = new TagHandler2();
|
141
|
1
|
th2.Buf = buf;
|
142
|
1
|
rule_["/tag1/tag3/tag4"] = th2;
|
143
|
1
|
XmlHandler handler = new XmlHandler(rule_);
|
144
|
1
|
XmlHandlerParser parser = new XmlHandlerParser(handler);
|
145
|
1
|
StreamReader stream = null;
|
146
|
1
|
try
|
147
|
|
{
|
148
|
1
|
stream = ResourceUtil.GetResourceAsStreamReader(XML_FILE_NAME,asm_);
|
149
|
1
|
parser.Parse(stream);
|
150
|
|
}
|
151
|
|
finally
|
152
|
|
{
|
153
|
1
|
if(stream != null) stream.Close();
|
154
|
|
}
|
155
|
1
|
Console.WriteLine(buf);
|
156
|
1
|
Assert.AreEqual("[eee]", buf.ToString());
|
157
|
|
}
|
158
|
|
|
159
|
1
|
[Test]
|
160
|
|
public void TestEnd()
|
161
|
|
{
|
162
|
1
|
rule_["/tag1/tag2"] = new TagHandler3();
|
163
|
1
|
XmlHandler handler = new XmlHandler(rule_);
|
164
|
1
|
XmlHandlerParser parser = new XmlHandlerParser(handler);
|
165
|
1
|
object result = parser.Parse(XML_FILE_NAME);
|
166
|
1
|
Assert.AreEqual("c c", result);
|
167
|
|
}
|
168
|
|
|
169
|
|
public class TagHandler3 : TagHandler
|
170
|
|
{
|
171
|
1
|
public override void End(TagHandlerContext context, string body)
|
172
|
|
{
|
173
|
1
|
context.Push(body);
|
174
|
|
}
|
175
|
|
}
|
176
|
|
|
177
|
1
|
[Test]
|
178
|
|
public void TestException()
|
179
|
|
{
|
180
|
1
|
rule_["/tag1/tag3"] = new TagHandler4();
|
181
|
1
|
XmlHandler handler = new XmlHandler(rule_);
|
182
|
1
|
XmlHandlerParser parser = new XmlHandlerParser(handler);
|
183
|
1
|
StreamReader stream = ResourceUtil.GetResourceAsStreamReader(XML_FILE_NAME,asm_);
|
184
|
1
|
try
|
185
|
|
{
|
186
|
1
|
parser.Parse(stream);
|
187
|
0
|
Assert.Fail();
|
188
|
|
}
|
189
|
|
catch(ApplicationException ex)
|
190
|
|
{
|
191
|
1
|
Console.WriteLine(ex);
|
192
|
|
}
|
193
|
|
finally
|
194
|
|
{
|
195
|
1
|
stream.Close();
|
196
|
|
}
|
197
|
|
}
|
198
|
|
|
199
|
|
public class TagHandler4 : TagHandler
|
200
|
|
{
|
201
|
1
|
public override void Start(TagHandlerContext context, IAttributes attributes)
|
202
|
|
{
|
203
|
1
|
throw new ApplicationException("testException");
|
204
|
|
}
|
205
|
|
}
|
206
|
|
|
207
|
1
|
[Test]
|
208
|
|
public void TestTagMatching()
|
209
|
|
{
|
210
|
1
|
TagHandler th = new TagHandler5();
|
211
|
1
|
rule_["tag1"] = th;
|
212
|
1
|
rule_["tag2"] = th;
|
213
|
1
|
rule_["tag3"] = th;
|
214
|
1
|
rule_["tag4"] = th;
|
215
|
1
|
rule_["tag5"] = th;
|
216
|
1
|
Console.WriteLine("tagMatching");
|
217
|
1
|
XmlHandler handler = new XmlHandler(rule_);
|
218
|
1
|
XmlHandlerParser parser = new XmlHandlerParser(handler);
|
219
|
1
|
StreamReader stream = ResourceUtil.GetResourceAsStreamReader(XML_FILE_NAME,asm_);
|
220
|
1
|
try
|
221
|
|
{
|
222
|
1
|
parser.Parse(stream);
|
223
|
|
}
|
224
|
|
finally
|
225
|
|
{
|
226
|
1
|
stream.Close();
|
227
|
|
}
|
228
|
|
}
|
229
|
|
|
230
|
|
public class TagHandler5 : TagHandler
|
231
|
|
{
|
232
|
5
|
public override void Start(TagHandlerContext context, IAttributes attributes)
|
233
|
|
{
|
234
|
5
|
Console.WriteLine(context.DetailPath);
|
235
|
|
}
|
236
|
|
}
|
237
|
|
}
|
238
|
|
}
|
239
|
|
|