11600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyar/* 21600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyar * Copyright (C) 2016 The Android Open Source Project 31600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyar * 41600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyar * Licensed under the Apache License, Version 2.0 (the "License"); 51600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyar * you may not use this file except in compliance with the License. 61600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyar * You may obtain a copy of the License at 71600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyar * 81600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyar * http://www.apache.org/licenses/LICENSE-2.0 91600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyar * 101600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyar * Unless required by applicable law or agreed to in writing, software 111600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyar * distributed under the License is distributed on an "AS IS" BASIS, 121600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyar * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 131600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyar * See the License for the specific language governing permissions and 141600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyar * limitations under the License. 151600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyar */ 161600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyar 17ba069d50913c3fb250bb60ec310439db36895337Alan Viverettepackage androidx.room.processor 181600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyar 19ba069d50913c3fb250bb60ec310439db36895337Alan Viveretteimport androidx.annotation.NonNull 20ba069d50913c3fb250bb60ec310439db36895337Alan Viveretteimport androidx.room.Embedded 21ba069d50913c3fb250bb60ec310439db36895337Alan Viveretteimport androidx.room.testing.TestInvocation 22ba069d50913c3fb250bb60ec310439db36895337Alan Viveretteimport androidx.room.testing.TestProcessor 23ba069d50913c3fb250bb60ec310439db36895337Alan Viveretteimport androidx.room.vo.Entity 241600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyarimport com.google.auto.common.MoreElements 251600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyarimport com.google.common.truth.Truth 261600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyarimport com.google.testing.compile.CompileTester 271600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyarimport com.google.testing.compile.JavaFileObjects 28dc18ce63fe07921b1080e48d3e597e2b5240d17aYigit Boyarimport com.google.testing.compile.JavaSourcesSubjectFactory 29dc18ce63fe07921b1080e48d3e597e2b5240d17aYigit Boyarimport javax.tools.JavaFileObject 301600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyar 311600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyarabstract class BaseEntityParserTest { 321600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyar companion object { 331600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyar const val ENTITY_PREFIX = """ 341600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyar package foo.bar; 35ba069d50913c3fb250bb60ec310439db36895337Alan Viverette import androidx.room.*; 36ba069d50913c3fb250bb60ec310439db36895337Alan Viverette import androidx.annotation.NonNull; 370fc66ddc60bdc71d5466bb1db1a218e5a3d9c1fcYigit Boyar @Entity%s 38dc18ce63fe07921b1080e48d3e597e2b5240d17aYigit Boyar public class MyEntity %s { 391600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyar """ 401600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyar const val ENTITY_SUFFIX = "}" 411600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyar } 421600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyar 430fc66ddc60bdc71d5466bb1db1a218e5a3d9c1fcYigit Boyar fun singleEntity(input: String, attributes: Map<String, String> = mapOf(), 446f1f5567abe765d30fda9c8fedce5617ecdeda9cAurimas Liutikas baseClass: String = "", 456f1f5567abe765d30fda9c8fedce5617ecdeda9cAurimas Liutikas jfos: List<JavaFileObject> = emptyList(), 46c678b3a4eebc102a1a3b5923c5e07478c0eecae3Yigit Boyar handler: (Entity, TestInvocation) -> Unit): CompileTester { 476f1f5567abe765d30fda9c8fedce5617ecdeda9cAurimas Liutikas val attributesReplacement: String 480fc66ddc60bdc71d5466bb1db1a218e5a3d9c1fcYigit Boyar if (attributes.isEmpty()) { 490fc66ddc60bdc71d5466bb1db1a218e5a3d9c1fcYigit Boyar attributesReplacement = "" 500fc66ddc60bdc71d5466bb1db1a218e5a3d9c1fcYigit Boyar } else { 510fc66ddc60bdc71d5466bb1db1a218e5a3d9c1fcYigit Boyar attributesReplacement = "(" + 52c21394d1dcb24518061aabde879baff891a426e3Sergey Vasilinets attributes.entries.joinToString(",") { "${it.key} = ${it.value}" } + 530fc66ddc60bdc71d5466bb1db1a218e5a3d9c1fcYigit Boyar ")".trimIndent() 540fc66ddc60bdc71d5466bb1db1a218e5a3d9c1fcYigit Boyar } 556f1f5567abe765d30fda9c8fedce5617ecdeda9cAurimas Liutikas val baseClassReplacement: String 56dc18ce63fe07921b1080e48d3e597e2b5240d17aYigit Boyar if (baseClass == "") { 57dc18ce63fe07921b1080e48d3e597e2b5240d17aYigit Boyar baseClassReplacement = "" 58dc18ce63fe07921b1080e48d3e597e2b5240d17aYigit Boyar } else { 59dc18ce63fe07921b1080e48d3e597e2b5240d17aYigit Boyar baseClassReplacement = " extends $baseClass" 60dc18ce63fe07921b1080e48d3e597e2b5240d17aYigit Boyar } 61dc18ce63fe07921b1080e48d3e597e2b5240d17aYigit Boyar return Truth.assertAbout(JavaSourcesSubjectFactory.javaSources()) 62dc18ce63fe07921b1080e48d3e597e2b5240d17aYigit Boyar .that(jfos + JavaFileObjects.forSourceString("foo.bar.MyEntity", 63dc18ce63fe07921b1080e48d3e597e2b5240d17aYigit Boyar ENTITY_PREFIX.format(attributesReplacement, baseClassReplacement) 64dc18ce63fe07921b1080e48d3e597e2b5240d17aYigit Boyar + input + ENTITY_SUFFIX 651600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyar )) 661600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyar .processedWith(TestProcessor.builder() 67ba069d50913c3fb250bb60ec310439db36895337Alan Viverette .forAnnotations(androidx.room.Entity::class, 68ba069d50913c3fb250bb60ec310439db36895337Alan Viverette androidx.room.PrimaryKey::class, 69ba069d50913c3fb250bb60ec310439db36895337Alan Viverette androidx.room.Ignore::class, 704d4bae3f216e55f824d7d7fbfe2f8861906ee3e2Yigit Boyar Embedded::class, 71ba069d50913c3fb250bb60ec310439db36895337Alan Viverette androidx.room.ColumnInfo::class, 72540e3498175652abe452d8e0ed1c252e718ddf5aFlorina Muntenescu NonNull::class) 731600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyar .nextRunHandler { invocation -> 741600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyar val entity = invocation.roundEnv 751600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyar .getElementsAnnotatedWith( 76ba069d50913c3fb250bb60ec310439db36895337Alan Viverette androidx.room.Entity::class.java) 77dc18ce63fe07921b1080e48d3e597e2b5240d17aYigit Boyar .first { it.toString() == "foo.bar.MyEntity" } 78aa82fce1d73394bdc7f4c2510cf94a3572032b24Yigit Boyar val parser = EntityProcessor(invocation.context, 79aa82fce1d73394bdc7f4c2510cf94a3572032b24Yigit Boyar MoreElements.asType(entity)) 80aa82fce1d73394bdc7f4c2510cf94a3572032b24Yigit Boyar val parsedQuery = parser.process() 811600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyar handler(parsedQuery, invocation) 821600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyar true 831600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyar } 841600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyar .build()) 851600cc11df868b62b6ae3995d94a3ec0b86559adYigit Boyar } 860fc66ddc60bdc71d5466bb1db1a218e5a3d9c1fcYigit Boyar} 87