2004-07-09
■ Db2Excel 改
S2のexampleにあるDb2Excelを元に、引数でdiconファイルを指定できるようにしたものを作っておきます(Db2Excel.diconを残したいので)。
Db2Excel.java package test.util; import org.seasar.extension.dataset.impl.SqlReader; import org.seasar.extension.dataset.impl.XlsWriter; import org.seasar.framework.container.S2Container; import org.seasar.framework.container.factory.S2ContainerFactory; public class Db2Excel { private static String dicon = "Db2Excel.dicon"; public static void main(String[] args) { if (args != null && args.length > 0) { dicon = args[0]; } S2Container container = S2ContainerFactory.create(dicon); container.init(); try { SqlReader reader = (SqlReader) container.getComponent(SqlReader.class); XlsWriter writer = (XlsWriter) container.getComponent(XlsWriter.class); writer.write(reader.read()); } finally { container.destroy(); } } }
■ EmployeeDao のテストの準備
まず、テストデータの作成から。テスト時にDBにインポートしたいデータ用のdiconと結果検証の元となるデータ用のdiconを作成して、先程つくったDb2ExcelでEXCELを生成。
Db2Excel4Prepare.dicon "http://www.seasar.org/dtd/components.dtd"> <components> <include path="j2ee.dicon"/> <component class="org.seasar.extension.dataset.impl.SqlReader"> <initMethod>#self.addTable("emp", null)</initMethod> <initMethod>#self.addTable("dept", null)</initMethod> </component> <component class="org.seasar.extension.dataset.impl.XlsWriter" instance="prototype"> <arg>"../src/test/s2study/app/employeeref/dao/getEmployeesPrepare.xls"</arg> </component> </components>
Db2Excel4Result.dicon "http://www.seasar.org/dtd/components.dtd"> <components> <include path="j2ee.dicon"/> <component class="org.seasar.extension.dataset.impl.SqlReader"> <initMethod> #self.addSql("SELECT e.*, d.dname, d.loc FROM emp e, dept d WHERE e.deptno = d.deptno", "emp") </initMethod> </component> <component class="org.seasar.extension.dataset.impl.XlsWriter" instance="prototype"> <arg>"../src/test/s2study/app/employeeref/dao/getEmployeesResult.xls"</arg> </component> </components>
次に、テストケースを検討。Daoに渡される引数の組み合わせによっていろんなパターンが考えられますが、とりあえず以下のようなマトリクスに基づいてテストします。
empNoFrom | empNoTo | empName | hireDateFrom | hireDateTo | salaryFrom | salaryTo | |
CASE01 | null | null | null | null | null | null | null |
CASE02 | 7800 | null | null | null | null | null | null |
CASE03 | null | 7800 | null | null | null | null | null |
CASE04 | null | null | M | null | null | null | null |
CASE05 | null | null | null | 1981-09-01 | null | null | null |
CASE06 | null | null | null | null | 1981-08-31 | null | null |
CASE07 | null | null | null | null | null | 2000 | null |
CASE08 | null | null | null | null | null | null | 2000 |
CASE09 | 7500 | 7900 | A | 1980-09-01 | 1981-09-01 | 1500 | 2500 |
各ケースの実行結果に含まれているべきレコードは以下の通り。これに基づいて、それぞれの結果検証用EXCELデータを作成。(getEmployeesResult.xlsをコピーして○のないレコードを削除)
EMPNO | ENAME | CASE01 | CASE02 | CASE03 | CASE04 | CASE05 | CASE06 | CASE07 | CASE08 | CASE09 |
7369 | SMITH | ○ | ○ | ○ | ○ | ○ | ||||
7499 | ALLEN | ○ | ○ | ○ | ○ | |||||
7521 | WARD | ○ | ○ | ○ | ○ | |||||
7566 | JONES | ○ | ○ | ○ | ○ | |||||
7654 | MARTIN | ○ | ○ | ○ | ○ | ○ | ||||
7698 | BLAKE | ○ | ○ | ○ | ○ | |||||
7782 | CLARK | ○ | ○ | ○ | ○ | ○ | ||||
7788 | SCOTT | ○ | ○ | ○ | ○ | |||||
7839 | KING | ○ | ○ | ○ | ○ | |||||
7844 | TURNER | ○ | ○ | ○ | ○ | |||||
7876 | ADAMS | ○ | ○ | ○ | ○ | ○ | ||||
7900 | JAMES | ○ | ○ | ○ | ○ | ○ | ||||
7902 | FORD | ○ | ○ | ○ | ○ | |||||
7934 | MILLER | ○ | ○ | ○ | ○ | ○ |
■ EmployeeDaoTestの作成
上記のケース分testメソッドを作成します。
EmployeeDaoTest.java package test.s2study.app.employeeref.dao; import java.util.Calendar; import java.util.Date; import java.util.Iterator; import java.util.List; import junit.framework.Test; import junit.framework.TestSuite; import org.seasar.extension.dataset.DataSet; import org.seasar.extension.dataset.DataTable; import org.seasar.extension.dataset.impl.DataTableImpl; import org.seasar.extension.unit.S2TestCase; import s2study.app.employeeref.dao.EmployeeDao; import s2study.app.entity.Employee; public class EmployeeDaoTest extends S2TestCase { private EmployeeDao dao_; public EmployeeDaoTest(String name) { super(name); } protected void setUp() throws Exception { include("j2ee.dicon"); include("EmployeeDaoTest.dicon"); } protected void tearDown() throws Exception { } public static Test suite() { return new TestSuite(EmployeeDaoTest.class); } public static void main(String[] args) { junit.textui.TestRunner.run(suite()); } public void testGetEmployees01Tx() throws Exception { readXlsReplaceDb("getEmployeesPrepare.xls"); List employees = dao_.getEmployees(null, null, null, null, null, null, null); dumpBeans(employees); DataTable result = new DataTableImpl("emp"); result.setupColumns(Employee.class); result.copyFrom(employees); DataSet dataSet = readXls("getEmployeesResult01.xls"); DataTable expect = dataSet.getTable("emp"); assertEquals("1", expect, result); } public void testGetEmployees02Tx() throws Exception { readXlsReplaceDb("getEmployeesPrepare.xls"); List employees = dao_.getEmployees( new Integer(7800), null, null, null, null, null, null); dumpBeans(employees); DataTable result = new DataTableImpl("emp"); result.setupColumns(Employee.class); result.copyFrom(employees); DataSet dataSet = readXls("getEmployeesResult02.xls"); DataTable expect = dataSet.getTable("emp"); assertEquals("1", expect, result); } public void testGetEmployees03Tx() throws Exception { readXlsReplaceDb("getEmployeesPrepare.xls"); List employees = dao_.getEmployees( null, new Integer(7800), null, null, null, null, null); dumpBeans(employees); DataTable result = new DataTableImpl("emp"); result.setupColumns(Employee.class); result.copyFrom(employees); DataSet dataSet = readXls("getEmployeesResult03.xls"); DataTable expect = dataSet.getTable("emp"); assertEquals("1", expect, result); } public void testGetEmployees04Tx() throws Exception { readXlsReplaceDb("getEmployeesPrepare.xls"); List employees = dao_.getEmployees(null, null, "%M%", null, null, null, null); dumpBeans(employees); DataTable result = new DataTableImpl("emp"); result.setupColumns(Employee.class); result.copyFrom(employees); DataSet dataSet = readXls("getEmployeesResult04.xls"); DataTable expect = dataSet.getTable("emp"); assertEquals("1", expect, result); } public void testGetEmployees05Tx() throws Exception { readXlsReplaceDb("getEmployeesPrepare.xls"); List employees = dao_.getEmployees( null, null, null, createDate(1981, 9, 1), null, null, null); dumpBeans(employees); DataTable result = new DataTableImpl("emp"); result.setupColumns(Employee.class); result.copyFrom(employees); DataSet dataSet = readXls("getEmployeesResult05.xls"); DataTable expect = dataSet.getTable("emp"); assertEquals("1", expect, result); } public void testGetEmployees06Tx() throws Exception { readXlsReplaceDb("getEmployeesPrepare.xls"); List employees = dao_.getEmployees( null, null, null, null, createDate(1981, 8, 31), null, null); dumpBeans(employees); DataTable result = new DataTableImpl("emp"); result.setupColumns(Employee.class); result.copyFrom(employees); DataSet dataSet = readXls("getEmployeesResult06.xls"); DataTable expect = dataSet.getTable("emp"); assertEquals("1", expect, result); } public void testGetEmployees07Tx() throws Exception { readXlsReplaceDb("getEmployeesPrepare.xls"); List employees = dao_.getEmployees( null, null, null, null, null, new Integer(2000), null); dumpBeans(employees); DataTable result = new DataTableImpl("emp"); result.setupColumns(Employee.class); result.copyFrom(employees); DataSet dataSet = readXls("getEmployeesResult07.xls"); DataTable expect = dataSet.getTable("emp"); assertEquals("1", expect, result); } public void testGetEmployees08Tx() throws Exception { readXlsReplaceDb("getEmployeesPrepare.xls"); List employees = dao_.getEmployees( null, null, null, null, null, null, new Integer(2000)); dumpBeans(employees); DataTable result = new DataTableImpl("emp"); result.setupColumns(Employee.class); result.copyFrom(employees); DataSet dataSet = readXls("getEmployeesResult08.xls"); DataTable expect = dataSet.getTable("emp"); assertEquals("1", expect, result); } public void testGetEmployees09Tx() throws Exception { readXlsReplaceDb("getEmployeesPrepare.xls"); List employees = dao_.getEmployees( new Integer(7500), new Integer(7900), "%A%", createDate(1980, 9, 01), createDate(1981, 9, 01), new Integer(1500), new Integer(2500)); dumpBeans(employees); DataTable result = new DataTableImpl("emp"); result.setupColumns(Employee.class); result.copyFrom(employees); DataSet dataSet = readXls("getEmployeesResult09.xls"); DataTable expect = dataSet.getTable("emp"); assertEquals("1", expect, result); } private void dumpBeans(List beans) { Iterator iter = beans.iterator(); while (iter.hasNext()) { System.out.println(iter.next()); } } private Date createDate(int year, int month, int date) { Calendar cal = Calendar.getInstance(); cal.set(year, month - 1, date); return cal.getTime(); } }
EmployeeDaoTest.dicon "http://www.seasar.org/dtd/components.dtd"> <components> <component class="s2study.app.employeeref.dao.EmployeeDao"> <aspect> <component class="org.seasar.dao.interceptors.S2DaoInterceptor"/> </aspect> </component> </components>
注意点は、empNameの引数には、「%」を含めておくこと。最初これがなくて2つがFailureになりました。sqlファイルの記述でなんとかしようといろいろ試行錯誤したんですが断念。ここはサービス層で吸収することにします。
正直ここまでのテストが必要なのか?とも思いました。S2Daoのsqlファイルはシンプルで分かりやすいのでインスペクションで十分な気も。いや、まあ、でもDaoのテストもちゃんとあった方がいいのかなぁ。やっぱり。
コメントを書く