refactor: replace Groovy tests in :src:jorphan with Kotlin

This commit is contained in:
Vladimir Sitnikov 2023-12-26 15:08:23 +03:00
parent a671815c95
commit 23f922817f
2 changed files with 170 additions and 208 deletions

View File

@ -17,89 +17,65 @@
package org.apache.jorphan.io
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.api.io.TempDir
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.MethodSource
import org.junit.jupiter.params.provider.ValueSource
import java.io.File
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets
import org.apache.commons.io.FileUtils
import spock.lang.Specification
import spock.lang.Unroll
@Unroll
class TextFileSpec extends Specification {
def tmpFile = File.createTempFile("TextFile", ".unittest")
def tmpPath = tmpFile.getAbsolutePath()
def setup() {
tmpFile.deleteOnExit()
class TextFileTest {
data class GetTextCase(val input: String, val charset: Charset?)
companion object {
@JvmStatic
fun getTextCases() = listOf(
GetTextCase("", Charsets.UTF_8),
GetTextCase("a\nb\nc", null),
GetTextCase("\"a\nb\nc\n", null),
GetTextCase("a\nb\nc", Charsets.UTF_8),
GetTextCase("\"a\nb\nc\n", Charsets.UTF_8),
GetTextCase("a\nb\nc", Charsets.UTF_16),
GetTextCase("\"a\nb\nc\n", Charsets.UTF_16),
GetTextCase("a\nb\nc", Charsets.ISO_8859_1),
GetTextCase("\"a\nb\nc\n", Charsets.ISO_8859_1),
GetTextCase("丈, 😃, and नि", Charsets.UTF_8),
GetTextCase("丈, 😃, and नि", Charsets.UTF_16),
)
}
def "getText of empty file is empty string"() {
given:
def sut = new TextFile(tmpPath)
expect:
sut.getText() == ""
@TempDir
lateinit var tempDir: File
@ParameterizedTest
@MethodSource("getTextCases")
fun getText(case: GetTextCase) {
val tmpPath = tempDir.resolve("file.txt")
tmpPath.writeText(case.input, case.charset ?: Charsets.UTF_8)
assertEquals(case.input, TextFile(tmpPath, case.charset?.name()).text) {
"TextFile(tmpPath, encoding=${case.charset?.name()}).getText()"
}
}
def "getText returns exact content of file"() {
given:
def sut = new TextFile(tmpPath)
FileUtils.write(tmpFile, content, Charset.defaultCharset())
expect:
sut.getText() == content
where:
content << ["a\nb\nc", "\"a\nb\nc\n"]
@ParameterizedTest
@MethodSource("getTextCases")
fun setText(case: GetTextCase) {
val tmpPath = tempDir.resolve("file.txt")
TextFile(tmpPath, case.charset?.name()).text = case.input
assertEquals(case.input, tmpPath.readText(case.charset ?: Charsets.UTF_8)) {
"contents of TextFile(tmpPath, encoding=${case.charset?.name()}).setText(${case.input})"
}
}
def "getText returns exact content of file with specific charset"() {
given:
def sut = new TextFile(tmpPath, encoding)
FileUtils.write(tmpFile, content, charset)
expect:
sut.getText() == content
where:
content | charset | encoding
"a\nb\nc" | StandardCharsets.UTF_16 | "UTF_16"
"a\nb\nc\n" | StandardCharsets.UTF_16 | "UTF_16"
"a\nb\nc" | StandardCharsets.ISO_8859_1 | "ISO_8859_1"
"a\nb\nc\n" | StandardCharsets.ISO_8859_1 | "ISO_8859_1"
}
def "setText sets exact content of file"() {
given:
def sut = new TextFile(tmpPath)
when:
sut.setText(content)
then:
sut.getText() == content
where:
content << ["a\nb\nc", "\"a\nb\nc\n"]
}
def "setText sets exact content of file other charset"() {
given:
def sut = new TextFile(tmpPath, encoding)
when:
sut.setText(content, encoding)
then:
sut.getText(encoding) == content
where:
content | encoding
"a\nb\nc" | "UTF_16"
"a\nb\nc\n" | "UTF_16"
"a\nb\nc" | "ISO_8859_1"
"a\nb\nc\n" | "ISO_8859_1"
}
def "getText throws exception with invalid encoding"() {
given:
def sut = new TextFile(tmpPath, invalidEncoding)
when:
sut.getText()
then:
thrown(IllegalArgumentException)
where:
invalidEncoding << ["", "invalid", "invalid encoding"]
@ParameterizedTest
@ValueSource(strings = ["", "invalid", "invalid encoding"])
fun `getText throws exception with invalid encoding`(charset: String) {
val tmpFile = tempDir.resolve("file.txt")
tmpFile.writeBytes(byteArrayOf())
val file = TextFile(tmpFile, charset)
assertThrows<IllegalArgumentException> {
file.text
}
}
}

View File

@ -17,149 +17,135 @@
package org.apache.jorphan.util
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.MethodSource
import java.io.File
import java.text.DateFormat
import java.util.Date
import java.util.Locale
import spock.lang.Specification
import spock.lang.Unroll
@Unroll
class ConverterSpec extends Specification {
def 'convert #value to #type should give "" when value or type is null'() {
expect:
Converter.convert(value, type) == ""
where:
value | type
null | null
"anything" | null
23 | null
null | String.class
null | Number.class
class ConverterTest {
data class ConvertCase(val input: Any?, val type: Class<*>?, val expected: Any?, val message: String? = null) {
override fun toString(): String =
"Case(input=${
try {
input?.toString()
} catch (t: Throwable) {
"exception from input.toString: ${t.message}"
}
def "convert #value to #type should downcast gracefully and give [#expected]"() {
expect:
Converter.convert(value, type) == expected
where:
value | type | expected
"anything" | Object.class | "anything"
23 | Number.class | 23
}, type=${type?.name}, expected=$expected)"
}
data class InsertLineBreaksCase(val source: String?, val expected: String?, val message: String? = null)
def "convert #value to string should give [#expected]"() {
expect:
Converter.convert(value, String.class) == expected
where:
value | expected
"anything" | "anything"
23 | "23"
42L | "42"
64f | "64.0"
}
companion object {
@JvmStatic
fun insertLineBreaks() = listOf(
InsertLineBreaksCase(null, ""),
InsertLineBreaksCase("bar", "bar"),
InsertLineBreaksCase("\nbar", "foobar"),
)
def "convert #value to number #type should give number [#expected]"() {
expect:
Converter.convert(value, type) == expected
where:
value | type | expected
23f | float.class | 23f
42f | Float.class | 42f
"42" | Float.class | 42f
23f | double.class | 23d
42f | Double.class | 42d
"42" | Double.class | 42d
23L | int.class | 23
42 | Integer.class | 42
"42" | Integer.class | 42
23L | long.class | 23L
42 | Long.class | 42L
"42" | Long.class | 42L
"invalid" | Float.class | 0f
"invalid" | float.class | 0f
"invalid" | double.class | 0d
"invalid" | Double.class | 0d
"invalid" | int.class | 0
"invalid" | Integer.class | 0
}
@JvmStatic
fun conversionCases() = listOf(
// Null input or type
ConvertCase(null, null, "", "null input and null type"),
ConvertCase("anything", null, "", "null type"),
ConvertCase(23, null, "", "null type"),
ConvertCase(null, String::class.java, "", "null input"),
ConvertCase(null, Number::class.java, "", "null input"),
// No change
ConvertCase("anything", Any::class.java, "anything"),
ConvertCase(23, Number::class.java, 23),
// To string
ConvertCase("anything", String::class.java, "anything"),
ConvertCase(23, String::class.java, "23"),
ConvertCase(42L, String::class.java, "42"),
ConvertCase(64f, String::class.java, "64.0"),
// To number
ConvertCase(23f, Float::class.javaPrimitiveType, 23f),
ConvertCase(42f, Float::class.javaObjectType, 42f),
ConvertCase("42", Float::class.javaObjectType, 42f),
ConvertCase(23f, Double::class.javaPrimitiveType, 23.0),
ConvertCase(42f, Double::class.javaObjectType, 42.0),
ConvertCase("42", Double::class.javaObjectType, 42.0),
ConvertCase(23L, Int::class.javaPrimitiveType, 23),
ConvertCase(42, Int::class.javaObjectType, 42),
ConvertCase("42", Int::class.javaObjectType, 42),
ConvertCase(23L, Long::class.javaPrimitiveType, 23L),
ConvertCase(42, Long::class.javaObjectType, 42L),
ConvertCase("42", Long::class.javaObjectType, 42L),
ConvertCase("invalid", Float::class.javaObjectType, 0f),
ConvertCase("invalid", Float::class.javaPrimitiveType, 0f),
ConvertCase("invalid", Double::class.javaPrimitiveType, 0.0),
ConvertCase("invalid", Double::class.javaObjectType, 0.0),
ConvertCase("invalid", Int::class.javaPrimitiveType, 0),
ConvertCase("invalid", Int::class.javaObjectType, 0),
// To Class
ConvertCase("java.lang.String", Class::class.java, String::class.java),
ConvertCase("not.a.valid.class", Class::class.java, "not.a.valid.class"),
// Other cases
ConvertCase("", Boolean::class.javaObjectType, false),
ConvertCase("true", Boolean::class.javaObjectType, true),
ConvertCase(true, Boolean::class.javaObjectType, true),
ConvertCase(false, Boolean::class.javaObjectType, false),
ConvertCase("", Boolean::class.javaPrimitiveType, false),
ConvertCase("true", Boolean::class.javaPrimitiveType, true),
ConvertCase(true, Boolean::class.javaPrimitiveType, true),
ConvertCase(false, Boolean::class.javaPrimitiveType, false),
ConvertCase("filename", File::class.java, File("filename")),
ConvertCase(File("filename"), File::class.java, File("filename")),
ConvertCase("c", Character::class.javaObjectType, 'c'),
ConvertCase("c", Character::class.javaPrimitiveType, 'c'),
ConvertCase("char", Character::class.javaPrimitiveType, 'c'),
ConvertCase(65, Character::class.javaPrimitiveType, 'A'),
ConvertCase("", Character::class.javaPrimitiveType, ' '),
ConvertCase(65.toChar(), Character::class.javaPrimitiveType, 'A'),
ConvertCase(65.toByte(), Character::class.javaPrimitiveType, 'A'),
ConvertCase(
object {
override fun toString(): String =
throw Exception("deliberate exception to test Converter.convert()")
},
Character::class.javaPrimitiveType,
' ',
"toString() throws exception"
),
// Date
ConvertCase(Date(30000), Date::class.java, Date(30000)),
ConvertCase(
toLocalDateFormat("08/20/2019", DateFormat.SHORT),
Date::class.java,
toLocalDate("08/20/2019", DateFormat.SHORT)
)
)
def "Convert #value to Class gives #expected"() {
expect:
Converter.convert(value, Class.class) == expected
where:
value | expected
"java.lang.String" | String.class
"not.a.valid.class" | "not.a.valid.class"
}
def "Convert '#value' to #type"() {
expect:
if (value == "OBJECT_WITH_BROKEN_TO_STRING") {
value = new Object() { String toString() { 1/0 } }
}
Converter.convert(value, type) == expected
where:
value | type | expected
"" | Boolean.class | false
"true" | Boolean.class | true
true | Boolean.class | true
false | Boolean.class | false
"" | boolean.class | false
"true" | boolean.class | true
true | boolean.class | true
false | boolean.class | false
"filename" | File.class | new File("filename")
new File("filename") | File.class | new File("filename")
"c" | Character.class | 'c'
"c" | char.class | 'c'
"char" | char.class | 'c'
65 | char.class | 'A'
'' | char.class | ' '
Character.valueOf((char) 65) | char.class | 'A'
Byte.valueOf((byte)65) | char.class | 'A'
"OBJECT_WITH_BROKEN_TO_STRING" | char.class | ' '
}
def "Convert to date from '#value'"() {
expect:
Math.abs(Converter.convert(value, Date.class).getTime() - expected.getTime()) < 1000
where:
value | expected
"" | new Date()
new Date(30000) | new Date(30000)
toLocalDateFormat("08/20/2019", DateFormat.SHORT) | toLocalDate("08/20/2019", DateFormat.SHORT)
}
def "Convert to Calendar from '#value'"() {
expect:
Math.abs(
Converter.convert(value, Calendar.class).getTime().getTime() - expected.getTime()) < 1000
where:
value | expected
"" | new Date()
new Date(30000) | new Date(30000)
toLocalDateFormat("08/20/2019", DateFormat.SHORT) | toLocalDate("08/20/2019", DateFormat.SHORT)
}
def toLocalDateFormat(String dateString, int format) {
def date = toLocalDate(dateString, format)
private fun toLocalDateFormat(dateString: String, format: Int): String {
val date = toLocalDate(dateString, format)
return DateFormat.getDateInstance(format).format(date)
}
def toLocalDate(String dateString, int format) {
def date = DateFormat
fun toLocalDate(dateString: String, format: Int): Date {
val date = DateFormat
.getDateInstance(format, Locale.forLanguageTag("en-US"))
.parse(dateString)
return date
}
}
def "line breaks should be replaced in '#source' to '#expected'"() {
expect:
Converter.insertLineBreaks(source, "foo") == expected
where:
source | expected
null | ""
"bar" | "bar"
"\nbar"| "foobar"
@ParameterizedTest
@MethodSource("conversionCases")
fun convert(case: ConvertCase) {
assertEquals(case.expected, Converter.convert(case.input, case.type)) {
"Converter.convert(${case.input}, ${case.type}): ${case.message}"
}
}
@ParameterizedTest
@MethodSource("insertLineBreaks")
fun insertLineBreaks(case: InsertLineBreaksCase) {
assertEquals(case.expected, Converter.insertLineBreaks(case.source, "foo")) {
"Converter.insertLineBreaks(${case.source}, \"foo\")"
}
}
}