1 /* 2 * Copyright (C) 2009 The Android Open Source Project 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 17 package android.database.sqlite.cts; 18 19 import android.database.sqlite.SQLiteClosable; 20 import android.test.AndroidTestCase; 21 22 public class SQLiteClosableTest extends AndroidTestCase { 23 private class MockSQLiteClosable extends SQLiteClosable { 24 private boolean mOnAllReferencesReleasedCalled = false; 25 private boolean mOnAllReferencesReleasedFromContainerCalled = false; 26 27 @Override 28 protected void onAllReferencesReleased() { 29 mOnAllReferencesReleasedCalled = true; 30 } 31 32 protected void onAllReferencesReleasedFromContainer() { 33 mOnAllReferencesReleasedFromContainerCalled = true; 34 } 35 36 public boolean isOnAllReferencesReleasedCalled() { 37 return mOnAllReferencesReleasedCalled; 38 } 39 40 public boolean isOnAllReferencesReleasedFromContainerCalled() { 41 return mOnAllReferencesReleasedFromContainerCalled; 42 } 43 } 44 45 public void testAcquireReference() { 46 MockSQLiteClosable closable = new MockSQLiteClosable(); 47 48 closable.acquireReference(); 49 closable.releaseReference(); 50 51 assertFalse(closable.isOnAllReferencesReleasedCalled()); 52 closable.releaseReference(); 53 // the reference count is 0 now. 54 assertTrue(closable.isOnAllReferencesReleasedCalled()); 55 56 try { 57 closable.acquireReference(); 58 fail("should throw IllegalStateException."); 59 } catch (IllegalStateException e) { 60 } 61 } 62 63 public void testReleaseReferenceFromContainer() { 64 MockSQLiteClosable closable = new MockSQLiteClosable(); 65 66 closable.acquireReference(); 67 closable.releaseReferenceFromContainer(); 68 69 // the reference count is 1 now. 70 assertFalse(closable.isOnAllReferencesReleasedFromContainerCalled()); 71 closable.releaseReferenceFromContainer(); 72 // the reference count is 0 now. 73 assertTrue(closable.isOnAllReferencesReleasedFromContainerCalled()); 74 } 75 } 76