Skip to content

Commit

Permalink
Merge pull request #102 from kennethshackleton/patch-0.13.0
Browse files Browse the repository at this point in the history
Patch 0.13.0.
  • Loading branch information
kennethshackleton authored May 21, 2021
2 parents 73aa257 + be4a3be commit f2743ed
Show file tree
Hide file tree
Showing 55 changed files with 750 additions and 296 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.bloomberg.selekt.android

import android.content.ContentValues
import android.util.Log
import androidx.annotation.IntRange
import androidx.annotation.Size
import com.bloomberg.selekt.CommonThreadLocalRandom
Expand All @@ -37,32 +36,8 @@ import java.io.File
import java.io.InputStream
import java.io.OutputStream
import java.util.Locale
import javax.annotation.concurrent.GuardedBy
import javax.annotation.concurrent.ThreadSafe

internal object SQLiteDatabaseRegistry {
private val lock = Any()
@GuardedBy("lock")
private val store = mutableSetOf<SQLiteDatabase>()

fun register(database: SQLiteDatabase) = synchronized(lock) {
check(store.add(database)) { "Failed to register a database, ${store.count()} registered." }
}

fun unregister(database: SQLiteDatabase) = synchronized(lock) {
check(store.remove(database)) { "Failed to unregister a database, ${store.count()} registered." }
}

fun releaseMemory(priority: Priority) {
synchronized(lock) { store.toList() }.run {
forEach {
it.releaseMemory(priority)
}
Log.d(Selekt.TAG, "Released resources from ${count()} databases.")
}
}
}

/**
* @since v0.1.0.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2021 Bloomberg Finance L.P.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.bloomberg.selekt.android

import android.util.Log
import com.bloomberg.selekt.pools.Priority
import javax.annotation.concurrent.GuardedBy

internal object SQLiteDatabaseRegistry {
private val lock = Any()
@GuardedBy("lock")
private val store = mutableSetOf<SQLiteDatabase>()

fun register(database: SQLiteDatabase) = synchronized(lock) {
check(store.add(database)) { "Failed to register a database, ${store.count()} registered." }
}

fun unregister(database: SQLiteDatabase) = synchronized(lock) {
check(store.remove(database)) { "Failed to unregister a database, ${store.count()} registered." }
}

fun releaseMemory(priority: Priority) {
synchronized(lock) { store.toList() }.run {
forEach {
it.releaseMemory(priority)
}
Log.d(Selekt.TAG, "Released resources from ${count()} databases.")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ package com.bloomberg.selekt.android
import android.database.Cursor
import com.bloomberg.selekt.ColumnType
import com.bloomberg.selekt.ICursor
import com.nhaarman.mockitokotlin2.doReturn
import com.nhaarman.mockitokotlin2.eq
import com.nhaarman.mockitokotlin2.times
import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.whenever
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.eq
import org.mockito.kotlin.times
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import org.junit.Assert
import org.junit.Assert.assertArrayEquals
import org.junit.Before
Expand All @@ -50,7 +50,7 @@ internal class CursorWrapperTest {

@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
MockitoAnnotations.openMocks(this)
doReturn(emptyArray<String>()).whenever(cursor).columnNames()
wrapper = cursor.asAndroidCursor()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ import android.content.ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW
import android.content.ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE
import android.content.ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN
import com.bloomberg.selekt.pools.Priority
import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.never
import com.nhaarman.mockitokotlin2.same
import com.nhaarman.mockitokotlin2.times
import com.nhaarman.mockitokotlin2.verify
import org.mockito.kotlin.any
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.same
import org.mockito.kotlin.times
import org.mockito.kotlin.verify
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import com.bloomberg.selekt.ContentValues
import com.bloomberg.selekt.SQLDatabase
import com.bloomberg.selekt.SQLiteJournalMode
import com.bloomberg.selekt.SimpleSQLQuery
import com.nhaarman.mockitokotlin2.mock
import org.mockito.kotlin.mock
import org.assertj.core.api.Assertions.assertThatExceptionOfType
import org.junit.After
import org.junit.Assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import com.bloomberg.selekt.ContentValues
import com.bloomberg.selekt.SQLDatabase
import com.bloomberg.selekt.SQLTransactionListener
import com.bloomberg.selekt.SQLiteJournalMode
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.never
import com.nhaarman.mockitokotlin2.times
import com.nhaarman.mockitokotlin2.verify
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.times
import org.mockito.kotlin.verify
import org.assertj.core.api.Assertions.assertThatExceptionOfType
import org.junit.After
import org.junit.Before
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2021 Bloomberg Finance L.P.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.bloomberg.selekt.android

import org.assertj.core.api.Assertions.assertThatExceptionOfType
import org.junit.Test
import org.mockito.kotlin.mock

internal class SQLiteDatabaseRegistryTest {
@Test
fun registeringTwiceThrows() {
mock<SQLiteDatabase>().let {
SQLiteDatabaseRegistry.register(it)
try {
assertThatExceptionOfType(IllegalStateException::class.java).isThrownBy {
SQLiteDatabaseRegistry.register(it)
}
} finally {
SQLiteDatabaseRegistry.unregister(it)
}
}
}

@Test
fun unregisterThrows() {
assertThatExceptionOfType(IllegalStateException::class.java).isThrownBy {
SQLiteDatabaseRegistry.unregister(mock())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ import com.bloomberg.selekt.SQLTransactionListener
import com.bloomberg.selekt.commons.deleteDatabase
import com.bloomberg.selekt.SQLiteJournalMode
import com.bloomberg.selekt.SQLiteTransactionMode
import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.doReturn
import com.nhaarman.mockitokotlin2.doThrow
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.never
import com.nhaarman.mockitokotlin2.times
import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.whenever
import org.mockito.kotlin.any
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.doThrow
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.times
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import org.assertj.core.api.Assertions.assertThatExceptionOfType
import org.junit.After
import org.junit.Rule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ import com.bloomberg.selekt.Experimental
import com.bloomberg.selekt.SQLTransactionListener
import com.bloomberg.selekt.SQLiteAutoVacuumMode
import com.bloomberg.selekt.SQLiteJournalMode
import com.nhaarman.mockitokotlin2.doThrow
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.times
import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.whenever
import com.bloomberg.selekt.commons.deleteDatabase
import org.mockito.kotlin.doThrow
import org.mockito.kotlin.mock
import org.mockito.kotlin.times
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import org.assertj.core.api.Assertions.assertThatExceptionOfType
import org.junit.After
import org.junit.Before
Expand Down Expand Up @@ -68,6 +69,12 @@ internal class SQLiteDatabaseWALTest {
}
}

@Test
fun deleteDatabase() {
SQLiteDatabase.deleteDatabase(file)
assertFalse(file.exists())
}

@Test
fun journalMode(): Unit = database.run {
assertEquals(SQLiteJournalMode.WAL, journalMode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.bloomberg.selekt.android

import android.database.sqlite.SQLiteException
import com.nhaarman.mockitokotlin2.mock
import org.mockito.kotlin.mock
import org.assertj.core.api.Assertions.assertThatExceptionOfType
import org.junit.Test

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ package com.bloomberg.selekt.android
import android.content.Context
import com.bloomberg.selekt.commons.deleteDatabase
import com.bloomberg.selekt.SQLiteJournalMode
import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.doReturn
import com.nhaarman.mockitokotlin2.eq
import com.nhaarman.mockitokotlin2.inOrder
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.never
import com.nhaarman.mockitokotlin2.same
import com.nhaarman.mockitokotlin2.times
import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.whenever
import org.mockito.kotlin.any
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.eq
import org.mockito.kotlin.inOrder
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.same
import org.mockito.kotlin.times
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import org.assertj.core.api.Assertions.assertThatExceptionOfType
import org.junit.After
import org.junit.Rule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ package com.bloomberg.selekt.android

import android.app.Application
import com.bloomberg.selekt.Experimental
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.same
import com.nhaarman.mockitokotlin2.times
import com.nhaarman.mockitokotlin2.verify
import org.mockito.kotlin.mock
import org.mockito.kotlin.same
import org.mockito.kotlin.times
import org.mockito.kotlin.verify
import org.junit.Rule
import org.junit.Test
import org.junit.rules.DisableOnDebug
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ import androidx.sqlite.db.SupportSQLiteQuery
import com.bloomberg.selekt.SQLiteJournalMode
import com.bloomberg.selekt.android.ConflictAlgorithm
import com.bloomberg.selekt.android.SQLiteDatabase
import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.anyOrNull
import com.nhaarman.mockitokotlin2.doReturn
import com.nhaarman.mockitokotlin2.eq
import com.nhaarman.mockitokotlin2.isNull
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.only
import com.nhaarman.mockitokotlin2.same
import com.nhaarman.mockitokotlin2.times
import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.verifyNoMoreInteractions
import com.nhaarman.mockitokotlin2.whenever
import org.mockito.kotlin.any
import org.mockito.kotlin.anyOrNull
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.eq
import org.mockito.kotlin.isNull
import org.mockito.kotlin.mock
import org.mockito.kotlin.only
import org.mockito.kotlin.same
import org.mockito.kotlin.times
import org.mockito.kotlin.verify
import org.mockito.kotlin.verifyNoMoreInteractions
import org.mockito.kotlin.whenever
import org.assertj.core.api.Assertions.assertThatExceptionOfType
import org.junit.Before
import org.junit.Rule
Expand Down Expand Up @@ -65,7 +65,7 @@ internal class SupportSQLiteDatabaseTest {

@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
MockitoAnnotations.openMocks(this)
supportDatabase = database.asSupportSQLiteDatabase()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
package com.bloomberg.selekt.android.support

import androidx.sqlite.db.SupportSQLiteOpenHelper
import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.eq
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.times
import com.nhaarman.mockitokotlin2.verify
import org.mockito.kotlin.any
import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
import org.mockito.kotlin.times
import org.mockito.kotlin.verify
import org.junit.Test

internal class SupportSQLiteOpenHelperCallbackTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ import androidx.room.Room
import androidx.room.RoomDatabase
import com.bloomberg.selekt.SQLiteJournalMode
import com.bloomberg.selekt.android.SQLiteDatabase
import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.doReturn
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.whenever
import org.mockito.kotlin.any
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever
import org.assertj.core.api.Assertions.assertThatExceptionOfType
import org.junit.After
import org.junit.Rule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.bloomberg.selekt.android.support

import androidx.sqlite.db.SupportSQLiteOpenHelper
import com.nhaarman.mockitokotlin2.mock
import org.mockito.kotlin.mock
import org.assertj.core.api.Assertions.assertThatExceptionOfType
import org.junit.Test

Expand Down
Loading

0 comments on commit f2743ed

Please sign in to comment.