|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package groovy.grape.ivy |
| 20 | + |
| 21 | +import org.apache.ivy.core.module.id.ModuleRevisionId |
| 22 | +import org.junit.jupiter.api.AfterEach |
| 23 | +import org.junit.jupiter.api.BeforeEach |
| 24 | +import org.junit.jupiter.api.Test |
| 25 | +import org.junit.jupiter.api.io.TempDir |
| 26 | + |
| 27 | +import static org.junit.jupiter.api.Assertions.assertEquals |
| 28 | +import static org.junit.jupiter.api.Assertions.assertFalse |
| 29 | +import static org.junit.jupiter.api.Assertions.assertNull |
| 30 | +import static org.junit.jupiter.api.Assertions.assertTrue |
| 31 | + |
| 32 | +final class StrictLocalM2ResolverTest { |
| 33 | + |
| 34 | + private static final String ENABLE_PROPERTY = 'groovy.grape.strict-localm2' |
| 35 | + |
| 36 | + @TempDir |
| 37 | + File m2 |
| 38 | + |
| 39 | + StrictLocalM2Resolver resolver |
| 40 | + |
| 41 | + @BeforeEach |
| 42 | + void setUp() { |
| 43 | + resolver = new StrictLocalM2Resolver() |
| 44 | + resolver.setRoot(m2.toURI().toURL().toString()) |
| 45 | + resolver.setM2compatible(true) |
| 46 | + System.setProperty(ENABLE_PROPERTY, 'true') |
| 47 | + } |
| 48 | + |
| 49 | + @AfterEach |
| 50 | + void tearDown() { |
| 51 | + System.clearProperty(ENABLE_PROPERTY) |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void rejects_pomOnly_packagingJar_default() { |
| 56 | + File dir = layout('com.example', 'foo', '1.0') |
| 57 | + writePom(dir, 'foo', '1.0', null) |
| 58 | + // no JAR |
| 59 | + assertTrue resolver.shouldRejectAsHalfPopulated( |
| 60 | + ModuleRevisionId.newInstance('com.example', 'foo', '1.0'), |
| 61 | + new File(dir, 'foo-1.0.pom')) |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void rejects_pomOnly_packagingJar_explicit() { |
| 66 | + File dir = layout('com.example', 'foo', '1.0') |
| 67 | + writePom(dir, 'foo', '1.0', 'jar') |
| 68 | + assertTrue resolver.shouldRejectAsHalfPopulated( |
| 69 | + ModuleRevisionId.newInstance('com.example', 'foo', '1.0'), |
| 70 | + new File(dir, 'foo-1.0.pom')) |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void accepts_pomOnly_packagingPom() { |
| 75 | + File dir = layout('com.example', 'parent', '1.0') |
| 76 | + writePom(dir, 'parent', '1.0', 'pom') |
| 77 | + assertFalse resolver.shouldRejectAsHalfPopulated( |
| 78 | + ModuleRevisionId.newInstance('com.example', 'parent', '1.0'), |
| 79 | + new File(dir, 'parent-1.0.pom')) |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void accepts_pomAndJar_present() { |
| 84 | + File dir = layout('com.example', 'foo', '1.0') |
| 85 | + writePom(dir, 'foo', '1.0', 'jar') |
| 86 | + new File(dir, 'foo-1.0.jar') << 'fake-jar-bytes' |
| 87 | + assertFalse resolver.shouldRejectAsHalfPopulated( |
| 88 | + ModuleRevisionId.newInstance('com.example', 'foo', '1.0'), |
| 89 | + new File(dir, 'foo-1.0.pom')) |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void rejects_pomOnly_packagingWar_warAbsent() { |
| 94 | + File dir = layout('com.example', 'webapp', '1.0') |
| 95 | + writePom(dir, 'webapp', '1.0', 'war') |
| 96 | + // no .war file |
| 97 | + assertTrue resolver.shouldRejectAsHalfPopulated( |
| 98 | + ModuleRevisionId.newInstance('com.example', 'webapp', '1.0'), |
| 99 | + new File(dir, 'webapp-1.0.pom')) |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + void accepts_pomOnly_packagingWar_warPresent() { |
| 104 | + File dir = layout('com.example', 'webapp', '1.0') |
| 105 | + writePom(dir, 'webapp', '1.0', 'war') |
| 106 | + new File(dir, 'webapp-1.0.war') << 'fake-war-bytes' |
| 107 | + assertFalse resolver.shouldRejectAsHalfPopulated( |
| 108 | + ModuleRevisionId.newInstance('com.example', 'webapp', '1.0'), |
| 109 | + new File(dir, 'webapp-1.0.pom')) |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + void accepts_packagingBundle_jarPresent() { |
| 114 | + // OSGi bundle packaging produces a .jar file in Maven layout. |
| 115 | + File dir = layout('com.example', 'osgi-thing', '1.0') |
| 116 | + writePom(dir, 'osgi-thing', '1.0', 'bundle') |
| 117 | + new File(dir, 'osgi-thing-1.0.jar') << 'fake-jar-bytes' |
| 118 | + assertFalse resolver.shouldRejectAsHalfPopulated( |
| 119 | + ModuleRevisionId.newInstance('com.example', 'osgi-thing', '1.0'), |
| 120 | + new File(dir, 'osgi-thing-1.0.pom')) |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + void accepts_snapshotRevision_unconditionally() { |
| 125 | + // Snapshot filenames are timestamp-based; literal name check would |
| 126 | + // false-fail. Skip strictness for snapshots. |
| 127 | + File dir = layout('com.example', 'snap', '1.0-SNAPSHOT') |
| 128 | + writePom(dir, 'snap', '1.0-SNAPSHOT', 'jar') |
| 129 | + // no JAR alongside the POM (yet still accept) |
| 130 | + assertFalse resolver.shouldRejectAsHalfPopulated( |
| 131 | + ModuleRevisionId.newInstance('com.example', 'snap', '1.0-SNAPSHOT'), |
| 132 | + new File(dir, 'snap-1.0-SNAPSHOT.pom')) |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + void accepts_when_strictness_disabled_via_system_property() { |
| 137 | + System.setProperty(ENABLE_PROPERTY, 'false') |
| 138 | + File dir = layout('com.example', 'foo', '1.0') |
| 139 | + writePom(dir, 'foo', '1.0', 'jar') |
| 140 | + // No JAR; would normally reject. |
| 141 | + assertFalse resolver.shouldRejectAsHalfPopulated( |
| 142 | + ModuleRevisionId.newInstance('com.example', 'foo', '1.0'), |
| 143 | + new File(dir, 'foo-1.0.pom')) |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + void accepts_when_not_m2compatible() { |
| 148 | + resolver.setM2compatible(false) |
| 149 | + File dir = layout('com.example', 'foo', '1.0') |
| 150 | + writePom(dir, 'foo', '1.0', 'jar') |
| 151 | + assertFalse resolver.shouldRejectAsHalfPopulated( |
| 152 | + ModuleRevisionId.newInstance('com.example', 'foo', '1.0'), |
| 153 | + new File(dir, 'foo-1.0.pom')) |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + void readPackaging_findsLiteralValue() { |
| 158 | + File pom = File.createTempFile('pom-', '.xml') |
| 159 | + pom.deleteOnExit() |
| 160 | + pom.text = '<project><packaging>war</packaging></project>' |
| 161 | + assertEquals 'war', StrictLocalM2Resolver.readPackaging(pom) |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + void readPackaging_returnsNullWhenAbsent() { |
| 166 | + File pom = File.createTempFile('pom-', '.xml') |
| 167 | + pom.deleteOnExit() |
| 168 | + pom.text = '<project><groupId>x</groupId></project>' |
| 169 | + assertNull StrictLocalM2Resolver.readPackaging(pom) |
| 170 | + } |
| 171 | + |
| 172 | + @Test |
| 173 | + void readPackaging_returnsNullForMalformedFile() { |
| 174 | + File pom = File.createTempFile('pom-', '.xml') |
| 175 | + pom.deleteOnExit() |
| 176 | + pom.text = 'not a valid xml file' |
| 177 | + assertNull StrictLocalM2Resolver.readPackaging(pom) |
| 178 | + } |
| 179 | + |
| 180 | + @Test |
| 181 | + void computeM2Dir_returnsExpectedPath() { |
| 182 | + File dir = resolver.computeM2Dir( |
| 183 | + ModuleRevisionId.newInstance('org.apache.commons', 'commons-lang3', '3.9')) |
| 184 | + assertEquals new File(m2, 'org/apache/commons/commons-lang3/3.9'), dir |
| 185 | + } |
| 186 | + |
| 187 | + @Test |
| 188 | + void computeM2Dir_handlesDoubleSlashRoot() { |
| 189 | + // Mimic the rendered ${user.home.url}/.m2/repository/ which can yield |
| 190 | + // a double-slash mid-path (e.g. file:/Users/x//.m2/repository/). |
| 191 | + resolver.setRoot('file:' + m2.absolutePath.replaceFirst('/', '//')) |
| 192 | + File dir = resolver.computeM2Dir( |
| 193 | + ModuleRevisionId.newInstance('com.example', 'foo', '1.0')) |
| 194 | + assertEquals new File(m2, 'com/example/foo/1.0'), dir |
| 195 | + } |
| 196 | + |
| 197 | + private File layout(String org, String mod, String rev) { |
| 198 | + File dir = new File(m2, "${org.replace('.', '/')}/${mod}/${rev}") |
| 199 | + dir.mkdirs() |
| 200 | + dir |
| 201 | + } |
| 202 | + |
| 203 | + private static void writePom(File dir, String mod, String rev, String packaging) { |
| 204 | + StringBuilder pom = new StringBuilder() |
| 205 | + pom << '<project xmlns="http://maven.apache.org/POM/4.0.0">\n' |
| 206 | + pom << " <groupId>com.example</groupId>\n" |
| 207 | + pom << " <artifactId>${mod}</artifactId>\n" |
| 208 | + pom << " <version>${rev}</version>\n" |
| 209 | + if (packaging != null) { |
| 210 | + pom << " <packaging>${packaging}</packaging>\n" |
| 211 | + } |
| 212 | + pom << '</project>\n' |
| 213 | + new File(dir, "${mod}-${rev}.pom").text = pom.toString() |
| 214 | + } |
| 215 | +} |
0 commit comments