1/*
2 * Copyright (C) 2016 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
17package androidx.room.processor
18
19import androidx.annotation.NonNull
20import androidx.room.Embedded
21import androidx.room.testing.TestInvocation
22import androidx.room.testing.TestProcessor
23import androidx.room.vo.Entity
24import com.google.auto.common.MoreElements
25import com.google.common.truth.Truth
26import com.google.testing.compile.CompileTester
27import com.google.testing.compile.JavaFileObjects
28import com.google.testing.compile.JavaSourcesSubjectFactory
29import javax.tools.JavaFileObject
30
31abstract class BaseEntityParserTest {
32    companion object {
33        const val ENTITY_PREFIX = """
34            package foo.bar;
35            import androidx.room.*;
36            import androidx.annotation.NonNull;
37            @Entity%s
38            public class MyEntity %s {
39            """
40        const val ENTITY_SUFFIX = "}"
41    }
42
43    fun singleEntity(input: String, attributes: Map<String, String> = mapOf(),
44                     baseClass: String = "",
45                     jfos: List<JavaFileObject> = emptyList(),
46                     handler: (Entity, TestInvocation) -> Unit): CompileTester {
47        val attributesReplacement: String
48        if (attributes.isEmpty()) {
49            attributesReplacement = ""
50        } else {
51            attributesReplacement = "(" +
52                    attributes.entries.joinToString(",") { "${it.key} = ${it.value}" } +
53                    ")".trimIndent()
54        }
55        val baseClassReplacement: String
56        if (baseClass == "") {
57            baseClassReplacement = ""
58        } else {
59            baseClassReplacement = " extends $baseClass"
60        }
61        return Truth.assertAbout(JavaSourcesSubjectFactory.javaSources())
62                .that(jfos + JavaFileObjects.forSourceString("foo.bar.MyEntity",
63                        ENTITY_PREFIX.format(attributesReplacement, baseClassReplacement)
64                                + input + ENTITY_SUFFIX
65                ))
66                .processedWith(TestProcessor.builder()
67                        .forAnnotations(androidx.room.Entity::class,
68                                androidx.room.PrimaryKey::class,
69                                androidx.room.Ignore::class,
70                                Embedded::class,
71                                androidx.room.ColumnInfo::class,
72                                NonNull::class)
73                        .nextRunHandler { invocation ->
74                            val entity = invocation.roundEnv
75                                    .getElementsAnnotatedWith(
76                                            androidx.room.Entity::class.java)
77                                    .first { it.toString() == "foo.bar.MyEntity" }
78                            val parser = EntityProcessor(invocation.context,
79                                    MoreElements.asType(entity))
80                            val parsedQuery = parser.process()
81                            handler(parsedQuery, invocation)
82                            true
83                        }
84                        .build())
85    }
86}
87