1   package com.sri.emo.test;
2   
3   import java.io.InputStream;
4   import java.sql.Connection;
5   import com.jcorporate.expresso.core.db.DBConnection;
6   import com.jcorporate.expresso.core.db.DBConnectionPool;
7   import org.dbunit.database.DatabaseConnection;
8   import org.dbunit.database.IDatabaseConnection;
9   import org.dbunit.dataset.IDataSet;
10  import org.dbunit.dataset.xml.FlatXmlDataSet;
11  import org.dbunit.operation.DatabaseOperation;
12  
13  /***
14   * Database Test Fixture that loads any specified DBUnit XML FlatDataSet
15   * file into the current data context.  Typical Usage:
16   * <code><pre>
17   * public class TestSomeDatabase extends TestCase {
18   *     private DatabaseTestFixture testFixture;
19   *     public void setUp() {
20   *          testFixture = new DatabaseTestFixture(TestSystemInitializer.getTestContext(),
21   *                 TestSomeDatabase.class.getResourceAsStream("DatabaseData.xml"));
22   *          testFixture.setUp();
23   *     }
24   *
25   *
26   *    public void tearDown() {
27   *          testFixture.tearDown();
28   *          testFixture = null;
29   *    }
30   * }
31   * </pre></code>
32   *
33   * @author Michael Rimov
34   */
35  public class DatabaseTestFixture {
36      /***
37       * The data context we load into.
38       */
39      private final String thisContext;
40  
41      /***
42       * The dataset created from the XML file.
43       */
44      private IDataSet dataSet;
45  
46  
47      /***
48       * Id for a testing wizard with 3 steps.
49       */
50      public static final int DEFAULT_WIZ_ID = 12;
51  
52  
53      public static final int DEFAULT_WELCOME_STEP = 21;
54  
55      public static final int DEFAULT_MOVIE_RATING_STEP = 22;
56  
57      public static final int DEFAULT_USER_RATING_STEP = 23;
58  
59  
60      /***
61       * ID for wizard with only 1 welcome step in it.
62       */
63      public static final int SINGLE_STEP_WIZ_ID = 14;
64  
65      public static final int SINGLE_WIZ_WELCOME_STEP = 24;
66  
67  
68      /***
69       * Default wizard steps included in the model.
70       */
71      public static final int DEFAULT_WIZ_STEPS = 3;
72  
73      /***
74       * Default number of pertinent steps for the model.
75       */
76      public static final int DEFAULT_WIZ_PERTINENT_STEPS = 2;
77  
78      /***
79       * Default wizard title.
80       */
81      public static final String DEFAULT_WIZ_TITLE = "Pick A Movie";
82  
83      /***
84       * Constructs the database test fixture with a given data context
85       * and a stream of XML data that equals a database connection.
86       *
87       * @param dataContext String the database context to use.
88       * @param xmlData     InputStream the Input Stream for the data.
89       * @throws Exception usually upon FlatXmlDataSet construction error if
90       *                   the xmlData wasn't proper.
91       */
92      public DatabaseTestFixture(String dataContext, InputStream xmlData) throws Exception {
93          super();
94  
95          assert dataContext != null;
96          assert xmlData != null;
97          thisContext = dataContext;
98          dataSet = new FlatXmlDataSet(xmlData);
99      }
100 
101     /***
102      * Sets
103      *
104      * @throws Exception
105      */
106     public void setUp() throws Exception {
107         DBConnection expressoConnection = DBConnectionPool
108                 .getInstance(thisContext).getConnection("DBUnit Load Connection");
109         try {
110             Connection sqlConnection = expressoConnection.getConnection();
111             IDatabaseConnection connection = new DatabaseConnection(sqlConnection);
112             DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
113         } finally {
114             expressoConnection.release();
115         }
116     }
117 
118     public void tearDown() throws Exception {
119         //Currently does nothing.
120     }
121 }