mirror of https://github.com/apache/jmeter.git
refactor: replace Groovy tests in :src:jorphan with Kotlin
This commit is contained in:
parent
a671815c95
commit
23f922817f
|
@ -17,89 +17,65 @@
|
||||||
|
|
||||||
package org.apache.jorphan.io
|
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.Charset
|
||||||
import java.nio.charset.StandardCharsets
|
|
||||||
|
|
||||||
import org.apache.commons.io.FileUtils
|
class TextFileTest {
|
||||||
|
data class GetTextCase(val input: String, val charset: Charset?)
|
||||||
import spock.lang.Specification
|
companion object {
|
||||||
import spock.lang.Unroll
|
@JvmStatic
|
||||||
|
fun getTextCases() = listOf(
|
||||||
@Unroll
|
GetTextCase("", Charsets.UTF_8),
|
||||||
class TextFileSpec extends Specification {
|
GetTextCase("a\nb\nc", null),
|
||||||
|
GetTextCase("\"a\nb\nc\n", null),
|
||||||
def tmpFile = File.createTempFile("TextFile", ".unittest")
|
GetTextCase("a\nb\nc", Charsets.UTF_8),
|
||||||
def tmpPath = tmpFile.getAbsolutePath()
|
GetTextCase("\"a\nb\nc\n", Charsets.UTF_8),
|
||||||
|
GetTextCase("a\nb\nc", Charsets.UTF_16),
|
||||||
def setup() {
|
GetTextCase("\"a\nb\nc\n", Charsets.UTF_16),
|
||||||
tmpFile.deleteOnExit()
|
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"() {
|
@TempDir
|
||||||
given:
|
lateinit var tempDir: File
|
||||||
def sut = new TextFile(tmpPath)
|
|
||||||
expect:
|
@ParameterizedTest
|
||||||
sut.getText() == ""
|
@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"() {
|
@ParameterizedTest
|
||||||
given:
|
@MethodSource("getTextCases")
|
||||||
def sut = new TextFile(tmpPath)
|
fun setText(case: GetTextCase) {
|
||||||
FileUtils.write(tmpFile, content, Charset.defaultCharset())
|
val tmpPath = tempDir.resolve("file.txt")
|
||||||
expect:
|
TextFile(tmpPath, case.charset?.name()).text = case.input
|
||||||
sut.getText() == content
|
assertEquals(case.input, tmpPath.readText(case.charset ?: Charsets.UTF_8)) {
|
||||||
where:
|
"contents of TextFile(tmpPath, encoding=${case.charset?.name()}).setText(${case.input})"
|
||||||
content << ["a\nb\nc", "\"a\nb\nc\n"]
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
def "getText returns exact content of file with specific charset"() {
|
@ParameterizedTest
|
||||||
given:
|
@ValueSource(strings = ["", "invalid", "invalid encoding"])
|
||||||
def sut = new TextFile(tmpPath, encoding)
|
fun `getText throws exception with invalid encoding`(charset: String) {
|
||||||
FileUtils.write(tmpFile, content, charset)
|
val tmpFile = tempDir.resolve("file.txt")
|
||||||
expect:
|
tmpFile.writeBytes(byteArrayOf())
|
||||||
sut.getText() == content
|
val file = TextFile(tmpFile, charset)
|
||||||
where:
|
assertThrows<IllegalArgumentException> {
|
||||||
content | charset | encoding
|
file.text
|
||||||
"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"]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,149 +17,135 @@
|
||||||
|
|
||||||
package org.apache.jorphan.util
|
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.text.DateFormat
|
||||||
|
import java.util.Date
|
||||||
|
import java.util.Locale
|
||||||
|
|
||||||
import spock.lang.Specification
|
class ConverterTest {
|
||||||
import spock.lang.Unroll
|
data class ConvertCase(val input: Any?, val type: Class<*>?, val expected: Any?, val message: String? = null) {
|
||||||
|
override fun toString(): String =
|
||||||
@Unroll
|
"Case(input=${
|
||||||
class ConverterSpec extends Specification {
|
try {
|
||||||
|
input?.toString()
|
||||||
def 'convert #value to #type should give "" when value or type is null'() {
|
} catch (t: Throwable) {
|
||||||
expect:
|
"exception from input.toString: ${t.message}"
|
||||||
Converter.convert(value, type) == ""
|
|
||||||
where:
|
|
||||||
value | type
|
|
||||||
null | null
|
|
||||||
"anything" | null
|
|
||||||
23 | null
|
|
||||||
null | String.class
|
|
||||||
null | Number.class
|
|
||||||
}
|
}
|
||||||
|
}, type=${type?.name}, expected=$expected)"
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
data class InsertLineBreaksCase(val source: String?, val expected: String?, val message: String? = null)
|
||||||
|
|
||||||
def "convert #value to string should give [#expected]"() {
|
companion object {
|
||||||
expect:
|
@JvmStatic
|
||||||
Converter.convert(value, String.class) == expected
|
fun insertLineBreaks() = listOf(
|
||||||
where:
|
InsertLineBreaksCase(null, ""),
|
||||||
value | expected
|
InsertLineBreaksCase("bar", "bar"),
|
||||||
"anything" | "anything"
|
InsertLineBreaksCase("\nbar", "foobar"),
|
||||||
23 | "23"
|
)
|
||||||
42L | "42"
|
|
||||||
64f | "64.0"
|
|
||||||
}
|
|
||||||
|
|
||||||
def "convert #value to number #type should give number [#expected]"() {
|
@JvmStatic
|
||||||
expect:
|
fun conversionCases() = listOf(
|
||||||
Converter.convert(value, type) == expected
|
// Null input or type
|
||||||
where:
|
ConvertCase(null, null, "", "null input and null type"),
|
||||||
value | type | expected
|
ConvertCase("anything", null, "", "null type"),
|
||||||
23f | float.class | 23f
|
ConvertCase(23, null, "", "null type"),
|
||||||
42f | Float.class | 42f
|
ConvertCase(null, String::class.java, "", "null input"),
|
||||||
"42" | Float.class | 42f
|
ConvertCase(null, Number::class.java, "", "null input"),
|
||||||
23f | double.class | 23d
|
// No change
|
||||||
42f | Double.class | 42d
|
ConvertCase("anything", Any::class.java, "anything"),
|
||||||
"42" | Double.class | 42d
|
ConvertCase(23, Number::class.java, 23),
|
||||||
23L | int.class | 23
|
// To string
|
||||||
42 | Integer.class | 42
|
ConvertCase("anything", String::class.java, "anything"),
|
||||||
"42" | Integer.class | 42
|
ConvertCase(23, String::class.java, "23"),
|
||||||
23L | long.class | 23L
|
ConvertCase(42L, String::class.java, "42"),
|
||||||
42 | Long.class | 42L
|
ConvertCase(64f, String::class.java, "64.0"),
|
||||||
"42" | Long.class | 42L
|
// To number
|
||||||
"invalid" | Float.class | 0f
|
ConvertCase(23f, Float::class.javaPrimitiveType, 23f),
|
||||||
"invalid" | float.class | 0f
|
ConvertCase(42f, Float::class.javaObjectType, 42f),
|
||||||
"invalid" | double.class | 0d
|
ConvertCase("42", Float::class.javaObjectType, 42f),
|
||||||
"invalid" | Double.class | 0d
|
ConvertCase(23f, Double::class.javaPrimitiveType, 23.0),
|
||||||
"invalid" | int.class | 0
|
ConvertCase(42f, Double::class.javaObjectType, 42.0),
|
||||||
"invalid" | Integer.class | 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"() {
|
private fun toLocalDateFormat(dateString: String, format: Int): String {
|
||||||
expect:
|
val date = toLocalDate(dateString, format)
|
||||||
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)
|
|
||||||
return DateFormat.getDateInstance(format).format(date)
|
return DateFormat.getDateInstance(format).format(date)
|
||||||
}
|
}
|
||||||
|
|
||||||
def toLocalDate(String dateString, int format) {
|
fun toLocalDate(dateString: String, format: Int): Date {
|
||||||
def date = DateFormat
|
val date = DateFormat
|
||||||
.getDateInstance(format, Locale.forLanguageTag("en-US"))
|
.getDateInstance(format, Locale.forLanguageTag("en-US"))
|
||||||
.parse(dateString)
|
.parse(dateString)
|
||||||
return date
|
return date
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
def "line breaks should be replaced in '#source' to '#expected'"() {
|
@ParameterizedTest
|
||||||
expect:
|
@MethodSource("conversionCases")
|
||||||
Converter.insertLineBreaks(source, "foo") == expected
|
fun convert(case: ConvertCase) {
|
||||||
where:
|
assertEquals(case.expected, Converter.convert(case.input, case.type)) {
|
||||||
source | expected
|
"Converter.convert(${case.input}, ${case.type}): ${case.message}"
|
||||||
null | ""
|
}
|
||||||
"bar" | "bar"
|
}
|
||||||
"\nbar"| "foobar"
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource("insertLineBreaks")
|
||||||
|
fun insertLineBreaks(case: InsertLineBreaksCase) {
|
||||||
|
assertEquals(case.expected, Converter.insertLineBreaks(case.source, "foo")) {
|
||||||
|
"Converter.insertLineBreaks(${case.source}, \"foo\")"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue