From 5903aaa689646260f17642d0de5f03aeb5d729ab Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Fri, 14 Apr 2017 10:11:58 +0200 Subject: [PATCH] Added a printing of exceptions when failing to download a stub without this change the root reason for exception is not presented with this change we're printing that reason fixes #248 --- .../stubrunner/AetherStubDownloader.java | 31 ++----- .../FailFastLoanApplicationServiceTests.java | 84 +++++++++++-------- 2 files changed, 58 insertions(+), 57 deletions(-) diff --git a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/AetherStubDownloader.java b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/AetherStubDownloader.java index ae2d73e3a6..8552f92047 100644 --- a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/AetherStubDownloader.java +++ b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/AetherStubDownloader.java @@ -16,6 +16,11 @@ package org.springframework.cloud.contract.stubrunner; +import static java.nio.file.Files.createTempDirectory; +import static org.springframework.cloud.contract.stubrunner.AetherFactories.newRepositorySystem; +import static org.springframework.cloud.contract.stubrunner.AetherFactories.newSession; +import static org.springframework.cloud.contract.stubrunner.util.ZipCategory.unzipTo; + import java.io.File; import java.io.IOException; import java.net.URI; @@ -35,20 +40,12 @@ import org.eclipse.aether.resolution.ArtifactResult; import org.eclipse.aether.resolution.VersionRangeRequest; import org.eclipse.aether.resolution.VersionRangeResolutionException; import org.eclipse.aether.resolution.VersionRangeResult; -import org.eclipse.aether.resolution.VersionRequest; -import org.eclipse.aether.resolution.VersionResolutionException; -import org.eclipse.aether.resolution.VersionResult; import org.eclipse.aether.util.repository.AuthenticationBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.cloud.contract.stubrunner.StubRunnerOptions.StubRunnerProxyOptions; import org.springframework.util.StringUtils; -import static java.nio.file.Files.createTempDirectory; -import static org.springframework.cloud.contract.stubrunner.AetherFactories.newRepositorySystem; -import static org.springframework.cloud.contract.stubrunner.AetherFactories.newSession; -import static org.springframework.cloud.contract.stubrunner.util.ZipCategory.unzipTo; - /** * @author Mariusz Smykula */ @@ -215,26 +212,12 @@ public class AetherStubDownloader implements StubDownloader { } if (rangeResult.getHighestVersion() == null) { throw new IllegalArgumentException("For groupId [" + stubsGroup + "] artifactId [" + stubsModule + "] " - + "and classifier [" + classifier + "] the version was not resolved!"); + + "and classifier [" + classifier + "] the version was not resolved! The following exceptions took place " + + rangeResult.getExceptions()); } return rangeResult.getHighestVersion() == null ? null : rangeResult.getHighestVersion().toString(); } - private String resolveArtifactVersion(String stubsGroup, String stubsModule, - String version, String classifier) { - Artifact artifact = new DefaultArtifact(stubsGroup, stubsModule, classifier, - ARTIFACT_EXTENSION, version); - VersionRequest versionRequest = new VersionRequest(artifact, this.remoteRepos, null); - VersionResult versionResult; - try { - versionResult = this.repositorySystem.resolveVersion(this.session, versionRequest); - } - catch (VersionResolutionException e) { - throw new IllegalStateException("Cannot resolve version", e); - } - return versionResult.getVersion() == null ? null : versionResult.getVersion(); - } - private static File unpackStubJarToATemporaryFolder(URI stubJarUri) { File tmpDirWhereStubsWillBeUnzipped; try { diff --git a/tests/spring-cloud-contract-stub-runner-context-path/src/test/java/com/example/loan/FailFastLoanApplicationServiceTests.java b/tests/spring-cloud-contract-stub-runner-context-path/src/test/java/com/example/loan/FailFastLoanApplicationServiceTests.java index d4246a5ee9..974978060d 100644 --- a/tests/spring-cloud-contract-stub-runner-context-path/src/test/java/com/example/loan/FailFastLoanApplicationServiceTests.java +++ b/tests/spring-cloud-contract-stub-runner-context-path/src/test/java/com/example/loan/FailFastLoanApplicationServiceTests.java @@ -1,52 +1,70 @@ +/* + * Copyright 2013-2017 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 com.example.loan; -import com.google.common.collect.ImmutableMap; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.catchThrowable; + import org.junit.Test; import org.springframework.beans.BeanInstantiationException; import org.springframework.beans.factory.BeanCreationException; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.contract.stubrunner.spring.StubRunnerConfiguration; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.catchThrowable; +import com.google.common.collect.ImmutableMap; /** * @author Andrew Morgan */ public class FailFastLoanApplicationServiceTests { - @Test - public void shouldFailToStartContextWhenNoStubCanBeFound() { - // When - final Throwable throwable = catchThrowable(() -> new SpringApplicationBuilder(Application.class, StubRunnerConfiguration.class) - .properties(ImmutableMap.of( - "stubrunner.repositoryRoot", "classpath:m2repo/repository/", - "stubrunner.ids", new String[]{"org.springframework.cloud.contract.verifier.stubs:should-not-be-found"})) - .run()); + @Test + public void shouldFailToStartContextWhenNoStubCanBeFound() { + // When + final Throwable throwable = catchThrowable(() -> new SpringApplicationBuilder(Application.class, StubRunnerConfiguration.class) + .properties(ImmutableMap.of( + "stubrunner.repositoryRoot", "classpath:m2repo/repository/", + "stubrunner.ids", new String[]{"org.springframework.cloud.contract.verifier.stubs:should-not-be-found"})) + .run()); - // Then - assertThat(throwable).isInstanceOf(BeanCreationException.class); - assertThat(throwable.getCause()).isInstanceOf(BeanInstantiationException.class); - assertThat(throwable.getCause().getCause()) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("For groupId [org.springframework.cloud.contract.verifier.stubs] artifactId [should-not-be-found] and classifier [stubs] the version was not resolved!"); - } + // Then + assertThat(throwable).isInstanceOf(BeanCreationException.class); + assertThat(throwable.getCause()).isInstanceOf(BeanInstantiationException.class); + assertThat(throwable.getCause().getCause()) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("For groupId [org.springframework.cloud.contract.verifier.stubs] artifactId [should-not-be-found] " + + "and classifier [stubs] the version was not resolved! The following exceptions took place"); + } - @Test - public void shouldNotTryAndWorkOfflineWhenWorkOfflineIsSetToFalse() { - // When - final Throwable throwable = catchThrowable(() -> new SpringApplicationBuilder(Application.class, StubRunnerConfiguration.class) - .properties(ImmutableMap.of( - "stubrunner.workOffline", "false", - "stubrunner.ids", new String[]{"org.springframework.cloud.contract.verifier.stubs:should-not-be-found"})) - .run()); + @Test + public void shouldNotTryAndWorkOfflineWhenWorkOfflineIsSetToFalse() { + // When + final Throwable throwable = catchThrowable(() -> new SpringApplicationBuilder(Application.class, StubRunnerConfiguration.class) + .properties(ImmutableMap.of( + "stubrunner.workOffline", "false", + "stubrunner.ids", new String[]{"org.springframework.cloud.contract.verifier.stubs:should-not-be-found"})) + .run()); - // Then - assertThat(throwable).isInstanceOf(BeanCreationException.class); - assertThat(throwable.getCause()).isInstanceOf(BeanInstantiationException.class); - assertThat(throwable.getCause().getCause()) - .isInstanceOf(IllegalStateException.class) - .hasMessage("Remote repositories for stubs are not specified and work offline flag wasn't passed"); - } + // Then + assertThat(throwable).isInstanceOf(BeanCreationException.class); + assertThat(throwable.getCause()).isInstanceOf(BeanInstantiationException.class); + assertThat(throwable.getCause().getCause()) + .isInstanceOf(IllegalStateException.class) + .hasMessage("Remote repositories for stubs are not specified and work offline flag wasn't passed"); + } }