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.writer 18 19import COMMON 20import androidx.room.ext.RoomTypeNames 21import androidx.room.processor.DaoProcessor 22import androidx.room.testing.TestProcessor 23import com.google.auto.common.MoreElements 24import com.google.auto.common.MoreTypes 25import com.google.common.truth.Truth 26import com.google.testing.compile.CompileTester 27import com.google.testing.compile.JavaSourcesSubjectFactory 28import createVerifierFromEntities 29import loadJavaCode 30import org.junit.Test 31import org.junit.runner.RunWith 32import org.junit.runners.JUnit4 33import javax.tools.JavaFileObject 34 35@RunWith(JUnit4::class) 36class DaoWriterTest { 37 @Test 38 fun complexDao() { 39 singleDao( 40 loadJavaCode("databasewriter/input/ComplexDatabase.java", 41 "foo.bar.ComplexDatabase"), 42 loadJavaCode("daoWriter/input/ComplexDao.java", "foo.bar.ComplexDao") 43 ).compilesWithoutError().and().generatesSources( 44 loadJavaCode("daoWriter/output/ComplexDao.java", "foo.bar.ComplexDao_Impl") 45 ) 46 } 47 48 @Test 49 fun writerDao() { 50 singleDao( 51 loadJavaCode("daoWriter/input/WriterDao.java", "foo.bar.WriterDao") 52 ).compilesWithoutError().and().generatesSources( 53 loadJavaCode("daoWriter/output/WriterDao.java", "foo.bar.WriterDao_Impl") 54 ) 55 } 56 57 @Test 58 fun deletionDao() { 59 singleDao( 60 loadJavaCode("daoWriter/input/DeletionDao.java", "foo.bar.DeletionDao") 61 ).compilesWithoutError().and().generatesSources( 62 loadJavaCode("daoWriter/output/DeletionDao.java", "foo.bar.DeletionDao_Impl") 63 ) 64 } 65 66 @Test 67 fun updateDao() { 68 singleDao( 69 loadJavaCode("daoWriter/input/UpdateDao.java", "foo.bar.UpdateDao") 70 ).compilesWithoutError().and().generatesSources( 71 loadJavaCode("daoWriter/output/UpdateDao.java", "foo.bar.UpdateDao_Impl") 72 ) 73 } 74 75 fun singleDao(vararg jfo: JavaFileObject): CompileTester { 76 return Truth.assertAbout(JavaSourcesSubjectFactory.javaSources()) 77 .that(jfo.toList() + COMMON.USER + COMMON.MULTI_PKEY_ENTITY + COMMON.BOOK + 78 COMMON.LIVE_DATA + COMMON.COMPUTABLE_LIVE_DATA) 79 .processedWith(TestProcessor.builder() 80 .forAnnotations(androidx.room.Dao::class) 81 .nextRunHandler { invocation -> 82 val dao = invocation.roundEnv 83 .getElementsAnnotatedWith( 84 androidx.room.Dao::class.java) 85 .first() 86 val db = invocation.roundEnv 87 .getElementsAnnotatedWith( 88 androidx.room.Database::class.java) 89 .firstOrNull() 90 val dbType = MoreTypes.asDeclared(if (db != null) { 91 db.asType() 92 } else { 93 invocation.context.processingEnv.elementUtils 94 .getTypeElement(RoomTypeNames.ROOM_DB.toString()).asType() 95 }) 96 val parser = DaoProcessor( 97 baseContext = invocation.context, 98 element = MoreElements.asType(dao), 99 dbType = dbType, 100 dbVerifier = createVerifierFromEntities(invocation)) 101 val parsedDao = parser.process() 102 DaoWriter(parsedDao, invocation.processingEnv) 103 .write(invocation.processingEnv) 104 true 105 } 106 .build()) 107 } 108} 109