75813b929e937d5065d289a983dbbfc363ad907c
[cfb.git] / test / net / jaekl / cfb / db / driver / DbDriverMock.java
1 package net.jaekl.cfb.db.driver;
2
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;
7
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;
13
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;
22
23 public class DbDriverMock extends DbDriver {
24
25         private HashMap<String, TableMock> m_tables;
26         private HashMap<String, SequenceMock> m_sequences;
27         
28         public DbDriverMock() {
29                 super();
30                 m_tables = new HashMap<String, TableMock>();
31                 m_sequences = new HashMap<String, SequenceMock>();
32         }
33
34         @Override
35         public void load() throws ClassNotFoundException {
36                 // no-op
37         }
38
39         @Override
40         public Connection connect(String host, int port, String dbName, String user, String pass) 
41                 throws SQLException 
42         {
43                 return null;
44         }
45         
46         @Override
47         public boolean createTable(Connection con, Table table) throws SQLException
48         {
49                 assertNotNull(con);
50                 assertNotNull(table);
51                 assertFalse(m_tables.containsKey(table.getName()));
52                 
53                 TableMock tm = new TableMock(table);
54                 m_tables.put(table.getName(), tm);
55                 return true;
56         }
57         
58         @Override
59         public void dropTable(Connection con, Table table) throws SQLException 
60         {
61                 assertNotNull(con);
62                 assertNotNull(table);
63                 assertTrue(table instanceof TableMock);
64                 
65                 if (m_tables.containsKey(table.getName())) {
66                         m_tables.remove(table.getName());
67                 }
68                 else {
69                         throw new SQLException("Table " + table.getName() + " does not exist.");
70                 }
71         }
72         
73         @Override
74         public boolean createSequence(Connection con, Sequence seq) throws SQLException 
75         {
76                 assertNotNull(con);
77                 assertNotNull(seq);
78                 assertFalse(m_sequences.containsKey(seq.getName()));
79                 
80                 SequenceMock sm = (seq instanceof SequenceMock) ? (SequenceMock)seq : new SequenceMock(seq);
81                 
82                 m_sequences.put(seq.getName(), sm);
83                 return true;
84         }
85         
86         public void dropSequence(Connection con, Sequence seq) throws SQLException
87         {
88                 assertNotNull(con);
89                 assertNotNull(seq);
90                 assertTrue(seq instanceof SequenceMock);
91
92                 if (m_sequences.containsKey(seq.getName())) {
93                         m_sequences.remove(seq.getName());
94                 }
95                 else {
96                         throw new SQLException("Sequence " + seq.getName() + " does not exist.");
97                 }
98         }
99         
100         public List<Row> select(Connection con, Column[] columns, Table[] tables, Condition[] conditions, Sort[] sorts, int limit)
101                 throws SQLException
102         {
103                 assertNotNull(con);
104                 assertNotNull(columns);
105                 assertNotNull(tables);
106                 
107                 // TODO:  produce sensible output
108                 return new ArrayList<Row>();
109         }
110         
111         // Returns the number of rows inserted
112         @Override
113         public int insert(Connection con, Table table, Object[][] values) throws SQLException 
114         {
115                 assertNotNull(con);
116                 assertNotNull(table);
117                 assertNotNull(values);
118                 
119                 TableMock tm = m_tables.get(table.getName());
120                 if (null == tm) {
121                         throw new SQLException("Table " + table.getName() + " does not exist.");
122                 }
123                 Column[] columns = tm.mock_getColumns();
124
125                 for (Object[] rowVals : values) {
126                         assertNotNull(rowVals);
127                         assertEquals(table.getNumColumns(), rowVals.length);
128                         
129                         tm.mock_insert(new Row(columns, rowVals));
130                 }
131                 
132                 return values.length;
133         }
134
135
136         public long nextVal(Connection con, Sequence seq) throws SQLException
137         {
138                 assertNotNull(con);
139                 assertNotNull(seq);
140                 
141                 SequenceMock sm = m_sequences.get(seq.getName());
142                 return sm.mock_nextVal();
143         }
144 }