mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
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.
This commit is contained in:
parent
39aea5d944
commit
d88e1b4b7a
@ -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
|
||||
|
@ -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<String, Object> args = new HashMap<String, Object>();
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
BIN
spring-boot-cli/src/test/resources/foo.jar
Normal file
BIN
spring-boot-cli/src/test/resources/foo.jar
Normal file
Binary file not shown.
19
spring-boot-cli/src/test/resources/foo.pom
Normal file
19
spring-boot-cli/src/test/resources/foo.pom
Normal file
@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="
|
||||
http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>foo</groupId>
|
||||
<artifactId>foo</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<version>1.0</version>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
</project>
|
Loading…
Reference in New Issue
Block a user