001: package whitebox.sql;
002:
003: import java.sql.SQLException;
004: import java.util.Arrays;
005: import java.util.List;
006: import java.util.ArrayList;
007: import java.util.HashMap;
008: import java.util.Map;
009: import java.io.PrintWriter;
010:
011: import org.junit.*;
012: import static org.junit.Assert.*;
013:
014: import velosurf.sql.Database;
015: import velosurf.sql.PooledPreparedStatement;
016: import velosurf.context.RowIterator;
017: import velosurf.context.Instance;
018: import velosurf.model.Action;
019: import velosurf.model.Entity;
020: import velosurf.model.Transaction;
021: import velosurf.model.Attribute;
022: import velosurf.util.Logger;
023: import velosurf.util.UserContext;
024:
025: public class WhiteboxTests {
026: protected static final String MODEL_FILE = "conf/model.xml";
027:
028: protected static Database database = null;
029:
030: public static @BeforeClass
031: void openDatabase() throws Exception {
032: Logger.setWriter(new PrintWriter("log/whitebox.log"));
033: database = Database.getInstance(MODEL_FILE);
034: }
035:
036: public @Test
037: void testQuery() throws SQLException {
038: RowIterator iterator = database
039: .query("select * from publisher order by publisher_id");
040: assertTrue(iterator.hasNext());
041: Object row = iterator.next();
042: assertFalse(iterator.hasNext());
043: assertNotNull(row);
044: assertTrue(row instanceof Instance);
045: Instance i = (Instance) row;
046: assertTrue(i.get("publisher_id") != null);
047: assertTrue(i.get("name") != null);
048: }
049:
050: public @Test
051: void testQuery2() throws SQLException {
052: Entity book = database.getEntity("book");
053: assertNotNull(book);
054: RowIterator iterator = book.query();
055: assertTrue(iterator.hasNext());
056: Object row = iterator.next();
057: assertNotNull(row);
058: assertTrue(row instanceof Instance);
059: Instance instance = (Instance) row;
060: assertTrue(instance.getEntity() != null);
061: assertTrue(instance.get("book_id") != null);
062: assertTrue(instance.get("title") != null);
063: assertTrue(instance.get("ref") != null);
064: assertTrue(instance.get("publisher_id") != null);
065: assertTrue(instance.get("author_id") != null);
066: assertTrue(iterator.hasNext());
067: row = iterator.next();
068: assertNotNull(row);
069: instance = (Instance) row;
070: assertTrue(instance.getEntity() != null);
071: assertTrue(instance.get("book_id") != null);
072: assertTrue(instance.get("title") != null);
073: assertTrue(instance.get("ref") != null);
074: assertTrue(instance.get("publisher_id") != null);
075: assertTrue(instance.get("author_id") != null);
076: assertFalse(iterator.hasNext());
077: row = iterator.next();
078: assertNull(row);
079: }
080:
081: public @Test
082: void testEvaluate() throws SQLException {
083: Object scalar = database
084: .evaluate("select count(*) from publisher");
085: assertTrue(scalar instanceof Number);
086: }
087:
088: public @Test
089: void testPrepare() throws SQLException {
090: PooledPreparedStatement prep = database
091: .prepare("select * from publisher where publisher_id=?");
092: Object result = prep.fetch(Arrays.asList(new String[] { "1" }),
093: database.getEntity("publisher"));
094: assertTrue(result != null && result instanceof Instance);
095: }
096:
097: public @Test
098: void testEmptyTable() throws SQLException {
099: Entity empty = database.getEntity("empty");
100: assertNotNull(empty);
101: RowIterator iterator = empty.query();
102: assertFalse(iterator.hasNext());
103: Object row = iterator.next();
104: assertNull(row);
105: }
106:
107: public @Test
108: void testInsert() throws SQLException {
109: UserContext ctx = new UserContext();
110: database.setUserContext(ctx);
111: Entity user = database.getEntity("user");
112: assertNotNull(user);
113: Map<String, Object> row = new HashMap<String, Object>();
114: row.put("login", "donald");
115: row.put("password", "duck");
116: assertTrue(user.insert(row));
117: long id = ctx.getLastInsertedID(user);
118: assertTrue(id == 2);
119: }
120:
121: public @Test
122: void testSuccessfullTransaction() throws SQLException {
123: Entity root = database.getRootEntity();
124: Transaction transaction = new Transaction(
125: "testSuccessfullTransaction", root);
126: transaction
127: .setQueries(Arrays
128: .asList(new String[] {
129: "insert into publisher (publisher_id,name) values (200,'test')",
130: "delete from publisher where publisher_id=200" }));
131: List params = new ArrayList<List>();
132: params.add(new ArrayList<String>());
133: params.add(new ArrayList<String>());
134: transaction.setParamNamesLists(params);
135: transaction.perform(new HashMap<String, Object>());
136: }
137:
138: public @Test(expected=SQLException.class)
139: void testUnsuccessfullTransaction() throws SQLException {
140: try {
141: Entity root = database.getRootEntity();
142: Transaction transaction = new Transaction(
143: "testUnsuccessfullTransaction", root);
144: transaction
145: .setQueries(Arrays
146: .asList(new String[] {
147: "insert into publisher (publisher_id,name) values (201,'test')",
148: "insert into publisher (publisher_id,name) values (1,'test')", }));
149: List params = new ArrayList<List>();
150: params.add(new ArrayList<String>());
151: params.add(new ArrayList<String>());
152: transaction.setParamNamesLists(params);
153: transaction.perform(new HashMap<String, Object>());
154: } finally {
155: Instance row = database.getEntity("publisher").fetch(201);
156: assertNull(row);
157: }
158: }
159:
160: public @Test
161: void testXInclude() throws SQLException {
162: /* testing the included attribute */
163: Attribute attribute = database.getRootEntity().getAttribute(
164: "count_publishers");
165: Object result = attribute.evaluate(null);
166: assertEquals("java.lang.Integer", result.getClass().getName());
167: assertEquals(1, result);
168: }
169:
170: public static @AfterClass
171: void closeDatabase() throws SQLException {
172: database.close();
173: }
174:
175: }
|