From 53dc34f7b4d756dab98d3eed6889bf98d088dff4 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Wed, 5 Feb 2020 11:04:18 +0100 Subject: [PATCH] allow to override scm version to simplify testing --- .../java/sonia/scm/BasicContextProvider.java | 23 +++++-- .../sonia/scm/BasicContextProviderTest.java | 66 +++++++++++++------ 2 files changed, 66 insertions(+), 23 deletions(-) diff --git a/scm-core/src/main/java/sonia/scm/BasicContextProvider.java b/scm-core/src/main/java/sonia/scm/BasicContextProvider.java index f4ed04c3f5..1764aceba8 100644 --- a/scm-core/src/main/java/sonia/scm/BasicContextProvider.java +++ b/scm-core/src/main/java/sonia/scm/BasicContextProvider.java @@ -36,6 +36,7 @@ package sonia.scm; //~--- non-JDK imports -------------------------------------------------------- import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Strings; import sonia.scm.util.Util; //~--- JDK imports ------------------------------------------------------------ @@ -57,7 +58,13 @@ public class BasicContextProvider implements SCMContextProvider { /** Default version {@link String} */ - public static final String DEFAULT_VERSION = "unknown"; + public static final String VERSION_DEFAULT = "unknown"; + + /** + * System property to override scm-manager version. + * Note: This should only be used for testing and never in production. + **/ + public static final String VERSION_PROPERTY = "sonia.scm.version.override"; /** Default name of the SCM-Manager base directory */ public static final String DIRECTORY_DEFAULT = ".scm"; @@ -95,7 +102,7 @@ public class BasicContextProvider implements SCMContextProvider try { baseDirectory = findBaseDirectory(); - version = loadVersion(); + version = determineVersion(); stage = loadProjectStage(); } catch (Exception ex) @@ -157,7 +164,7 @@ public class BasicContextProvider implements SCMContextProvider /** * Returns the version of the SCM-Manager. If the version is not set, the - * {@link #DEFAULT_VERSION} is returned. + * {@link #VERSION_DEFAULT} is returned. * * * @return the version of the SCM-Manager @@ -257,6 +264,14 @@ public class BasicContextProvider implements SCMContextProvider return s; } + private String determineVersion() { + String version = System.getProperty(VERSION_PROPERTY); + if (Strings.isNullOrEmpty(version)) { + version = loadVersion(); + } + return version; + } + /** * Loads the version of the SCM-Manager from maven properties file. * @@ -295,7 +310,7 @@ public class BasicContextProvider implements SCMContextProvider } } - return properties.getProperty(MAVEN_PROPERTY_VERSION, DEFAULT_VERSION); + return properties.getProperty(MAVEN_PROPERTY_VERSION, VERSION_DEFAULT); } //~--- get methods ---------------------------------------------------------- diff --git a/scm-core/src/test/java/sonia/scm/BasicContextProviderTest.java b/scm-core/src/test/java/sonia/scm/BasicContextProviderTest.java index 4fb9dfd4fa..523e579b17 100644 --- a/scm-core/src/test/java/sonia/scm/BasicContextProviderTest.java +++ b/scm-core/src/test/java/sonia/scm/BasicContextProviderTest.java @@ -1,6 +1,7 @@ package sonia.scm; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junitpioneer.jupiter.TempDirectory; @@ -13,32 +14,59 @@ import static org.assertj.core.api.Assertions.assertThat; @ExtendWith(TempDirectory.class) class BasicContextProviderTest { - private Path baseDirectory; + @Nested + class VersionTests { - private BasicContextProvider context; + @Test + void shouldReturnVersionFromSystemProperty() { + System.setProperty(BasicContextProvider.VERSION_PROPERTY, "3.0.0"); + try { + SCMContextProvider context = new BasicContextProvider(); + assertThat(context.getVersion()).isEqualTo("3.0.0"); + } finally { + System.clearProperty(BasicContextProvider.VERSION_PROPERTY); + } + } + + @Test + void shouldReturnDefaultVersion() { + SCMContextProvider context = new BasicContextProvider(); + assertThat(context.getVersion()).isEqualTo(BasicContextProvider.VERSION_DEFAULT); + } - @BeforeEach - void setUpContext(@TempDirectory.TempDir Path baseDirectory) { - this.baseDirectory = baseDirectory; - context = new BasicContextProvider(baseDirectory.toFile(), "x.y.z", Stage.PRODUCTION); } - @Test - void shouldReturnAbsolutePathAsIs(@TempDirectory.TempDir Path path) { - Path absolutePath = path.toAbsolutePath(); - Path resolved = context.resolve(absolutePath); + @Nested + class PathTests { - assertThat(resolved).isSameAs(absolutePath); - } + private Path baseDirectory; - @Test - void shouldResolveRelatePath() { - Path path = Paths.get("repos", "42"); - Path resolved = context.resolve(path); + private BasicContextProvider context; + + @BeforeEach + void setUpContext(@TempDirectory.TempDir Path baseDirectory) { + this.baseDirectory = baseDirectory; + context = new BasicContextProvider(baseDirectory.toFile(), "x.y.z", Stage.PRODUCTION); + } + + @Test + void shouldReturnAbsolutePathAsIs(@TempDirectory.TempDir Path path) { + Path absolutePath = path.toAbsolutePath(); + Path resolved = context.resolve(absolutePath); + + assertThat(resolved).isSameAs(absolutePath); + } + + @Test + void shouldResolveRelatePath() { + Path path = Paths.get("repos", "42"); + Path resolved = context.resolve(path); + + assertThat(resolved).isAbsolute(); + assertThat(resolved).startsWithRaw(baseDirectory); + assertThat(resolved).endsWithRaw(path); + } - assertThat(resolved).isAbsolute(); - assertThat(resolved).startsWithRaw(baseDirectory); - assertThat(resolved).endsWithRaw(path); } }