Skip to content

Commit fad1070

Browse files
committed
add Room-based test
1 parent c819857 commit fad1070

File tree

3 files changed

+298
-14
lines changed

3 files changed

+298
-14
lines changed

app/build.gradle

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
apply plugin: 'com.android.application'
22

33
android {
4-
compileSdkVersion 26
5-
buildToolsVersion '28.0.3'
4+
compileSdkVersion 29
5+
66
defaultConfig {
77
applicationId "net.zetetic.sqlcipher.test"
88
minSdkVersion 26
9-
targetSdkVersion 26
9+
targetSdkVersion 29
1010
versionCode 1
1111
versionName "1.0"
1212
}
@@ -28,6 +28,9 @@ dependencies {
2828
// implementation (name: 'android-database-sqlcipher-4.0.1', ext: 'aar')
2929
implementation files('libs/android-database-sqlcipher-4.0.1-debug.aar')
3030
implementation "androidx.sqlite:sqlite:2.0.1"
31+
implementation "androidx.room:room-runtime:2.1.0"
32+
annotationProcessor "androidx.room:room-compiler:2.1.0"
33+
3134

3235
// For testing on remote AAR references:
3336
//implementation 'net.zetetic:android-database-sqlcipher:4.0.1@aar'
Lines changed: 291 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,291 @@
1+
package net.zetetic.tests.support;
2+
3+
import android.annotation.SuppressLint;
4+
import android.app.Activity;
5+
import net.sqlcipher.database.SQLiteDatabase;
6+
import net.sqlcipher.database.SupportFactory;
7+
import net.zetetic.ZeteticApplication;
8+
import net.zetetic.tests.TestResult;
9+
import java.io.File;
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
import java.util.UUID;
13+
import androidx.annotation.NonNull;
14+
import androidx.room.Dao;
15+
import androidx.room.Database;
16+
import androidx.room.Delete;
17+
import androidx.room.Entity;
18+
import androidx.room.ForeignKey;
19+
import androidx.room.Index;
20+
import androidx.room.Insert;
21+
import androidx.room.PrimaryKey;
22+
import androidx.room.Query;
23+
import androidx.room.Room;
24+
import androidx.room.RoomDatabase;
25+
import androidx.room.Update;
26+
27+
public class RoomTest implements ISupportTest {
28+
private static final String DB_NAME = "room.db";
29+
30+
private final Activity activity;
31+
32+
@Entity()
33+
public static class ParentEntity {
34+
@PrimaryKey(autoGenerate = true) long id;
35+
int intValue;
36+
float floatValue;
37+
double doubleValue;
38+
char charValue;
39+
boolean boolValue;
40+
}
41+
42+
@Entity(foreignKeys = @ForeignKey(entity = ParentEntity.class,
43+
parentColumns = "id", childColumns = "parentId", onDelete = ForeignKey.CASCADE),
44+
indices = @Index("parentId"))
45+
public static class ChildEntity {
46+
@PrimaryKey @NonNull String uuid;
47+
String stringValue;
48+
long parentId;
49+
}
50+
51+
@Dao
52+
public static abstract class TestDao {
53+
@Query("SELECT * FROM ParentEntity")
54+
abstract List<ParentEntity> getAllParents();
55+
56+
@Query("SELECT * FROM ParentEntity WHERE id = :id")
57+
abstract ParentEntity findParentById(long id);
58+
59+
@Query("SELECT * FROM ChildEntity WHERE parentId = :parentId")
60+
abstract List<ChildEntity> getAllChildrenForParent(long parentId);
61+
62+
@Query("SELECT * FROM ChildEntity WHERE uuid = :uuid")
63+
abstract ChildEntity findChildById(String uuid);
64+
65+
@Insert
66+
abstract void insert(List<ChildEntity> entities);
67+
68+
@Insert
69+
abstract long insert(ParentEntity entity);
70+
71+
@Update
72+
abstract void update(ChildEntity entity);
73+
74+
@Update
75+
abstract void update(ParentEntity entity);
76+
77+
@Delete
78+
abstract void delete(ChildEntity entity);
79+
80+
@Delete
81+
abstract void delete(ParentEntity entity);
82+
}
83+
84+
@Database(entities = {ParentEntity.class, ChildEntity.class}, version = 1)
85+
public static abstract class TestDatabase extends RoomDatabase {
86+
abstract TestDao testDao();
87+
}
88+
89+
public RoomTest(Activity activity) {
90+
this.activity = activity;
91+
}
92+
93+
@SuppressLint("DefaultLocale")
94+
public TestResult run() {
95+
File dbFile = ZeteticApplication.getInstance().getDatabasePath(DB_NAME);
96+
97+
if (dbFile.exists()){
98+
dbFile.delete();
99+
}
100+
101+
final TestResult result = new TestResult(getName(), false);
102+
final byte[] passphrase = SQLiteDatabase.getBytes(ZeteticApplication.DATABASE_PASSWORD.toCharArray());
103+
final SupportFactory factory = new SupportFactory(passphrase);
104+
final TestDatabase room = Room.databaseBuilder(activity, TestDatabase.class, DB_NAME)
105+
.openHelperFactory(factory)
106+
.build();
107+
ParentEntity parent = new ParentEntity();
108+
109+
parent.boolValue = true;
110+
parent.charValue = 'x';
111+
parent.intValue = 1337;
112+
parent.doubleValue = 3.14159;
113+
parent.floatValue = 2.71828f;
114+
parent.id = room.testDao().insert(parent);
115+
116+
List<ParentEntity> parents = room.testDao().getAllParents();
117+
118+
if (parents.size() != 1) {
119+
result.setResult(false);
120+
result.setMessage(String.format("expected 1 parent, found %d", parents.size()));
121+
return result;
122+
}
123+
124+
ParentEntity retrievedParent = parents.get(0);
125+
126+
if (!assertParent(retrievedParent, parent)) {
127+
result.setResult(false);
128+
result.setMessage("retrieved parent from getAllParents() did not match original");
129+
return result;
130+
}
131+
132+
retrievedParent = room.testDao().findParentById(parent.id);
133+
134+
if (retrievedParent == null) {
135+
result.setResult(false);
136+
result.setMessage(String.format("retrieved parent from findParentById() was null for %d", parent.id));
137+
return result;
138+
}
139+
140+
if (!assertParent(retrievedParent, parent)) {
141+
result.setResult(false);
142+
result.setMessage("retrieved parent from findParentById() did not match original");
143+
return result;
144+
}
145+
146+
result.setResult(true);
147+
148+
ChildEntity firstChild = new ChildEntity();
149+
150+
firstChild.uuid = UUID.randomUUID().toString();
151+
firstChild.stringValue = "Um, hi!";
152+
firstChild.parentId = parent.id;
153+
154+
ChildEntity secondChild = new ChildEntity();
155+
156+
secondChild.uuid = UUID.randomUUID().toString();
157+
secondChild.stringValue = "And now for something completely different";
158+
secondChild.parentId = parent.id;
159+
160+
List<ChildEntity> children = new ArrayList<>();
161+
162+
children.add(firstChild);
163+
children.add(secondChild);
164+
165+
room.testDao().insert(children);
166+
167+
List<ChildEntity> retrievedChildren = room.testDao().getAllChildrenForParent(parent.id);
168+
169+
if (retrievedChildren.size() != 2) {
170+
result.setResult(false);
171+
result.setMessage(String.format("expected 2 children, found %d", retrievedChildren.size()));
172+
return result;
173+
}
174+
175+
ChildEntity retrievedChild = retrievedChildren.get(0);
176+
177+
if (!assertChild(retrievedChild, firstChild) && !assertChild(retrievedChild, secondChild)) {
178+
result.setResult(false);
179+
result.setMessage("retrieved child from getAllChildrenForParent() did not match either original");
180+
return result;
181+
}
182+
183+
retrievedChild = retrievedChildren.get(1);
184+
185+
if (!assertChild(retrievedChild, firstChild) && !assertChild(retrievedChild, secondChild)) {
186+
result.setResult(false);
187+
result.setMessage("retrieved child from getAllChildrenForParent() did not match either original");
188+
return result;
189+
}
190+
191+
parent.boolValue = false;
192+
parent.charValue = 'z';
193+
parent.intValue = 65536;
194+
parent.doubleValue = 2.02214076e23; // # of atoms in a mole
195+
parent.floatValue = 299729.458f; // speed of light in km/s
196+
197+
room.testDao().update(parent);
198+
199+
retrievedParent = room.testDao().findParentById(parent.id);
200+
201+
if (!assertParent(retrievedParent, parent)) {
202+
result.setResult(false);
203+
result.setMessage("retrieved parent from post-update findParentById() did not match original");
204+
return result;
205+
}
206+
207+
secondChild.stringValue = "urgent pegboard untied kimono boiler downstairs";
208+
209+
room.testDao().update(secondChild);
210+
211+
retrievedChild = room.testDao().findChildById(secondChild.uuid);
212+
213+
if (!assertChild(retrievedChild, secondChild)) {
214+
result.setResult(false);
215+
result.setMessage("retrieved child from post-updated findChildById() did not match original");
216+
return result;
217+
}
218+
219+
room.testDao().delete(firstChild);
220+
221+
retrievedChildren = room.testDao().getAllChildrenForParent(parent.id);
222+
223+
if (retrievedChildren.size() != 1) {
224+
result.setResult(false);
225+
result.setMessage(String.format("expected 1 children post-delete, found %d", retrievedChildren.size()));
226+
return result;
227+
}
228+
229+
retrievedChild = retrievedChildren.get(0);
230+
231+
if (!assertChild(retrievedChild, secondChild)) {
232+
result.setResult(false);
233+
result.setMessage("retrieved child from post-delete getAllChildrenForParent() did not match original");
234+
return result;
235+
}
236+
237+
room.testDao().delete(parent);
238+
239+
if (room.testDao().getAllParents().size() != 0) {
240+
result.setResult(false);
241+
result.setMessage("after delete of parent, parent count != 0");
242+
return result;
243+
}
244+
245+
if (room.testDao().findParentById(parent.id) != null) {
246+
result.setResult(false);
247+
result.setMessage("after delete of parent, was able to retrieve parent");
248+
return result;
249+
}
250+
251+
if (room.testDao().getAllChildrenForParent(parent.id).size() != 0) {
252+
result.setResult(false);
253+
result.setMessage("after delete of parent, child count != 0");
254+
return result;
255+
}
256+
257+
if (room.testDao().findChildById(firstChild.uuid) != null) {
258+
result.setResult(false);
259+
result.setMessage("after delete of parent, was able to retrieve first child");
260+
return result;
261+
}
262+
263+
if (room.testDao().findChildById(secondChild.uuid) != null) {
264+
result.setResult(false);
265+
result.setMessage("after delete of parent, was able to retrieve second child");
266+
return result;
267+
}
268+
269+
return result;
270+
}
271+
272+
@Override
273+
public String getName() {
274+
return "Room Test";
275+
}
276+
277+
private boolean assertParent(ParentEntity one, ParentEntity two) {
278+
return one.id == two.id &&
279+
one.boolValue == two.boolValue &&
280+
one.charValue == two.charValue &&
281+
one.intValue == two.intValue &&
282+
Math.abs(one.floatValue - two.floatValue) < 0.01 &&
283+
Math.abs(one.doubleValue - two.doubleValue) < 0.01;
284+
}
285+
286+
private boolean assertChild(ChildEntity one, ChildEntity two) {
287+
return one.uuid.equals(two.uuid) &&
288+
one.stringValue.equals(two.stringValue) &&
289+
one.parentId == two.parentId;
290+
}
291+
}

app/src/main/java/net/zetetic/tests/support/SupportSuiteRunner.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,6 @@ private List<ISupportTest> getTestsToRun() {
107107
tests.add(new TransactionNonExclusiveTest());
108108
tests.add(new TransactionWithListenerTest());
109109
tests.add(new LargeDatabaseCursorAccessTest());
110-
111-
/*
112-
113-
//// tests.add(new TimeLargeByteArrayQueryTest());
114-
*/
115-
116110
tests.add(new QueryLimitTest());
117111
tests.add(new RTreeTest());
118112
tests.add(new ReadWriteDatabaseToExternalStorageTest());
@@ -132,7 +126,6 @@ private List<ISupportTest> getTestsToRun() {
132126
tests.add(new LoopingQueryTest());
133127
tests.add(new LoopingCountQueryTest());
134128
tests.add(new AttachNewDatabaseTest());
135-
// TODO rewrite tests.add(new AttachExistingDatabaseTest());
136129
tests.add(new CanThrowSQLiteExceptionTest());
137130
tests.add(new RawExecSQLTest());
138131
tests.add(new RawExecSQLExceptionTest());
@@ -155,7 +148,6 @@ private List<ISupportTest> getTestsToRun() {
155148
tests.add(new SoundexTest());
156149
tests.add(new RawQueryTest());
157150
tests.add(new RawRekeyTest());
158-
// TODO rewrite tests.add(new MultiThreadReadWriteTest());
159151
tests.add(new VerifyUTF8EncodingForKeyTest());
160152
tests.add(new TextAsIntegerTest());
161153
tests.add(new TextAsDoubleTest());
@@ -168,8 +160,6 @@ private List<ISupportTest> getTestsToRun() {
168160
tests.add(new CopyStringToBufferTestStringSmallBuffer());
169161
tests.add(new CopyStringToBufferTestStringLargeBuffer());
170162
tests.add(new CopyStringToBufferNullTest());
171-
// TODO rewrite tests.add(new OpenSQLCipher3DatabaseTest());
172-
// TODO rewrite tests.add(new MUTF8ToUTF8WithNullMigrationTest());
173163
tests.add(new RawQuerySyntaxErrorMessageTest());
174164
tests.add(new RawQueryNonsenseStatementErrorMessageTest());
175165
tests.add(new RawQueryNoSuchFunctionErrorMessageTest());
@@ -184,7 +174,7 @@ private List<ISupportTest> getTestsToRun() {
184174
tests.add(new BindFloatRawQueryTest());
185175
tests.add(new BindByteArrayRawQueryTest());
186176
tests.add(new NullRawQueryTest());
187-
// TODO rewrite tests.add(new ReadWriteDatabaseToExternalStorageTest());
177+
tests.add(new RoomTest(activity));
188178

189179
return tests;
190180
}

0 commit comments

Comments
 (0)