add Java tests for verifying @JvmStatic (#14)

This commit is contained in:
Adam 2024-02-25 13:17:08 +01:00 committed by GitHub
parent 61137e0878
commit 171570159e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 102 additions and 22 deletions

View file

@ -36,7 +36,7 @@ kotlin {
targets.withType<KotlinJvmTarget>().configureEach { targets.withType<KotlinJvmTarget>().configureEach {
testRuns.configureEach { testRuns.configureEach {
executionTask.configure { executionTask.configure {
// useJUnitPlatform() useJUnitPlatform()
} }
} }
} }

View file

@ -0,0 +1,43 @@
/*
* Copyright 2001-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.thauvin.erik.urlencoder
import kotlin.jvm.JvmField
const val standardContent = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVQXYZ0123456789-_."
val invalidContent = listOf("sdkjfh%", "sdkjfh%6", "sdkjfh%xx", "sdfjfh%-1")
/**
* List of unencoded content paired with the encoded content.
*/
val decodedToEncoded = listOf(
TestData("a test &", "a%20test%20%26"),
TestData(
"!abcdefghijklmnopqrstuvwxyz%%ABCDEFGHIJKLMNOPQRSTUVQXYZ0123456789-_.~=",
"%21abcdefghijklmnopqrstuvwxyz%25%25ABCDEFGHIJKLMNOPQRSTUVQXYZ0123456789-_.%7E%3D"
),
TestData("%#okékÉȢ smile!😁", "%25%23ok%C3%A9k%C3%89%C8%A2%20smile%21%F0%9F%98%81"),
TestData("\uD808\uDC00\uD809\uDD00\uD808\uDF00\uD808\uDD00", "%F0%92%80%80%F0%92%94%80%F0%92%8C%80%F0%92%84%80"),
)
data class TestData(
@JvmField
val unencoded: String,
@JvmField
val encoded: String,
)

View file

@ -23,30 +23,17 @@ import kotlin.test.DefaultAsserter.assertEquals
import kotlin.test.DefaultAsserter.assertSame import kotlin.test.DefaultAsserter.assertSame
class UrlEncoderUtilTest { class UrlEncoderUtilTest {
private val same = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVQXYZ0123456789-_."
companion object {
val invalid = listOf("sdkjfh%", "sdkjfh%6", "sdkjfh%xx", "sdfjfh%-1")
val validMap = listOf(
"a test &" to "a%20test%20%26",
"!abcdefghijklmnopqrstuvwxyz%%ABCDEFGHIJKLMNOPQRSTUVQXYZ0123456789-_.~=" to
"%21abcdefghijklmnopqrstuvwxyz%25%25ABCDEFGHIJKLMNOPQRSTUVQXYZ0123456789-_.%7E%3D",
"%#okékÉȢ smile!😁" to "%25%23ok%C3%A9k%C3%89%C8%A2%20smile%21%F0%9F%98%81",
"\uD808\uDC00\uD809\uDD00\uD808\uDF00\uD808\uDD00" to "%F0%92%80%80%F0%92%94%80%F0%92%8C%80%F0%92%84%80",
)
}
@Test @Test
fun decodeURL() { fun decodeURL() {
for (m in validMap) { for ((unencoded, encoded) in decodedToEncoded) {
assertEquals(m.first, decode(m.second)) assertEquals(unencoded, decode(encoded))
} }
} }
@Test @Test
fun decodeWithException() { fun decodeWithException() {
for (source in invalid) { for (source in invalidContent) {
assertFailsWith<IllegalArgumentException>( assertFailsWith<IllegalArgumentException>(
message = "decode($source)", message = "decode($source)",
block = { decode(source) } block = { decode(source) }
@ -56,7 +43,7 @@ class UrlEncoderUtilTest {
@Test @Test
fun decodeWhenNoneNeeded() { fun decodeWhenNoneNeeded() {
assertSame(same, decode(same)) assertSame(standardContent, decode(standardContent))
assertEquals("decode('')", decode(""), "") assertEquals("decode('')", decode(""), "")
assertEquals("decode(' ')", decode(" "), " ") assertEquals("decode(' ')", decode(" "), " ")
} }
@ -72,8 +59,8 @@ class UrlEncoderUtilTest {
@Test @Test
fun encodeURL() { fun encodeURL() {
for (m in validMap) { for ((unencoded, encoded) in decodedToEncoded) {
assertEquals(m.second, encode(m.first)) assertEquals(encoded, encode(unencoded))
} }
} }
@ -86,8 +73,8 @@ class UrlEncoderUtilTest {
@Test @Test
fun encodeWhenNoneNeeded() { fun encodeWhenNoneNeeded() {
assertSame(encode(same), same) assertSame(encode(standardContent), standardContent)
assertSame("with empty allow", encode(same, allow = ""), same) assertSame("with empty allow", encode(standardContent, allow = ""), standardContent)
} }
@Test @Test

View file

@ -0,0 +1,50 @@
/*
* Copyright 2001-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.thauvin.erik.urlencoder;
import org.junit.jupiter.api.Test;
import static net.thauvin.erik.urlencoder.TestDataKt.getDecodedToEncoded;
import static net.thauvin.erik.urlencoder.UrlEncoderUtil.decode;
import static net.thauvin.erik.urlencoder.UrlEncoderUtil.encode;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
class UrlEncoderJavaTest {
@Test
public void decodeURL() {
assertAll(
getDecodedToEncoded()
.stream()
.map(data ->
() -> assertEquals(data.unencoded, decode(data.encoded))
)
);
}
@Test
public void encodeURL() {
assertAll(
getDecodedToEncoded()
.stream()
.map(data ->
() -> assertEquals(data.encoded, encode(data.unencoded))
)
);
}
}