2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 import SQLite.Database;
20 import SQLite.Exception;
21 import SQLite.Function;
22 import SQLite.FunctionContext;
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;
32 import junit.framework.TestCase;
34 import java.io.UnsupportedEncodingException;
35 import java.sql.SQLException;
36 import java.sql.Statement;
38 import tests.support.DatabaseCreator;
40 @TestTargetClass(FunctionContext.class)
41 public class FunctionContextTest extends SQLiteTest {
43 private Database db = null;
45 public void setUp() throws java.lang.Exception {
49 db.open(dbFile.getPath(), 0);
50 st = conn.createStatement();
51 st.execute(DatabaseCreator.CREATE_TABLE2);
52 st.execute(DatabaseCreator.CREATE_TABLE_SIMPLE1);
57 * @see junit.framework.TestCase#tearDown()
59 public void tearDown() {
64 * Test method for {@link SQLite.FunctionContext#set_result(java.lang.String)}.
68 level = TestLevel.SUFFICIENT,
69 notes = "indirectly tested invoking function",
70 method = "set_result",
71 args = {java.lang.String.class}
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);
83 assertEquals("TestInput", val);
87 * Test method for {@link SQLite.FunctionContext#set_result(int)}.
91 level = TestLevel.SUFFICIENT,
92 notes = "method test",
93 method = "set_result",
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);
106 assertEquals(testInt.intVal, Integer.parseInt(val));
110 * Test method for {@link SQLite.FunctionContext#set_result(double)}.
114 level = TestLevel.SUFFICIENT,
115 notes = "indirectly tested",
116 method = "set_result",
117 args = {double.class}
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);
129 assertEquals(testD.testDouble, Double.parseDouble(val));
131 assertTrue(testD.functionCalled);
135 * Test method for {@link SQLite.FunctionContext#set_error(java.lang.String)}.
139 level = TestLevel.COMPLETE,
140 notes = "method test",
141 method = "set_error",
142 args = {java.lang.String.class}
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);
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());
159 assertFalse(testD.functionCalled);
163 * Test method for {@link SQLite.FunctionContext#set_result(byte[])}.
165 * @throws UnsupportedEncodingException
168 level = TestLevel.COMPLETE,
169 notes = "method test",
170 method = "set_result",
171 args = {byte[].class}
173 public void testSet_resultByteArray() throws Exception, UnsupportedEncodingException {
175 TestFCByteArray testBinArrayFnc = new TestFCByteArray();
176 String expected = "";
177 expected = "X'" + getHexString(testBinArrayFnc.byteVal) + "'";
180 db.exec("create table testBinaryData (binVal BINARY) ;", null);
183 st = db.prepare("insert into testBinaryData values (?)");
184 st.bind(1, testBinArrayFnc.byteVal);
188 db.create_function("testBinArray", 1, testBinArrayFnc);
190 .get_table("select testBinArray(binVal) from testBinaryData");
192 String row[] = (String[]) res.rows.elementAt(0);
195 assertTrue(expected.equalsIgnoreCase(val));
197 assertTrue(testBinArrayFnc.functionCalled);
201 db.exec("drop table testBinaryData;", null);
206 * Test method for {@link SQLite.FunctionContext#set_result_zeroblob(int)}.
208 * @throws UnsupportedEncodingException
211 level = TestLevel.COMPLETE,
212 notes = "method test",
213 method = "set_result_zeroblob",
216 @AndroidOnly("ZeroBlob not supported")
217 public void testSet_result_zeroblob() throws Exception,
218 UnsupportedEncodingException {
220 TestFCZeroBlob testZeroBlobFnc = new TestFCZeroBlob();
221 byte[] byteVal = {(byte) 1, (byte) 2, (byte) 3};
225 db.exec("create table testBinaryData (binVal BINARY) ;", null);
228 st = db.prepare("insert into testBinaryData values (?)");
233 db.create_function("testZeroBlob", 0, testZeroBlobFnc);
235 .get_table("select testZeroBlob() from testBinaryData");
236 TableResult res2 = db.get_table("select zeroblob("
237 + testZeroBlobFnc.numBytes + ") from testBinaryData");
239 String row[] = (String[]) res.rows.elementAt(0);
244 assertEquals(((String[]) res2.rows.elementAt(0))[0], val);
245 assertTrue(testZeroBlobFnc.functionCalled);
249 db.exec("drop table if exists testBinaryData;", null);
254 * Test method for {@link SQLite.FunctionContext#count()}.
255 * @throws SQLException
259 level = TestLevel.COMPLETE,
260 notes = "method test",
264 @AndroidOnly("Test Method results in a segmentation fault.")
265 public void testCount() throws SQLException, Exception {
266 TestFCCount countTest = new TestFCCount();
269 assertFalse(countTest.functionCalled);
271 DatabaseCreator.fillTestTable2(conn, inputCount);
272 db.create_function("testCount", 0, countTest);
273 // the invokation of testCount leads to a Segmentation fault
276 .get_table("select testCount() from "+DatabaseCreator.TEST_TABLE2);
278 String row[] = (String[]) res.rows.elementAt(0);
281 assertTrue(countTest.functionCalled);
282 assertEquals(inputCount,Integer.parseInt(val));
287 class TestFCError implements Function {
288 public boolean functionCalled = false;
289 public String errorMsg = "FunctionError";
291 public void function(FunctionContext fc, String args[]) {
292 functionCalled = true;
293 fc.set_error(errorMsg);
296 public void last_step(FunctionContext fc) {
297 // TODO Auto-generated method stub
301 public void step(FunctionContext fc, String[] args) {
302 // TODO Auto-generated method stub
307 class TestFCCount implements Function {
308 public boolean functionCalled = false;
309 public int noOfRows = 0;
311 public void function(FunctionContext fc, String args[]) {
312 functionCalled = true;
313 noOfRows = fc.count();
314 fc.set_result(noOfRows);
317 public void last_step(FunctionContext fc) {
318 // TODO Auto-generated method stub
322 public void step(FunctionContext fc, String[] args) {
323 // TODO Auto-generated method stub
328 class TestFCZeroBlob implements Function {
329 public int numBytes = 16;
330 public boolean functionCalled = false;
332 public void function(FunctionContext fc, String args[]) {
333 functionCalled = true;
334 fc.set_result_zeroblob(numBytes);
337 public void last_step(FunctionContext fc) {
338 // TODO Auto-generated method stub
342 public void step(FunctionContext fc, String[] args) {
343 // TODO Auto-generated method stub
348 class TestFCString implements Function {
349 public String testString = "TestString";
350 public boolean functionCalled;
352 public void function(FunctionContext fc, String args[]) {
354 functionCalled = true;
355 fc.set_result(args[0]);
358 public void last_step(FunctionContext fc) {
359 // TODO Auto-generated method stub
363 public void step(FunctionContext fc, String[] args) {
364 // TODO Auto-generated method stub
369 class TestFCInt implements Function {
370 public int intVal = Integer.MAX_VALUE;
371 public boolean functionCalled;
373 public void function(FunctionContext fc, String args[]) {
375 functionCalled = true;
376 fc.set_result(Integer.parseInt(args[0]));
379 public void last_step(FunctionContext fc) {
380 // TODO Auto-generated method stub
384 public void step(FunctionContext fc, String[] args) {
385 // TODO Auto-generated method stub
390 class TestFCByteArray implements Function {
391 public byte[] byteVal = {(byte) 1, (byte) 2, (byte) 3};
392 public boolean functionCalled;
394 public void function(FunctionContext fc, String args[]) {
396 functionCalled = true;
397 fc.set_result(args[0].getBytes());
400 public void last_step(FunctionContext fc) {
401 // TODO Auto-generated method stub
405 public void step(FunctionContext fc, String[] args) {
406 // TODO Auto-generated method stub
411 class SinFunc implements Function {
413 public Double testDouble = 3.0;
414 public boolean functionCalled = false;
416 public void function(FunctionContext fc, String args[]) {
417 Double d = new Double(args[0]);
418 functionCalled = true;
419 fc.set_result(d.doubleValue());
422 public void last_step(FunctionContext fc) {
423 // TODO Auto-generated method stub
427 public void step(FunctionContext fc, String[] args) {
428 // TODO Auto-generated method stub
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'
440 public static String getHexString(byte[] raw)
441 throws UnsupportedEncodingException {
442 byte[] hex = new byte[2 * raw.length];
447 hex[index++] = HEX_CHAR_TABLE[v >>> 4];
448 hex[index++] = HEX_CHAR_TABLE[v & 0xF];
450 return new String(hex, "ASCII");