Make Regex class serializable on JVM

#KT-16447 Fixed

Refactor helper functions for serialization tests.
This commit is contained in:
Ilya Gorbunov
2017-04-22 02:24:17 +03:00
parent 99bd54116e
commit 16d3d2f764
6 changed files with 80 additions and 13 deletions
@@ -1,6 +1,7 @@
package test.collections
import test.*
import kotlin.reflect.KProperty1
import kotlin.test.*
public fun <T> compare(expected: T, actual: T, block:CompareContext<T>.() -> Unit) {
@@ -12,6 +13,11 @@ public class CompareContext<out T>(public val expected: T, public val actual: T)
public fun equals(message: String = "") {
assertEquals(expected, actual, message)
}
public fun <P> propertyEquals(property: KProperty1<in T, P>) {
propertyEquals(property.name, property)
}
public fun <P> propertyEquals(message: String = "", getter: T.() -> P) {
assertEquals(expected.getter(), actual.getter(), message)
}
+15 -8
View File
@@ -57,25 +57,32 @@ class SerializableTest {
}
}
public fun <T> serializeAndDeserialize(value: T): T {
public fun <T> serializeToByteArray(value: T): ByteArray {
val outputStream = ByteArrayOutputStream()
val objectOutputStream = ObjectOutputStream(outputStream)
objectOutputStream.writeObject(value)
objectOutputStream.close()
outputStream.close()
return outputStream.toByteArray()
}
val inputStream = ByteArrayInputStream(outputStream.toByteArray())
public fun <T> deserializeFromByteArray(bytes: ByteArray): T {
val inputStream = ByteArrayInputStream(bytes)
val inputObjectStream = ObjectInputStream(inputStream)
@Suppress("UNCHECKED_CAST")
return inputObjectStream.readObject() as T
}
public fun <T> serializeAndDeserialize(value: T): T {
val bytes = serializeToByteArray(value)
return deserializeFromByteArray(bytes)
}
private fun hexToBytes(value: String): ByteArray = value.split(" ").map { Integer.parseInt(it, 16).toByte() }.toByteArray()
public fun <T> deserializeFromHex(value: String) = hexToBytes(value).let {
val inputStream = ByteArrayInputStream(it)
val inputObjectStream = ObjectInputStream(inputStream)
@Suppress("UNCHECKED_CAST")
inputObjectStream.readObject() as T
}
public fun <T> deserializeFromHex(value: String) = deserializeFromByteArray<T>(hexToBytes(value))
public fun <T> serializeToHex(value: T) =
serializeToByteArray(value).joinToString(" ") { (it.toInt() and 0xFF).toString(16).padStart(2, '0') }
@@ -1,8 +1,27 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* 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
*
* http://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.
*/
@file:kotlin.jvm.JvmVersion
package test.text
import kotlin.test.*
import org.junit.Test
import test.collections.compare
import test.io.*
import java.util.regex.Pattern
class RegexJVMTest {
@@ -27,4 +46,28 @@ class RegexJVMTest {
assertEquals("b", m2.groups[2]?.value)
assertEquals(4..4, m2.groups[2]?.range)
}
private fun compareRegex(expected: Regex, actual: Regex) = compare(expected, actual) {
propertyEquals(Regex::pattern)
propertyEquals(Regex::options)
propertyEquals("flags") { toPattern().flags() }
}
private fun equivalentAfterDeserialization(regex: Regex) = compareRegex(regex, serializeAndDeserialize(regex))
@Test fun serializeDeserializeRegex() {
equivalentAfterDeserialization(Regex(""))
equivalentAfterDeserialization(Regex("\\w+"))
equivalentAfterDeserialization(Regex("\\w+", RegexOption.IGNORE_CASE))
equivalentAfterDeserialization(Regex("\\w+", setOf(RegexOption.LITERAL, RegexOption.MULTILINE)))
equivalentAfterDeserialization(Pattern.compile("\\w+", Pattern.UNICODE_CASE).toRegex())
}
@Test fun deserializeRegexFromHex() {
val expected = Regex("\\w+", RegexOption.IGNORE_CASE)
// println(serializeToHex(expected))
val deserialized = deserializeFromHex<Regex>("ac ed 00 05 73 72 00 1c 6b 6f 74 6c 69 6e 2e 74 65 78 74 2e 52 65 67 65 78 24 53 65 72 69 61 6c 69 7a 65 64 00 00 00 00 00 00 00 00 02 00 02 49 00 05 66 6c 61 67 73 4c 00 07 70 61 74 74 65 72 6e 74 00 12 4c 6a 61 76 61 2f 6c 61 6e 67 2f 53 74 72 69 6e 67 3b 78 70 00 00 00 42 74 00 03 5c 77 2b")
compareRegex(expected, deserialized)
}
}