Coverage Report - org.jtheque.core.utils.test.AbstractDBUnitTest
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractDBUnitTest
0 %
0/55
0 %
0/10
2.667
 
 1  
 package org.jtheque.core.utils.test;
 2  
 
 3  
 import org.dbunit.database.DatabaseDataSourceConnection;
 4  
 import org.dbunit.database.IDatabaseConnection;
 5  
 import org.dbunit.dataset.Column;
 6  
 import org.dbunit.dataset.DataSetException;
 7  
 import org.dbunit.dataset.IDataSet;
 8  
 import org.dbunit.dataset.ITable;
 9  
 import org.dbunit.dataset.ITableMetaData;
 10  
 import org.dbunit.dataset.xml.FlatXmlDataSet;
 11  
 import org.dbunit.operation.DatabaseOperation;
 12  
 import org.junit.Before;
 13  
 
 14  
 import javax.sql.DataSource;
 15  
 import java.io.IOException;
 16  
 import java.sql.Connection;
 17  
 import java.sql.SQLException;
 18  
 
 19  
 import static org.junit.Assert.fail;
 20  
 
 21  
 /*
 22  
  * This file is part of JTheque.
 23  
  *            
 24  
  * JTheque is free software: you can redistribute it and/or modify
 25  
  * it under the terms of the GNU General Public License as published by
 26  
  * the Free Software Foundation, either version 3 of the License. 
 27  
  *
 28  
  * JTheque is distributed in the hope that it will be useful,
 29  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 30  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 31  
  * GNU General Public License for more details.
 32  
  *
 33  
  * You should have received a copy of the GNU General Public License
 34  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 35  
  */
 36  
 
 37  
 /**
 38  
  * An abstract class for unit testing with DBUnit.
 39  
  *
 40  
  * @author Baptiste Wicht
 41  
  */
 42  
 public abstract class AbstractDBUnitTest {
 43  
     private IDatabaseConnection connection;
 44  
 
 45  
     private final String datasetPath;
 46  
 
 47  
     /**
 48  
      * Construct a new AbstractDBUnitTest.
 49  
      *
 50  
      * @param datasetPath The path to the dataset.
 51  
      */
 52  
     protected AbstractDBUnitTest(String datasetPath) {
 53  0
         super();
 54  
 
 55  0
         this.datasetPath = datasetPath;
 56  0
     }
 57  
 
 58  
     /**
 59  
      * Init the DB with the specific data source.
 60  
      *
 61  
      * @param dataSource The data source to use to populate the DB.
 62  
      */
 63  
     protected void initDB(DataSource dataSource) {
 64  
         try {
 65  0
             connection = new DatabaseDataSourceConnection(dataSource);
 66  
 
 67  0
             IDataSet dataSet = new FlatXmlDataSet(getClass().getResource(datasetPath));
 68  
 
 69  0
             createHsqldbTables(dataSet, connection.getConnection());
 70  0
         } catch (SQLException e) {
 71  0
             fail(e.getMessage());
 72  0
         } catch (IOException e) {
 73  0
             fail(e.getMessage());
 74  0
         } catch (DataSetException e) {
 75  0
             fail(e.getMessage());
 76  0
         }
 77  0
     }
 78  
 
 79  
     /**
 80  
      * Set up the test environment.
 81  
      */
 82  
     @Before
 83  
     public void setUp() {
 84  
         try {
 85  0
             IDataSet dataSet = new FlatXmlDataSet(getClass().getResource(datasetPath));
 86  
 
 87  0
             DatabaseOperation.CLEAN_INSERT.execute(
 88  
                     connection,
 89  
                     dataSet);
 90  0
         } catch (Exception e) {
 91  0
             fail(e.getMessage());
 92  0
         }
 93  0
     }
 94  
 
 95  
     /**
 96  
      * Return the table with the specified name.
 97  
      *
 98  
      * @param s The name of the table.
 99  
      * @return The table if found else null.
 100  
      */
 101  
     protected final ITable getTable(String s) {
 102  
         try {
 103  0
             return connection.createDataSet().getTable(s);
 104  0
         } catch (Exception e) {
 105  0
             fail(e.getMessage());
 106  
         }
 107  
 
 108  0
         return null;
 109  
     }
 110  
 
 111  
     /**
 112  
      * Return the value in the specified table, at the specified column and at the row specified by the primary key.
 113  
      *
 114  
      * @param table  The table.
 115  
      * @param pk     The primary key.
 116  
      * @param column The column.
 117  
      * @return The value.
 118  
      */
 119  
     protected Object getValue(String table, int pk, String column) {
 120  
         try {
 121  0
             return getTable(table).getValue(pk, column);
 122  0
         } catch (Exception e) {
 123  0
             fail(e.getMessage());
 124  
         }
 125  
 
 126  0
         return null;
 127  
     }
 128  
 
 129  
     /**
 130  
      * Return the row count in the specified table.
 131  
      *
 132  
      * @param table The table to count the row.
 133  
      * @return The row count of the table.
 134  
      */
 135  
     protected int getRowCount(String table) {
 136  0
         return getTable(table).getRowCount();
 137  
     }
 138  
 
 139  
     /**
 140  
      * Return the database connection.
 141  
      *
 142  
      * @return The database connection.
 143  
      */
 144  
     protected final IDatabaseConnection getConnection() {
 145  0
         return connection;
 146  
     }
 147  
 
 148  
     /**
 149  
      * Create the HSQL tables.
 150  
      *
 151  
      * @param dataSet    The dataset.
 152  
      * @param connection The database connection.
 153  
      * @throws DataSetException If an error occurs during dataset reading.
 154  
      * @throws SQLException     If an error occurs during populating database.
 155  
      */
 156  
     protected static final void createHsqldbTables(IDataSet dataSet, Connection connection) throws DataSetException, SQLException {
 157  0
         String[] tableNames = dataSet.getTableNames();
 158  
 
 159  0
         StringBuilder sql = new StringBuilder("");
 160  
 
 161  0
         for (String tableName : tableNames) {
 162  0
             ITable table = dataSet.getTable(tableName);
 163  0
             ITableMetaData metadata = table.getTableMetaData();
 164  0
             Column[] columns = metadata.getColumns();
 165  
 
 166  0
             sql.append("create table if not exists ").append(tableName).append("( ");
 167  
 
 168  0
             boolean first = true;
 169  0
             for (Column column : columns) {
 170  0
                 if (!first) {
 171  0
                     sql.append(", ");
 172  
                 }
 173  
 
 174  0
                 String columnName = column.getColumnName();
 175  0
                 String type = resolveType((String) table.getValue(0, columnName));
 176  0
                 sql.append(columnName).append(' ').append(type);
 177  
 
 178  0
                 if (first) {
 179  0
                     if (!columnName.contains("FK")) {
 180  0
                         sql.append(" IDENTITY PRIMARY KEY");
 181  
                     }
 182  
 
 183  0
                     first = false;
 184  
                 }
 185  
             }
 186  0
             sql.append("); ");
 187  
         }
 188  
 
 189  0
         connection.prepareStatement(sql.toString()).executeUpdate();
 190  0
     }
 191  
 
 192  
     /**
 193  
      * Resolve the DB type from the string.
 194  
      *
 195  
      * @param str The string value.
 196  
      * @return The DB type.
 197  
      */
 198  
     private static String resolveType(String str) {
 199  
         try {
 200  0
                         Integer.parseInt(str);
 201  
 
 202  0
                         return "int";
 203  0
         } catch (Exception e) {
 204  
             //Nothing to do here
 205  
         }
 206  
 
 207  0
         return "varchar";
 208  
     }
 209  
 }