|
| 1 | +package com.maxmind.device |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import com.maxmind.device.config.SdkConfig |
| 5 | +import io.mockk.every |
| 6 | +import io.mockk.mockk |
| 7 | +import org.junit.jupiter.api.Assertions.assertEquals |
| 8 | +import org.junit.jupiter.api.Assertions.assertFalse |
| 9 | +import org.junit.jupiter.api.Assertions.assertNotNull |
| 10 | +import org.junit.jupiter.api.Assertions.assertSame |
| 11 | +import org.junit.jupiter.api.Assertions.assertThrows |
| 12 | +import org.junit.jupiter.api.Assertions.assertTrue |
| 13 | +import org.junit.jupiter.api.BeforeAll |
| 14 | +import org.junit.jupiter.api.MethodOrderer |
| 15 | +import org.junit.jupiter.api.Order |
| 16 | +import org.junit.jupiter.api.Test |
| 17 | +import org.junit.jupiter.api.TestInstance |
| 18 | +import org.junit.jupiter.api.TestMethodOrder |
| 19 | + |
| 20 | +/** |
| 21 | + * Tests for [DeviceTracker] singleton behavior. |
| 22 | + * |
| 23 | + * These tests exercise the real singleton lifecycle in a controlled sequence. |
| 24 | + * Tests are ordered to form a complete lifecycle test: |
| 25 | + * 1. Verify uninitialized state |
| 26 | + * 2. Verify getInstance throws when uninitialized |
| 27 | + * 3. Initialize and verify |
| 28 | + * 4. Verify getInstance returns same instance |
| 29 | + * 5. Verify double-init throws |
| 30 | + * 6. Verify shutdown works |
| 31 | + * |
| 32 | + * Note: The singleton cannot be reliably reset between tests due to |
| 33 | + * Kotlin @Volatile semantics and JVM security restrictions. Therefore, |
| 34 | + * tests are ordered to exercise the natural lifecycle. |
| 35 | + */ |
| 36 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 37 | +@TestMethodOrder(MethodOrderer.OrderAnnotation::class) |
| 38 | +internal class DeviceTrackerTest { |
| 39 | + private lateinit var mockContext: Context |
| 40 | + private lateinit var mockApplicationContext: Context |
| 41 | + private lateinit var config: SdkConfig |
| 42 | + private var initializedTracker: DeviceTracker? = null |
| 43 | + |
| 44 | + @BeforeAll |
| 45 | + internal fun setUp() { |
| 46 | + // Reset singleton at start of test class (best effort) |
| 47 | + resetSingleton() |
| 48 | + |
| 49 | + mockApplicationContext = mockk(relaxed = true) |
| 50 | + mockContext = mockk(relaxed = true) { |
| 51 | + every { applicationContext } returns mockApplicationContext |
| 52 | + } |
| 53 | + config = SdkConfig.Builder(12345).build() |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + @Order(1) |
| 58 | + internal fun `01 isInitialized returns false before initialize`() { |
| 59 | + // Verify real singleton state - should be false initially |
| 60 | + assertFalse(DeviceTracker.isInitialized(), "SDK should not be initialized at start") |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + @Order(2) |
| 65 | + internal fun `02 getInstance throws before initialize`() { |
| 66 | + // Verify real exception is thrown when not initialized |
| 67 | + val exception = assertThrows(IllegalStateException::class.java) { |
| 68 | + DeviceTracker.getInstance() |
| 69 | + } |
| 70 | + |
| 71 | + assertEquals("SDK not initialized. Call initialize() first.", exception.message) |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + @Order(3) |
| 76 | + internal fun `03 initialize creates instance and sets initialized state`() { |
| 77 | + assertFalse(DeviceTracker.isInitialized(), "Precondition: SDK should not be initialized") |
| 78 | + |
| 79 | + initializedTracker = DeviceTracker.initialize(mockContext, config) |
| 80 | + |
| 81 | + assertNotNull(initializedTracker) |
| 82 | + assertTrue(DeviceTracker.isInitialized(), "SDK should be initialized after initialize()") |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + @Order(4) |
| 87 | + internal fun `04 getInstance returns same instance after initialize`() { |
| 88 | + assertTrue(DeviceTracker.isInitialized(), "Precondition: SDK should be initialized") |
| 89 | + |
| 90 | + val retrieved = DeviceTracker.getInstance() |
| 91 | + |
| 92 | + assertSame(initializedTracker, retrieved, "getInstance should return the same instance") |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + @Order(5) |
| 97 | + internal fun `05 initialize throws if already initialized`() { |
| 98 | + assertTrue(DeviceTracker.isInitialized(), "Precondition: SDK should be initialized") |
| 99 | + |
| 100 | + // Second initialization should throw |
| 101 | + val exception = assertThrows(IllegalStateException::class.java) { |
| 102 | + DeviceTracker.initialize(mockContext, config) |
| 103 | + } |
| 104 | + |
| 105 | + assertEquals("SDK is already initialized", exception.message) |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + @Order(6) |
| 110 | + internal fun `06 shutdown can be called after initialize`() { |
| 111 | + assertTrue(DeviceTracker.isInitialized(), "Precondition: SDK should be initialized") |
| 112 | + |
| 113 | + val tracker = DeviceTracker.getInstance() |
| 114 | + |
| 115 | + // Should not throw |
| 116 | + tracker.shutdown() |
| 117 | + |
| 118 | + // Note: shutdown doesn't reset the singleton instance, |
| 119 | + // it just cancels coroutines and closes the HTTP client |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + @Order(7) |
| 124 | + internal fun `07 config builder creates valid config`() { |
| 125 | + // This test doesn't depend on singleton state |
| 126 | + val testConfig = SdkConfig.Builder(67890) |
| 127 | + .enableLogging(true) |
| 128 | + .collectionInterval(30000) |
| 129 | + .build() |
| 130 | + |
| 131 | + assertEquals(67890, testConfig.accountID) |
| 132 | + assertTrue(testConfig.enableLogging) |
| 133 | + assertEquals(30000L, testConfig.collectionIntervalMs) |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + @Order(8) |
| 138 | + internal fun `08 config builder validates accountID`() { |
| 139 | + val exception = assertThrows(IllegalArgumentException::class.java) { |
| 140 | + SdkConfig.Builder(0).build() |
| 141 | + } |
| 142 | + |
| 143 | + assertEquals("Account ID must be positive", exception.message) |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + @Order(9) |
| 148 | + internal fun `09 config builder validates negative accountID`() { |
| 149 | + val exception = assertThrows(IllegalArgumentException::class.java) { |
| 150 | + SdkConfig.Builder(-1).build() |
| 151 | + } |
| 152 | + |
| 153 | + assertEquals("Account ID must be positive", exception.message) |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Resets the singleton instance using reflection. |
| 158 | + * |
| 159 | + * @throws AssertionError if reset fails - tests should not continue with stale state |
| 160 | + */ |
| 161 | + private fun resetSingleton() { |
| 162 | + try { |
| 163 | + // The 'instance' field is a static field on DeviceTracker class itself, |
| 164 | + // not on the companion object (Kotlin compiles companion val/var to static fields) |
| 165 | + val instanceField = DeviceTracker::class.java.getDeclaredField("instance") |
| 166 | + instanceField.isAccessible = true |
| 167 | + instanceField.set(null, null) |
| 168 | + |
| 169 | + // Verify reset worked |
| 170 | + check(!DeviceTracker.isInitialized()) { "Singleton reset failed - instance still exists" } |
| 171 | + } catch (e: Exception) { |
| 172 | + throw AssertionError("Cannot reset DeviceTracker singleton - tests invalid: ${e.message}", e) |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments