1 /* 2 * Copyright 2018 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 package androidx.build.dependencyTracker 17 18 import junit.framework.TestCase.assertEquals 19 import junit.framework.TestCase.assertNull 20 import org.gradle.api.plugins.ExtraPropertiesExtension 21 import org.gradle.testfixtures.ProjectBuilder 22 import org.junit.Rule 23 import org.junit.Test 24 import org.junit.rules.TemporaryFolder 25 import org.junit.runner.RunWith 26 import org.junit.runners.JUnit4 27 import java.io.File 28 29 @RunWith(JUnit4::class) 30 class ProjectGraphTest { 31 @Rule 32 @JvmField 33 val tmpFolder = TemporaryFolder() 34 35 @Test testSimplenull36 fun testSimple() { 37 val tmpDir = tmpFolder.root 38 val root = ProjectBuilder.builder() 39 .withProjectDir(tmpDir) 40 .withName("root") 41 .build() 42 // Project Graph expects supportRootFolder. 43 (root.properties.get("ext") as ExtraPropertiesExtension).set("supportRootFolder", tmpDir) 44 val p1 = ProjectBuilder.builder() 45 .withProjectDir(tmpDir.resolve("p1")) 46 .withName("p1") 47 .withParent(root) 48 .build() 49 val p2 = ProjectBuilder.builder() 50 .withProjectDir(tmpDir.resolve("p2")) 51 .withName("p2") 52 .withParent(root) 53 .build() 54 val p3 = ProjectBuilder.builder() 55 .withProjectDir(tmpDir.resolve("p1").resolve("p3")) 56 .withName("p3") 57 .withParent(p1) 58 .build() 59 val graph = ProjectGraph(root) 60 assertNull(graph.findContainingProject("nowhere")) 61 assertNull( 62 "When root project is the root folder, changes in it shouldn't be detected", 63 graph.findContainingProject("rootfile.java") 64 ) 65 assertEquals( 66 p1.path, 67 graph.findContainingProject("p1/px/x.java".toLocalPath()) 68 ) 69 assertEquals( 70 p1.path, 71 graph.findContainingProject("p1/a.java".toLocalPath()) 72 ) 73 assertEquals( 74 p3.path, 75 graph.findContainingProject("p1/p3/a.java".toLocalPath()) 76 ) 77 assertEquals( 78 p2.path, 79 graph.findContainingProject("p2/a/b/c/d/e/f/a.java".toLocalPath()) 80 ) 81 assertNull(graph.findContainingProject("root/x.java")) 82 } 83 84 @Test rootProjectChangenull85 fun rootProjectChange() { 86 val tmpDir = tmpFolder.root 87 val root = ProjectBuilder.builder() 88 .withProjectDir(tmpDir.resolve("subRoot")) 89 .withName("subRoot") 90 .build() 91 (root.properties.get("ext") as ExtraPropertiesExtension).set("supportRootFolder", tmpDir) 92 val p1 = ProjectBuilder.builder() 93 .withProjectDir(tmpDir.resolve("p1")) 94 .withName("p1") 95 .withParent(root) 96 .build() 97 val graph = ProjectGraph(root) 98 assertEquals( 99 p1.path, 100 graph.findContainingProject("p1/px/x.java".toLocalPath()) 101 ) 102 assertEquals( 103 "When root project is not support root, changes in it should be detected", 104 root.path, 105 graph.findContainingProject("subRoot/x.gradle".toLocalPath()) 106 ) 107 } 108 toLocalPathnull109 private fun String.toLocalPath() = this.split("/").joinToString(File.separator) 110 } 111