Skip to content
This repository has been archived by the owner on Mar 10, 2022. It is now read-only.

Remove Android dependencies from shared code #1884

Merged
merged 2 commits into from
Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion android/CouchbaseLite/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ android {
}
}
}

compileOptions {
targetCompatibility 1.8
sourceCompatibility 1.8
}

buildTypes {
release {
minifyEnabled false
Expand Down Expand Up @@ -442,5 +448,5 @@ task smoketest(dependsOn: ['checkstyle', 'lint', 'findbugsHtml', 'pmd', 'testDeb

task checkLocal(dependsOn: ['checkstyle', 'lint', 'findbugsHtml', 'pmd', 'testDebugUnitTest', 'connectedDebugAndroidTest'])

task buildJenkins(dependsOn: ['checkstyle', 'lint', 'findbugsXml', 'pmd', 'check', 'connectedAndroidTest'])
task buildCI(dependsOn: ['checkstyle', 'lint', 'findbugsXml', 'pmd', 'check', 'connectedAndroidTest'])

Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,9 @@
package com.couchbase.lite;

import android.content.Context;
import android.content.res.AssetManager;
import android.support.test.InstrumentationRegistry;

import com.couchbase.lite.internal.support.Log;
import com.couchbase.lite.internal.utils.ExecutorUtils;
import com.couchbase.lite.internal.utils.JsonUtils;
import com.couchbase.lite.utils.Config;
import com.couchbase.lite.utils.FileUtils;
import com.couchbase.lite.internal.core.C4Constants;

import org.json.JSONObject;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.ExpectedException;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
Expand All @@ -45,26 +33,36 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static com.couchbase.lite.utils.Config.EE_TEST_PROPERTIES_FILE;
import static com.couchbase.lite.utils.Config.TEST_PROPERTIES_FILE;
import org.json.JSONObject;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.ExpectedException;

import com.couchbase.lite.internal.core.C4Constants;
import com.couchbase.lite.internal.utils.ExecutorUtils;
import com.couchbase.lite.internal.utils.JsonUtils;
import com.couchbase.lite.utils.Config;
import com.couchbase.lite.utils.FileUtils;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;


public class BaseTest implements C4Constants, CBLError.Domain, CBLError.Code {
public static final String TAG = "Test";

protected final static String kDatabaseName = "testdb";

protected Config config;
protected Context context;
private File dir = null;
protected Database db = null;
ExecutorService executor = null;

@Rule
public ExpectedException thrown = ExpectedException.none();
public final ExpectedException thrown = ExpectedException.none();

protected File getDir() {
return dir;
Expand All @@ -77,8 +75,9 @@ protected void setDir(File dir) {

// https://stackoverflow.com/questions/2799097/how-can-i-detect-when-an-android-application-is-running-in-the-emulator?noredirect=1&lq=1
private static String getSystemProperty(String name) throws Exception {
Class systemPropertyClazz = Class.forName("android.os.SystemProperties");
return (String) systemPropertyClazz.getMethod("get", new Class[]{String.class}).invoke(systemPropertyClazz, new Object[]{name});
Class<?> systemPropertyClazz = Class.forName("android.os.SystemProperties");
return (String) systemPropertyClazz
.getMethod("get", new Class[] {String.class}).invoke(systemPropertyClazz, new Object[] {name});
}

protected static boolean isEmulator() {
Expand All @@ -87,7 +86,8 @@ protected static boolean isEmulator() {
boolean emu = getSystemProperty("ro.kernel.qemu").length() > 0;
boolean sdk = getSystemProperty("ro.product.model").equals("sdk");
return goldfish || emu || sdk;
} catch (Exception e) {
}
catch (Exception e) {
return false;
}
}
Expand All @@ -101,14 +101,17 @@ protected static boolean isARM() {
public void setUp() throws Exception {
executor = Executors.newSingleThreadExecutor();

context = InstrumentationRegistry.getTargetContext();
final Context ctxt = InstrumentationRegistry.getTargetContext();

CouchbaseLite.init(ctxt);
try {
config = new Config(openTestPropertiesFile());
} catch (IOException e) {
}
catch (IOException e) {
fail("Failed to load test.properties");
}

setDir(new File(context.getFilesDir(), "CouchbaseLiteTest"));
setDir(new File(ctxt.getFilesDir(), "CouchbaseLiteTest"));

// database exist, delete it
deleteDatabase(kDatabaseName);
Expand All @@ -120,10 +123,12 @@ public void setUp() throws Exception {
}

private InputStream openTestPropertiesFile() throws IOException {
final AssetManager assets = CouchbaseLite.getContext().getAssets();
try {
return context.getAssets().open(EE_TEST_PROPERTIES_FILE);
} catch (IOException e) {
return context.getAssets().open(TEST_PROPERTIES_FILE);
return assets.open(Config.EE_TEST_PROPERTIES_FILE);
}
catch (IOException e) {
return assets.open(Config.TEST_PROPERTIES_FILE);
}
}

Expand All @@ -142,8 +147,7 @@ public void tearDown() throws Exception {
}

protected void deleteDatabase(String dbName) throws CouchbaseLiteException {
if (config != null && !config.deleteDatabaseInTearDown())
return;
if (config != null && !config.deleteDatabaseInTearDown()) { return; }

// database exist, delete it
if (Database.exists(dbName, getDir())) {
Expand All @@ -153,13 +157,16 @@ protected void deleteDatabase(String dbName) throws CouchbaseLiteException {
try {
Database.delete(dbName, getDir());
break;
} catch (CouchbaseLiteException ex) {
}
catch (CouchbaseLiteException ex) {
if (ex.getCode() == CBLErrorBusy) {
try {
Thread.sleep(500);
} catch (Exception e) {
}
} else {
catch (Exception e) {
}
}
else {
throw ex;
}
}
Expand All @@ -168,7 +175,7 @@ protected void deleteDatabase(String dbName) throws CouchbaseLiteException {
}

protected Database open(String name) throws CouchbaseLiteException {
DatabaseConfiguration config = new DatabaseConfiguration(this.context);
DatabaseConfiguration config = new DatabaseConfiguration();
config.setDirectory(dir.getAbsolutePath());
return new Database(name, config);
}
Expand Down Expand Up @@ -209,7 +216,7 @@ protected void loadJSONResource(String name) throws Exception {
String line;
int n = 0;
while ((line = reader.readLine()) != null) {
if (line.trim().isEmpty()) continue;
if (line.trim().isEmpty()) { continue; }
n += 1;
JSONObject json = new JSONObject(line);
Map<String, Object> props = JsonUtils.fromJson(json);
Expand Down Expand Up @@ -286,7 +293,8 @@ public void run() {
String docID;
try {
docID = createDocNumbered(i, to);
} catch (CouchbaseLiteException e) {
}
catch (CouchbaseLiteException e) {
throw new RuntimeException(e);
}
numbers.add(db.getDocument(docID).toMap());
Expand Down Expand Up @@ -337,14 +345,15 @@ protected static void expectError(String domain, int code, Execution execution)
try {
execution.run();
fail();
} catch (CouchbaseLiteException e) {
}
catch (CouchbaseLiteException e) {
assertEquals(domain, e.getDomain());
assertEquals(code, e.getCode());
}
}

protected static void log(LogLevel level, String message) {
switch(level) {
switch (level) {
case DEBUG:
android.util.Log.d("CouchbaseLite/" + TAG, message);
break;
Expand Down
Loading