1b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar/*
2b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar * Copyright (C) 2017 The Android Open Source Project
3b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar *
4b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar * Licensed under the Apache License, Version 2.0 (the "License");
5b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar * you may not use this file except in compliance with the License.
6b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar * You may obtain a copy of the License at
7b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar *
8b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar *      http://www.apache.org/licenses/LICENSE-2.0
9b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar *
10b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar * Unless required by applicable law or agreed to in writing, software
11b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar * distributed under the License is distributed on an "AS IS" BASIS,
12b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar * See the License for the specific language governing permissions and
14b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar * limitations under the License.
15b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar */
16b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar
17bdc4c86d3dff74f6634a38e2f7b316b0e823a2c8Alan Viverettepackage androidx.room;
18b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar
19b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyarimport static org.hamcrest.CoreMatchers.is;
20b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyarimport static org.hamcrest.CoreMatchers.not;
21b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyarimport static org.hamcrest.CoreMatchers.sameInstance;
22b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyarimport static org.hamcrest.MatcherAssert.assertThat;
23b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyarimport static org.mockito.Mockito.mock;
24b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyarimport static org.mockito.Mockito.verify;
25b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar
26bdc4c86d3dff74f6634a38e2f7b316b0e823a2c8Alan Viveretteimport androidx.sqlite.db.SupportSQLiteProgram;
27b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar
28b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyarimport org.junit.Before;
29b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyarimport org.junit.Test;
30b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyarimport org.junit.runner.RunWith;
31b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyarimport org.junit.runners.JUnit4;
32b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar
33b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyarimport java.util.ArrayList;
34b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyarimport java.util.Iterator;
35b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyarimport java.util.List;
36b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar
37b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar@RunWith(JUnit4.class)
38b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyarpublic class RoomSQLiteQueryTest {
39b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar    @Before
40b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar    public void clear() {
41b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        RoomSQLiteQuery.sQueryPool.clear();
42b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar    }
43b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar
44b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar    @Test
45b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar    public void acquireBasic() {
46b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        RoomSQLiteQuery query = RoomSQLiteQuery.acquire("abc", 3);
47b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        assertThat(query.getSql(), is("abc"));
48b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        assertThat(query.mArgCount, is(3));
49b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        assertThat(query.mBlobBindings.length, is(4));
50b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        assertThat(query.mLongBindings.length, is(4));
51b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        assertThat(query.mStringBindings.length, is(4));
52b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        assertThat(query.mDoubleBindings.length, is(4));
53b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar    }
54b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar
55b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar    @Test
56b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar    public void acquireSameSizeAgain() {
57b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        RoomSQLiteQuery query = RoomSQLiteQuery.acquire("abc", 3);
58b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        query.release();
59b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        assertThat(RoomSQLiteQuery.acquire("blah", 3), sameInstance(query));
60b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar    }
61b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar
62b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar    @Test
63b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar    public void acquireSameSizeWithoutRelease() {
64b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        RoomSQLiteQuery query = RoomSQLiteQuery.acquire("abc", 3);
65b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        assertThat(RoomSQLiteQuery.acquire("fda", 3), not(sameInstance(query)));
66b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar    }
67b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar
68b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar    @Test
69b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar    public void bindings() {
70b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        RoomSQLiteQuery query = RoomSQLiteQuery.acquire("abc", 6);
71b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        byte[] myBlob = new byte[3];
72b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        long myLong = 3L;
73b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        double myDouble = 7.0;
74b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        String myString = "ss";
75b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        query.bindBlob(1, myBlob);
76b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        query.bindLong(2, myLong);
77b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        query.bindNull(3);
78b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        query.bindDouble(4, myDouble);
79b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        query.bindString(5, myString);
80b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        query.bindNull(6);
81b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        SupportSQLiteProgram program = mock(SupportSQLiteProgram.class);
82b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        query.bindTo(program);
83b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar
84b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        verify(program).bindBlob(1, myBlob);
85b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        verify(program).bindLong(2, myLong);
86b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        verify(program).bindNull(3);
87b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        verify(program).bindDouble(4, myDouble);
88b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        verify(program).bindString(5, myString);
89b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        verify(program).bindNull(6);
90b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar    }
91b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar
92b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar    @Test
93b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar    public void dontKeepSameSizeTwice() {
94b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        RoomSQLiteQuery query1 = RoomSQLiteQuery.acquire("abc", 3);
95b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        RoomSQLiteQuery query2 = RoomSQLiteQuery.acquire("zx", 3);
96b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        RoomSQLiteQuery query3 = RoomSQLiteQuery.acquire("qw", 0);
97b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar
98b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        query1.release();
99b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        query2.release();
100b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        assertThat(RoomSQLiteQuery.sQueryPool.size(), is(1));
101b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar
102b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        query3.release();
103b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        assertThat(RoomSQLiteQuery.sQueryPool.size(), is(2));
104b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar    }
105b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar
106b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar    @Test
107b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar    public void returnExistingForSmallerSize() {
108b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        RoomSQLiteQuery query = RoomSQLiteQuery.acquire("abc", 3);
109b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        query.release();
110b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        assertThat(RoomSQLiteQuery.acquire("dsa", 2), sameInstance(query));
111b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar    }
112b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar
113b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar    @Test
114b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar    public void returnNewForBigger() {
115b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        RoomSQLiteQuery query = RoomSQLiteQuery.acquire("abc", 3);
116b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        query.release();
117b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        assertThat(RoomSQLiteQuery.acquire("dsa", 4), not(sameInstance(query)));
118b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar    }
119b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar
120b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar    @Test
121b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar    public void pruneCache() {
122b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        for (int i = 0; i < RoomSQLiteQuery.POOL_LIMIT; i++) {
123b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar            RoomSQLiteQuery.acquire("dsdsa", i).release();
124b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        }
125b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        pruneCacheTest();
126b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar    }
127b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar
128b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar    @Test
129b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar    public void pruneCacheReverseInsertion() {
130b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        List<RoomSQLiteQuery> queries = new ArrayList<>();
131b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        for (int i = RoomSQLiteQuery.POOL_LIMIT - 1; i >= 0; i--) {
132b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar            queries.add(RoomSQLiteQuery.acquire("dsdsa", i));
133b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        }
134b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        for (RoomSQLiteQuery query : queries) {
135b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar            query.release();
136b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        }
137b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        pruneCacheTest();
138b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar    }
139b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar
140b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar    private void pruneCacheTest() {
141b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        assertThat(RoomSQLiteQuery.sQueryPool.size(), is(RoomSQLiteQuery.POOL_LIMIT));
142b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        RoomSQLiteQuery.acquire("dsadsa", RoomSQLiteQuery.POOL_LIMIT + 1).release();
143b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        assertThat(RoomSQLiteQuery.sQueryPool.size(), is(RoomSQLiteQuery.DESIRED_POOL_SIZE));
144b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        Iterator<RoomSQLiteQuery> itr = RoomSQLiteQuery.sQueryPool.values().iterator();
145b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        for (int i = 0; i < RoomSQLiteQuery.DESIRED_POOL_SIZE; i++) {
146b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar            assertThat(itr.next().mCapacity, is(i));
147b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar        }
148b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar    }
149b030dcb5b7a62854c0bfe85bf04eaf60caeb83bbYigit Boyar}
150