• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3  */
4 
5 package kotlinx.serialization.features
6 
7 import kotlinx.serialization.*
8 import kotlinx.serialization.json.*
9 import org.junit.*
10 import org.junit.Assert.*
11 
12 class InternalInheritanceTest : JsonTestBase() {
13     @Serializable
14     open class A(val parent: Int) {
15         private val rootOptional = "rootOptional"
16 
equalsnull17         override fun equals(other: Any?): Boolean {
18             if (this === other) return true
19             if (other !is A) return false
20 
21             if (parent != other.parent) return false
22             if (rootOptional != other.rootOptional) return false
23 
24             return true
25         }
26     }
27 
28     @Serializable
29     open class B(val parent2: Int, @Transient val transientDerived: String = "X", val derived: String) : A(parent2) {
30         protected val bodyDerived = "body"
31     }
32 
33     @Serializable
34     class C(val parent3: Int) : B(parent3, derived = "derived") {
35         val lastDerived = "optional"
36 
equalsnull37         override fun equals(other: Any?): Boolean {
38             if (this === other) return true
39             if (other?.javaClass != javaClass) return false
40 
41             other as C
42 
43             if (!super.equals(other)) return false
44             if (parent3 != other.parent3) return false
45             if (lastDerived != other.lastDerived) return false
46             if (parent2 != other.parent2) return false
47             if (transientDerived != other.transientDerived) return false
48             if (derived != other.derived) return false
49             if (bodyDerived != other.bodyDerived) return false
50 
51             return true
52         }
53     }
54 
55     @Test
testEncodeToStringnull56     fun testEncodeToString() {
57         assertEquals(
58             """{"parent":42,"rootOptional":"rootOptional","parent2":42,"derived":"derived",""" +
59                     """"bodyDerived":"body","parent3":42,"lastDerived":"optional"}""",
60             default.encodeToString(C(42))
61         )
62         assertEquals(
63             """{"parent":13,"rootOptional":"rootOptional","parent2":13,"derived":"bbb","bodyDerived":"body"}""",
64             default.encodeToString(B(13, derived = "bbb"))
65         )
66     }
67 
68     @Test
testParsenull69     fun testParse() {
70         assertEquals(
71             C(42),
72             default.decodeFromString<C>(
73                 """{"parent":42,"rootOptional":"rootOptional","parent2":42,""" +
74                         """"derived":"derived","bodyDerived":"body","parent3":42,"lastDerived":"optional"}"""
75             )
76         )
77         assertEquals(
78             C(43),
79             default.decodeFromString<C>("""{"parent":43,"rootOptional":"rootOptional","parent2":43,"derived":"derived",""" +
80                     """"bodyDerived":"body","parent3":43,"lastDerived":"optional"}""")
81         )
82     }
83 
84     @Test
testParseOptionalsnull85     fun testParseOptionals() {
86         assertEquals(
87             B(100, derived = "wowstring"),
88             default.decodeFromString<B>("""{"parent":100,"rootOptional":"rootOptional","parent2":100,"derived":"wowstring","bodyDerived":"body"}""")
89         )
90         assertEquals(
91             C(44),
92             default.decodeFromString<C>("""{"parent":44, "parent2":44,"derived":"derived","bodyDerived":"body","parent3":44}""")
93         )
94         assertEquals(
95             B(101, derived = "wowstring"),
96             default.decodeFromString<B>("""{"parent":101,"parent2":101,"derived":"wowstring","bodyDerived":"body"}""")
97         )
98         assertEquals(
99             A(77),
100             default.decodeFromString<A>("""{"parent":77,"rootOptional":"rootOptional"}""")
101         )
102         assertEquals(
103             A(78),
104             default.decodeFromString<A>("""{"parent":78}""")
105         )
106     }
107 
108     @Test(expected = SerializationException::class)
testThrowTransientnull109     fun testThrowTransient() {
110         Json.decodeFromString<B>("""{"parent":100,"rootOptional":"rootOptional","transientDerived":"X",""" +
111                 """"parent2":100,"derived":"wowstring","bodyDerived":"body"}""")
112     }
113 
114     @Test(expected = SerializationException::class)
testThrowMissingFieldnull115     fun testThrowMissingField() {
116         default.decodeFromString<C>("""{"parent":42,"rootOptional":"rootOptional","derived":"derived",""" +
117                 """"bodyDerived":"body","parent3":42,"lastDerived":"optional"}""")
118     }
119 
120     @Test
testSerializeAsParentnull121     fun testSerializeAsParent() {
122         val obj1: A = B(77, derived = "derived")
123         val obj2: A = C(77)
124         assertEquals("""{"parent":77,"rootOptional":"rootOptional"}""", default.encodeToString(obj1))
125         assertEquals("""{"parent":77,"rootOptional":"rootOptional"}""", default.encodeToString(obj2))
126     }
127 }
128