mirror of
https://github.com/LamGC/ScalaBot.git
synced 2025-04-30 06:37:29 +00:00
refactor(utils): 加强 getPriority 方法的优先值判断.
加强优先级判断, 有利于后续使用时防止出现意外情况的问题. 顺便补充一手单元测试.
This commit is contained in:
parent
8be0978783
commit
830f05c90a
@ -56,9 +56,14 @@ internal fun File.deepListFiles(
|
|||||||
* @return 获取 Finder 的优先级.
|
* @return 获取 Finder 的优先级.
|
||||||
* @throws NoSuchFieldException 如果 Finder 没有添加 [FinderRules] 注解时抛出该异常.
|
* @throws NoSuchFieldException 如果 Finder 没有添加 [FinderRules] 注解时抛出该异常.
|
||||||
*/
|
*/
|
||||||
internal fun ExtensionPackageFinder.getPriority() =
|
internal fun ExtensionPackageFinder.getPriority(): Int {
|
||||||
this::class.java.getDeclaredAnnotation(FinderRules::class.java)?.priority
|
val value = this::class.java.getDeclaredAnnotation(FinderRules::class.java)?.priority
|
||||||
?: throw NoSuchFieldException("Finder did not add `FinderRules` annotation")
|
?: throw NoSuchFieldException("Finder did not add `FinderRules` annotation")
|
||||||
|
if (value < 0) {
|
||||||
|
throw IllegalArgumentException("Priority cannot be lower than 0. (Class: ${this::class.java})")
|
||||||
|
}
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 为 [AutoCloseable] 对象注册 Jvm Shutdown 钩子.
|
* 为 [AutoCloseable] 对象注册 Jvm Shutdown 钩子.
|
||||||
|
@ -1,9 +1,18 @@
|
|||||||
package net.lamgc.scalabot.util
|
package net.lamgc.scalabot.util
|
||||||
|
|
||||||
|
import net.lamgc.scalabot.ExtensionPackageFinder
|
||||||
|
import net.lamgc.scalabot.FinderPriority
|
||||||
|
import net.lamgc.scalabot.FinderRules
|
||||||
|
import net.lamgc.scalabot.FoundExtensionPackage
|
||||||
|
import org.eclipse.aether.artifact.Artifact
|
||||||
import org.eclipse.aether.artifact.DefaultArtifact
|
import org.eclipse.aether.artifact.DefaultArtifact
|
||||||
import org.junit.jupiter.api.Assertions.*
|
import org.junit.jupiter.api.assertThrows
|
||||||
import org.junit.jupiter.api.Test
|
import java.io.File
|
||||||
import java.nio.charset.StandardCharsets
|
import java.nio.charset.StandardCharsets
|
||||||
|
import kotlin.test.Test
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
import kotlin.test.assertFalse
|
||||||
|
import kotlin.test.assertTrue
|
||||||
|
|
||||||
internal class UtilsKtTest {
|
internal class UtilsKtTest {
|
||||||
|
|
||||||
@ -22,4 +31,31 @@ internal class UtilsKtTest {
|
|||||||
assertEquals("48656c6c6f20576f726c64", "Hello World".toByteArray(StandardCharsets.UTF_8).toHexString())
|
assertEquals("48656c6c6f20576f726c64", "Hello World".toByteArray(StandardCharsets.UTF_8).toHexString())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `ExtensionPackageFinder - getPriority`() {
|
||||||
|
open class BaseTestFinder : ExtensionPackageFinder {
|
||||||
|
override fun findByArtifact(extensionArtifact: Artifact, extensionsPath: File): Set<FoundExtensionPackage> {
|
||||||
|
throw IllegalStateException("Calling this class is not allowed.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@FinderRules(FinderPriority.ALTERNATE)
|
||||||
|
class StandardTestFinder : BaseTestFinder()
|
||||||
|
assertEquals(
|
||||||
|
FinderPriority.ALTERNATE, StandardTestFinder().getPriority(),
|
||||||
|
"获取到的优先值与预期不符"
|
||||||
|
)
|
||||||
|
|
||||||
|
@FinderRules(-1)
|
||||||
|
class OutOfRangePriorityFinder : BaseTestFinder()
|
||||||
|
assertThrows<IllegalArgumentException>("getPriority 方法没有对超出范围的优先值抛出异常.") {
|
||||||
|
OutOfRangePriorityFinder().getPriority()
|
||||||
|
}
|
||||||
|
|
||||||
|
class NoAnnotationFinder : BaseTestFinder()
|
||||||
|
assertThrows<NoSuchFieldException> {
|
||||||
|
NoAnnotationFinder().getPriority()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user