Skip to content

Commit d05bd69

Browse files
authored
Add convenience methods for conditionalizing upgrade checks (#2834)
1 parent 321b25d commit d05bd69

2 files changed

Lines changed: 75 additions & 9 deletions

File tree

src/org/labkey/test/tests/upgrade/BaseUpgradeTest.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ public abstract class BaseUpgradeTest extends BaseWebDriverTest
3535
{
3636

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

4142
@Override
4243
protected boolean skipCleanup(boolean afterTest)
@@ -70,6 +71,29 @@ public List<String> getAssociatedModules()
7071
return Arrays.asList();
7172
}
7273

74+
/**
75+
* Checks if the setup for the current test was performed in a version prior to the specified version.
76+
*
77+
* @param version The version to check against.
78+
* @return {@code true} if the setup version is earlier than the specified version.
79+
*/
80+
protected boolean wasSetupBefore(String version)
81+
{
82+
return !wasSetupWithin(version, null);
83+
}
84+
85+
/**
86+
* Checks if the setup for the current test was performed within the specified version range (inclusive).
87+
*
88+
* @param earliestVersion The earliest version in the range (inclusive).
89+
* @param latestVersion The latest version in the range (inclusive).
90+
* @return {@code true} if the setup version is within the specified range.
91+
*/
92+
protected boolean wasSetupWithin(String earliestVersion, String latestVersion)
93+
{
94+
return VersionRange.versionRange(earliestVersion, latestVersion).contains(setupVersion);
95+
}
96+
7397
/**
7498
* Annotates test methods that should only run when upgrading from particular LabKey versions, as specified in
7599
* {@code webtest.upgradePreviousVersion}.<br>
@@ -104,7 +128,7 @@ private static class UpgradeVersionCheck implements TestRule
104128
String latestVersion = Optional.ofNullable(description.getAnnotation(LatestVersion.class))
105129
.map(LatestVersion::value).orElse(null);
106130

107-
if (isUpgradeSetupPhase || previousVersion == null || (earliestVersion == null && latestVersion == null))
131+
if (isUpgradeSetupPhase || setupVersion == null || (earliestVersion == null && latestVersion == null))
108132
{
109133
return base; // Run the test normally
110134
}
@@ -114,8 +138,8 @@ private static class UpgradeVersionCheck implements TestRule
114138
@Override
115139
public void evaluate() throws Throwable
116140
{
117-
Assume.assumeTrue("Test not valid when upgrading from version: " + previousVersion,
118-
VersionRange.versionRange(earliestVersion, latestVersion).contains(previousVersion)
141+
Assume.assumeTrue("Test not valid when upgrading from version: " + setupVersion,
142+
VersionRange.versionRange(earliestVersion, latestVersion).contains(setupVersion)
119143
);
120144
base.evaluate();
121145
}
Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,78 @@
11
package org.labkey.test.util;
22

3+
/**
4+
* Represents a range of LabKey versions.
5+
* Used for checking if a specific version falls within an inclusive range.
6+
*/
37
public class VersionRange
48
{
5-
private final Version eariestVersion;
9+
private final Version earliestVersion;
610
private final Version latestVersion;
711

8-
public VersionRange(Version eariestVersion, Version latestVersion)
12+
/**
13+
* Constructs a version range.
14+
*
15+
* @param earliestVersion The earliest version in the range (inclusive). If null, there is no lower bound.
16+
* @param latestVersion The latest version in the range (inclusive). If null, there is no upper bound.
17+
* @throws IllegalArgumentException if both versions are null, or if earliestVersion is after latestVersion.
18+
*/
19+
public VersionRange(Version earliestVersion, Version latestVersion)
920
{
10-
this.eariestVersion = eariestVersion;
21+
this.earliestVersion = earliestVersion;
1122
this.latestVersion = latestVersion;
23+
24+
if (earliestVersion == null && latestVersion == null)
25+
throw new IllegalArgumentException("Version range requires at least one version");
26+
if (earliestVersion != null && latestVersion != null && earliestVersion.compareTo(latestVersion) > 0)
27+
throw new IllegalArgumentException("%s is after %s".formatted(earliestVersion, latestVersion));
28+
1229
}
1330

31+
/**
32+
* Creates a version range starting from the specified version with no upper bound.
33+
*
34+
* @param version The earliest version (inclusive).
35+
* @return A new {@link VersionRange}.
36+
*/
1437
public static VersionRange from(String version)
1538
{
1639
return new VersionRange(new Version(version), null);
1740
}
1841

42+
/**
43+
* Creates a version range ending at the specified version with no lower bound.
44+
*
45+
* @param version The latest version (inclusive).
46+
* @return A new {@link VersionRange}.
47+
*/
1948
public static VersionRange until(String version)
2049
{
2150
return new VersionRange(null, new Version(version));
2251
}
2352

53+
/**
54+
* Creates a version range with specified bounds.
55+
*
56+
* @param earliestVersion The earliest version (inclusive). If null, there is no lower bound.
57+
* @param latestVersion The latest version (inclusive). If null, there is no upper bound.
58+
* @return A new {@link VersionRange}.
59+
*/
2460
public static VersionRange versionRange(String earliestVersion, String latestVersion)
2561
{
2662
Version earliest = earliestVersion == null ? null : new Version(earliestVersion);
2763
Version latest = latestVersion == null ? null : new Version(latestVersion);
2864
return new VersionRange(earliest, latest);
2965
}
3066

67+
/**
68+
* Checks if the specified version is within this range (inclusive).
69+
*
70+
* @param version The version to check.
71+
* @return {@code true} if the version is within the range.
72+
*/
3173
public boolean contains(Version version)
3274
{
33-
return (eariestVersion == null || eariestVersion.compareTo(version) <= 0) &&
75+
return (earliestVersion == null || earliestVersion.compareTo(version) <= 0) &&
3476
(latestVersion == null || latestVersion.compareTo(version) >= 0);
3577
}
3678
}

0 commit comments

Comments
 (0)