mirror of
https://github.com/LamGC/ScalaBot.git
synced 2025-04-29 22:27:31 +00:00
test: 完善序列化器的单元测试.
目前经测试, 已完善到 100% 覆盖率.
This commit is contained in:
parent
746221a085
commit
b5c85e213b
@ -18,13 +18,21 @@ internal class ArtifactSerializerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun serialize() {
|
||||
fun `Basic format serialization`() {
|
||||
val gav = "org.example.software:test:1.0.0-SNAPSHOT"
|
||||
val expectArtifact = DefaultArtifact(gav)
|
||||
val actualArtifact = DefaultArtifact(ArtifactSerializer.serialize(expectArtifact, null, null).asString)
|
||||
assertEquals(expectArtifact, actualArtifact)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Full format serialization`() {
|
||||
val gav = "org.example.software:test:war:javadoc:1.0.0-SNAPSHOT"
|
||||
val expectArtifact = DefaultArtifact(gav)
|
||||
val actualArtifact = DefaultArtifact(ArtifactSerializer.serialize(expectArtifact, null, null).asString)
|
||||
assertEquals(expectArtifact, actualArtifact)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun deserialize() {
|
||||
val gav = "org.example.software:test:1.0.0-SNAPSHOT"
|
||||
|
@ -3,19 +3,68 @@ package util
|
||||
import com.google.gson.*
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import io.mockk.verify
|
||||
import net.lamgc.scalabot.MavenRepositoryConfig
|
||||
import net.lamgc.scalabot.util.AuthenticationSerializer
|
||||
import net.lamgc.scalabot.util.MavenRepositoryConfigSerializer
|
||||
import net.lamgc.scalabot.util.ProxyTypeSerializer
|
||||
import org.eclipse.aether.repository.Authentication
|
||||
import org.eclipse.aether.repository.AuthenticationContext
|
||||
import org.eclipse.aether.repository.Proxy
|
||||
import org.intellij.lang.annotations.Language
|
||||
import org.junit.jupiter.api.Assertions.assertThrows
|
||||
import org.telegram.telegrambots.bots.DefaultBotOptions
|
||||
import java.lang.reflect.InvocationTargetException
|
||||
import java.lang.reflect.Method
|
||||
import java.lang.reflect.Type
|
||||
import java.net.URL
|
||||
import kotlin.test.*
|
||||
|
||||
internal class SerializersKtTest {
|
||||
|
||||
private val instance: Any
|
||||
private val method: Method
|
||||
|
||||
init {
|
||||
val clazz = Class.forName("net.lamgc.scalabot.util.SerializerUtils")
|
||||
method = clazz.getDeclaredMethod("checkJsonKey", JsonObject::class.java, String::class.java)
|
||||
method.isAccessible = true
|
||||
instance = clazz.getDeclaredField("INSTANCE").apply {
|
||||
isAccessible = true
|
||||
}.get(null)
|
||||
}
|
||||
|
||||
private fun invoke(json: JsonObject, key: String): String {
|
||||
try {
|
||||
return method.invoke(instance, json, key) as String
|
||||
} catch (e: InvocationTargetException) {
|
||||
throw e.targetException
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Json key checker test`() {
|
||||
assertThrows(JsonParseException::class.java) {
|
||||
invoke(JsonObject(), "NOT_EXIST_KEY")
|
||||
}
|
||||
assertThrows(JsonParseException::class.java) {
|
||||
invoke(JsonObject().apply { add("NULL_KEY", JsonNull.INSTANCE) }, "NULL_KEY")
|
||||
}
|
||||
assertThrows(JsonParseException::class.java) {
|
||||
invoke(JsonObject().apply { add("ARRAY_KEY", JsonArray()) }, "ARRAY_KEY")
|
||||
}
|
||||
assertThrows(JsonParseException::class.java) {
|
||||
invoke(JsonObject().apply { add("OBJECT_KEY", JsonObject()) }, "OBJECT_KEY")
|
||||
}
|
||||
|
||||
val expectKey = "TEST"
|
||||
val expectString = "testString"
|
||||
val json = JsonObject().apply { addProperty(expectKey, expectString) }
|
||||
|
||||
assertEquals(expectString, invoke(json, expectKey))
|
||||
}
|
||||
}
|
||||
|
||||
internal class ProxyTypeSerializerTest {
|
||||
|
||||
@Test
|
||||
@ -213,7 +262,9 @@ internal class MavenRepositoryConfigSerializerTest {
|
||||
assertNotNull(config.authentication)
|
||||
}
|
||||
|
||||
private object TestJsonDeserializationContext : JsonDeserializationContext {
|
||||
}
|
||||
|
||||
private object TestJsonDeserializationContext : JsonDeserializationContext {
|
||||
|
||||
private val gson = GsonBuilder()
|
||||
.registerTypeAdapter(Authentication::class.java, AuthenticationSerializer)
|
||||
@ -222,6 +273,53 @@ internal class MavenRepositoryConfigSerializerTest {
|
||||
override fun <T : Any?> deserialize(json: JsonElement, typeOfT: Type): T {
|
||||
return gson.fromJson(json, typeOfT)
|
||||
}
|
||||
}
|
||||
|
||||
internal class AuthenticationSerializerTest {
|
||||
|
||||
@Test
|
||||
fun `deserialize test`() {
|
||||
assertThrows(JsonParseException::class.java) {
|
||||
AuthenticationSerializer.deserialize(
|
||||
JsonNull.INSTANCE,
|
||||
Authentication::class.java, TestJsonDeserializationContext
|
||||
)
|
||||
}
|
||||
assertThrows(JsonParseException::class.java) {
|
||||
AuthenticationSerializer.deserialize(
|
||||
JsonArray(),
|
||||
Authentication::class.java, TestJsonDeserializationContext
|
||||
)
|
||||
}
|
||||
assertThrows(JsonParseException::class.java) {
|
||||
AuthenticationSerializer.deserialize(
|
||||
JsonPrimitive("A STRING"),
|
||||
Authentication::class.java, TestJsonDeserializationContext
|
||||
)
|
||||
}
|
||||
|
||||
val expectJsonObject = JsonObject().apply {
|
||||
addProperty("username", "testUsername")
|
||||
addProperty("password", "testPassword")
|
||||
}
|
||||
|
||||
val mockContext = mockk<AuthenticationContext> {
|
||||
every { put(any(), any()) }.answers { }
|
||||
}
|
||||
|
||||
val result = AuthenticationSerializer.deserialize(
|
||||
expectJsonObject,
|
||||
Authentication::class.java, TestJsonDeserializationContext
|
||||
)
|
||||
|
||||
assertNotNull(result)
|
||||
result.fill(mockContext, "username", null)
|
||||
result.fill(mockContext, "password", null)
|
||||
|
||||
verify {
|
||||
mockContext.put("username", "testUsername")
|
||||
mockContext.put("password", "testPassword".toCharArray())
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user