View Javadoc

1   /*
2    * joey-test and its relative products are published under the terms
3    * of the Apache Software License.
4    * 
5    * Created on 2004/05/25 1:11:38
6    */
7   package org.asyrinx.joey.test.dao;
8   
9   import java.sql.Connection;
10  
11  import javax.sql.DataSource;
12  
13  import org.dbunit.database.DatabaseConnection;
14  import org.dbunit.database.IDatabaseConnection;
15  import org.dbunit.dataset.IDataSet;
16  import org.dbunit.operation.DatabaseOperation;
17  import org.seasar.framework.container.ContainerConstants;
18  import org.seasar.framework.exception.EmptyRuntimeException;
19  import org.seasar.framework.util.DataSourceUtil;
20  
21  /***
22   * DBUnitのテストケースにS2TestCaseのコンテナ関係とDB接続の機能だけを付けたテストケースです。
23   * 
24   * @author akima
25   */
26  public abstract class DBTestCase extends S2TestCaseBase {
27  
28      private static final String DATASOURCE_NAME = "j2ee" + ContainerConstants.NS_SEP + "dataSource";
29  
30      private DataSource dataSource_;
31  
32      public DBTestCase(String name) {
33          super(name);
34      }
35  
36      public DataSource getDataSource() {
37          if (dataSource_ == null) {
38              throw new EmptyRuntimeException("dataSource");
39          }
40          return dataSource_;
41      }
42  
43      public Connection getConnection() {
44          return DataSourceUtil.getConnection(getDataSource());
45      }
46  
47      private void setupDataSource() {
48          try {
49              if (container_.hasComponentDef(DataSource.class)) {
50                  dataSource_ = (DataSource) container_.getComponent(DataSource.class);
51              } else if (container_.hasComponentDef(DATASOURCE_NAME)) {
52                  dataSource_ = (DataSource) container_.getComponent(DATASOURCE_NAME);
53              }
54          } catch (Throwable t) {
55              System.err.println(t);
56          }
57      }
58  
59      /* (non-Javadoc)
60       * @see org.asyrinx.joey.test.dao.S2TestCaseBase#executeRunBare()
61       */
62      protected void executeRunBare() throws Throwable {
63          setupDataSource();
64          super.executeRunBare();
65      }
66  
67      /*
68       * *********************************************************************
69       * from DatabaseTestCase of DBUnit
70       * *********************************************************************
71       */
72  
73  
74      /***
75       * Returns the test dataset.
76       */
77      protected abstract IDataSet getDataSet() throws Exception;
78  
79      /***
80       * Returns the test database connection.
81       */
82      protected IDatabaseConnection getDBUnitConnection() throws Exception {
83          return new DatabaseConnection(getConnection());
84      }
85  
86      /***
87       * Close the specified connection. Ovverride this method of you want to keep
88       * your connection alive between tests.
89       */
90      protected void closeConnection(IDatabaseConnection connection) throws Exception {
91          connection.close();
92      }
93  
94      /***
95       * Returns the database operation executed in test setup.
96       */
97      protected DatabaseOperation getSetUpOperation() throws Exception {
98          return DatabaseOperation.CLEAN_INSERT;
99      }
100 
101     /***
102      * Returns the database operation executed in test cleanup.
103      */
104     protected DatabaseOperation getTearDownOperation() throws Exception {
105         return DatabaseOperation.NONE;
106     }
107 
108     private void executeOperation(DatabaseOperation operation) throws Exception {
109         if (operation != DatabaseOperation.NONE) {
110             IDatabaseConnection connection = getDBUnitConnection();
111             try {
112                 operation.execute(connection, getDataSet());
113             } finally {
114                 closeConnection(connection);
115             }
116         }
117     }
118 
119     protected void setUp() throws Exception {
120         executeOperation(getSetUpOperation());
121         super.setUp();
122     }
123 
124     protected void tearDown() throws Exception {
125         super.tearDown();
126         executeOperation(getTearDownOperation());
127     }
128 
129 }