1+ /* *
2+  * Copyright 2021 Alec Barber. 
3+  * 
4+  * Licensed under the Apache License, Version 2.0 (the "License"); 
5+  * you may not use this file except in compliance with the License. 
6+  * You may obtain a copy of the License at 
7+  * 
8+  * http://www.apache.org/licenses/LICENSE-2.0 
9+  * 
10+  * Unless required by applicable law or agreed to in writing, software 
11+  * distributed under the License is distributed on an "AS IS" BASIS, 
12+  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13+  * See the License for the specific language governing permissions and 
14+  * limitations under the License. 
15+  * 
16+  * TODO: Transfer copyright to Realm Inc. 
17+  */  
18+ 
19+ package  io.realm 
20+ 
21+ import  android.util.Log 
22+ import  androidx.test.ext.junit.runners.AndroidJUnit4 
23+ import  androidx.test.platform.app.InstrumentationRegistry 
24+ import  io.realm.entities.Cat 
25+ import  io.realm.entities.Dog 
26+ import  io.realm.entities.DogPrimaryKey 
27+ import  io.realm.entities.Owner 
28+ import  org.junit.After 
29+ import  org.junit.Before 
30+ import  org.junit.Rule 
31+ import  org.junit.Test 
32+ import  org.junit.runner.RunWith 
33+ import  kotlin.test.assertEquals 
34+ import  kotlin.test.assertFalse 
35+ import  kotlin.test.assertTrue 
36+ 
37+ @RunWith(AndroidJUnit4 ::class )
38+ class  FrozenRealmTests  {
39+ 
40+     @get:Rule
41+     val  configFactory =  TestRealmConfigurationFactory ()
42+ 
43+     private  lateinit  var  realmConfiguration:  RealmConfiguration 
44+     private  lateinit  var  realm:  Realm 
45+ 
46+     @Before
47+     fun  setUp () {
48+         Realm .init (InstrumentationRegistry .getInstrumentation().targetContext)
49+         Log .i(" FrozenRealmTests"  , " Realm initialised"  )
50+         realmConfiguration =  configFactory.createConfigurationBuilder()
51+             .schema(Dog ::class .java, Cat ::class .java, DogPrimaryKey ::class .java, Owner ::class .java)
52+             .build()
53+         Log .i(" FrozenRealmTests"  , " Realm configured"  )
54+         realm =  Realm .getInstance(realmConfiguration)
55+         Log .i(" FrozenRealmTests"  , " Realm created"  )
56+     }
57+ 
58+     @After
59+     fun  tearDown () {
60+         realm.close()
61+         assertEquals(Realm .getGlobalInstanceCount(realmConfiguration), 0 )
62+     }
63+ 
64+     @Test
65+     fun  freezeTwice_closeTwice_noModification () {
66+         //  Freeze the realm twice at the same version
67+         val  realm1 =  realm.freeze()
68+         val  realm2 =  realm.freeze()
69+ 
70+         //  If we close one frozen instance, the other instance should be unaffected
71+         realm2.close()
72+         assertFalse { realm1.isClosed }
73+         realm1.close()
74+         //  Check that the only surviving instance is `realm`
75+         assertEquals(Realm .getGlobalInstanceCount(realmConfiguration), 1 )
76+     }
77+ 
78+     @Test
79+     fun  freezeTwice_closeTwice_writeTransaction () {
80+         //  Freeze the realm twice at different versions
81+         val  realm1 =  realm.freeze()
82+         realm.executeTransaction {
83+             realm.copyToRealm(Dog (" Woof"  , 3 ))
84+         }
85+         val  realm2 =  realm.freeze()
86+ 
87+         //  If we close one frozen instance, the other instance should be unaffected
88+         realm1.close()
89+         assertFalse { realm2.isClosed }
90+         realm2.close()
91+         //  Check that the only surviving instance is `realm`
92+         assertEquals(Realm .getGlobalInstanceCount(realmConfiguration), 1 )
93+     }
94+ 
95+     @Test
96+     fun  closeUnderlying_openFrozenRealms () {
97+         val  realm1 =  realm.freeze()
98+ 
99+         //  If we close the last global instance of the live realm, the frozen realm should be closed
100+         realm.close()
101+         assertTrue { realm1.isClosed }
102+ 
103+         //  Check that there are no global instances left
104+         assertEquals(Realm .getGlobalInstanceCount(realmConfiguration), 0 )
105+     }
106+ }
0 commit comments