Add some unit testing of the CfbSchema.
[cfb.git] / test / net / jaekl / cfb / db / CfbSchemaTest.java
1 package net.jaekl.cfb.db;
2
3 import static org.junit.Assert.*;
4
5 import java.io.ByteArrayInputStream;
6 import java.io.FileNotFoundException;
7 import java.io.IOException;
8 import java.sql.SQLException;
9 import java.util.List;
10
11 import net.jaekl.cfb.analyze.MessageMap;
12 import net.jaekl.cfb.db.driver.ConnectionMock;
13 import net.jaekl.cfb.db.driver.DbDriverMock;
14 import net.jaekl.cfb.xml.MessagesXmlData;
15
16 import org.junit.Test;
17 import org.xml.sax.InputSource;
18 import org.xml.sax.SAXException;
19
20 public class CfbSchemaTest {
21         @Test
22         public void testEnsureDbInitialized() throws SQLException, FileNotFoundException, IOException, SAXException {
23                 ConnectionMock con;
24                 DbDriverMock dbDriver;
25                 CfbSchema schema;
26                 con = new ConnectionMock();
27                 dbDriver = new DbDriverMock();
28                 schema = new CfbSchema(dbDriver);
29                 
30                 MessageMap msgMap = new MessageMap();
31                 msgMap.parse(new InputSource(new ByteArrayInputStream(MessagesXmlData.XML.getBytes("UTF-8"))));
32                 
33                 schema.setMessageMap(msgMap);
34                 schema.ensureDbInitialized(con);
35                 
36                 for (Table table : CfbSchema.TABLES) {
37                         assertTablePresent(con, dbDriver, table);
38                 }
39         }
40         
41         private void assertTablePresent(ConnectionMock con, DbDriverMock dbDriver, Table table)
42                 throws SQLException
43         {
44                 Column[] columns = new Column[table.getNumColumns()];
45                 Table[] tables = new Table[] { table };
46                 
47                 for (int i = 0; i < table.getNumColumns(); ++i) {
48                         columns[i] = table.getColumn(i);
49                 }
50                 List<Row> result = dbDriver.select(con, columns, tables, null);
51                 assertNotNull(result);
52         }
53 }