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 
17 package androidx.lifecycle.utils
18 
19 import androidx.lifecycle.LifecycleProcessor
20 import com.google.testing.compile.CompileTester
21 import com.google.testing.compile.JavaFileObjects
22 import com.google.testing.compile.JavaSourcesSubject
23 import java.io.File
24 import java.nio.charset.Charset
25 import javax.tools.JavaFileObject
26 
loadnull27 fun load(fullClassName: String, folder: String): JavaFileObject {
28     val folderPath = "src/test/test-data/${if (folder.isEmpty()) "" else folder + "/"}"
29     val split = fullClassName.split(".")
30     val code = File("$folderPath/${split.last()}.java").readText(Charset.defaultCharset())
31     return JavaFileObjects.forSourceString(fullClassName, code)
32 }
33 
processClassnull34 fun processClass(className: String, vararg fullClassNames: String): CompileTester {
35     val javaFiles = fullClassNames.map { load(it, "") }.toTypedArray()
36     val processedWith =
37         JavaSourcesSubject.assertThat(load(className, ""), *javaFiles)
38             .processedWith(LifecycleProcessor())
39     return checkNotNull(processedWith)
40 }
41