-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathBaseUpgradeTest.java
More file actions
150 lines (130 loc) · 5.48 KB
/
BaseUpgradeTest.java
File metadata and controls
150 lines (130 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package org.labkey.test.tests.upgrade;
import org.jetbrains.annotations.NotNull;
import org.junit.Assume;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.labkey.test.BaseWebDriverTest;
import org.labkey.test.TestProperties;
import org.labkey.test.util.TestLogger;
import org.labkey.test.util.Version;
import org.labkey.test.util.VersionRange;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import static org.apache.commons.lang3.StringUtils.trimToNull;
/**
* Base test class for tests that set up data and configure a server, then verify the persistence or modification of
* those data and configurations after upgrading to a newer version of LabKey.<br>
* The {@link EarliestVersion} and {@link LatestVersion} annotations can be used to skip particular tests when they are
* not relevant to the version of LabKey being upgraded from (specified in the {@code webtest.upgradePreviousVersion}
* system property).<br>
* The setup steps will be skipped if the {@code webtest.upgradeSetup} system property is set to {@code false}.
*/
public abstract class BaseUpgradeTest extends BaseWebDriverTest
{
protected static final boolean isUpgradeSetupPhase = TestProperties.getBooleanProperty("webtest.upgradeSetup", true);
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)
{
return afterTest || !isUpgradeSetupPhase;
}
@BeforeClass
public static void setupProject() throws Exception
{
BaseUpgradeTest currentTest = BaseWebDriverTest.getCurrentTest();
if (isUpgradeSetupPhase)
{
currentTest.doSetup();
}
else
{
TestLogger.info("Skipping setup for %s. Verifying upgrade.". formatted(currentTest.getClass().getSimpleName()));
}
}
protected abstract void doSetup() throws Exception;
@Rule
public final TestRule upgradeVersionCheck = new UpgradeVersionCheck();
@Override
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>
* Specifies the earliest version of the test class that performed the required setup for the annotated method.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
protected @interface EarliestVersion
{
String value();
}
/**
* Annotates test methods that should only run when upgrading from particular LabKey versions, as specified in
* {@code webtest.upgradePreviousVersion}.<br>
* Specifies the latest version of the test class that performed the required setup for the annotated method.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
protected @interface LatestVersion {
String value();
}
private static class UpgradeVersionCheck implements TestRule
{
@Override
public @NotNull Statement apply(Statement base, Description description)
{
String earliestVersion = Optional.ofNullable(description.getAnnotation(EarliestVersion.class))
.map(EarliestVersion::value).orElse(null);
String latestVersion = Optional.ofNullable(description.getAnnotation(LatestVersion.class))
.map(LatestVersion::value).orElse(null);
if (isUpgradeSetupPhase || setupVersion == null || (earliestVersion == null && latestVersion == null))
{
return base; // Run the test normally
}
return new Statement()
{
@Override
public void evaluate() throws Throwable
{
Assume.assumeTrue("Test not valid when upgrading from version: " + setupVersion,
VersionRange.versionRange(earliestVersion, latestVersion).contains(setupVersion)
);
base.evaluate();
}
};
}
}
}