From a712f4cdfc314e69986947ada3aca63a49e3bbc8 Mon Sep 17 00:00:00 2001 From: Peixin Liu <1281563+lpx1989@users.noreply.github.com> Date: Thu, 15 Sep 2022 12:00:38 -0700 Subject: [PATCH] Add ktlint and mock unit test (#427) Co-authored-by: Peixin Liu --- azure-communication-ui/chat/build.gradle | 17 +++ .../ui/chat/locator/ServiceLocatorTest.kt | 115 ++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 azure-communication-ui/chat/src/test/java/com/azure/android/communication/ui/chat/locator/ServiceLocatorTest.kt diff --git a/azure-communication-ui/chat/build.gradle b/azure-communication-ui/chat/build.gradle index a34c611e3..e283edf8a 100644 --- a/azure-communication-ui/chat/build.gradle +++ b/azure-communication-ui/chat/build.gradle @@ -39,6 +39,23 @@ android { excludes += '/META-INF/{AL2.0,LGPL2.1}' } } + + ktlint { + debug.set(false) + verbose.set(true) + android.set(true) + outputToConsole.set(true) + outputColorName.set("RED") + enableExperimentalRules.set(false) + disabledRules = ["import-ordering", "max-line-length", "parameter-list-wrapping"] + reporters { + reporter "checkstyle" + reporter "plain" + } + filter { + exclude({ it.file.absolutePath.contains("/generated/") }) + } + } } dependencies { diff --git a/azure-communication-ui/chat/src/test/java/com/azure/android/communication/ui/chat/locator/ServiceLocatorTest.kt b/azure-communication-ui/chat/src/test/java/com/azure/android/communication/ui/chat/locator/ServiceLocatorTest.kt new file mode 100644 index 000000000..aaeb8aacd --- /dev/null +++ b/azure-communication-ui/chat/src/test/java/com/azure/android/communication/ui/chat/locator/ServiceLocatorTest.kt @@ -0,0 +1,115 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +package com.azure.android.communication.ui.chat.locator + +import org.junit.Assert +import org.junit.Test + +// / Tests for the Service Locator Pattern +class ServiceLocatorTest { + + /* + ScaffoldingObjects + BasicObjectHello = stores: "hello" + BasicObjectWorld = stores: "world" + BasicObjectHelloWorld = Combines above objects to output "hello world" + */ + internal inner class BasicObjectHello { + val data = "hello" + } + + internal inner class BasicObjectWorld : ServiceLocator.Disposable { + val data = "world" + var disposed = false + override fun dispose() { + disposed = true + } + } + + internal inner class BasicObjectHelloWorld( + hello: BasicObjectHello?, + world: BasicObjectWorld? + ) { + val data: String + + init { + data = hello!!.data + " " + world!!.data + } + } + + // Test basic, no-dep location + @Test + fun basicTest() { + val locator = ServiceLocator() + locator.addTypedBuilder { + BasicObjectHello() + } + + locator.addTypedBuilder { + BasicObjectWorld() + } + + val basicObjectHello = locator.locate() + val basicObjectWorld = locator.locate() + + Assert.assertEquals(basicObjectHello!!.data, "hello") + Assert.assertEquals(basicObjectWorld!!.data, "world") + } + + // Test that Dispose is called after clear() + @Test + fun disposeTest() { + val locator = ServiceLocator() + + locator.addTypedBuilder { BasicObjectWorld() } + val basicObjectWorld = locator.locate() + Assert.assertEquals(basicObjectWorld!!.disposed, false) + locator.clear() + Assert.assertEquals(basicObjectWorld.disposed, true) + } + + // Test to see if the HelloWorld object can be built + @Test + fun dependenciesTest() { + val locator = ServiceLocator() + + locator.addTypedBuilder { + BasicObjectHello() + } + + locator.addTypedBuilder { + BasicObjectWorld() + } + + locator.addTypedBuilder { + BasicObjectHelloWorld( + locator.locate(), + locator.locate() + ) + } + + val basicObjectHello = locator.locate() + + Assert.assertEquals(basicObjectHello!!.data, "hello world") + } + + @Test + fun failedDependencyTestWithInitializeAll() { + val locator = ServiceLocator() + + locator.addTypedBuilder { + BasicObjectHello() + } + + locator.addTypedBuilder { + BasicObjectHelloWorld( + locator.locate(), + locator.locate() + ) + } + + Assert.assertThrows(java.lang.IllegalArgumentException::class.java,) { + locator.initializeAll() + } + } +}