Skip to content
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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ buildscript {
}
classpath 'gradle.plugin.com.github.spotbugs:spotbugs-gradle-plugin:1.6.6'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.0.0-RC14"
classpath "io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.0.1"
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/nextcloud/client/media/Player.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ internal class Player(

override fun onStartDownloading() {
trace("onStartDownloading()")
if (playedFile == null) {
throw IllegalStateException("File not set.")
}
checkNotNull(playedFile) { "File not set." }
playedFile?.let {
val client = buildClient()
val task = LoadUrlTask(client, it.remoteId, this@Player::onDownloaded)
Expand Down
42 changes: 25 additions & 17 deletions src/test/java/com/nextcloud/client/core/ManualAsyncRunnerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ import org.mockito.MockitoAnnotations

class ManualAsyncRunnerTest {

private companion object {
const val EMPTY = 0
const val ONE_TASK = 1
const val TWO_TASKS = 2
const val THREE_TASKS = 3
const val TIMEOUT = 10000L
}

private lateinit var runner: ManualAsyncRunner

@Mock
Expand All @@ -45,23 +53,23 @@ class ManualAsyncRunnerTest {
@Mock
private lateinit var onError: OnErrorCallback

private var taskCalls: Int = 0
private var taskCalls: Int = EMPTY

@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
runner = ManualAsyncRunner()
taskCalls = 0
taskCalls = EMPTY
whenever(task.invoke()).thenAnswer { taskCalls++; taskCalls }
}

@Test
fun `tasks are queued`() {
assertEquals(0, runner.size)
assertEquals(EMPTY, runner.size)
runner.post(task, onResult, onError)
runner.post(task, onResult, onError)
runner.post(task, onResult, onError)
assertEquals("Expected 3 tasks to be enqueued", 3, runner.size)
assertEquals("Expected 3 tasks to be enqueued", THREE_TASKS, runner.size)
}

@Test
Expand All @@ -70,25 +78,25 @@ class ManualAsyncRunnerTest {
runner.post(task, onResult, onError)
runner.post(task, onResult, onError)

assertEquals("Queue should contain all enqueued tasks", 3, runner.size)
assertEquals("Queue should contain all enqueued tasks", THREE_TASKS, runner.size)
val run = runner.runOne()
assertTrue("Executed task should be acknowledged", run)
assertEquals("One task should be run", 1, taskCalls)
verify(onResult).invoke(eq(1))
assertEquals("Only 1 task should be consumed", 2, runner.size)
assertEquals("One task should be run", ONE_TASK, taskCalls)
verify(onResult).invoke(eq(ONE_TASK))
assertEquals("Only 1 task should be consumed", TWO_TASKS, runner.size)
}

@Test
fun `run all enqueued tasks`() {
runner.post(task, onResult, onError)
runner.post(task, onResult, onError)

assertEquals("Queue should contain all enqueued tasks", 2, runner.size)
assertEquals("Queue should contain all enqueued tasks", TWO_TASKS, runner.size)
val count = runner.runAll()
assertEquals("Executed tasks should be acknowledged", 2, count)
verify(task, times(2)).invoke()
verify(onResult, times(2)).invoke(any())
assertEquals("Entire queue should be processed", 0, runner.size)
assertEquals("Executed tasks should be acknowledged", TWO_TASKS, count)
verify(task, times(TWO_TASKS)).invoke()
verify(onResult, times(TWO_TASKS)).invoke(any())
assertEquals("Entire queue should be processed", EMPTY, runner.size)
}

@Test
Expand All @@ -98,7 +106,7 @@ class ManualAsyncRunnerTest {

@Test
fun `run all tasks when queue is empty`() {
assertEquals("No task should be run", 0, runner.runAll())
assertEquals("No task should be run", EMPTY, runner.runAll())
}

@Test
Expand All @@ -112,18 +120,18 @@ class ManualAsyncRunnerTest {
runner.post(task)
})
})
assertEquals(1, runner.size)
assertEquals(ONE_TASK, runner.size)

// WHEN
// runs all
val count = runner.runAll()

// THEN
// all subsequently scheduled tasks are run too
assertEquals(3, count)
assertEquals(THREE_TASKS, count)
}

@Test(expected = IllegalStateException::class, timeout = 10000)
@Test(expected = IllegalStateException::class, timeout = TIMEOUT)
fun `runner detects infinite loops caused by scheduling tasks recusively`() {
val recursiveTask: () -> String = object : Function0<String> {
override fun invoke(): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ class ThreadPoolAsyncRunnerTest {
private lateinit var handler: Handler
private lateinit var r: ThreadPoolAsyncRunner

private companion object {
const val INIT_COUNT = 1
const val THREAD_SLEEP = 500L
}

@Before
fun setUp() {
handler = spy(Handler())
Expand Down Expand Up @@ -83,7 +88,7 @@ class ThreadPoolAsyncRunnerTest {

@Test
fun `returns error via handler`() {
val afterPostLatch = CountDownLatch(1)
val afterPostLatch = CountDownLatch(INIT_COUNT)
doAnswer {
(it.arguments[0] as Runnable).run()
afterPostLatch.countDown()
Expand All @@ -101,8 +106,8 @@ class ThreadPoolAsyncRunnerTest {

@Test
fun `cancelled task does not return result`() {
val taskIsCancelled = CountDownLatch(1)
val taskIsRunning = CountDownLatch(1)
val taskIsCancelled = CountDownLatch(INIT_COUNT)
val taskIsRunning = CountDownLatch(INIT_COUNT)
val t = r.post({
taskIsRunning.countDown()
taskIsCancelled.await()
Expand All @@ -111,23 +116,23 @@ class ThreadPoolAsyncRunnerTest {
assertAwait(taskIsRunning)
t.cancel()
taskIsCancelled.countDown()
Thread.sleep(500) // yuck!
Thread.sleep(THREAD_SLEEP) // yuck!
verify(handler, never()).post(any())
}

@Test
fun `cancelled task does not return error`() {
val taskIsCancelled = CountDownLatch(1)
val taskIsRunning = CountDownLatch(1)
val taskIsCancelled = CountDownLatch(INIT_COUNT)
val taskIsRunning = CountDownLatch(INIT_COUNT)
val t = r.post({
taskIsRunning.countDown()
taskIsCancelled.await()
throw RuntimeException("whatever")
throw IllegalStateException("whatever")
}, onResult = {}, onError = {})
assertAwait(taskIsRunning)
t.cancel()
taskIsCancelled.countDown()
Thread.sleep(500) // yuck!
Thread.sleep(THREAD_SLEEP) // yuck!
verify(handler, never()).post(any())
}
}
33 changes: 21 additions & 12 deletions src/test/java/com/nextcloud/client/logger/FileLogHandlerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,17 @@ import java.io.File
import java.nio.charset.Charset
import java.nio.file.Files

@Suppress("TooManyFunctions")
class FileLogHandlerTest {

private companion object {
const val FILE_SIZE = 1024L
const val MAX_FILE_SIZE = 20L
const val THREE_LOG_FILES = 3
const val EXPECTED_LINE_COUNT_6 = 6
const val EXPECTED_LINE_COUNT_12 = 12
}

private lateinit var logDir: File

private fun readLogFile(name: String): String {
Expand Down Expand Up @@ -64,7 +73,7 @@ class FileLogHandlerTest {

// WHEN
// file is opened
val handler = FileLogHandler(nonexistingLogsDir, "log.txt", 1000)
val handler = FileLogHandler(nonexistingLogsDir, "log.txt", FILE_SIZE)
handler.open()

// THEN
Expand All @@ -90,7 +99,7 @@ class FileLogHandlerTest {
writeLogFile("log.txt.1", "2")
writeLogFile("log.txt.2", "3")

val writer = FileLogHandler(logDir, "log.txt", 1024)
val writer = FileLogHandler(logDir, "log.txt", FILE_SIZE)

// WHEN
// files are rotated
Expand All @@ -112,7 +121,7 @@ class FileLogHandlerTest {
// log file limit is 20 bytes
// log writer is opened
writeLogFile("log.txt", "0123456789")
val writer = FileLogHandler(logDir, "log.txt", 20)
val writer = FileLogHandler(logDir, "log.txt", MAX_FILE_SIZE)
writer.open()

// WHEN
Expand All @@ -132,7 +141,7 @@ class FileLogHandlerTest {
// log file limit is 20 bytes
// log writer is opened
writeLogFile("log.txt", "0123456789")
val writer = FileLogHandler(logDir, "log.txt", 20)
val writer = FileLogHandler(logDir, "log.txt", MAX_FILE_SIZE)
writer.open()

// WHEN
Expand Down Expand Up @@ -162,14 +171,14 @@ class FileLogHandlerTest {

// WHEN
// log file is read including rotated content
val writer = FileLogHandler(logDir, "log.txt", 1000)
val rawLogs = writer.loadLogFiles(3)
val writer = FileLogHandler(logDir, "log.txt", FILE_SIZE)
val rawLogs = writer.loadLogFiles(THREE_LOG_FILES)

// THEN
// all files are loaded
// lines are loaded in correct order
// log files size is correctly reported
assertEquals(12, rawLogs.lines.size)
assertEquals(EXPECTED_LINE_COUNT_12, rawLogs.lines.size)
assertEquals(
listOf(
"line1", "line2", "line3",
Expand All @@ -193,21 +202,21 @@ class FileLogHandlerTest {

// WHEN
// log file is read including rotated content
val writer = FileLogHandler(logDir, "log.txt", 1000)
val lines = writer.loadLogFiles(3)
val writer = FileLogHandler(logDir, "log.txt", FILE_SIZE)
val lines = writer.loadLogFiles(THREE_LOG_FILES)

// THEN
// all files are loaded
// log file size is non-zero
assertEquals(6, lines.lines.size)
assertEquals(EXPECTED_LINE_COUNT_6, lines.lines.size)
assertTrue(lines.logSize > 0)
}

@Test(expected = IllegalArgumentException::class)
fun `load log lines - negative count is illegal`() {
// WHEN
// requesting negative number of rotated files
val writer = FileLogHandler(logDir, "log.txt", 1000)
val writer = FileLogHandler(logDir, "log.txt", FILE_SIZE)
val lines = writer.loadLogFiles(-1)

// THEN
Expand All @@ -218,7 +227,7 @@ class FileLogHandlerTest {
fun `all log files are deleted`() {
// GIVEN
// log files exist
val handler = FileLogHandler(logDir, "log.txt", 100)
val handler = FileLogHandler(logDir, "log.txt", MAX_FILE_SIZE)
for (i in 0 until handler.maxLogFilesCount) {
handler.rotateLogs()
handler.open()
Expand Down
11 changes: 7 additions & 4 deletions src/test/java/com/nextcloud/client/logger/LogEntryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,26 @@
*/
package com.nextcloud.client.logger

import java.util.Date
import java.util.SimpleTimeZone
import java.util.concurrent.TimeUnit
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Suite
import java.util.Date
import java.util.SimpleTimeZone
import java.util.concurrent.TimeUnit

@RunWith(Suite::class)
@Suite.SuiteClasses(
LogEntryTest.ToString::class,
LogEntryTest.Parse::class
)
class LogEntryTest {
private companion object {
const val SEVEN_HOUR = 7L
}

class ToString {
@Test
Expand All @@ -57,7 +60,7 @@ class LogEntryTest {
tag = "tag",
message = "some message"
)
val sevenHours = TimeUnit.HOURS.toMillis(7).toInt()
val sevenHours = TimeUnit.HOURS.toMillis(SEVEN_HOUR).toInt()
val tz = SimpleTimeZone(sevenHours, "+0700")
assertEquals("1970-01-01T07:00:00.000+0700;D;tag;some message", entry.toString(tz))
}
Expand Down
Loading