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.Collections;
|
21
|
|
using System.Data;
|
22
|
|
using System.Data.SqlClient;
|
23
|
|
using Seasar.Dao;
|
24
|
|
using Seasar.Dao.Dbms;
|
25
|
|
using Seasar.Dao.Impl;
|
26
|
|
using Seasar.Extension.ADO;
|
27
|
|
using Seasar.Extension.ADO.Impl;
|
28
|
|
using Seasar.Extension.ADO.Types;
|
29
|
|
using Seasar.Framework.Container;
|
30
|
|
using Seasar.Framework.Container.Factory;
|
31
|
|
using Seasar.Framework.Util;
|
32
|
|
using MbUnit.Framework;
|
33
|
|
|
34
|
|
namespace Seasar.Dao.Tests.Impl
|
35
|
|
{
|
36
|
|
|
37
|
|
[TestFixture]
|
38
|
|
public class BeanListMetaDataDataReaderHandlerTest
|
39
|
|
{
|
40
|
|
private const string PATH = "Tests.dicon";
|
41
|
|
private IBeanMetaData beanMetaData_;
|
42
|
|
|
43
|
3
|
[SetUp]
|
44
|
|
public void SetUp()
|
45
|
|
{
|
46
|
3
|
IS2Container container = S2ContainerFactory.Create(PATH);
|
47
|
3
|
IDataSource dataSource = (IDataSource) container.GetComponent(typeof(IDataSource));
|
48
|
|
|
49
|
3
|
IDbConnection cn = DataSourceUtil.GetConnection(dataSource);
|
50
|
3
|
IDbms dbms = new MSSQLServer();
|
51
|
3
|
beanMetaData_ = new BeanMetaDataImpl(typeof(Employee), new DatabaseMetaDataImpl(dataSource), dbms);
|
52
|
|
}
|
53
|
|
|
54
|
1
|
[Test]
|
55
|
|
public void TestHandle()
|
56
|
|
{
|
57
|
1
|
IDataReaderHandler handler = new BeanListMetaDataDataReaderHandler(
|
58
|
|
beanMetaData_);
|
59
|
1
|
String sql = "select * from emp";
|
60
|
|
|
61
|
1
|
IS2Container container = S2ContainerFactory.Create(PATH);
|
62
|
1
|
IDataSource dataSource = (IDataSource) container.GetComponent(typeof(IDataSource));
|
63
|
|
|
64
|
|
|
65
|
1
|
SqlConnection cn = (SqlConnection)DataSourceUtil.GetConnection(dataSource);
|
66
|
1
|
SqlCommand cmd = new SqlCommand(sql, cn);
|
67
|
|
|
68
|
1
|
IList ret = null;
|
69
|
1
|
try
|
70
|
|
{
|
71
|
1
|
SqlDataReader rs = cmd.ExecuteReader();
|
72
|
1
|
try
|
73
|
|
{
|
74
|
1
|
ret = (IList) handler.Handle(rs);
|
75
|
|
}
|
76
|
|
finally
|
77
|
|
{
|
78
|
1
|
rs.Close();
|
79
|
|
}
|
80
|
|
}
|
81
|
|
finally
|
82
|
|
{
|
83
|
1
|
cn.Close();
|
84
|
|
}
|
85
|
1
|
Assert.IsNotNull(ret, "1");
|
86
|
|
|
87
|
|
|
88
|
|
|
89
|
|
|
90
|
|
|
91
|
1
|
foreach (object itm in ret)
|
92
|
|
{
|
93
|
14
|
Employee emp = (Employee) itm;
|
94
|
|
|
95
|
|
}
|
96
|
|
|
97
|
|
}
|
98
|
|
|
99
|
1
|
[Test]
|
100
|
|
public void TestHandle2()
|
101
|
|
{
|
102
|
1
|
IDataReaderHandler handler = new BeanListMetaDataDataReaderHandler(
|
103
|
|
beanMetaData_);
|
104
|
1
|
String sql = "select emp.*, dept.dname as dname_0 from emp, dept where emp.deptno = dept.deptno and emp.deptno = 20";
|
105
|
|
|
106
|
1
|
IS2Container container = S2ContainerFactory.Create(PATH);
|
107
|
1
|
IDataSource dataSource = (IDataSource) container.GetComponent(typeof(IDataSource));
|
108
|
|
|
109
|
|
|
110
|
1
|
SqlConnection cn = (SqlConnection)DataSourceUtil.GetConnection(dataSource);
|
111
|
1
|
SqlCommand cmd = new SqlCommand(sql, cn);
|
112
|
|
|
113
|
1
|
IList ret = null;
|
114
|
1
|
try
|
115
|
|
{
|
116
|
1
|
SqlDataReader rs = cmd.ExecuteReader();
|
117
|
1
|
try
|
118
|
|
{
|
119
|
1
|
ret = (IList) handler.Handle(rs);
|
120
|
|
}
|
121
|
|
finally
|
122
|
|
{
|
123
|
1
|
rs.Close();
|
124
|
|
}
|
125
|
|
}
|
126
|
|
finally
|
127
|
|
{
|
128
|
1
|
cn.Close();
|
129
|
|
}
|
130
|
1
|
Assert.IsNotNull(ret, "1");
|
131
|
1
|
foreach (object itm in ret)
|
132
|
|
{
|
133
|
5
|
Employee emp = (Employee) itm;
|
134
|
|
|
135
|
5
|
Department dept = emp.Department;
|
136
|
5
|
Assert.IsNotNull(dept,"2");
|
137
|
5
|
Assert.AreEqual(emp.Deptno, dept.Deptno, "3");
|
138
|
5
|
Assert.IsNotNull(dept.Dname,"4");
|
139
|
|
}
|
140
|
|
|
141
|
|
}
|
142
|
|
|
143
|
1
|
[Test]
|
144
|
|
public void TestHandle3()
|
145
|
|
{
|
146
|
1
|
IDataReaderHandler handler = new BeanListMetaDataDataReaderHandler(
|
147
|
|
beanMetaData_);
|
148
|
1
|
String sql = "select emp.*, dept.deptno as deptno_0, dept.dname as dname_0 from emp, dept where dept.deptno = 20 and emp.deptno = dept.deptno";
|
149
|
1
|
IS2Container container = S2ContainerFactory.Create(PATH);
|
150
|
1
|
IDataSource dataSource = (IDataSource) container.GetComponent(typeof(IDataSource));
|
151
|
|
|
152
|
|
|
153
|
1
|
SqlConnection cn = (SqlConnection)DataSourceUtil.GetConnection(dataSource);
|
154
|
1
|
SqlCommand cmd = new SqlCommand(sql, cn);
|
155
|
|
|
156
|
1
|
IList ret = null;
|
157
|
1
|
try
|
158
|
|
{
|
159
|
1
|
SqlDataReader rs = cmd.ExecuteReader();
|
160
|
1
|
try
|
161
|
|
{
|
162
|
1
|
ret = (IList) handler.Handle(rs);
|
163
|
|
}
|
164
|
|
finally
|
165
|
|
{
|
166
|
1
|
rs.Close();
|
167
|
|
}
|
168
|
|
}
|
169
|
|
finally
|
170
|
|
{
|
171
|
1
|
cn.Close();
|
172
|
|
}
|
173
|
|
|
174
|
1
|
IEnumerator employees = ret.GetEnumerator();
|
175
|
1
|
employees.MoveNext();
|
176
|
1
|
Employee emp = (Employee) employees.Current;
|
177
|
1
|
employees.MoveNext();
|
178
|
1
|
Employee emp2 = (Employee) employees.Current;
|
179
|
1
|
Assert.AreSame(emp.Department, emp2.Department,"1");
|
180
|
|
}
|
181
|
|
|
182
|
|
}
|
183
|
|
}
|
184
|
|
|