| |||||||||||||||||
Source File | Conditionals | Statements | Methods | TOTAL | |||||||||||||
Seasar.Extension.Unit\S2TestCase.cs | 12.5% | 28.9% | 36.8% | 29.2% |
|
1 | #region Copyright | |
2 | /* | |
3 | * Copyright 2005 the Seasar Foundation and the Others. | |
4 | * | |
5 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
6 | * you may not use this file except in compliance with the License. | |
7 | * You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | |
14 | * either express or implied. See the License for the specific language | |
15 | * governing permissions and limitations under the License. | |
16 | */ | |
17 | #endregion | |
18 | ||
19 | using System.Data; | |
20 | using Seasar.Extension.ADO; | |
21 | using Seasar.Extension.ADO.Impl; | |
22 | using Seasar.Extension.DataSets; | |
23 | using Seasar.Extension.DataSets.Impl; | |
24 | using Seasar.Framework.Exceptions; | |
25 | using Seasar.Framework.Unit; | |
26 | using Seasar.Framework.Util; | |
27 | using IDataReader = Seasar.Extension.DataSets.IDataReader; | |
28 | ||
29 | namespace Seasar.Extension.Unit | |
30 | { | |
31 | public class S2TestCase : S2FrameworkTestCaseBase | |
32 | { | |
33 | private IDataSource dataSource; | |
34 | ||
35 | private IDbConnection connection; | |
36 | ||
37 | 19 | public S2TestCase() |
38 | { | |
39 | ||
40 | } | |
41 | ||
42 | public IDataSource DataSource | |
43 | { | |
44 | 38 | get |
45 | { | |
46 | 38 | if (dataSource == null) |
47 | 0 | throw new EmptyRuntimeException("dataSource"); |
48 | 38 | return dataSource; |
49 | } | |
50 | } | |
51 | ||
52 | 0 | public IDbConnection Connection |
53 | { | |
54 | get | |
55 | { | |
56 | if (connection != null) | |
57 | return connection; | |
58 | connection = DataSourceUtil.GetConnection(dataSource); | |
59 | return connection; | |
60 | } | |
61 | } | |
62 | ||
63 | public bool HasConnection | |
64 | { | |
65 | 42 | get { return connection != null; } |
66 | } | |
67 | ||
68 | 0 | internal void SetConnection(IDbConnection connection) |
69 | { | |
70 | this.connection = connection; | |
71 | } | |
72 | ||
73 | 76 | internal void SetDataSource(IDataSource dataSource) |
74 | { | |
75 | 76 | this.dataSource = dataSource; |
76 | } | |
77 | ||
78 | /// <summary> | |
79 | /// Excelファイルを読み、DataSetを作成します。 | |
80 | /// シート名をテーブル名、一行目をカラム名、二行目以降をデータ として読み込みます。 | |
81 | /// | |
82 | /// パスはAssemblyで指定されているディレクトリをルートとする。 | |
83 | /// 設定ファイルの絶対パスか、ファイル名のみを指定します。 | |
84 | /// ファイル名のみの場合、テストケースと同じパッケージにあるものとします。 | |
85 | /// <seealso cref="Seasar.Extension.DataSets.Impl.XlsReader.Read"/> | |
86 | /// </summary> | |
87 | /// <param name="path">Excelファイルのパス</param> | |
88 | /// <returns>Excelファイルの内容から作成したDataSet</returns> | |
89 | 3 | public DataSet ReadXls(string path) |
90 | { | |
91 | 3 | IDataReader reader = new XlsReader(ConvertPath(path)); |
92 | 3 | return reader.Read(); |
93 | } | |
94 | ||
95 | /// <summary> | |
96 | /// DataSetの内容から、Excelファイルを作成します。 | |
97 | /// シート名にテーブル名、一行目にカラム名、二行目以降にデータ を書き込みます。 | |
98 | /// | |
99 | /// パスはAssemblyで指定されているディレクトリをルートとする。 | |
100 | /// 設定ファイルの絶対パスか、ファイル名のみを指定します。 | |
101 | /// ファイル名のみの場合、テストケースと同じパッケージにあるものとします。 | |
102 | /// <seealso cref="Seasar.Extension.DataSets.Impl.XlsWriter.Write"/> | |
103 | /// </summary> | |
104 | /// <param name="path">Excelファイルのパス</param> | |
105 | /// <param name="dataSet">Excelファイルに書き込む内容のDataSet</param> | |
106 | 1 | public void WriteXls(string path, DataSet dataSet) |
107 | { | |
108 | 1 | IDataWriter writer = new XlsWriter(ConvertPath(path)); |
109 | 1 | writer.Write(dataSet); |
110 | } | |
111 | ||
112 | /// <summary> | |
113 | /// DataSetをDBに書き込みます。 | |
114 | /// <seealso cref="Seasar.Extension.DataSets.Impl.SqlWriter.Write"/> | |
115 | /// </summary> | |
116 | /// <param name="dataSet">データベースに書き込む内容のDataSet</param> | |
117 | 0 | public void WriteDb(DataSet dataSet) |
118 | { | |
119 | IDataWriter writer = new SqlWriter(DataSource); | |
120 | writer.Write(dataSet); | |
121 | } | |
122 | ||
123 | /// <summary> | |
124 | /// DBからレコードを読み込み、DataTableを作成します。 | |
125 | /// <seealso cref="Seasar.Extension.DataSets.Impl.SqlTableReader.Read"/> | |
126 | /// </summary> | |
127 | /// <param name="table">読み込むテーブル名</param> | |
128 | /// <returns>読み込んだ内容から作成したDataTable</returns> | |
129 | 0 | public DataTable ReadDbByTable(string table) |
130 | { | |
131 | return ReadDbByTable(table, null); | |
132 | } | |
133 | ||
134 | /// <summary> | |
135 | /// DBからレコードを読み込み、DataTableを作成します。 | |
136 | /// 読み込むレコードはconditionの条件を満たすレコードです。 conditionには" WHERE "より後ろをセットしてください。 | |
137 | /// <seealso cref="Seasar.Extension.DataSets.Impl.SqlTableReader.Read"/> | |
138 | /// </summary> | |
139 | /// <param name="table">読み込むテーブル名</param> | |
140 | /// <param name="condition">条件句(WHEREの後ろ)</param> | |
141 | /// <returns>読み込んだ内容から作成したDataTable</returns> | |
142 | 2 | public DataTable ReadDbByTable(string table, string condition) |
143 | { | |
144 | 2 | SqlTableReader reader = new SqlTableReader(DataSource); |
145 | 2 | reader.SetTable(table, condition); |
146 | 2 | return reader.Read(); |
147 | } | |
148 | ||
149 | /// <summary> | |
150 | /// DBからSQL文の実行結果を取得し、DataTableを作成します。 | |
151 | /// 作成したDataTableのテーブル名はtableNameになります。 | |
152 | /// <seealso cref="Seasar.Extension.DataSets.Impl.SqlTableReader.Read"/> | |
153 | /// </summary> | |
154 | /// <param name="sql">実行するSQL文</param> | |
155 | /// <param name="tableName">作成するDataTableのテーブル名</param> | |
156 | /// <returns>読み出した内容のDataTable</returns> | |
157 | 0 | public DataTable ReadDbBySql(string sql, string tableName) |
158 | { | |
159 | SqlTableReader reader = new SqlTableReader(DataSource); | |
160 | reader.SetSql(sql, tableName); | |
161 | return reader.Read(); | |
162 | ||
163 | } | |
164 | ||
165 | /// <summary> | |
166 | /// Excelファイルを読み込み、DBに書き込みます。 | |
167 | /// シート名をテーブル名、一行目をカラム名、二行目以降をデータ として読み込みます。 | |
168 | /// | |
169 | /// パスはAssemblyで指定されているディレクトリをルートとする。 | |
170 | /// 設定ファイルの絶対パスか、ファイル名のみを指定します。 | |
171 | /// ファイル名のみの場合、テストケースと同じパッケージにあるものとします。 | |
172 | /// <seealso cref="Seasar.Extension.DataSets.Impl.XlsReader.Read"/> | |
173 | /// <seealso cref="Seasar.Extension.DataSets.Impl.SqlWriter.Write"/> | |
174 | /// </summary> | |
175 | /// <param name="path">Excelファイルのパス</param> | |
176 | 0 | public void ReadXlsWriteDb(string path) |
177 | { | |
178 | WriteDb(ReadXls(path)); | |
179 | } | |
180 | ||
181 | /// <summary> | |
182 | /// Excelファイルを読み込み、DBに書き込みます。 | |
183 | /// シート名をテーブル名、一行目をカラム名、二行目以降をデータ として読み込みます。 | |
184 | /// Excelの内容とDBのレコードとで主キーが一致するものがあれば、 そのレコードを削除した後に書き込みます。 | |
185 | /// | |
186 | /// パスはAssemblyで指定されているディレクトリをルートとする。 | |
187 | /// 設定ファイルの絶対パスか、ファイル名のみを指定します。 | |
188 | /// ファイル名のみの場合、テストケースと同じパッケージにあるものとします。 | |
189 | /// <seealso cref="Seasar.Extension.DataSets.Impl.XlsReader.Read"/> | |
190 | /// <seealso cref="Seasar.Extension.DataSets.Impl.SqlWriter.Write"/> | |
191 | /// </summary> | |
192 | /// <param name="path">Excelファイルのパス</param> | |
193 | 0 | public void ReadXlsReplaceDb(string path) |
194 | { | |
195 | DataSet dataSet = ReadXls(path); | |
196 | DeleteDb(dataSet); | |
197 | WriteDb(dataSet); | |
198 | } | |
199 | ||
200 | /// <summary> | |
201 | /// Excelファイルを読み込み、DBに書き込みます。 | |
202 | /// シート名をテーブル名、一行目をカラム名、二行目以降をデータ として読み込みます。 | |
203 | /// 対象となるテーブルのレコードを全て削除した後に書き込みます。 | |
204 | /// | |
205 | /// パスはAssemblyで指定されているディレクトリをルートとする。 | |
206 | /// 設定ファイルの絶対パスか、ファイル名のみを指定します。 | |
207 | /// ファイル名のみの場合、テストケースと同じパッケージにあるものとします。 | |
208 | /// <seealso cref="Seasar.Extension.DataSets.Impl.XlsReader.Read"/> | |
209 | /// <seealso cref="Seasar.Extension.DataSets.Impl.SqlWriter.Write"/> | |
210 | /// </summary> | |
211 | /// <param name="path">Excelファイルのパス</param> | |
212 | 0 | public void ReadXlsAllReplaceDb(string path) |
213 | { | |
214 | DataSet dataSet = ReadXls(path); | |
215 | for (int i = dataSet.Tables.Count - 1; i >= 0; --i) | |
216 | { | |
217 | DeleteTable(dataSet.Tables[i].TableName); | |
218 | } | |
219 | WriteDb(dataSet); | |
220 | } | |
221 | ||
222 | /// <summary> | |
223 | /// DataSetに対応するDBのレコードを読み込み、DataSetを作成します 。 | |
224 | /// <seealso cref="Seasar.Extension.DataSets.Impl.SqlReloadReader.Read"/> | |
225 | /// </summary> | |
226 | /// <param name="dataSet">対象DBに対応するDataSet</param> | |
227 | /// <returns>最新状態のDataSet</returns> | |
228 | 0 | public DataSet Reload(DataSet dataSet) |
229 | { | |
230 | return new SqlReloadReader(DataSource, dataSet).Read(); | |
231 | } | |
232 | ||
233 | /// <summary> | |
234 | /// DataTableに対応するDBのレコードを読み込み、DataTableを作成 します。 | |
235 | /// <seealso cref="Seasar.Extension.DataSets.Impl.SqlReloadReader.Read"/> | |
236 | /// </summary> | |
237 | /// <param name="table">対象DBに対応するDataTable</param> | |
238 | /// <returns>最新状態のDataTable</returns> | |
239 | 0 | public DataTable Reload(DataTable table) |
240 | { | |
241 | return new SqlReloadTableReader(DataSource, table).Read(); | |
242 | } | |
243 | ||
244 | /// <summary> | |
245 | /// DataSetに対応するDBのレコードを削除します。 | |
246 | /// <seealso cref="Seasar.Extension.DataSets.Impl.SqlDeleteTableWriter.Write"/> | |
247 | /// </summary> | |
248 | /// <param name="dataSet">対象DBに対応するDataSet</param> | |
249 | 0 | public void DeleteDb(DataSet dataSet) |
250 | { | |
251 | SqlDeleteTableWriter writer = new SqlDeleteTableWriter(DataSource); | |
252 | for (int i = dataSet.Tables.Count - 1; i >= 0; --i) | |
253 | { | |
254 | writer.Write(dataSet.Tables[i]); | |
255 | } | |
256 | } | |
257 | ||
258 | /// <summary> | |
259 | /// DBから指定するテーブルの全レコードを削除します。 | |
260 | /// </summary> | |
261 | /// <param name="tableName">削除対象のテーブル名</param> | |
262 | 0 | public void DeleteTable(string tableName) |
263 | { | |
264 | IUpdateHandler handler = new BasicUpdateHandler( | |
265 | DataSource, | |
266 | "DELETE FROM " + tableName | |
267 | ); | |
268 | handler.Execute(null); | |
269 | } | |
270 | } | |
271 | } | |
272 |
|