Skip to content

Commit

Permalink
test: add several test cases (#502)
Browse files Browse the repository at this point in the history
  • Loading branch information
tangcent authored Jun 8, 2021
1 parent ecfd8bb commit 5253978
Show file tree
Hide file tree
Showing 13 changed files with 179 additions and 99 deletions.
2 changes: 1 addition & 1 deletion common-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ dependencies {

implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

implementation('com.itangcent:commons:1.0.3-SNAPSHOT') {
implementation('com.itangcent:commons:1.0.32-SNAPSHOT') {
exclude group: 'com.google.inject'
exclude group: 'com.google.code.gson'
}
Expand Down
5 changes: 3 additions & 2 deletions common-api/src/main/kotlin/com/itangcent/http/HttpRequest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,7 @@ abstract class AbstractHttpRequest : HttpRequest {
return headers
?.filter { it.name().equalIgnoreCase(headerName) }
?.mapNotNull { it.value() }
?.takeIf { it.isNotEmpty() }
?.toTypedArray()
}

Expand All @@ -583,7 +584,7 @@ abstract class AbstractHttpRequest : HttpRequest {
return headers
?.filter { it.name().equalIgnoreCase(headerName) }
?.map { it.value() }
?.first { it != null }
?.firstOrNull { it != null }
}

/**
Expand All @@ -598,7 +599,7 @@ abstract class AbstractHttpRequest : HttpRequest {
return headers
?.filter { it.name().equalIgnoreCase(headerName) }
?.map { it.value() }
?.last { it != null }
?.lastOrNull { it != null }
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ class ApacheHttpClientTest {
fun testHeaders() {
val httpClient: HttpClient = ApacheHttpClient()
val request = httpClient.request()

assertFalse(request.containsHeader("x-token"))
assertNull(request.headers("x-token"))
assertNull(request.firstHeader("x-token"))
assertNull(request.lastHeader("x-token"))

assertDoesNotThrow { request.removeHeaders("x-token") }
assertDoesNotThrow { request.removeHeader("x-token", "222222") }

assertFalse(request.containsHeader("x-token"))
assertNull(request.headers("x-token"))
assertNull(request.firstHeader("x-token"))
Expand All @@ -141,17 +150,18 @@ class ApacheHttpClientTest {
assertEquals("111111", request.firstHeader("x-token"))
assertEquals("111111", request.lastHeader("x-token"))

request.header(BasicHttpHeader("x-token", null))
request.header(BasicHttpHeader("x-token", "222222"))
assertTrue(request.containsHeader("x-token"))
assertArrayEquals(arrayOf("111111", "222222"), request.headers("x-token"))
assertEquals("111111", request.firstHeader("x-token"))
assertEquals("222222", request.lastHeader("x-token"))

request.removeHeader("x-token", "111111")
request.removeHeader("x-token", "222222")
assertTrue(request.containsHeader("x-token"))
assertArrayEquals(arrayOf("222222"), request.headers("x-token"))
assertEquals("222222", request.firstHeader("x-token"))
assertEquals("222222", request.lastHeader("x-token"))
assertArrayEquals(arrayOf("111111"), request.headers("x-token"))
assertEquals("111111", request.firstHeader("x-token"))
assertEquals("111111", request.lastHeader("x-token"))

request.removeHeaders("x-token")
assertFalse(request.containsHeader("x-token"))
Expand Down Expand Up @@ -210,6 +220,8 @@ class ApacheHttpClientTest {
assertEquals("text", it.type())
}

request.param("token", "xxxxx")
request.param("auth", null)
request.fileParam("auth", "222222")
assertTrue(request.containsParam("auth"))
assertArrayEquals(arrayOf("111111", "222222"), request.paramValues("auth"))
Expand Down Expand Up @@ -339,7 +351,7 @@ class ApacheHttpClientTest {
}
}

open class CallTest {
open class AbstractCallTest {

protected lateinit var httpClient: org.apache.http.client.HttpClient
protected lateinit var httpResponse: HttpResponse
Expand Down Expand Up @@ -399,6 +411,9 @@ class ApacheHttpClientTest {
.doAnswer { responseBody.byteInputStream(responseCharset) }
}
}
}

open class CallTest : AbstractCallTest() {

@Test
fun testCallPostJson() {
Expand Down Expand Up @@ -595,7 +610,7 @@ class ApacheHttpClientTest {
}
}

class PostFileTest : CallTest() {
class PostFileTest : AbstractCallTest() {

@JvmField
@TempDir
Expand Down
8 changes: 4 additions & 4 deletions idea-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,17 @@ dependencies {
// implementation fileTree(dir: 'libs', include: ['*.jar'])


implementation('com.itangcent:intellij-idea:1.0.3-SNAPSHOT') {
implementation('com.itangcent:intellij-idea:1.0.32-SNAPSHOT') {
exclude group: 'com.google.inject'
exclude group: 'com.google.code.gson'
}

implementation('com.itangcent:intellij-kotlin-support:1.0.3-SNAPSHOT') {
implementation('com.itangcent:intellij-kotlin-support:1.0.32-SNAPSHOT') {
exclude group: 'com.google.inject'
exclude group: 'com.google.code.gson'
}

// implementation('com.itangcent:intellij-scala-support:1.0.3-SNAPSHOT') {
// implementation('com.itangcent:intellij-scala-support:1.0.32-SNAPSHOT') {
// exclude group: 'com.google.inject'
// exclude group: 'com.google.code.gson'
// }
Expand Down Expand Up @@ -124,7 +124,7 @@ dependencies {
// https://mvnrepository.com/artifact/org.mockito/mockito-inline
testImplementation group: 'org.mockito', name: 'mockito-inline', version: '3.11.0'

testImplementation('com.itangcent:intellij-idea-test:1.0.3-SNAPSHOT') {
testImplementation('com.itangcent:intellij-idea-test:1.0.32-SNAPSHOT') {
exclude group: 'com.nhaarman.mockitokotlin2', module: 'mockito-kotlin'
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ fun JsonElement.unbox(): Any? {

fun JsonPrimitive.unbox(): Any? {
return when {
this.isJsonNull -> null
this.isBoolean -> this.asBoolean
this.isNumber -> this.asNumber
this.isString -> this.asString
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ object ResourceUtils {
fun readResource(resourceName: String): String {
return resourceCache.safeComputeIfAbsent(resourceName) {
(ResourceUtils::class.java.classLoader.getResourceAsStream(resourceName)
?: ResourceUtils::class.java.getResourceAsStream(resourceName))
?.readString(Charsets.UTF_8) ?: ""
?: ResourceUtils::class.java.getResourceAsStream(resourceName))
?.readString(Charsets.UTF_8) ?: ""
} ?: ""
}

Expand All @@ -25,8 +25,8 @@ object ResourceUtils {
}
}

private fun doFindResource(resourceName: String): URL {
private fun doFindResource(resourceName: String): URL? {
return ResourceUtils::class.java.classLoader.getResource(resourceName)
?: ResourceUtils::class.java.getResource(resourceName)
?: ResourceUtils::class.java.getResource(resourceName)
}
}
4 changes: 2 additions & 2 deletions idea-plugin/src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
<category>Web</category>

<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html for description -->
<idea-version since-build="183.0"/>
<!-- <idea-version since-build="183.0"/>-->

<!--for compatible-->
<!--<idea-version since-build="173.0" until-build="183.0"/>-->

<!--for dev-->
<!--<idea-version since-build="173.0"/>-->
<idea-version since-build="173.0"/>


<change-notes><![CDATA[ Change notes will be filled by gradle build ]]>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.itangcent.idea.plugin.api.export.core

import com.google.inject.Inject
import com.itangcent.common.model.MethodDoc
import com.itangcent.intellij.context.ActionContext
import com.itangcent.intellij.extend.guice.singleton
import com.itangcent.intellij.extend.guice.with
import com.itangcent.mock.AdvancedContextTest
import com.itangcent.mock.FakeExportContext
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals

/**
* Test case of [ComponentMethodDocBuilderListener]
*/
internal class ComponentMethodDocBuilderListenerTest : AdvancedContextTest() {

@Inject
private lateinit var methodDocBuilderListener: MethodDocBuilderListener

private lateinit var methodDoc: MethodDoc

override fun bind(builder: ActionContext.ActionContextBuilder) {
super.bind(builder)
builder.bind(MethodDocBuilderListener::class) {
it.with(ComponentMethodDocBuilderListener::class).singleton()
}
builder.bindInstance(
"AVAILABLE_METHOD_DOC_BUILDER_LISTENER",
arrayOf<Any>(DefaultMethodDocBuilderListener::class)
)
}

@BeforeEach
fun init() {
methodDoc = MethodDoc()
}

@Test
fun testSetName() {
methodDocBuilderListener.setName(
FakeExportContext.INSTANCE,
methodDoc, "test"
)
assertEquals("test", methodDoc.name)
}

@Test
fun testAppendDesc() {
methodDocBuilderListener.appendDesc(
FakeExportContext.INSTANCE,
methodDoc, "abc"
)
assertEquals("abc", methodDoc.desc)
methodDocBuilderListener.appendDesc(
FakeExportContext.INSTANCE,
methodDoc, "def"
)
assertEquals("abcdef", methodDoc.desc)
}

@Test
fun testAddParam() {
methodDocBuilderListener.addParam(
FakeExportContext.INSTANCE,
methodDoc, "token", "123", "token for auth", true
)
methodDoc.params!![0].let {
assertEquals("token", it.name)
assertEquals("123", it.value)
assertEquals("token for auth", it.desc)
assertEquals(true, it.required)
}
}

@Test
fun testSetRet() {
methodDocBuilderListener.setRet(
FakeExportContext.INSTANCE,
methodDoc, "ret"
)
assertEquals("ret", methodDoc.ret)
}

@Test
fun testAppendRetDesc() {
methodDocBuilderListener.appendRetDesc(
FakeExportContext.INSTANCE,
methodDoc, "abc"
)
assertEquals("abc", methodDoc.retDesc)
methodDocBuilderListener.appendRetDesc(
FakeExportContext.INSTANCE,
methodDoc, "def"
)
assertEquals("abc\ndef", methodDoc.retDesc)
}
}
Loading

0 comments on commit 5253978

Please sign in to comment.