Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions src/org/labkey/test/tests/upgrade/BaseUpgradeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ public abstract class BaseUpgradeTest extends BaseWebDriverTest
{

protected static final boolean isUpgradeSetupPhase = TestProperties.getBooleanProperty("webtest.upgradeSetup", true);
protected static final Version previousVersion = Optional.ofNullable(trimToNull(System.getProperty("webtest.upgradePreviousVersion")))
.map(Version::new).orElse(TestProperties.getProductVersion());
protected static final Version setupVersion = isUpgradeSetupPhase ? TestProperties.getProductVersion() :
Optional.ofNullable(trimToNull(System.getProperty("webtest.upgradePreviousVersion"))).map(Version::new)
.orElse(TestProperties.getProductVersion());

@Override
protected boolean skipCleanup(boolean afterTest)
Expand Down Expand Up @@ -70,6 +71,29 @@ public List<String> getAssociatedModules()
return Arrays.asList();
}

/**
* Checks if the setup for the current test was performed in a version prior to the specified version.
*
* @param version The version to check against.
* @return {@code true} if the setup version is earlier than the specified version.
*/
protected boolean wasSetupBefore(String version)
{
return !wasSetupWithin(version, null);
}

/**
* Checks if the setup for the current test was performed within the specified version range (inclusive).
*
* @param earliestVersion The earliest version in the range (inclusive).
* @param latestVersion The latest version in the range (inclusive).
* @return {@code true} if the setup version is within the specified range.
*/
protected boolean wasSetupWithin(String earliestVersion, String latestVersion)
{
return VersionRange.versionRange(earliestVersion, latestVersion).contains(setupVersion);
}

/**
* Annotates test methods that should only run when upgrading from particular LabKey versions, as specified in
* {@code webtest.upgradePreviousVersion}.<br>
Expand Down Expand Up @@ -104,7 +128,7 @@ private static class UpgradeVersionCheck implements TestRule
String latestVersion = Optional.ofNullable(description.getAnnotation(LatestVersion.class))
.map(LatestVersion::value).orElse(null);

if (isUpgradeSetupPhase || previousVersion == null || (earliestVersion == null && latestVersion == null))
if (isUpgradeSetupPhase || setupVersion == null || (earliestVersion == null && latestVersion == null))
{
return base; // Run the test normally
}
Expand All @@ -114,8 +138,8 @@ private static class UpgradeVersionCheck implements TestRule
@Override
public void evaluate() throws Throwable
{
Assume.assumeTrue("Test not valid when upgrading from version: " + previousVersion,
VersionRange.versionRange(earliestVersion, latestVersion).contains(previousVersion)
Assume.assumeTrue("Test not valid when upgrading from version: " + setupVersion,
VersionRange.versionRange(earliestVersion, latestVersion).contains(setupVersion)
);
base.evaluate();
}
Expand Down
50 changes: 46 additions & 4 deletions src/org/labkey/test/util/VersionRange.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,78 @@
package org.labkey.test.util;

/**
* Represents a range of LabKey versions.
* Used for checking if a specific version falls within an inclusive range.
*/
public class VersionRange
{
private final Version eariestVersion;
private final Version earliestVersion;
private final Version latestVersion;

public VersionRange(Version eariestVersion, Version latestVersion)
/**
* Constructs a version range.
*
* @param earliestVersion The earliest version in the range (inclusive). If null, there is no lower bound.
* @param latestVersion The latest version in the range (inclusive). If null, there is no upper bound.
* @throws IllegalArgumentException if both versions are null, or if earliestVersion is after latestVersion.
*/
public VersionRange(Version earliestVersion, Version latestVersion)
{
this.eariestVersion = eariestVersion;
this.earliestVersion = earliestVersion;
this.latestVersion = latestVersion;

if (earliestVersion == null && latestVersion == null)
throw new IllegalArgumentException("Version range requires at least one version");
if (earliestVersion != null && latestVersion != null && earliestVersion.compareTo(latestVersion) > 0)
throw new IllegalArgumentException("%s is after %s".formatted(earliestVersion, latestVersion));

}

/**
* Creates a version range starting from the specified version with no upper bound.
*
* @param version The earliest version (inclusive).
* @return A new {@link VersionRange}.
*/
public static VersionRange from(String version)
{
return new VersionRange(new Version(version), null);
}

/**
* Creates a version range ending at the specified version with no lower bound.
*
* @param version The latest version (inclusive).
* @return A new {@link VersionRange}.
*/
public static VersionRange until(String version)
{
return new VersionRange(null, new Version(version));
}

/**
* Creates a version range with specified bounds.
*
* @param earliestVersion The earliest version (inclusive). If null, there is no lower bound.
* @param latestVersion The latest version (inclusive). If null, there is no upper bound.
* @return A new {@link VersionRange}.
*/
public static VersionRange versionRange(String earliestVersion, String latestVersion)
{
Version earliest = earliestVersion == null ? null : new Version(earliestVersion);
Version latest = latestVersion == null ? null : new Version(latestVersion);
return new VersionRange(earliest, latest);
}

/**
* Checks if the specified version is within this range (inclusive).
*
* @param version The version to check.
* @return {@code true} if the version is within the range.
*/
public boolean contains(Version version)
{
return (eariestVersion == null || eariestVersion.compareTo(version) <= 0) &&
return (earliestVersion == null || earliestVersion.compareTo(version) <= 0) &&
(latestVersion == null || latestVersion.compareTo(version) >= 0);
}
}