• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 libcore.sqlite;
18 
19 import SQLite.Blob;
20 import SQLite.Database;
21 import SQLite.Exception;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import tests.support.Support_SQL;
26 
27 public final class OldBlobTest extends OldSQLiteTest {
28 
29     private static Blob testBlob = null;
30 
31     private static Database db = null;
32 
setUp()33     public void setUp() throws java.lang.Exception {
34         super.setUp();
35         testBlob = new Blob();
36 
37         super.setUp();
38         Support_SQL.loadDriver();
39         db = new Database();
40         db.open(dbFile.getPath(), 0);
41 
42         db.exec("create table B(id integer primary key, val blob)",null);
43         db.exec("insert into B values(1, zeroblob(128))", null);
44         db.exec("insert into B values(2, zeroblob(128))", null);
45         db.exec("insert into B values(3, zeroblob(128))", null);
46 
47         // can not fill Blob with data at this point...
48         /*
49         File resources = Support_Resources.createTempFolder();
50         BufferedReader r = null;
51         try {
52             Class c = Class.forName(this.getClass().getName());
53             assertNotNull(c);
54             file = Class.forName(this.getClass().getName())
55                     .getResourceAsStream("/blob.c");
56             r = new BufferedReader(new InputStreamReader(file));
57         } catch (NullPointerException e) {
58             fail("Should not throw NullPointerException reading file"
59                     + e.getMessage());
60         }
61         OutputStream out = testBlob.getOutputStream();
62         String s = null;
63         while ((s = r.readLine()) != null) {
64             out.write(r.readLine().getBytes());
65         }
66         out.flush();
67         out.close();
68         testBlob.close();
69         */
70     }
71 
tearDown()72     @Override public void tearDown() throws java.lang.Exception {
73         testBlob.close();
74         super.tearDown();
75     }
76 
77     /**
78      * db.open_blob is not supported.
79      */
testBlob()80     public void testBlob() throws Exception, IOException {
81         byte[] b = new byte[4];
82         byte[] b128 = new byte[128];
83         for (int i = 0; i < b128.length; i++) {
84         b128[i] = (byte) i;
85         }
86         Blob blob = db.open_blob(dbFile.getPath(), "B", "val", 1, true);
87         try {
88 
89         OutputStream os = blob.getOutputStream();
90         os.write(b128);
91         os.close();
92 
93         InputStream is = blob.getInputStream();
94         is.skip(96);
95         assertEquals(4,is.read(b));
96         is.close();
97         } finally {
98         blob.close();
99         }
100     }
101 
testGetInputStream()102     public void testGetInputStream() {
103         InputStream in = testBlob.getInputStream();
104         try {
105             in.read();
106             fail("Exception not thrown for invalid Blob.");
107         } catch (Throwable e) {
108             //ok
109         }
110     }
111 
testGetOutputStream()112     public void testGetOutputStream() {
113         OutputStream out = testBlob.getOutputStream();
114 
115         try {
116            out.write(null);
117            fail("Write operation unsupported");
118         } catch (Throwable e) {
119             assertEquals("Write operation unsupported", e.getMessage());
120         }
121     }
122 
testClose()123     public void testClose() {
124         assertNotNull(testBlob);
125 
126         testBlob.close();
127         // inputStream either null or some error occurs
128         try {
129             // TODO This does look a bit weird. Revisit later.
130             assertNull(testBlob.getInputStream());
131         } catch (Throwable e) {
132             //ok
133         }
134     }
135 }
136