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
17package tests.SQLite;
18
19import SQLite.Blob;
20import SQLite.Database;
21import SQLite.Exception;
22import SQLite.Stmt;
23import dalvik.annotation.KnownFailure;
24import dalvik.annotation.TestLevel;
25import dalvik.annotation.TestTargetClass;
26import dalvik.annotation.TestTargetNew;
27import dalvik.annotation.TestTargets;
28
29import junit.framework.TestCase;
30
31import tests.support.DatabaseCreator;
32import tests.support.Support_SQL;
33
34import java.io.IOException;
35import java.io.InputStream;
36import java.io.OutputStream;
37
38@TestTargetClass(Blob.class)
39public class BlobTest extends SQLiteTest {
40
41    private static Blob testBlob = null;
42
43    private byte[] blobInput= null;
44
45    private static InputStream file = null;
46
47    private static Database db = null;
48
49    private static Stmt st = null;
50
51    public class MockBlob extends Blob {
52        public void finalize() {
53            try {
54                super.finalize();
55            } catch (Throwable exception) {
56                fail("Test activity faild!");
57            }
58        }
59    }
60
61    public void setUp() throws java.lang.Exception {
62        super.setUp();
63        testBlob = new Blob();
64
65        super.setUp();
66        Support_SQL.loadDriver();
67        db = new Database();
68        db.open(dbFile.getPath(), 0);
69
70        db.exec("create table B(id integer primary key, val blob)",null);
71        db.exec("insert into B values(1, zeroblob(128))", null);
72        db.exec("insert into B values(2, zeroblob(128))", null);
73        db.exec("insert into B values(3, zeroblob(128))", null);
74
75        // can not fill Blob with data at this point...
76        /*
77        File resources = Support_Resources.createTempFolder();
78        BufferedReader r = null;
79        try {
80            Class c = Class.forName(this.getClass().getName());
81            assertNotNull(c);
82            file = Class.forName(this.getClass().getName())
83                    .getResourceAsStream("/blob.c");
84            r = new BufferedReader(new InputStreamReader(file));
85        } catch (NullPointerException e) {
86            fail("Should not throw NullPointerException reading file"
87                    + e.getMessage());
88        }
89        OutputStream out = testBlob.getOutputStream();
90        String s = null;
91        while ((s = r.readLine()) != null) {
92            out.write(r.readLine().getBytes());
93        }
94        out.flush();
95        out.close();
96        testBlob.close();
97        */
98    }
99
100    public void tearDown() {
101
102        testBlob.close();
103        super.tearDown();
104    }
105
106    /**
107     * @throws Exception
108     * @throws IOException
109     * @tests Blob#Blob()
110     */
111    @TestTargets ( {
112    @TestTargetNew(
113        level = TestLevel.NOT_FEASIBLE,
114        notes = "db.open_blob is not supported also for Stmt, therefore cannot test Blobs",
115        method = "Blob",
116        args = {}
117    ),
118    @TestTargetNew(
119        level = TestLevel.NOT_FEASIBLE,
120        notes = "functional test",
121        method = "getOutputStream",
122        args = {}
123    ),
124    @TestTargetNew(
125        level = TestLevel.NOT_FEASIBLE,
126        notes = "functional test",
127        method = "getInputStream",
128        args = {}
129    )
130    })
131    @KnownFailure("db.open_blob is not supported.")
132    public void testBlob() throws Exception, IOException {
133        byte[] b = new byte[4];
134        byte[] b128 = new byte[128];
135        for (int i = 0; i < b128.length; i++) {
136        b128[i] = (byte) i;
137        }
138        Blob blob = db.open_blob(dbFile.getPath(), "B", "val", 1, true);
139        try {
140
141        OutputStream os = blob.getOutputStream();
142        os.write(b128);
143        os.close();
144
145        InputStream is = blob.getInputStream();
146        is.skip(96);
147        assertEquals(4,is.read(b));
148        is.close();
149        } finally {
150        blob.close();
151        }
152    }
153
154    /**
155     * @tests Blob#finalize()
156     */
157    @TestTargetNew(
158        level = TestLevel.NOT_FEASIBLE,
159        notes = "Can not be checked. Should be tested in DX test package.",
160        method = "finalize",
161        args = {}
162    )
163    public void testFinalize() {
164
165    }
166
167    /**
168     * @tests Blob.getInputStream()
169     */
170    @TestTargetNew(
171        level = TestLevel.PARTIAL_COMPLETE,
172        notes = "Exception test",
173        method = "getInputStream",
174        args = {}
175    )
176    public void testGetInputStream() {
177        InputStream in = testBlob.getInputStream();
178
179        try {
180            in.read();
181            fail("Exception not thrown for invalid Blob.");
182        } catch (Throwable e) {
183            //ok
184        }
185    }
186
187    /**
188     * @tests Blob#getOutputStream()
189     */
190    @TestTargetNew(
191        level = TestLevel.COMPLETE,
192        notes = "Exception test",
193        method = "getOutputStream",
194        args = {}
195    )
196    public void testGetOutputStream() {
197        OutputStream out = testBlob.getOutputStream();
198
199        try {
200           out.write(null);
201           fail("Write operation unsupported");
202        } catch (Throwable e) {
203            assertEquals("Write operation unsupported", e.getMessage());
204        }
205    }
206
207    /**
208     * @tests Blob#close()
209     */
210    @TestTargetNew(
211        level = TestLevel.NOT_FEASIBLE,
212        notes = "not clear from spec what should happen when Blob is closed.",
213        method = "close",
214        args = {}
215    )
216//    @KnownFailure("Blob does not clean up inputStream.")
217    public void testClose() {
218    assertNotNull(testBlob);
219
220    testBlob.close();
221    // inputStream either null or some error occurs
222    try {
223        // TODO This does look a bit weird. Revisit later.
224        assertNull(testBlob.getInputStream());
225    } catch (Throwable e) {
226        //ok
227    }
228    }
229}
230