Alan Viverette | ba069d5 | 2018-03-08 18:02:39 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Yigit Boyar | 91d6056 | 2020-07-01 14:34:43 -0700 | [diff] [blame] | 19 | import COMMON |
Alan Viverette | ba069d5 | 2018-03-08 18:02:39 -0500 | [diff] [blame] | 20 | import androidx.room.Dao |
| 21 | import androidx.room.Transaction |
Daniel Santiago Rivera | 2514b02 | 2024-04-15 17:30:42 -0400 | [diff] [blame^] | 22 | import androidx.room.compiler.codegen.CodeLanguage |
Eli Hart | 96d8d4d | 2021-04-06 19:06:18 +0000 | [diff] [blame] | 23 | import androidx.room.compiler.processing.XTypeElement |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 24 | import androidx.room.compiler.processing.util.Source |
| 25 | import androidx.room.compiler.processing.util.XTestInvocation |
| 26 | import androidx.room.compiler.processing.util.runProcessorTest |
elifbilgin | 1f76654 | 2022-11-21 17:21:44 -0800 | [diff] [blame] | 27 | import androidx.room.ext.GuavaUtilConcurrentTypeNames.LISTENABLE_FUTURE |
| 28 | import androidx.room.ext.KotlinTypeNames.FLOW |
| 29 | import androidx.room.ext.LifecyclesTypeNames.COMPUTABLE_LIVE_DATA |
| 30 | import androidx.room.ext.LifecyclesTypeNames.LIVE_DATA |
| 31 | import androidx.room.ext.ReactiveStreamsTypeNames.PUBLISHER |
| 32 | import androidx.room.ext.RxJava2TypeNames |
| 33 | import androidx.room.ext.RxJava3TypeNames |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 34 | import androidx.room.testing.context |
Alan Viverette | ba069d5 | 2018-03-08 18:02:39 -0500 | [diff] [blame] | 35 | import androidx.room.vo.TransactionMethod |
Alan Viverette | ba069d5 | 2018-03-08 18:02:39 -0500 | [diff] [blame] | 36 | import org.hamcrest.CoreMatchers.`is` |
| 37 | import org.hamcrest.MatcherAssert.assertThat |
| 38 | import org.junit.Test |
| 39 | import org.junit.runner.RunWith |
| 40 | import org.junit.runners.JUnit4 |
| 41 | |
| 42 | @Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN") |
| 43 | @RunWith(JUnit4::class) |
| 44 | class TransactionMethodProcessorTest { |
| 45 | |
| 46 | companion object { |
| 47 | const val DAO_PREFIX = """ |
| 48 | package foo.bar; |
| 49 | import androidx.room.*; |
| 50 | import java.util.*; |
Daniel Santiago Rivera | 9b78b58 | 2019-01-15 13:10:58 -0800 | [diff] [blame] | 51 | import androidx.lifecycle.*; |
Daniel Santiago Rivera | 9b78b58 | 2019-01-15 13:10:58 -0800 | [diff] [blame] | 52 | import com.google.common.util.concurrent.*; |
mzgreen | 76c0bb2 | 2021-12-08 15:27:37 +0100 | [diff] [blame] | 53 | import org.reactivestreams.*; |
Alan Viverette | ba069d5 | 2018-03-08 18:02:39 -0500 | [diff] [blame] | 54 | @Dao |
| 55 | abstract class MyClass { |
| 56 | """ |
| 57 | const val DAO_SUFFIX = "}" |
| 58 | } |
| 59 | |
| 60 | @Test |
| 61 | fun simple() { |
| 62 | singleTransactionMethod( |
Jeff Gaston | 63c2310 | 2020-09-18 12:53:59 -0400 | [diff] [blame] | 63 | """ |
Alan Viverette | ba069d5 | 2018-03-08 18:02:39 -0500 | [diff] [blame] | 64 | @Transaction |
| 65 | public String doInTransaction(int param) { return null; } |
Jeff Gaston | 63c2310 | 2020-09-18 12:53:59 -0400 | [diff] [blame] | 66 | """ |
| 67 | ) { transaction, _ -> |
Yigit Boyar | dd1f1d0 | 2021-11-12 23:19:09 -0800 | [diff] [blame] | 68 | assertThat(transaction.jvmName, `is`("doInTransaction")) |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 69 | } |
Alan Viverette | ba069d5 | 2018-03-08 18:02:39 -0500 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @Test |
| 73 | fun modifier_private() { |
| 74 | singleTransactionMethod( |
Jeff Gaston | 63c2310 | 2020-09-18 12:53:59 -0400 | [diff] [blame] | 75 | """ |
Alan Viverette | ba069d5 | 2018-03-08 18:02:39 -0500 | [diff] [blame] | 76 | @Transaction |
| 77 | private String doInTransaction(int param) { return null; } |
Jeff Gaston | 63c2310 | 2020-09-18 12:53:59 -0400 | [diff] [blame] | 78 | """ |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 79 | ) { transaction, invocation -> |
Yigit Boyar | dd1f1d0 | 2021-11-12 23:19:09 -0800 | [diff] [blame] | 80 | assertThat(transaction.jvmName, `is`("doInTransaction")) |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 81 | invocation.assertCompilationResult { |
| 82 | hasErrorContaining( |
| 83 | ProcessorErrors.TRANSACTION_METHOD_MODIFIERS |
| 84 | ) |
| 85 | } |
| 86 | } |
Alan Viverette | ba069d5 | 2018-03-08 18:02:39 -0500 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Test |
| 90 | fun modifier_final() { |
| 91 | singleTransactionMethod( |
Jeff Gaston | 63c2310 | 2020-09-18 12:53:59 -0400 | [diff] [blame] | 92 | """ |
Alan Viverette | ba069d5 | 2018-03-08 18:02:39 -0500 | [diff] [blame] | 93 | @Transaction |
| 94 | public final String doInTransaction(int param) { return null; } |
Jeff Gaston | 63c2310 | 2020-09-18 12:53:59 -0400 | [diff] [blame] | 95 | """ |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 96 | ) { transaction, invocation -> |
Yigit Boyar | dd1f1d0 | 2021-11-12 23:19:09 -0800 | [diff] [blame] | 97 | assertThat(transaction.jvmName, `is`("doInTransaction")) |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 98 | invocation.assertCompilationResult { |
| 99 | hasErrorContaining(ProcessorErrors.TRANSACTION_METHOD_MODIFIERS) |
| 100 | } |
| 101 | } |
Alan Viverette | ba069d5 | 2018-03-08 18:02:39 -0500 | [diff] [blame] | 102 | } |
| 103 | |
Daniel Santiago Rivera | 9b78b58 | 2019-01-15 13:10:58 -0800 | [diff] [blame] | 104 | @Test |
Elif Bilgin | 5f3b474 | 2021-06-07 11:49:20 -0700 | [diff] [blame] | 105 | fun deferredReturnType_flow() { |
| 106 | singleTransactionMethod( |
| 107 | """ |
| 108 | @Transaction |
| 109 | public kotlinx.coroutines.flow.Flow<String> doInTransaction(int param) { |
| 110 | return null; |
| 111 | } |
| 112 | """ |
| 113 | ) { transaction, invocation -> |
Yigit Boyar | dd1f1d0 | 2021-11-12 23:19:09 -0800 | [diff] [blame] | 114 | assertThat(transaction.jvmName, `is`("doInTransaction")) |
Elif Bilgin | 5f3b474 | 2021-06-07 11:49:20 -0700 | [diff] [blame] | 115 | invocation.assertCompilationResult { |
| 116 | hasErrorContaining( |
| 117 | ProcessorErrors.transactionMethodAsync( |
Daniel Santiago Rivera | 2514b02 | 2024-04-15 17:30:42 -0400 | [diff] [blame^] | 118 | FLOW.rawTypeName.toString(CodeLanguage.JAVA) |
Elif Bilgin | 5f3b474 | 2021-06-07 11:49:20 -0700 | [diff] [blame] | 119 | ) |
| 120 | ) |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | @Test |
Daniel Santiago Rivera | 9b78b58 | 2019-01-15 13:10:58 -0800 | [diff] [blame] | 126 | fun deferredReturnType_liveData() { |
| 127 | singleTransactionMethod( |
| 128 | """ |
| 129 | @Transaction |
| 130 | public LiveData<String> doInTransaction(int param) { return null; } |
Jeff Gaston | 63c2310 | 2020-09-18 12:53:59 -0400 | [diff] [blame] | 131 | """ |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 132 | ) { transaction, invocation -> |
Yigit Boyar | dd1f1d0 | 2021-11-12 23:19:09 -0800 | [diff] [blame] | 133 | assertThat(transaction.jvmName, `is`("doInTransaction")) |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 134 | invocation.assertCompilationResult { |
| 135 | hasErrorContaining( |
| 136 | ProcessorErrors.transactionMethodAsync( |
Daniel Santiago Rivera | 2514b02 | 2024-04-15 17:30:42 -0400 | [diff] [blame^] | 137 | LIVE_DATA.rawTypeName.toString(CodeLanguage.JAVA) |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 138 | ) |
Daniel Santiago Rivera | 9b78b58 | 2019-01-15 13:10:58 -0800 | [diff] [blame] | 139 | ) |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
Daniel Santiago Rivera | 9b78b58 | 2019-01-15 13:10:58 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Test |
mzgreen | 76c0bb2 | 2021-12-08 15:27:37 +0100 | [diff] [blame] | 145 | fun deferredReturnType_computableLiveData() { |
| 146 | singleTransactionMethod( |
| 147 | """ |
| 148 | @Transaction |
| 149 | public ComputableLiveData<String> doInTransaction(int param) { return null; } |
| 150 | """ |
| 151 | ) { transaction, invocation -> |
| 152 | assertThat(transaction.jvmName, `is`("doInTransaction")) |
| 153 | invocation.assertCompilationResult { |
| 154 | hasErrorContaining( |
| 155 | ProcessorErrors.transactionMethodAsync( |
Daniel Santiago Rivera | 2514b02 | 2024-04-15 17:30:42 -0400 | [diff] [blame^] | 156 | COMPUTABLE_LIVE_DATA.rawTypeName.toString(CodeLanguage.JAVA) |
mzgreen | 76c0bb2 | 2021-12-08 15:27:37 +0100 | [diff] [blame] | 157 | ) |
| 158 | ) |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | @Test |
Daniel Santiago Rivera | 2056c16 | 2020-06-03 17:12:43 -0700 | [diff] [blame] | 164 | fun deferredReturnType_rx2_flowable() { |
Daniel Santiago Rivera | 9b78b58 | 2019-01-15 13:10:58 -0800 | [diff] [blame] | 165 | singleTransactionMethod( |
| 166 | """ |
| 167 | @Transaction |
Daniel Santiago Rivera | 2056c16 | 2020-06-03 17:12:43 -0700 | [diff] [blame] | 168 | public io.reactivex.Flowable<String> doInTransaction(int param) { return null; } |
Jeff Gaston | 63c2310 | 2020-09-18 12:53:59 -0400 | [diff] [blame] | 169 | """ |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 170 | ) { transaction, invocation -> |
Yigit Boyar | dd1f1d0 | 2021-11-12 23:19:09 -0800 | [diff] [blame] | 171 | assertThat(transaction.jvmName, `is`("doInTransaction")) |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 172 | invocation.assertCompilationResult { |
| 173 | hasErrorContaining( |
| 174 | ProcessorErrors.transactionMethodAsync( |
Daniel Santiago Rivera | 2514b02 | 2024-04-15 17:30:42 -0400 | [diff] [blame^] | 175 | RxJava2TypeNames.FLOWABLE.rawTypeName.toString(CodeLanguage.JAVA) |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 176 | ) |
Daniel Santiago Rivera | 9b78b58 | 2019-01-15 13:10:58 -0800 | [diff] [blame] | 177 | ) |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 178 | } |
| 179 | } |
Daniel Santiago Rivera | 9b78b58 | 2019-01-15 13:10:58 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | @Test |
Daniel Santiago Rivera | 2056c16 | 2020-06-03 17:12:43 -0700 | [diff] [blame] | 183 | fun deferredReturnType_rx3_flowable() { |
Daniel Santiago Rivera | 9b78b58 | 2019-01-15 13:10:58 -0800 | [diff] [blame] | 184 | singleTransactionMethod( |
| 185 | """ |
| 186 | @Transaction |
Daniel Santiago Rivera | 2056c16 | 2020-06-03 17:12:43 -0700 | [diff] [blame] | 187 | public io.reactivex.rxjava3.core.Flowable<String> doInTransaction(int param) { |
| 188 | return null; |
| 189 | } |
Jeff Gaston | 63c2310 | 2020-09-18 12:53:59 -0400 | [diff] [blame] | 190 | """ |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 191 | ) { transaction, invocation -> |
Yigit Boyar | dd1f1d0 | 2021-11-12 23:19:09 -0800 | [diff] [blame] | 192 | assertThat(transaction.jvmName, `is`("doInTransaction")) |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 193 | invocation.assertCompilationResult { |
| 194 | hasErrorContaining( |
| 195 | ProcessorErrors.transactionMethodAsync( |
Daniel Santiago Rivera | 2514b02 | 2024-04-15 17:30:42 -0400 | [diff] [blame^] | 196 | RxJava3TypeNames.FLOWABLE.rawTypeName.toString(CodeLanguage.JAVA) |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 197 | ) |
Daniel Santiago Rivera | 2056c16 | 2020-06-03 17:12:43 -0700 | [diff] [blame] | 198 | ) |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 199 | } |
| 200 | } |
Daniel Santiago Rivera | 2056c16 | 2020-06-03 17:12:43 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | @Test |
| 204 | fun deferredReturnType_rx2_completable() { |
| 205 | singleTransactionMethod( |
| 206 | """ |
| 207 | @Transaction |
| 208 | public io.reactivex.Completable doInTransaction(int param) { return null; } |
Jeff Gaston | 63c2310 | 2020-09-18 12:53:59 -0400 | [diff] [blame] | 209 | """ |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 210 | ) { transaction, invocation -> |
Yigit Boyar | dd1f1d0 | 2021-11-12 23:19:09 -0800 | [diff] [blame] | 211 | assertThat(transaction.jvmName, `is`("doInTransaction")) |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 212 | invocation.assertCompilationResult { |
| 213 | hasErrorContaining( |
| 214 | ProcessorErrors.transactionMethodAsync( |
Daniel Santiago Rivera | 2514b02 | 2024-04-15 17:30:42 -0400 | [diff] [blame^] | 215 | RxJava2TypeNames.COMPLETABLE.rawTypeName.toString(CodeLanguage.JAVA) |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 216 | ) |
Daniel Santiago Rivera | 9b78b58 | 2019-01-15 13:10:58 -0800 | [diff] [blame] | 217 | ) |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 218 | } |
| 219 | } |
Daniel Santiago Rivera | 9b78b58 | 2019-01-15 13:10:58 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | @Test |
Daniel Santiago Rivera | 2056c16 | 2020-06-03 17:12:43 -0700 | [diff] [blame] | 223 | fun deferredReturnType_rx3_completable() { |
Daniel Santiago Rivera | 9b78b58 | 2019-01-15 13:10:58 -0800 | [diff] [blame] | 224 | singleTransactionMethod( |
| 225 | """ |
| 226 | @Transaction |
Daniel Santiago Rivera | 2056c16 | 2020-06-03 17:12:43 -0700 | [diff] [blame] | 227 | public io.reactivex.rxjava3.core.Completable doInTransaction(int param) { |
| 228 | return null; |
| 229 | } |
Jeff Gaston | 63c2310 | 2020-09-18 12:53:59 -0400 | [diff] [blame] | 230 | """ |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 231 | ) { transaction, invocation -> |
Yigit Boyar | dd1f1d0 | 2021-11-12 23:19:09 -0800 | [diff] [blame] | 232 | assertThat(transaction.jvmName, `is`("doInTransaction")) |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 233 | invocation.assertCompilationResult { |
| 234 | hasErrorContaining( |
| 235 | ProcessorErrors.transactionMethodAsync( |
Daniel Santiago Rivera | 2514b02 | 2024-04-15 17:30:42 -0400 | [diff] [blame^] | 236 | RxJava3TypeNames.COMPLETABLE.rawTypeName.toString(CodeLanguage.JAVA) |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 237 | ) |
Daniel Santiago Rivera | 2056c16 | 2020-06-03 17:12:43 -0700 | [diff] [blame] | 238 | ) |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 239 | } |
| 240 | } |
Daniel Santiago Rivera | 2056c16 | 2020-06-03 17:12:43 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | @Test |
| 244 | fun deferredReturnType_rx2_single() { |
| 245 | singleTransactionMethod( |
| 246 | """ |
| 247 | @Transaction |
| 248 | public io.reactivex.Single<String> doInTransaction(int param) { return null; } |
Jeff Gaston | 63c2310 | 2020-09-18 12:53:59 -0400 | [diff] [blame] | 249 | """ |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 250 | ) { transaction, invocation -> |
Yigit Boyar | dd1f1d0 | 2021-11-12 23:19:09 -0800 | [diff] [blame] | 251 | assertThat(transaction.jvmName, `is`("doInTransaction")) |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 252 | invocation.assertCompilationResult { |
| 253 | hasErrorContaining( |
| 254 | ProcessorErrors.transactionMethodAsync( |
Daniel Santiago Rivera | 2514b02 | 2024-04-15 17:30:42 -0400 | [diff] [blame^] | 255 | RxJava2TypeNames.SINGLE.rawTypeName.toString(CodeLanguage.JAVA) |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 256 | ) |
Daniel Santiago Rivera | 9b78b58 | 2019-01-15 13:10:58 -0800 | [diff] [blame] | 257 | ) |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 258 | } |
| 259 | } |
Daniel Santiago Rivera | 9b78b58 | 2019-01-15 13:10:58 -0800 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | @Test |
Daniel Santiago Rivera | 2056c16 | 2020-06-03 17:12:43 -0700 | [diff] [blame] | 263 | fun deferredReturnType_rx3_single() { |
| 264 | singleTransactionMethod( |
| 265 | """ |
| 266 | @Transaction |
| 267 | public io.reactivex.rxjava3.core.Single<String> doInTransaction(int param) { |
| 268 | return null; |
| 269 | } |
Jeff Gaston | 63c2310 | 2020-09-18 12:53:59 -0400 | [diff] [blame] | 270 | """ |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 271 | ) { transaction, invocation -> |
Yigit Boyar | dd1f1d0 | 2021-11-12 23:19:09 -0800 | [diff] [blame] | 272 | assertThat(transaction.jvmName, `is`("doInTransaction")) |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 273 | invocation.assertCompilationResult { |
| 274 | hasErrorContaining( |
| 275 | ProcessorErrors.transactionMethodAsync( |
Daniel Santiago Rivera | 2514b02 | 2024-04-15 17:30:42 -0400 | [diff] [blame^] | 276 | RxJava3TypeNames.SINGLE.rawTypeName.toString(CodeLanguage.JAVA) |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 277 | ) |
Daniel Santiago Rivera | 2056c16 | 2020-06-03 17:12:43 -0700 | [diff] [blame] | 278 | ) |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 279 | } |
| 280 | } |
Daniel Santiago Rivera | 2056c16 | 2020-06-03 17:12:43 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | @Test |
Daniel Santiago Rivera | 9b78b58 | 2019-01-15 13:10:58 -0800 | [diff] [blame] | 284 | fun deferredReturnType_listenableFuture() { |
| 285 | singleTransactionMethod( |
| 286 | """ |
| 287 | @Transaction |
| 288 | public ListenableFuture<String> doInTransaction(int param) { return null; } |
Jeff Gaston | 63c2310 | 2020-09-18 12:53:59 -0400 | [diff] [blame] | 289 | """ |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 290 | ) { transaction, invocation -> |
Yigit Boyar | dd1f1d0 | 2021-11-12 23:19:09 -0800 | [diff] [blame] | 291 | assertThat(transaction.jvmName, `is`("doInTransaction")) |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 292 | invocation.assertCompilationResult { |
| 293 | hasErrorContaining( |
| 294 | ProcessorErrors.transactionMethodAsync( |
Daniel Santiago Rivera | 2514b02 | 2024-04-15 17:30:42 -0400 | [diff] [blame^] | 295 | LISTENABLE_FUTURE.rawTypeName.toString(CodeLanguage.JAVA) |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 296 | ) |
Daniel Santiago Rivera | 9b78b58 | 2019-01-15 13:10:58 -0800 | [diff] [blame] | 297 | ) |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 298 | } |
| 299 | } |
Daniel Santiago Rivera | 9b78b58 | 2019-01-15 13:10:58 -0800 | [diff] [blame] | 300 | } |
| 301 | |
mzgreen | 76c0bb2 | 2021-12-08 15:27:37 +0100 | [diff] [blame] | 302 | @Test |
| 303 | fun deferredReturnType_publisher() { |
| 304 | singleTransactionMethod( |
| 305 | """ |
| 306 | @Transaction |
| 307 | public Publisher<String> doInTransaction(int param) { return null; } |
| 308 | """ |
| 309 | ) { transaction, invocation -> |
| 310 | assertThat(transaction.jvmName, `is`("doInTransaction")) |
| 311 | invocation.assertCompilationResult { |
| 312 | hasErrorContaining( |
| 313 | ProcessorErrors.transactionMethodAsync( |
Daniel Santiago Rivera | 2514b02 | 2024-04-15 17:30:42 -0400 | [diff] [blame^] | 314 | PUBLISHER.rawTypeName.toString(CodeLanguage.JAVA) |
mzgreen | 76c0bb2 | 2021-12-08 15:27:37 +0100 | [diff] [blame] | 315 | ) |
| 316 | ) |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
Yigit Boyar | dd1f1d0 | 2021-11-12 23:19:09 -0800 | [diff] [blame] | 321 | private val TransactionMethod.jvmName: String |
| 322 | get() = element.jvmName |
Daniel Santiago Rivera | 633c84b | 2019-02-11 17:13:04 -0800 | [diff] [blame] | 323 | |
Daniel Santiago Rivera | 9b78b58 | 2019-01-15 13:10:58 -0800 | [diff] [blame] | 324 | private fun singleTransactionMethod( |
| 325 | vararg input: String, |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 326 | handler: (TransactionMethod, XTestInvocation) -> Unit |
| 327 | ) { |
| 328 | val inputSource = listOf( |
| 329 | Source.java( |
| 330 | "foo.bar.MyClass", |
| 331 | DAO_PREFIX + input.joinToString("\n") + DAO_SUFFIX |
Jeff Gaston | 63c2310 | 2020-09-18 12:53:59 -0400 | [diff] [blame] | 332 | ) |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 333 | ) |
| 334 | val otherSources = listOf( |
mzgreen | 76c0bb2 | 2021-12-08 15:27:37 +0100 | [diff] [blame] | 335 | COMMON.LIVE_DATA, COMMON.COMPUTABLE_LIVE_DATA, COMMON.RX2_FLOWABLE, COMMON.PUBLISHER, |
| 336 | COMMON.RX2_COMPLETABLE, COMMON.RX2_SINGLE, COMMON.RX3_FLOWABLE, COMMON.RX3_COMPLETABLE, |
Elif Bilgin | 5f3b474 | 2021-06-07 11:49:20 -0700 | [diff] [blame] | 337 | COMMON.RX3_SINGLE, COMMON.LISTENABLE_FUTURE, COMMON.FLOW |
Yigit Boyar | ccdf423 | 2021-04-08 18:13:35 -0700 | [diff] [blame] | 338 | ) |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 339 | runProcessorTest( |
Daniel Santiago Rivera | 2514b02 | 2024-04-15 17:30:42 -0400 | [diff] [blame^] | 340 | sources = inputSource + otherSources, |
| 341 | options = mapOf(Context.BooleanProcessorOptions.GENERATE_KOTLIN.argName to "false") |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 342 | ) { invocation -> |
| 343 | val (owner, methods) = invocation.roundEnv |
| 344 | .getElementsAnnotatedWith(Dao::class.qualifiedName!!) |
| 345 | .filterIsInstance<XTypeElement>() |
| 346 | .map { |
| 347 | Pair( |
| 348 | it, |
| 349 | it.getAllMethods().filter { |
| 350 | it.hasAnnotation(Transaction::class) |
Yigit Boyar | ad311f1 | 2021-06-28 11:14:26 -0700 | [diff] [blame] | 351 | }.toList() |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 352 | ) |
| 353 | }.first { it.second.isNotEmpty() } |
| 354 | val processor = TransactionMethodProcessor( |
| 355 | baseContext = invocation.context, |
Yigit Boyar | 577a3a6 | 2021-09-13 18:40:16 -0700 | [diff] [blame] | 356 | containingElement = owner, |
| 357 | containingType = owner.type, |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 358 | executableElement = methods.first() |
Jeff Gaston | 63c2310 | 2020-09-18 12:53:59 -0400 | [diff] [blame] | 359 | ) |
Yigit Boyar | bdffe9a | 2021-04-05 19:14:06 -0700 | [diff] [blame] | 360 | val processed = processor.process() |
| 361 | handler(processed, invocation) |
| 362 | } |
Alan Viverette | ba069d5 | 2018-03-08 18:02:39 -0500 | [diff] [blame] | 363 | } |
| 364 | } |