-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
731 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
ossrhUsername= | ||
ossrhPassword= | ||
android.enableJetifier=true | ||
android.useAndroidX=true | ||
|
||
kotlin.stdlib.default.dependency=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
186 changes: 186 additions & 0 deletions
186
src/androidTest/java/tests/database/DatabaseInitializationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
package tests.database; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertNull; | ||
|
||
import android.content.Context; | ||
|
||
import androidx.test.platform.app.InstrumentationRegistry; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.util.Arrays; | ||
import java.util.concurrent.LinkedBlockingDeque; | ||
|
||
import fake.HttpClientMock; | ||
import fake.HttpResponseMock; | ||
import fake.HttpResponseMockDispatcher; | ||
import fake.HttpStreamResponseMock; | ||
import helper.IntegrationHelper; | ||
import io.split.android.client.SplitClientConfig; | ||
import io.split.android.client.SplitFactory; | ||
import io.split.android.client.api.Key; | ||
import io.split.android.client.network.HttpMethod; | ||
|
||
public class DatabaseInitializationTest { | ||
|
||
private HttpClientMock mHttpClientMock; | ||
private Context mContext; | ||
|
||
@Before | ||
public void setUp() throws IOException { | ||
mContext = InstrumentationRegistry.getInstrumentation().getContext(); | ||
mHttpClientMock = new HttpClientMock(new HttpResponseMockDispatcher() { | ||
@Override | ||
public HttpResponseMock getResponse(URI uri, HttpMethod method, String body) { | ||
return new HttpResponseMock(400, ""); | ||
} | ||
|
||
@Override | ||
public HttpStreamResponseMock getStreamResponse(URI uri) { | ||
try { | ||
return new HttpStreamResponseMock(400, new LinkedBlockingDeque<>(0)); | ||
} catch (IOException e) { | ||
return null; | ||
} | ||
} | ||
}); | ||
} | ||
|
||
@Test | ||
public void initializationWithNullApiKeyResultsInNullFactory() throws IOException { | ||
SplitFactory factory = IntegrationHelper.buildFactory(null, | ||
new Key("matchingKey"), | ||
IntegrationHelper.basicConfig(), | ||
mContext, | ||
mHttpClientMock); | ||
|
||
assertNull(factory); | ||
} | ||
|
||
@Test | ||
public void initializationWithoutPrefixCreatesCorrectDatabaseName() { | ||
String[] initialDatabaseList = getDbList(mContext); | ||
SplitFactory factory = IntegrationHelper.buildFactory("abcdefghijkl", | ||
new Key("matchingKey"), | ||
IntegrationHelper.basicConfig(), | ||
mContext, | ||
mHttpClientMock); | ||
|
||
String[] finalDatabaseList = getDbList(mContext); | ||
|
||
assertNotNull(factory); | ||
assertEquals(0, initialDatabaseList.length); | ||
assertEquals("abcdijkl", finalDatabaseList[0]); | ||
} | ||
|
||
@Test | ||
public void initializationWithPrefixCreatesCorrectDatabaseName() { | ||
String[] initialDatabaseList = getDbList(mContext); | ||
SplitFactory factory = IntegrationHelper.buildFactory("abcdefghijkl", | ||
new Key("matchingKey"), | ||
SplitClientConfig.builder().prefix("my_prefix").build(), | ||
mContext, | ||
mHttpClientMock); | ||
|
||
String[] finalDatabaseList = getDbList(mContext); | ||
|
||
assertNotNull(factory); | ||
assertEquals(0, initialDatabaseList.length); | ||
assertEquals("my_prefixabcdijkl", finalDatabaseList[0]); | ||
} | ||
|
||
@Test | ||
public void factoriesWithSameSdkKeyCreateOnlyOneDatabase() { | ||
String[] initialDatabaseList = getDbList(mContext); | ||
SplitFactory factory1 = IntegrationHelper.buildFactory("abcdefghijkl", | ||
new Key("matchingKey"), | ||
IntegrationHelper.basicConfig(), | ||
mContext, | ||
mHttpClientMock); | ||
|
||
SplitFactory factory2 = IntegrationHelper.buildFactory("abcdefghijkl", | ||
new Key("matchingKey2"), | ||
IntegrationHelper.basicConfig(), | ||
mContext, | ||
mHttpClientMock); | ||
|
||
String[] finalDatabaseList = getDbList(mContext); | ||
|
||
assertNotNull(factory1); | ||
assertNotNull(factory2); | ||
assertEquals(0, initialDatabaseList.length); | ||
assertEquals(1, finalDatabaseList.length); | ||
assertEquals("abcdijkl", finalDatabaseList[0]); | ||
} | ||
|
||
@Test | ||
public void oneFactoryWithPrefixCreatesNewDatabase() { | ||
String[] initialDatabaseList = getDbList(mContext); | ||
SplitFactory factory1 = IntegrationHelper.buildFactory("abcdefghijkl", | ||
new Key("matchingKey"), | ||
SplitClientConfig.builder().build(), | ||
mContext, | ||
mHttpClientMock); | ||
|
||
SplitFactory factory2 = IntegrationHelper.buildFactory("abcdefghijkl", | ||
new Key("matchingKey"), | ||
SplitClientConfig.builder().prefix("my_prefix").build(), | ||
mContext, | ||
mHttpClientMock); | ||
|
||
String[] finalDatabaseList = getDbList(mContext); | ||
|
||
assertNotNull(factory1); | ||
assertNotNull(factory2); | ||
assertEquals(0, initialDatabaseList.length); | ||
assertEquals(2, finalDatabaseList.length); | ||
assertEquals("abcdijkl", finalDatabaseList[0]); | ||
assertEquals("my_prefixabcdijkl", finalDatabaseList[1]); | ||
} | ||
|
||
@Test | ||
public void usingInvalidPrefixResultsInIgnoredPrefix() { | ||
String[] initialDatabaseList = getDbList(mContext); | ||
|
||
SplitFactory factory = IntegrationHelper.buildFactory("abcdefghijkl", | ||
new Key("matchingKey"), | ||
SplitClientConfig.builder().prefix(":l").build(), | ||
mContext, | ||
mHttpClientMock); | ||
|
||
String[] finalDatabaseList = getDbList(mContext); | ||
|
||
assertNotNull(factory); | ||
assertEquals(0, initialDatabaseList.length); | ||
assertEquals(1, finalDatabaseList.length); | ||
assertEquals("abcdijkl", finalDatabaseList[0]); | ||
} | ||
|
||
@Test | ||
public void usingNullPrefixResultsInIgnoredPrefix() { | ||
String[] initialDatabaseList = getDbList(mContext); | ||
|
||
SplitFactory factory = IntegrationHelper.buildFactory("abcdefghijkl", | ||
new Key("matchingKey"), | ||
SplitClientConfig.builder().prefix(null).build(), | ||
mContext, | ||
mHttpClientMock); | ||
|
||
String[] finalDatabaseList = getDbList(mContext); | ||
|
||
assertNotNull(factory); | ||
assertEquals(0, initialDatabaseList.length); | ||
assertEquals(1, finalDatabaseList.length); | ||
assertEquals("abcdijkl", finalDatabaseList[0]); | ||
} | ||
|
||
private static String[] getDbList(Context context) { | ||
// remove -journal dbs since we're not interested in them | ||
return Arrays.stream(context.databaseList()).filter(db -> !db.endsWith("-journal")).toArray(String[]::new); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.