#100 purge mockito_android

This commit is contained in:
Sean Mac Gillicuddy 2020-02-21 10:22:28 +00:00
parent 6e4d433a44
commit 7e7991f891
9 changed files with 318 additions and 366 deletions

View File

@ -1,65 +0,0 @@
/*
* Kiwix Android
* Copyright (c) 2019 Kiwix <android.kiwix.org>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.kiwix.kiwixmobile.core.di.modules;
import dagger.Module;
import dagger.Provides;
import javax.inject.Singleton;
import org.kiwix.kiwixlib.JNIKiwix;
import org.mockito.Mockito;
/**
* Created by mhutti1 on 13/04/17.
*/
@Module
public class TestJNIModule {
@Provides
@Singleton
public JNIKiwix providesJNIKiwix() {
JNIKiwix jniKiwix = Mockito.mock(JNIKiwix.class);
/*doReturn("A/index.htm").when(jniKiwix).getMainPage();
doReturn(true).when(jniKiwix).loadZIM(any());
doReturn(true).when(jniKiwix).loadFulltextIndex(any());
doReturn("mockid").when(jniKiwix).getId();
doReturn("mockname").when(jniKiwix).getName();
doReturn("Test Description").when(jniKiwix).getDescription();
doAnswer(invocation -> {
((JNIKiwixString) invocation.getArgument(0)).value = "Test Title";
return true;
}).when(jniKiwix).getTitle(any());
doReturn("Test Publisher").when(jniKiwix).getPublisher();
doReturn("Test Date").when(jniKiwix).getDate();
doReturn("Test Language").when(jniKiwix).getLanguage();
try {
InputStream inStream = TestJNIModule.class.getClassLoader().getResourceAsStream("summary");
byte[] summary = IOUtils.toByteArray(inStream);
InputStream inStream2 = TestJNIModule.class.getClassLoader().getResourceAsStream("testpage");
byte[] fool = IOUtils.toByteArray(inStream2);
doReturn(summary).when(jniKiwix).getContent(eq("A/index.htm"),any(),any(),any());
doReturn(fool).when(jniKiwix).getContent(eq("A/A_Fool_for_You.html"),any(),any(),any());
} catch (IOException e) {
e.printStackTrace();
}*/
return jniKiwix;
}
}

View File

@ -0,0 +1,32 @@
/*
* Kiwix Android
* Copyright (c) 2019 Kiwix <android.kiwix.org>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.kiwix.kiwixmobile.core.di.modules
import dagger.Module
import dagger.Provides
import io.mockk.mockk
import org.kiwix.kiwixlib.JNIKiwix
import javax.inject.Singleton
/**
* Created by mhutti1 on 13/04/17.
*/
@Module
class TestJNIModule {
@Provides @Singleton fun providesJNIKiwix(): JNIKiwix = mockk()
}

View File

@ -1,229 +0,0 @@
/*
* Kiwix Android
* Copyright (c) 2019 Kiwix <android.kiwix.org>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.kiwix.kiwixmobile.data.local.dao;
import android.content.Context;
import androidx.test.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.kiwix.kiwixmobile.core.data.local.KiwixDatabase;
import org.kiwix.kiwixmobile.core.data.local.dao.BookDao;
import org.kiwix.kiwixmobile.core.data.local.entity.BookDatabaseEntity;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import static org.junit.Assert.assertEquals;
import static org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity.Book;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(AndroidJUnit4.class)
public class BookDaoTest {
private Context context;
private @Mock
KiwixDatabase kiwixDatabase;
private BookDao bookDao;
private boolean mockInitialized = false;
private File testDir;
@Before
public void executeBefore() {
if (!mockInitialized) {
MockitoAnnotations.initMocks(this);
mockInitialized = true;
}
context = InstrumentationRegistry.getTargetContext();
//kiwixDatabase = new KiwixDatabase(context);
bookDao = new BookDao(kiwixDatabase);
// Create a temporary directory where all the test files will be saved
testDir = context.getDir("testDir", Context.MODE_PRIVATE);
}
//TODO : test books are saved after downloading the list of available zim files
@Test
public void testGetBooks() throws IOException {
// Save the fake data to test
String testId = "6qq5301d-2cr0-ebg5-474h-6db70j52864p";
String fileName = testDir.getPath() + "/" + testId + "testFile";
ArrayList<Book> booksToAdd = getFakeData(fileName);
// Set up the mocks
when(kiwixDatabase.deleteWhere(any(), any())).thenReturn(0);
// Get the filtered book list from the database (using the internal selection logic in BookDao)
ArrayList<Book> booksRetrieved = bookDao.filterBookResults(booksToAdd);
// Test whether the correct books are returned
// Filename ends with .zim and the file exists in memory
if (!booksRetrieved.contains(booksToAdd.get(0))) assertEquals(0, 1);
verify(kiwixDatabase, never()).deleteWhere(BookDatabaseEntity.class,
BookDatabaseEntity.URL.eq(booksToAdd.get(0).file.getPath()));
// Filename ends with .part and the file exists in memory
if (booksRetrieved.contains(booksToAdd.get(1))) assertEquals(0, 1);
verify(kiwixDatabase, never()).deleteWhere(BookDatabaseEntity.class,
BookDatabaseEntity.URL.eq(booksToAdd.get(1).file.getPath()));
// Filename ends with .zim, however only the .zim.part file exists in memory
if (booksRetrieved.contains(booksToAdd.get(2))) assertEquals(0, 1);
verify(kiwixDatabase, never()).deleteWhere(BookDatabaseEntity.class,
BookDatabaseEntity.URL.eq(booksToAdd.get(2).file.getPath()));
// Filename ends with .zim but neither the .zim, nor the .zim.part file exists in memory
if (booksRetrieved.contains(booksToAdd.get(3))) assertEquals(0, 1);
verify(kiwixDatabase).deleteWhere(BookDatabaseEntity.class,
BookDatabaseEntity.URL.eq(booksToAdd.get(3).file.getPath()));
// Filename ends with .zim and both the .zim, and the .zim.part files exists in memory
if (!booksRetrieved.contains(booksToAdd.get(4))) assertEquals(0, 1);
verify(kiwixDatabase, never()).deleteWhere(BookDatabaseEntity.class,
BookDatabaseEntity.URL.eq(booksToAdd.get(4).file.getPath()));
// If the filename ends with .zimXX
// FileName.zimXX.part does not exist for any value of "XX" from "aa" till "dr", but FileName.zimXX exists for all "XX" from "aa', till "ds", then it does not exist
// Also, the file inside the BooksToAdd does exist in memory
if (!booksRetrieved.contains(booksToAdd.get(5))) assertEquals(0, 1);
verify(kiwixDatabase, never()).deleteWhere(BookDatabaseEntity.class,
BookDatabaseEntity.URL.eq(booksToAdd.get(5).file.getPath()));
// FileName.zimXX.part does not exist for any value of "XX" from "aa" till "dr", but FileName.zimXX exists for all "XX" from "aa', till "ds", then it does not exist
// Also, the file inside the BooksToAdd also not exist in memory
if (booksRetrieved.contains(booksToAdd.get(6))) assertEquals(0, 1);
verify(kiwixDatabase).deleteWhere(BookDatabaseEntity.class,
BookDatabaseEntity.URL.eq(booksToAdd.get(6).file.getPath()));
// FileName.zimXX.part exists for some "XX" between "aa" till "bl"
// And FileName.zimXX exists for all "XX" from "aa', till "bk", and then it does not exist
// Also, the file inside the BooksToAdd does exist in memory
if (!booksRetrieved.contains(booksToAdd.get(7))) assertEquals(0, 1);
verify(kiwixDatabase, never()).deleteWhere(BookDatabaseEntity.class,
BookDatabaseEntity.URL.eq(booksToAdd.get(7).file.getPath()));
// FileName.zimXX.part exists for some "XX" between "aa" till "bl"
// And FileName.zimXX exists for all "XX" from "aa', till "bk", and then it does not exist
// Also, the file inside the BooksToAdd does not exist in memory
if (booksRetrieved.contains(booksToAdd.get(8))) assertEquals(0, 1);
verify(kiwixDatabase).deleteWhere(BookDatabaseEntity.class,
BookDatabaseEntity.URL.eq(booksToAdd.get(8).file.getPath()));
}
private ArrayList<Book> getFakeData(String baseFileName) throws IOException {
ArrayList<Book> books = new ArrayList<>();
for (int i = 0; i < 9; i++) {
Book book = new Book();
book.bookName = "Test Copy " + i;
book.id = "Test ID " + i;
String fileName = baseFileName + i;
switch (i) {
case 0:
book.file = new File(fileName + ".zim");
book.file.createNewFile();
break;
case 1:
book.file = new File(fileName + ".part");
book.file.createNewFile();
break;
case 2:
book.file = new File(fileName + ".zim");
File t2 = new File(fileName + ".zim.part");
t2.createNewFile();
break;
case 3:
book.file = new File(fileName + ".zim");
break;
case 4:
book.file = new File(fileName + ".zim");
book.file.createNewFile();
File t4 = new File(fileName + ".zim.part");
t4.createNewFile();
break;
case 5:
book.file = new File(fileName + ".zimdg");
setupCase1(fileName);
break;
case 6:
book.file = new File(fileName + ".zimyr");
setupCase2(fileName);
break;
case 7:
book.file = new File(fileName + ".zimdg");
setupCase1(fileName);
break;
case 8:
book.file = new File(fileName + ".zimyr");
setupCase2(fileName);
break;
}
books.add(book);
}
return books;
}
private void setupCase1(String fileName) throws IOException {
for (char char1 = 'a'; char1 <= 'z'; char1++) {
for (char char2 = 'a'; char2 <= 'z'; char2++) {
File file = new File(fileName + ".zim" + char1 + char2);
file.createNewFile();
if (char1 == 'd' && char2 == 'r') {
break;
}
}
if (char1 == 'd') {
break;
}
}
}
private void setupCase2(String fileName) throws IOException {
for (char char1 = 'a'; char1 <= 'z'; char1++) {
for (char char2 = 'a'; char2 <= 'z'; char2++) {
File file = new File(fileName + ".zim" + char1 + char2);
file.createNewFile();
if (char1 == 'd' && char2 == 'r') {
break;
}
}
if (char1 == 'd') {
break;
}
}
File t = new File(fileName + ".zimcp.part");
}
@After
public void RemoveTestDirectory() {
for (File child : testDir.listFiles()) {
child.delete();
}
testDir.delete();
}
}

View File

@ -0,0 +1,234 @@
/*
* Kiwix Android
* Copyright (c) 2019 Kiwix <android.kiwix.org>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.kiwix.kiwixmobile.data.local.dao
import android.content.Context
import androidx.test.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4
import io.mockk.MockKAnnotations
import io.mockk.every
import io.mockk.impl.annotations.MockK
import io.mockk.verify
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.jupiter.api.fail
import org.junit.runner.RunWith
import org.kiwix.kiwixmobile.core.data.local.KiwixDatabase
import org.kiwix.kiwixmobile.core.data.local.dao.BookDao
import org.kiwix.kiwixmobile.core.data.local.entity.BookDatabaseEntity
import org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity.Book
import java.io.File
import java.io.IOException
import java.util.ArrayList
@RunWith(AndroidJUnit4::class)
class BookDaoTest {
private lateinit var context: Context
@MockK
private lateinit var kiwixDatabase: KiwixDatabase
private lateinit var bookDao: BookDao
private lateinit var testDir: File
@Before
fun executeBefore() {
MockKAnnotations.init(this)
context = InstrumentationRegistry.getTargetContext()
bookDao = BookDao(kiwixDatabase)
// Create a temporary directory where all the test files will be saved
testDir = context.getDir("testDir", Context.MODE_PRIVATE)
}
// TODO : test books are saved after downloading the list of available zim files
@Test @Throws(IOException::class)
fun testGetBooks() { // Save the fake data to test
val testId = "6qq5301d-2cr0-ebg5-474h-6db70j52864p"
val fileName = testDir.path + "/" + testId + "testFile"
val booksToAdd = getFakeData(fileName)
every { kiwixDatabase.deleteWhere(any(), any()) } returns 0
val booksRetrieved =
bookDao.filterBookResults(booksToAdd)
if (!booksRetrieved.contains(booksToAdd[0])) fail()
verify(exactly = 0) {
kiwixDatabase.deleteWhere(
BookDatabaseEntity::class.java,
BookDatabaseEntity.URL.eq(booksToAdd[0].file.path)
)
}
if (booksRetrieved.contains(booksToAdd[1])) fail()
verify(exactly = 0) {
kiwixDatabase.deleteWhere(
BookDatabaseEntity::class.java,
BookDatabaseEntity.URL.eq(booksToAdd[1].file.path)
)
}
if (booksRetrieved.contains(booksToAdd[2])) fail()
verify(exactly = 0) {
kiwixDatabase.deleteWhere(
BookDatabaseEntity::class.java,
BookDatabaseEntity.URL.eq(booksToAdd[2].file.path)
)
}
if (booksRetrieved.contains(booksToAdd[3])) fail()
verify {
kiwixDatabase.deleteWhere(
BookDatabaseEntity::class.java,
BookDatabaseEntity.URL.eq(booksToAdd[3].file.path)
)
}
if (!booksRetrieved.contains(booksToAdd[4])) fail()
verify(exactly = 0) {
kiwixDatabase.deleteWhere(
BookDatabaseEntity::class.java,
BookDatabaseEntity.URL.eq(booksToAdd[4].file.path)
)
}
if (!booksRetrieved.contains(booksToAdd[5])) fail()
verify(exactly = 0) {
kiwixDatabase.deleteWhere(
BookDatabaseEntity::class.java,
BookDatabaseEntity.URL.eq(booksToAdd[5].file.path)
)
}
if (booksRetrieved.contains(booksToAdd[6])) fail()
verify {
kiwixDatabase.deleteWhere(
BookDatabaseEntity::class.java,
BookDatabaseEntity.URL.eq(booksToAdd[6].file.path)
)
}
if (!booksRetrieved.contains(booksToAdd[7])) fail()
verify(exactly = 0) {
kiwixDatabase.deleteWhere(
BookDatabaseEntity::class.java,
BookDatabaseEntity.URL.eq(booksToAdd[7].file.path)
)
}
if (booksRetrieved.contains(booksToAdd[8])) fail()
verify {
kiwixDatabase.deleteWhere(
BookDatabaseEntity::class.java,
BookDatabaseEntity.URL.eq(booksToAdd[8].file.path)
)
}
}
private fun fail() {
fail { "shouldn't happen" }
}
@Throws(IOException::class)
private fun getFakeData(baseFileName: String): ArrayList<Book> {
val books =
ArrayList<Book>()
for (i in 0..8) {
val book = Book()
book.bookName = "Test Copy $i"
book.id = "Test ID $i"
val fileName = baseFileName + i
when (i) {
0 -> {
book.file = File("$fileName.zim")
book.file.createNewFile()
}
1 -> {
book.file = File("$fileName.part")
book.file.createNewFile()
}
2 -> {
book.file = File("$fileName.zim")
val t2 = File("$fileName.zim.part")
t2.createNewFile()
}
3 -> book.file = File("$fileName.zim")
4 -> {
book.file = File("$fileName.zim")
book.file.createNewFile()
val t4 = File("$fileName.zim.part")
t4.createNewFile()
}
5 -> {
book.file = File("$fileName.zimdg")
setupCase1(fileName)
}
6 -> {
book.file = File("$fileName.zimyr")
setupCase2(fileName)
}
7 -> {
book.file = File("$fileName.zimdg")
setupCase1(fileName)
}
8 -> {
book.file = File("$fileName.zimyr")
setupCase2(fileName)
}
}
books.add(book)
}
return books
}
@Throws(IOException::class)
private fun setupCase1(fileName: String) {
var char1 = 'a'
while (char1 <= 'z') {
var char2 = 'a'
while (char2 <= 'z') {
val file = File("$fileName.zim$char1$char2")
file.createNewFile()
if (char1 == 'd' && char2 == 'r') {
break
}
char2++
}
if (char1 == 'd') {
break
}
char1++
}
}
@Throws(IOException::class)
private fun setupCase2(fileName: String) {
var char1 = 'a'
while (char1 <= 'z') {
var char2 = 'a'
while (char2 <= 'z') {
val file = File("$fileName.zim$char1$char2")
file.createNewFile()
if (char1 == 'd' && char2 == 'r') {
break
}
char2++
}
if (char1 == 'd') {
break
}
char1++
}
val t = File("$fileName.zimcp.part")
}
@After fun removeTestDirectory() {
for (child in testDir.listFiles()) {
child.delete()
}
testDir.delete()
}
}

View File

@ -1,64 +0,0 @@
/*
* Kiwix Android
* Copyright (c) 2019 Kiwix <android.kiwix.org>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.kiwix.kiwixmobile.data.local.dao;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.yahoo.squidb.data.AbstractModel;
import com.yahoo.squidb.data.SquidCursor;
import com.yahoo.squidb.sql.Query;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.kiwix.kiwixmobile.core.data.local.KiwixDatabase;
import org.kiwix.kiwixmobile.core.data.local.dao.RecentSearchDao;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(AndroidJUnit4.class)
public class RecentSearchDaoTest {
@Mock private KiwixDatabase kiwixDatabase;
private RecentSearchDao recentSearchDao;
private boolean mockInitialized = false;
@Mock private SquidCursor<AbstractModel> mockedCursor;
@Before
public void executeBefore() {
if (!mockInitialized) {
MockitoAnnotations.initMocks(this);
mockInitialized = true;
}
when(kiwixDatabase.query(any(), any())).thenReturn(mockedCursor);
recentSearchDao = new RecentSearchDao(kiwixDatabase);
}
// verify the correct database query was called
@Test
public void testGetRecentSearches() {
recentSearchDao.getRecentSearches();
// verify ordering is in descending order of search ID and the results are limited to 5 only
verify(kiwixDatabase).query(any(), eq(Query.select()));
}
}

View File

@ -0,0 +1,52 @@
/*
* Kiwix Android
* Copyright (c) 2019 Kiwix <android.kiwix.org>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.kiwix.kiwixmobile.data.local.dao
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.yahoo.squidb.data.AbstractModel
import com.yahoo.squidb.data.SquidCursor
import com.yahoo.squidb.sql.Query
import io.mockk.MockKAnnotations
import io.mockk.every
import io.mockk.impl.annotations.MockK
import io.mockk.verify
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.kiwix.kiwixmobile.core.data.local.KiwixDatabase
import org.kiwix.kiwixmobile.core.data.local.dao.RecentSearchDao
@RunWith(AndroidJUnit4::class)
class RecentSearchDaoTest {
@MockK
private lateinit var kiwixDatabase: KiwixDatabase
private lateinit var recentSearchDao: RecentSearchDao
@MockK(relaxed = true)
private lateinit var mockedCursor: SquidCursor<AbstractModel>
@Before fun executeBefore() {
MockKAnnotations.init(this)
every { kiwixDatabase.query(any<Class<AbstractModel>>(), any()) } returns mockedCursor
recentSearchDao = RecentSearchDao(kiwixDatabase)
}
@Test fun testGetRecentSearches() {
recentSearchDao.recentSearches
verify { kiwixDatabase.query(any<Class<AbstractModel>>(), Query.select()) }
}
}

View File

@ -258,11 +258,6 @@ object Libs {
const val constraintlayout: String = "androidx.constraintlayout:constraintlayout:" +
Versions.constraintlayout
/**
* https://github.com/mockito/mockito
*/
const val mockito_android: String = "org.mockito:mockito-android:" + Versions.mockito_android
/**
* http://developer.android.com/tools/extras/support-library.html
*/

View File

@ -53,8 +53,6 @@ object Versions {
const val constraintlayout: String = "1.1.3"
const val mockito_android: String = "2.24.5" // available: "3.2.4"
const val collection_ktx: String = "1.1.0"
const val jacoco_android: String = "0.1.4"

View File

@ -101,7 +101,6 @@ class AppConfigurer {
exclude(module = "xpp3")
}
androidTestUtil(Libs.orchestrator)
androidTestImplementation(Libs.mockito_android)
androidTestCompileOnly(Libs.javax_annotation_api)
kaptAndroidTest(Libs.dagger_compiler)
androidTestImplementation(Libs.mockk_android)