1 /* <lambda>null2 * 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 17 package androidx.room.processor 18 19 import androidx.room.compiler.processing.XTypeElement 20 import androidx.room.compiler.processing.util.Source 21 import androidx.room.compiler.processing.util.XTestInvocation 22 import androidx.room.compiler.processing.util.runProcessorTest 23 import androidx.room.testing.context 24 import androidx.room.vo.Entity 25 import java.io.File 26 27 abstract class BaseEntityParserTest { 28 companion object { 29 const val ENTITY_PREFIX = 30 """ 31 package foo.bar; 32 import androidx.room.*; 33 import androidx.annotation.NonNull; 34 import java.util.*; 35 @Entity%s 36 public class MyEntity %s { 37 """ 38 const val ENTITY_SUFFIX = "}" 39 } 40 41 fun singleEntity( 42 input: String, 43 attributes: Map<String, String> = mapOf(), 44 baseClass: String = "", 45 sources: List<Source> = emptyList(), 46 classpathFiles: List<File> = emptyList(), 47 handler: (Entity, XTestInvocation) -> Unit 48 ) { 49 val attributesReplacement: String 50 if (attributes.isEmpty()) { 51 attributesReplacement = "" 52 } else { 53 attributesReplacement = 54 "(" + 55 attributes.entries.joinToString(",") { "${it.key} = ${it.value}" } + 56 ")".trimIndent() 57 } 58 val baseClassReplacement: String 59 if (baseClass == "") { 60 baseClassReplacement = "" 61 } else { 62 baseClassReplacement = " extends $baseClass" 63 } 64 runProcessorTest( 65 sources = 66 sources + 67 Source.java( 68 qName = "foo.bar.MyEntity", 69 code = 70 ENTITY_PREFIX.format(attributesReplacement, baseClassReplacement) + 71 input + 72 ENTITY_SUFFIX 73 ), 74 options = mapOf(Context.BooleanProcessorOptions.GENERATE_KOTLIN.argName to "false"), 75 kotlincArguments = listOf("-jvm-target=11"), 76 classpath = classpathFiles 77 ) { invocation -> 78 val entity = 79 invocation.roundEnv 80 .getElementsAnnotatedWith(androidx.room.Entity::class.qualifiedName!!) 81 .filterIsInstance<XTypeElement>() 82 .first { it.qualifiedName == "foo.bar.MyEntity" } 83 val parser = TableEntityProcessor(invocation.context, entity) 84 val parsedQuery = parser.process() 85 handler(parsedQuery, invocation) 86 } 87 } 88 } 89