From f083175ca55e89ddbe84192be1da1efc1186d86e Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Wed, 21 Aug 2013 08:43:31 +0100 Subject: [PATCH] Add test case for ivy customization A bug in ivy (tickled by maven leaving a pom but no jar in the local repo) would make the default Grapes ivy config fail (cannot grab...). Phil's workaround now has a test case. --- .../cli/compiler/GrapeEngineCustomizer.java | 9 +- .../compiler/GrapeEngineCustomizerTests.java | 161 ++++++++++++++++++ spring-boot-cli/src/test/resources/foo.jar | Bin 0 -> 2533 bytes spring-boot-cli/src/test/resources/foo.pom | 19 +++ 4 files changed, 187 insertions(+), 2 deletions(-) create mode 100644 spring-boot-cli/src/test/java/org/springframework/boot/cli/compiler/GrapeEngineCustomizerTests.java create mode 100644 spring-boot-cli/src/test/resources/foo.jar create mode 100644 spring-boot-cli/src/test/resources/foo.pom diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GrapeEngineCustomizer.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GrapeEngineCustomizer.java index 824cdd6f2d6..9fb1f573880 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GrapeEngineCustomizer.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/GrapeEngineCustomizer.java @@ -48,14 +48,18 @@ import org.apache.ivy.plugins.resolver.ChainResolver; import org.apache.ivy.plugins.resolver.DependencyResolver; import org.apache.ivy.plugins.resolver.IBiblioResolver; import org.apache.ivy.util.AbstractMessageLogger; +import org.apache.ivy.util.Message; import org.apache.ivy.util.MessageLogger; import org.springframework.boot.cli.Log; /** - * Customizes the groovy grape engine to download from Spring repos and provide simple log - * progress feedback. + * Customizes the groovy grape engine to enhance and patch the behaviour of ivy. Can add + * Spring repos to the search path, provide simple log progress feedback if downloads are + * taking a long time, and also fixes a problem where ivy cannot use a local Maven cache + * repo. * * @author Phillip Webb + * @author Dave Syer */ class GrapeEngineCustomizer { @@ -300,6 +304,7 @@ class GrapeEngineCustomizer { @Override public void log(String msg, int level) { logDownloadingMessage(); + Message.getDefaultLogger().log(msg, level); } @Override diff --git a/spring-boot-cli/src/test/java/org/springframework/boot/cli/compiler/GrapeEngineCustomizerTests.java b/spring-boot-cli/src/test/java/org/springframework/boot/cli/compiler/GrapeEngineCustomizerTests.java new file mode 100644 index 00000000000..60347431f79 --- /dev/null +++ b/spring-boot-cli/src/test/java/org/springframework/boot/cli/compiler/GrapeEngineCustomizerTests.java @@ -0,0 +1,161 @@ +/* + * Copyright 2012-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.cli.compiler; + +import groovy.grape.GrapeIvy; +import groovy.grape.IvyGrabRecord; + +import java.io.File; +import java.io.IOException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import org.apache.ivy.core.module.id.ModuleId; +import org.apache.ivy.core.module.id.ModuleRevisionId; +import org.apache.ivy.core.report.ResolveReport; +import org.apache.ivy.plugins.resolver.ChainResolver; +import org.apache.ivy.plugins.resolver.IBiblioResolver; +import org.apache.ivy.util.FileUtil; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +/** + * @author Dave Syer + */ +public class GrapeEngineCustomizerTests { + + @Rule + public ExpectedException expected = ExpectedException.none(); + private String level; + + @Before + public void setup() { + this.level = System.getProperty("ivy.message.logger.level"); + System.setProperty("ivy.message.logger.level", "3"); + System.setProperty("disableSpringSnapshotRepos", "true"); + } + + @After + public void shutdown() { + if (this.level == null) { + System.clearProperty("ivy.message.logger.level"); + } + else { + System.setProperty("ivy.message.logger.level", this.level); + } + } + + @Test + public void vanillaEngineWithPomExistsAndJarDoesToo() throws Exception { + GrapeIvy engine = new GrapeIvy(); + prepareFoo(engine, true); + ResolveReport report = resolveFoo(engine, "foo", "foo", "1.0"); + assertFalse(report.hasError()); + } + + @Test + public void vanillaEngineWithPomExistsButJarDoesNot() throws Exception { + GrapeIvy engine = new GrapeIvy(); + prepareFoo(engine, false); + this.expected.expectMessage("Error grabbing Grapes"); + ResolveReport report = resolveFoo(engine, "foo", "foo", "1.0"); + assertTrue(report.hasError()); + } + + @SuppressWarnings("unchecked") + @Test + public void customizedEngineWithPomExistsButJarCanBeResolved() throws Exception { + + GrapeIvy engine = new GrapeIvy(); + GrapeEngineCustomizer customizer = new GrapeEngineCustomizer(engine); + ChainResolver grapesResolver = (ChainResolver) engine.getSettings().getResolver( + "downloadGrapes"); + + // Add a resolver that will actually resolve the artifact + IBiblioResolver resolver = new IBiblioResolver(); + resolver.setName("target"); + resolver.setRoot("file:" + System.getProperty("user.dir") + "/target/repository"); + resolver.setM2compatible(true); + resolver.setSettings(engine.getSettings()); + grapesResolver.getResolvers().add(resolver); + + // Allow resolvers to be customized + customizer.customize(); + prepareFoo(engine, false); + prepareFoo(engine, "target/repository/foo/foo/1.0", true); + ResolveReport report = resolveFoo(engine, "foo", "foo", "1.0"); + assertFalse(report.hasError()); + + } + + @Test + public void customizedEngineWithPomExistsButJarCannotBeResolved() throws Exception { + + GrapeIvy engine = new GrapeIvy(); + GrapeEngineCustomizer customizer = new GrapeEngineCustomizer(engine); + + // Allow resolvers to be customized + customizer.customize(); + prepareFoo(engine, false); + this.expected.expectMessage("Error grabbing Grapes"); + ResolveReport report = resolveFoo(engine, "foo", "foo", "1.0"); + assertFalse(report.hasError()); + + } + + private ResolveReport resolveFoo(GrapeIvy engine, String group, String artifact, + String version) { + Map args = new HashMap(); + args.put("autoDownload", true); + IvyGrabRecord record = new IvyGrabRecord(); + record.setConf(Arrays.asList("default")); + record.setForce(true); + record.setTransitive(true); + record.setExt(""); + record.setType(""); + record.setMrid(new ModuleRevisionId(new ModuleId(group, artifact), version)); + ResolveReport report = engine.getDependencies(args, record); + return report; + } + + private void prepareFoo(GrapeIvy engine, boolean includeJar) throws IOException { + prepareFoo(engine, System.getProperty("user.home") + + "/.m2/repository/foo/foo/1.0", includeJar); + } + + private void prepareFoo(GrapeIvy engine, String root, boolean includeJar) + throws IOException { + File maven = new File(root); + FileUtil.forceDelete(maven); + FileUtil.copy(new File("src/test/resources/foo.pom"), new File(maven, + "foo-1.0.pom"), null); + if (includeJar) { + FileUtil.copy(new File("src/test/resources/foo.jar"), new File(maven, + "foo-1.0.jar"), null); + } + File ivy = new File(engine.getGrapeCacheDir() + "/foo"); + FileUtil.forceDelete(ivy); + } + +} diff --git a/spring-boot-cli/src/test/resources/foo.jar b/spring-boot-cli/src/test/resources/foo.jar new file mode 100644 index 0000000000000000000000000000000000000000..6b017f90e884d32125c6b332115fc67370c09e18 GIT binary patch literal 2533 zcmah~2{@E%8y*H@OfeWCMTMpTht&XlC$#(gznPcA~ z>l`Y^TBwFBNgPx^C0ldyf77v@QvZ8h@B4n=^*;A~J@0cp?|oxXU^Xrgb4X$lx?evw z7AUZz^~|&t4NZ{Bn2j(_&>r~DJnhL zi&B*9>FTY-stl-(zwuEp>SrDu`nNncraDU=7;NEBZV_ci6?rU6KU^ zvSI^)_5gV~`dxB&cE-CXxs!a0O*>6mRp1TNc5m~bd@&-?iv#=#JTFcRpG*zx9+G=E z_FCF3&peAQsQi@XQdhg1e8u(nq2!LaPU{gFTGd~Ttr#hcw?1Y?6#ADEyZGZKZUs$y zS(e5_{Hd)9EG}y%1pT6*B9Wx%gSp1+o^Sv2#wXS#Bqds?IyO~3C^_TKAf_p|F=1wS zkf?ft%Vs3nIH8v3?ibs8hl?ln#_k`_^2eo%%}98p#GnusoCH{x6M>V;H_YJzc z&Rv^GbJ6thpH#lEuPR)MG_T#KEe!bp#~5+-g^GNLl_MAGE4^QK%xWFdi4?h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K8RcjFIHY@^+nzee_$<0e&3*EL3IyqtA)(1L;Y_lWe(*NjNFaS$Yo?jn~$DJLGEqB6|<3d}Af z#_LuPo(0?w8O@=3Za!~L5Cw1o4y%eRrbD>`Q}+KqBQGCMFT9WMB|OR0|6Dgp(MZ7% zecpg#Hjh?7b-%`P%c9M_6wsC`M=&aCWk=L%SdXMf=!OOBI+aumWKVQ4PF~bK1wI8H zu|M-T%${c*Ub2|Yc*@#~7$y%)@RhV>le?R~z0sgV?0vQ7X z4tl4}+_$u88@9U-GlQ8UH(Ul-w_rP5c_+hej`;+c2Bt1=GZ_AWVN0bqiFViN wO{%K!h@TT}sP=YUc5m2iz7CGvueOmGlW)6yF(`H*oaxt4kQGo+e+cvMe@eooKL7v# literal 0 HcmV?d00001 diff --git a/spring-boot-cli/src/test/resources/foo.pom b/spring-boot-cli/src/test/resources/foo.pom new file mode 100644 index 00000000000..f88c9caa3fe --- /dev/null +++ b/spring-boot-cli/src/test/resources/foo.pom @@ -0,0 +1,19 @@ + + + + 4.0.0 + + foo + foo + jar + 1.0 + + + UTF-8 + + +