1 package net.jaekl.cfb.db.driver;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertNotNull;
6 import static org.junit.Assert.assertTrue;
8 import java.sql.Connection;
9 import java.sql.SQLException;
10 import java.util.ArrayList;
11 import java.util.HashMap;
12 import java.util.List;
14 import net.jaekl.cfb.db.Column;
15 import net.jaekl.cfb.db.Condition;
16 import net.jaekl.cfb.db.Row;
17 import net.jaekl.cfb.db.Sequence;
18 import net.jaekl.cfb.db.SequenceMock;
19 import net.jaekl.cfb.db.Sort;
20 import net.jaekl.cfb.db.Table;
21 import net.jaekl.cfb.db.TableMock;
23 public class DbDriverMock extends DbDriver {
25 private HashMap<String, TableMock> m_tables;
26 private HashMap<String, SequenceMock> m_sequences;
28 public DbDriverMock() {
30 m_tables = new HashMap<String, TableMock>();
31 m_sequences = new HashMap<String, SequenceMock>();
35 public void load() throws ClassNotFoundException {
40 public Connection connect(String host, int port, String dbName, String user, String pass)
47 public boolean createTable(Connection con, Table table) throws SQLException
51 assertFalse(m_tables.containsKey(table.getName()));
53 TableMock tm = new TableMock(table);
54 m_tables.put(table.getName(), tm);
59 public void dropTable(Connection con, Table table) throws SQLException
63 assertTrue(table instanceof TableMock);
65 if (m_tables.containsKey(table.getName())) {
66 m_tables.remove(table.getName());
69 throw new SQLException("Table " + table.getName() + " does not exist.");
74 public boolean createSequence(Connection con, Sequence seq) throws SQLException
78 assertFalse(m_sequences.containsKey(seq.getName()));
80 SequenceMock sm = (seq instanceof SequenceMock) ? (SequenceMock)seq : new SequenceMock(seq);
82 m_sequences.put(seq.getName(), sm);
86 public void dropSequence(Connection con, Sequence seq) throws SQLException
90 assertTrue(seq instanceof SequenceMock);
92 if (m_sequences.containsKey(seq.getName())) {
93 m_sequences.remove(seq.getName());
96 throw new SQLException("Sequence " + seq.getName() + " does not exist.");
100 public List<Row> select(Connection con, Column[] columns, Table[] tables, Condition[] conditions, Sort[] sorts, int limit)
104 assertNotNull(columns);
105 assertNotNull(tables);
107 // TODO: produce sensible output
108 return new ArrayList<Row>();
111 // Returns the number of rows inserted
113 public int insert(Connection con, Table table, Object[][] values) throws SQLException
116 assertNotNull(table);
117 assertNotNull(values);
119 TableMock tm = m_tables.get(table.getName());
121 throw new SQLException("Table " + table.getName() + " does not exist.");
123 Column[] columns = tm.mock_getColumns();
125 for (Object[] rowVals : values) {
126 assertNotNull(rowVals);
127 assertEquals(table.getNumColumns(), rowVals.length);
129 tm.mock_insert(new Row(columns, rowVals));
132 return values.length;
136 public long nextVal(Connection con, Sequence seq) throws SQLException
141 SequenceMock sm = m_sequences.get(seq.getName());
142 return sm.mock_nextVal();