This is an automated email from the ASF dual-hosted git repository.
paulk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/master by this push:
new fe1818a additional record test
fe1818a is described below
commit fe1818ab261723fe14eb44c7c6eacf3fcefd5f76
Author: Paul King <[email protected]>
AuthorDate: Thu Nov 4 18:03:00 2021 +1000
additional record test
---
.../org/codehaus/groovy/classgen/RecordTest.groovy | 72 +++++++++++++++++++---
1 file changed, 64 insertions(+), 8 deletions(-)
diff --git a/src/test/groovy/org/codehaus/groovy/classgen/RecordTest.groovy
b/src/test/groovy/org/codehaus/groovy/classgen/RecordTest.groovy
index 96408da..ddab9dc 100644
--- a/src/test/groovy/org/codehaus/groovy/classgen/RecordTest.groovy
+++ b/src/test/groovy/org/codehaus/groovy/classgen/RecordTest.groovy
@@ -343,40 +343,40 @@ class RecordTest {
@groovy.transform.CompileDynamic
record PersonDynamic(String name, int age) {}
record PersonStatic(String name, int age) {}
-
+
def testDynamic() {
PersonDynamic p = ['Daniel', 37]
assert 'Daniel' == p.name()
assert 37 == p.age()
-
+
PersonDynamic p2 = [age: 37, name: 'Daniel']
assert 'Daniel' == p2.name()
assert 37 == p2.age()
-
+
PersonStatic p3 = ['Daniel', 37]
assert 'Daniel' == p3.name()
assert 37 == p3.age()
-
+
PersonStatic p4 = [age: 37, name: 'Daniel']
assert 'Daniel' == p4.name()
assert 37 == p4.age()
}
testDynamic()
-
+
@groovy.transform.CompileStatic
def testStatic() {
PersonStatic p = ['Daniel', 37]
assert 'Daniel' == p.name()
assert 37 == p.age()
-
+
PersonStatic p2 = [age: 37, name: 'Daniel']
assert 'Daniel' == p2.name()
assert 37 == p2.age()
-
+
PersonDynamic p3 = ['Daniel', 37]
assert 'Daniel' == p3.name()
assert 37 == p3.age()
-
+
PersonDynamic p4 = [age: 37, name: 'Daniel']
assert 'Daniel' == p4.name()
assert 37 == p4.age()
@@ -384,4 +384,60 @@ class RecordTest {
testStatic()
'''
}
+
+ @Test
+ void testSerialization() {
+ // inspired by:
+ // https://inside.java/2020/07/20/record-serialization/
+
+ assertScript '''
+ @groovy.transform.ToString(includeNames=true, includeFields=true)
+ class RangeClass implements Serializable {
+ private final int lo
+ private final int hi
+ RangeClass(int lo, int hi) {
+ this.lo = lo
+ this.hi = hi
+ if (lo > hi) throw new IllegalArgumentException("$lo should
not be greater than $hi")
+ }
+ // backdoor to emulate hacking of datastream
+ RangeClass(int[] pair) {
+ this.lo = pair[0]
+ this.hi = pair[1]
+ }
+ }
+
+ var data = File.createTempFile("serial", ".data")
+ var rc = [new RangeClass([5, 10] as int[]), new RangeClass([10, 5] as
int[])]
+ data.withObjectOutputStream { out -> rc.each{ out << it } }
+ data.withObjectInputStream(getClass().classLoader) { in ->
+ assert in.readObject().toString() == 'RangeClass(lo:5, hi:10)'
+ assert in.readObject().toString() == 'RangeClass(lo:10, hi:5)'
+ }
+ '''
+
+ assertScript '''
+ import static groovy.test.GroovyAssert.shouldFail
+
+ record RangeRecord(int lo, int hi) implements Serializable {
+ public RangeRecord {
+ if (lo > hi) throw new IllegalArgumentException("$lo should
not be greater than $hi")
+ }
+ // backdoor to emulate hacking of datastream
+ RangeRecord(int[] pair) {
+ this.lo = pair[0]
+ this.hi = pair[1]
+ }
+ }
+
+ var data = File.createTempFile("serial", ".data")
+ var rr = [new RangeRecord([5, 10] as int[]), new RangeRecord([10, 5]
as int[])]
+ data.withObjectOutputStream { out -> rr.each{ out << it } }
+ data.withObjectInputStream(getClass().classLoader) { in ->
+ assert in.readObject().toString() == 'RangeRecord[lo=5, hi=10]'
+ def ex = shouldFail(InvalidObjectException) { in.readObject() }
+ assert ex.message == '10 should not be greater than 5'
+ }
+ '''
+ }
}