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 17package com.android.tools.build.jetifier.core.config 18 19import com.google.common.truth.Truth 20import org.junit.Test 21 22class ConfigParserTest { 23 24 @Test fun parseConfig_validInput() { 25 val confStr = 26 "{\n" + 27 " restrictToPackagePrefixes: [\"android/support/\"],\n" + 28 " reversedRestrictToPackagePrefixes: [\"androidx/\"],\n" + 29 " # Sample comment \n" + 30 " rules: [\n" + 31 " {\n" + 32 " from: \"android/support/v14/preferences/(.*)\",\n" + 33 " to: \"android/jetpack/prefs/main/{0}\"\n" + 34 " },\n" + 35 " {\n" + 36 " from: \"android/support/v14/preferences/(.*)\",\n" + 37 " to: \"android/jetpack/prefs/main/{0}\",\n" + 38 " fieldSelectors: [\"dialog_(.*)\"]\n" + 39 " }\n" + 40 " ],\n" + 41 " pomRules: [\n" + 42 " {\n" + 43 " from: {groupId: \"g\", artifactId: \"a\", version: \"1.0\"},\n" + 44 " to: {groupId: \"g\", artifactId: \"a\", version: \"2.0\"} \n" + 45 " }\n" + 46 " ],\n" + 47 " versions: {\n" + 48 " \"latestReleased\": {\n" + 49 " \"something\": \"1.0.0\"\n" + 50 " }\n" + 51 " }," + 52 " proGuardMap: {\n" + 53 " rules: {\n" + 54 " \"android/support/**\": [\"androidx/**\"]\n" + 55 " }\n" + 56 " }" + 57 "}" 58 59 val config = ConfigParser.parseFromString(confStr) 60 val jsonConfig = config!!.toJson() 61 62 Truth.assertThat(config).isNotNull() 63 Truth.assertThat(config!!.restrictToPackagePrefixes.first()).isEqualTo("android/support/") 64 Truth.assertThat(config!!.reversedRestrictToPackagePrefixes.first()).isEqualTo("androidx/") 65 Truth.assertThat(config.rulesMap.rewriteRules.size).isEqualTo(2) 66 Truth.assertThat(config.versionsMap.data.size).isEqualTo(1) 67 Truth.assertThat(config.versionsMap.data["latestReleased"]) 68 .containsExactly("something", "1.0.0") 69 Truth.assertThat(config.proGuardMap.toJson().rules.size).isEqualTo(1) 70 71 Truth.assertThat(jsonConfig.versions!!.size).isEqualTo(1) 72 Truth.assertThat(jsonConfig.versions!!["latestReleased"]) 73 .containsExactly("something", "1.0.0") 74 } 75 76 @Test(expected = IllegalArgumentException::class) 77 fun parseConfig_pomMissingGroup_shouldFail() { 78 val confStr = 79 "{\n" + 80 " restrictToPackagePrefixes: [\"android/support/\"],\n" + 81 " rules: [\n" + 82 " ],\n" + 83 " pomRules: [\n" + 84 " {\n" + 85 " from: {artifactId: \"a\", version: \"1.0\"},\n" + 86 " to: {artifactId: \"a\", groupId: \"g\", version: \"1.0\"}\n" + 87 " }\n" + 88 " ]\n" + 89 "}" 90 ConfigParser.parseFromString(confStr) 91 } 92 93 @Test(expected = IllegalArgumentException::class) 94 fun parseConfig_pomMissingArtifact_shouldFail() { 95 val confStr = 96 "{\n" + 97 " restrictToPackagePrefixes: [\"android/support/\"],\n" + 98 " rules: [\n" + 99 " ],\n" + 100 " pomRules: [\n" + 101 " {\n" + 102 " from: {groupId: \"g\", version: \"1.0\"},\n" + 103 " to: {artifactId: \"a\", groupId: \"g\", version: \"1.0\"}\n" + 104 " }\n" + 105 " ]\n" + 106 "}" 107 ConfigParser.parseFromString(confStr) 108 } 109 110 @Test(expected = IllegalArgumentException::class) 111 fun parseConfig_pomMissingVersion_shouldFail() { 112 val confStr = 113 "{\n" + 114 " restrictToPackagePrefixes: [\"android/support/\"],\n" + 115 " rules: [\n" + 116 " ],\n" + 117 " pomRules: [\n" + 118 " {\n" + 119 " from: {artifactId: \"a\", groupId: \"g\"},\n" + 120 " to: {artifactId: \"a\", groupId: \"g\"}\n" + 121 " }\n" + 122 " ]\n" + 123 "}" 124 ConfigParser.parseFromString(confStr) 125 } 126 127 @Test(expected = IllegalArgumentException::class) 128 fun parseConfig_duplicity_shouldFail() { 129 val confStr = 130 "{\n" + 131 " restrictToPackagePrefixes: [\"android/support/\"],\n" + 132 " rules: [\n" + 133 " ],\n" + 134 " pomRules: [\n" + 135 " {\n" + 136 " from: {artifactId: \"a\", groupId: \"g\", version: \"1.0\"},\n" + 137 " to: {artifactId: \"b\", groupId: \"g\", version: \"1.0\"}\n" + 138 " },\n" + 139 " {\n" + 140 " from: {artifactId: \"a\", groupId: \"g\", version: \"2.0\"},\n" + 141 " to: {artifactId: \"c\", groupId: \"g\", version: \"1.0\"}\n" + 142 " }\n" + 143 " ]\n" + 144 "}" 145 ConfigParser.parseFromString(confStr) 146 } 147}