summaryrefslogtreecommitdiff
path: root/compiler/src/test/java/android/databinding/tool/reflection/ClassFinderCacheTest.kt
blob: b6b4df6235ace40250337c9b0bd5c68348124ff0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.databinding.tool.reflection

import org.hamcrest.CoreMatchers.`is`
import org.hamcrest.CoreMatchers.nullValue
import org.hamcrest.MatcherAssert.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.mockito.Mockito

@RunWith(JUnit4::class)
class ClassFinderCacheTest {
    private val loggingFinder = LoggingFinder()
    @Test
    fun simple_fail() {
        val cache = ClassFinderCache(loggingFinder::doFind)
        val result = cache.find("foo", null)
        assertThat(loggingFinder.calls, `is`(
                listOf(
                        DoFindCall("foo", null)
                )
        ))
        assertThat(result, nullValue())
    }

    @Test
    fun simple_succeed() {
        val cache = ClassFinderCache(loggingFinder::doFind)
        val fake = loggingFinder.addClassFor("foo")
        val result = cache.find("foo", null)
        assertThat(loggingFinder.calls, `is`(
                listOf(
                        DoFindCall("foo", null)
                )
        ))
        assertThat(result, `is`(fake))
        cache.find("foo", null)
        // should return from cache
        assertThat(loggingFinder.calls, `is`(
                listOf(
                        DoFindCall("foo", null)
                )
        ))
    }

    @Test
    fun import_different() {
        val cache = ClassFinderCache(loggingFinder::doFind)
        val fake = loggingFinder.addClassFor("foo")
        val result = cache.find("foo", null)
        assertThat(loggingFinder.calls, `is`(
                listOf(
                        DoFindCall("foo", null)
                )
        ))
        assertThat(result, `is`(fake))
        val result2 = cache.find("foo", ImportBag.EMPTY)
        assertThat(loggingFinder.calls, `is`(
                listOf(
                        DoFindCall("foo", null),
                        DoFindCall("foo", ImportBag.EMPTY.toImmutable())
                )
        ))
        assertThat(result2, `is`(fake))
    }

    @Test
    fun import_mutated() {
        val cache = ClassFinderCache(loggingFinder::doFind)
        val fake = loggingFinder.addClassFor("foo")
        val imports = MutableImportBag().also {
            it.put("baz", "foo.baz")
        }
        val imports1 = imports.toImmutable()
        val result = cache.find("foo", imports)
        assertThat(loggingFinder.calls, `is`(
                listOf(
                        DoFindCall("foo", imports1)
                )
        ))
        assertThat(result, `is`(fake))

        imports.put("bazar", "foo.bazar")
        val imports2 = imports.toImmutable()
        val result2 = cache.find("foo", imports2)
        assertThat(loggingFinder.calls, `is`(
                listOf(
                        DoFindCall("foo", imports1),
                        DoFindCall("foo", imports2)
                )
        ))
        assertThat(result2, `is`(fake))

        val result3 = cache.find("foo", MutableImportBag().also {
            it.put("baz", "foo.baz")
        })
        // should come from cache
        assertThat(loggingFinder.calls, `is`(
                listOf(
                        DoFindCall("foo", imports1),
                        DoFindCall("foo", imports2)
                )
        ))
        assertThat(result3, `is`(fake))


    }

    class LoggingFinder {
        val calls = arrayListOf<DoFindCall>()
        private val classes = mutableMapOf<String, ModelClass>()
        fun doFind(className: String, imports: ImportBag?): ModelClass? {
            calls.add(DoFindCall(
                    className = className,
                    imports = imports?.toImmutable()
            ))
            return classes[className]
        }

        fun addClassFor(className: String): ModelClass {
            val mock = Mockito.mock(ModelClass::class.java)
            classes[className] = mock
            return mock
        }
    }

    data class DoFindCall(val className: String, val imports: ImmutableImportBag?)
}