View Javadoc

1   package db;
2   
3   import java.sql.SQLException;
4   
5   import org.dbunit.database.DatabaseConnection;
6   import org.dbunit.dataset.DataSetException;
7   import org.dbunit.dataset.ITable;
8   
9   import fit.Fixture;
10  import fit.Parse;
11  
12  public class TableAssert extends Fixture {
13  	
14  	private boolean queryExecuted = false;
15  	
16  	private boolean errorFound = false;
17  	
18  	protected ITable actualTable;
19  	
20  	private String[] columns;
21  	
22  	private int row = 0;
23  	
24  	@Override
25  	public void doTable(Parse table) {
26  		super.doTable(table);
27  //		tearDown(table);
28  	}
29  	
30  	private void tearDown(Parse cells) {
31  		if(row != actualTable.getRowCount())
32  		{
33  			error(cells, "Actual row count is :" 
34  					+ actualTable.getRowCount()+1 + " expected row cont is :" + row+1);
35  		}
36  	}
37  
38  	@Override
39  	public void doCells(Parse cells) {
40  		if(!queryExecuted)
41  		{
42  			try {
43  				initTable(cells.more.text());
44  				queryExecuted = true;
45  			} 
46  			catch (Exception e) 
47  			{
48  				errorFound = true;
49  				exception(cells, e);
50  			}
51  		}
52  		else if(errorFound)
53  		{
54  			ignore(cells);
55  		}
56  		else
57  		{
58  			try {
59  				checkCells(cells);
60  			} catch (DataSetException e) {
61  				exception(cells, e);
62  			}
63  		}
64  	}
65  
66  	protected void initTable(String tableName) throws ClassNotFoundException, SQLException, DataSetException {
67  		DatabaseConnection conn = DatabaseContext.createDatabaseConnection();
68  		try
69  		{
70  			actualTable = conn.createDataSet().getTable(tableName);
71  		}
72  		finally
73  		{
74  			conn.close();
75  		}
76  	}
77  
78  
79  	private void checkCells(Parse cells) throws DataSetException 
80  	{
81  		if(columns == null)
82  		{
83  			int size = cells.size();
84  			columns = new String[size];
85  			for (int index = 0; index < size; index++) {
86  				columns[index] = cells.at(index).text();
87  			}
88  		}
89  		else
90  		{
91  			int size = cells.size();
92  			for (int index = 0; index < size; index++) {
93  				Parse cell = cells.at(index);
94  				Object actual = actualTable.getValue(row, columns[index]);
95  				String expected = cell.text();
96  				if(expected.equals("[NULL]"))
97  				{
98  					if(actual == null)
99  					{
100 						right(cell);
101 					}
102 					else
103 					{
104 						wrong(cell, actual.toString());
105 					}
106 				}
107 				else if(actual.toString().equals(expected)){
108 					right(cell);
109 				}
110 				else
111 				{
112 					wrong(cell, actual.toString());
113 				}
114 			}
115 			row++;
116 		}
117 	}
118 	
119 	
120 	
121 }