OSDN Git Service

AI 143664: am: CL 143631 am: CL 143455 Bringing the SQL tests down to zero failures...
[android-x86/dalvik.git] / libcore / sql / src / test / java / tests / SQLite / FunctionContextTest.java
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package tests.SQLite;
18
19 import SQLite.Database;
20 import SQLite.Exception;
21 import SQLite.Function;
22 import SQLite.FunctionContext;
23 import SQLite.Stmt;
24 import SQLite.TableResult;
25 import dalvik.annotation.AndroidOnly;
26 import dalvik.annotation.KnownFailure;
27 import dalvik.annotation.TestTargets;
28 import dalvik.annotation.TestLevel;
29 import dalvik.annotation.TestTargetNew;
30 import dalvik.annotation.TestTargetClass;
31
32 import junit.framework.TestCase;
33
34 import java.io.UnsupportedEncodingException;
35 import java.sql.SQLException;
36 import java.sql.Statement;
37
38 import tests.support.DatabaseCreator;
39
40 @TestTargetClass(FunctionContext.class)
41 public class FunctionContextTest extends SQLiteTest {
42     
43     private Database db = null;
44
45     public void setUp() throws java.lang.Exception {
46         Statement st = null;
47         super.setUp();
48         db = new Database();
49         db.open(dbFile.getPath(), 0);
50         st = conn.createStatement();
51         st.execute(DatabaseCreator.CREATE_TABLE2);
52         st.execute(DatabaseCreator.CREATE_TABLE_SIMPLE1);
53         st.close();
54     }
55
56     /* (non-Javadoc)
57      * @see junit.framework.TestCase#tearDown()
58      */
59     public void tearDown() {
60         super.tearDown();
61     }
62
63     /**
64      * Test method for {@link SQLite.FunctionContext#set_result(java.lang.String)}.
65      * @throws Exception 
66      */
67     @TestTargetNew(
68         level = TestLevel.SUFFICIENT,
69         notes = "indirectly tested invoking function",
70         method = "set_result",
71         args = {java.lang.String.class}
72     )
73     public void testSet_resultString() throws Exception {
74         TestFCString testString = new TestFCString();
75         db.exec("insert into " + DatabaseCreator.TEST_TABLE2
76                 + " (ftext) values ('TestInput')", null);
77         db.create_function("test", 1, testString);
78         TableResult res = db.get_table("select test(ftext) from "
79                 + DatabaseCreator.TEST_TABLE2);
80         String row[] = (String[]) res.rows.elementAt(0);
81         String val = row[0];
82
83         assertEquals("TestInput", val);
84     }
85
86     /**
87      * Test method for {@link SQLite.FunctionContext#set_result(int)}.
88      * @throws Exception 
89      */
90     @TestTargetNew(
91         level = TestLevel.SUFFICIENT,
92         notes = "method test",
93         method = "set_result",
94         args = {int.class}
95     )
96     public void testSet_resultInt() throws Exception {
97         TestFCInt testInt = new TestFCInt();
98         db.exec("insert into " + DatabaseCreator.SIMPLE_TABLE1
99                 + "  values (1,'" + testInt.intVal + "',3)", null);
100         db.create_function("testInt", 1, testInt);
101         TableResult res = db.get_table("select testInt(speed) from "
102                 + DatabaseCreator.SIMPLE_TABLE1);
103         String row[] = (String[]) res.rows.elementAt(0);
104         String val = row[0];
105
106         assertEquals(testInt.intVal, Integer.parseInt(val));
107     }
108
109     /**
110      * Test method for {@link SQLite.FunctionContext#set_result(double)}.
111      * @throws Exception 
112      */
113     @TestTargetNew(
114         level = TestLevel.SUFFICIENT,
115         notes = "indirectly tested",
116         method = "set_result",
117         args = {double.class}
118     )
119     public void testSet_resultDouble() throws Exception {
120         SinFunc testD = new SinFunc();
121         db.exec("insert into " + DatabaseCreator.TEST_TABLE2
122                 + " (fdouble)  values (" + testD.testDouble + ")", null);
123         db.create_function("testDouble", 1, testD);
124         TableResult res = db.get_table("select testDouble(fdouble) from "
125                 + DatabaseCreator.TEST_TABLE2);
126         String row[] = (String[]) res.rows.elementAt(0);
127         String val = row[0];
128
129         assertEquals(testD.testDouble, Double.parseDouble(val));
130         
131         assertTrue(testD.functionCalled);
132     }
133
134     /**
135      * Test method for {@link SQLite.FunctionContext#set_error(java.lang.String)}.
136      * @throws Exception 
137      */
138     @TestTargetNew(
139         level = TestLevel.COMPLETE,
140         notes = "method test",
141         method = "set_error",
142         args = {java.lang.String.class}
143     )
144     public void testSet_error() throws Exception {
145         TestFCError testError = new TestFCError();
146         SinFunc testD = new SinFunc();
147         db.exec("insert into " + DatabaseCreator.TEST_TABLE2
148                 + " (fdouble)  values (" + testD.testDouble + ")", null);
149         db.create_function("testError", 1, testError);
150         
151         try {
152         TableResult res = db.get_table("select testError(fdouble) from "
153                 + DatabaseCreator.TEST_TABLE2);
154         fail("Should get Exception");
155         } catch (Exception e) {
156             assertEquals("error in step", e.getMessage());
157         }
158         
159         assertFalse(testD.functionCalled);
160     }
161
162     /**
163      * Test method for {@link SQLite.FunctionContext#set_result(byte[])}.
164      * @throws Exception 
165      * @throws UnsupportedEncodingException 
166      */
167     @TestTargetNew(
168         level = TestLevel.COMPLETE,
169         notes = "method test",
170         method = "set_result",
171         args = {byte[].class}
172     )
173     public void testSet_resultByteArray() throws Exception, UnsupportedEncodingException {     
174         Stmt st = null;
175         TestFCByteArray testBinArrayFnc = new TestFCByteArray();
176         String expected = "";
177         expected = "X'" + getHexString(testBinArrayFnc.byteVal) + "'";
178         
179         // setup
180         db.exec("create table testBinaryData (binVal BINARY) ;", null);
181         
182         try {
183         st = db.prepare("insert into testBinaryData values (?)");
184         st.bind(1, testBinArrayFnc.byteVal);
185         st.step();
186
187
188         db.create_function("testBinArray", 1, testBinArrayFnc);
189         TableResult res = db
190                 .get_table("select testBinArray(binVal) from testBinaryData");
191
192         String row[] = (String[]) res.rows.elementAt(0);
193         String val = row[0];
194        
195         assertTrue(expected.equalsIgnoreCase(val));
196         
197         assertTrue(testBinArrayFnc.functionCalled);
198         
199         } finally {
200             //teardown
201             db.exec("drop table testBinaryData;", null);
202         }
203     }
204
205     /**
206      * Test method for {@link SQLite.FunctionContext#set_result_zeroblob(int)}.
207      * @throws Exception 
208      * @throws UnsupportedEncodingException 
209      */
210     @TestTargetNew(
211         level = TestLevel.COMPLETE,
212         notes = "method test",
213         method = "set_result_zeroblob",
214         args = {int.class}
215     )
216     @AndroidOnly("ZeroBlob not supported")
217     public void testSet_result_zeroblob() throws Exception,
218             UnsupportedEncodingException {
219         Stmt st = null;
220         TestFCZeroBlob testZeroBlobFnc = new TestFCZeroBlob();
221         byte[] byteVal = {(byte) 1, (byte) 2, (byte) 3};
222         
223         
224         // setup
225         db.exec("create table testBinaryData (binVal BINARY) ;", null);
226         
227         try {
228         st = db.prepare("insert into testBinaryData values (?)");
229         st.bind(1, byteVal);
230         st.step();
231
232
233         db.create_function("testZeroBlob", 0, testZeroBlobFnc);
234         TableResult res = db
235                 .get_table("select testZeroBlob() from testBinaryData");
236         TableResult res2 = db.get_table("select zeroblob("
237                 + testZeroBlobFnc.numBytes + ") from testBinaryData");
238
239         String row[] = (String[]) res.rows.elementAt(0);
240         String val = row[0];
241
242         assertNotNull(val);
243
244         assertEquals(((String[]) res2.rows.elementAt(0))[0], val);
245         assertTrue(testZeroBlobFnc.functionCalled);
246         
247         } finally  {
248          // teardown
249             db.exec("drop table if exists testBinaryData;", null);
250         }
251     }
252
253     /**
254      * Test method for {@link SQLite.FunctionContext#count()}.
255      * @throws SQLException 
256      * @throws Exception 
257      */
258     @TestTargetNew(
259         level = TestLevel.COMPLETE,
260         notes = "method test",
261         method = "count",
262         args = {}
263     )
264     @AndroidOnly("Test Method results in a segmentation fault.")
265     public void testCount() throws SQLException, Exception {
266         TestFCCount countTest = new TestFCCount();
267         int inputCount = 10;
268         
269         assertFalse(countTest.functionCalled);
270         
271         DatabaseCreator.fillTestTable2(conn, inputCount);
272         db.create_function("testCount", 0, countTest);
273         // the invokation of testCount leads to a Segmentation fault
274         /*
275         TableResult res = db
276                 .get_table("select testCount() from "+DatabaseCreator.TEST_TABLE2);
277        
278         String row[] = (String[]) res.rows.elementAt(0);
279         String val = row[0];
280         
281         assertTrue(countTest.functionCalled);
282         assertEquals(inputCount,Integer.parseInt(val));
283         */
284         
285     }
286     
287     class TestFCError implements Function {
288         public boolean functionCalled = false;
289         public String errorMsg = "FunctionError";
290         
291         public void function(FunctionContext fc, String args[]) {
292             functionCalled = true;
293             fc.set_error(errorMsg);
294         }
295
296         public void last_step(FunctionContext fc) {
297             // TODO Auto-generated method stub
298
299         }
300
301         public void step(FunctionContext fc, String[] args) {
302             // TODO Auto-generated method stub
303
304         }
305     }
306     
307     class TestFCCount implements Function {
308         public boolean functionCalled = false;
309         public int noOfRows = 0;
310         
311         public void function(FunctionContext fc, String args[]) {
312             functionCalled = true;
313             noOfRows = fc.count();
314             fc.set_result(noOfRows);
315         }
316
317         public void last_step(FunctionContext fc) {
318             // TODO Auto-generated method stub
319
320         }
321
322         public void step(FunctionContext fc, String[] args) {
323             // TODO Auto-generated method stub
324
325         }
326     }
327     
328     class TestFCZeroBlob implements Function {
329         public int numBytes = 16;
330         public boolean functionCalled = false;
331         
332         public void function(FunctionContext fc, String args[]) {
333             functionCalled = true;
334             fc.set_result_zeroblob(numBytes);
335         }
336
337         public void last_step(FunctionContext fc) {
338             // TODO Auto-generated method stub
339
340         }
341
342         public void step(FunctionContext fc, String[] args) {
343             // TODO Auto-generated method stub
344
345         }
346     }
347     
348     class TestFCString implements Function {
349         public String testString = "TestString";
350         public boolean functionCalled;
351         
352         public void function(FunctionContext fc, String args[]) {
353             assertNotNull(args);
354             functionCalled = true;
355             fc.set_result(args[0]);
356         }
357
358         public void last_step(FunctionContext fc) {
359             // TODO Auto-generated method stub
360
361         }
362
363         public void step(FunctionContext fc, String[] args) {
364             // TODO Auto-generated method stub
365
366         }
367     }
368     
369     class TestFCInt implements Function {
370         public int intVal = Integer.MAX_VALUE;
371         public boolean functionCalled;
372         
373         public void function(FunctionContext fc, String args[]) {
374             assertNotNull(args);
375             functionCalled = true;
376             fc.set_result(Integer.parseInt(args[0]));
377         }
378
379         public void last_step(FunctionContext fc) {
380             // TODO Auto-generated method stub
381
382         }
383
384         public void step(FunctionContext fc, String[] args) {
385             // TODO Auto-generated method stub
386
387         }
388     }
389     
390     class TestFCByteArray implements Function {
391         public byte[] byteVal = {(byte)  1, (byte) 2, (byte) 3};
392         public boolean functionCalled;
393         
394         public void function(FunctionContext fc, String args[]) {
395             assertNotNull(args);
396             functionCalled = true;
397             fc.set_result(args[0].getBytes());
398         }
399
400         public void last_step(FunctionContext fc) {
401             // TODO Auto-generated method stub
402
403         }
404
405         public void step(FunctionContext fc, String[] args) {
406             // TODO Auto-generated method stub
407
408         }
409     }
410     
411         class SinFunc implements Function {
412             
413         public Double testDouble = 3.0;
414         public boolean functionCalled = false;
415         
416         public void function(FunctionContext fc, String args[]) {
417             Double d = new Double(args[0]);
418             functionCalled = true;
419             fc.set_result(d.doubleValue());
420         }
421
422         public void last_step(FunctionContext fc) {
423             // TODO Auto-generated method stub
424
425         }
426
427         public void step(FunctionContext fc, String[] args) {
428             // TODO Auto-generated method stub
429
430         }
431     }
432     
433     static final byte[] HEX_CHAR_TABLE = {
434             (byte)'0', (byte)'1', (byte)'2', (byte)'3',
435             (byte)'4', (byte)'5', (byte)'6', (byte)'7',
436             (byte)'8', (byte)'9', (byte)'a', (byte)'b',
437             (byte)'c', (byte)'d', (byte)'e', (byte)'f'
438           };    
439
440      public static String getHexString(byte[] raw)
441             throws UnsupportedEncodingException {
442         byte[] hex = new byte[2 * raw.length];
443         int index = 0;
444
445         for (byte b : raw) {
446             int v = b & 0xFF;
447             hex[index++] = HEX_CHAR_TABLE[v >>> 4];
448             hex[index++] = HEX_CHAR_TABLE[v & 0xF];
449         }
450         return new String(hex, "ASCII");
451     }
452
453 }