1010import org .labkey .test .BaseWebDriverTest ;
1111import org .labkey .test .TestProperties ;
1212import org .labkey .test .util .TestLogger ;
13+ import org .labkey .test .util .Version ;
14+ import org .labkey .test .util .VersionRange ;
1315
16+ import java .lang .annotation .ElementType ;
17+ import java .lang .annotation .Retention ;
18+ import java .lang .annotation .RetentionPolicy ;
19+ import java .lang .annotation .Target ;
1420import java .util .Arrays ;
1521import java .util .List ;
16-
22+ import java .util .Optional ;
23+
24+ import static org .apache .commons .lang3 .StringUtils .trimToNull ;
25+
26+ /**
27+ * Base test class for tests that setup data and configure a server then verify the persistence or modification of those
28+ * data and configurations after upgrading to a newer version of LabKey.<br>
29+ * The {@code EariestVersion} and {@code LatestVersion} annotations can be used to skip particular tests when they are
30+ * not relevant to the version of LabKey being upgraded from (specified in the {@code webtest.upgradePreviousVersion}
31+ * system property).<br>
32+ * The setup steps will be skipped if the {@code webtest.upgradeSetup} system property is set to {@code false}.
33+ */
1734public abstract class BaseUpgradeTest extends BaseWebDriverTest
1835{
1936
2037 protected static final boolean isUpgradeSetupPhase = TestProperties .getBooleanProperty ("webtest.upgradeSetup" , true );
21- protected static final double previousVersion = TestProperties .getDoubleProperty ("webtest.upgradePreviousVersion" , 0.0 );
38+ protected static final Version previousVersion = Optional .ofNullable (trimToNull (System .getProperty ("webtest.upgradePreviousVersion" )))
39+ .map (Version ::new ).orElse (null );
2240
2341 @ Override
2442 protected boolean skipCleanup (boolean afterTest )
@@ -52,12 +70,26 @@ public List<String> getAssociatedModules()
5270 return Arrays .asList ();
5371 }
5472
73+ /**
74+ * Annotates test methods that should only run when upgrading from particular LabKey versions, as specified in
75+ * {@code webtest.upgradePreviousVersion}.<br>
76+ * Specifies the earliest version of the test class that performed the required setup for the annotated method.
77+ */
78+ @ Retention (RetentionPolicy .RUNTIME )
79+ @ Target ({ElementType .METHOD })
5580 protected @interface EariestVersion {
56- double value ();
81+ String value ();
5782 }
5883
84+ /**
85+ * Annotates test methods that should only run when upgrading from particular LabKey versions, as specified in
86+ * {@code webtest.upgradePreviousVersion}.<br>
87+ * Specifies the latest version of the test class that performed the required setup for the annotated method.
88+ */
89+ @ Retention (RetentionPolicy .RUNTIME )
90+ @ Target ({ElementType .METHOD })
5991 protected @interface LatestVersion {
60- double value ();
92+ String value ();
6193 }
6294
6395 private static class UpgradeVersionCheck implements TestRule
@@ -66,9 +98,12 @@ private static class UpgradeVersionCheck implements TestRule
6698 @ Override
6799 public @ NotNull Statement apply (Statement base , Description description )
68100 {
69- EariestVersion eariestVersion = description .getAnnotation (EariestVersion .class );
70- LatestVersion latestVersion = description .getAnnotation (LatestVersion .class );
71- if (isUpgradeSetupPhase || previousVersion <= 0 )
101+ String eariestVersion = Optional .ofNullable (description .getAnnotation (EariestVersion .class ))
102+ .map (EariestVersion ::value ).orElse (null );
103+ String latestVersion = Optional .ofNullable (description .getAnnotation (LatestVersion .class ))
104+ .map (LatestVersion ::value ).orElse (null );
105+
106+ if (isUpgradeSetupPhase || previousVersion == null || (eariestVersion == null && latestVersion == null ))
72107 {
73108 return base ; // Run the test normally
74109 }
@@ -79,8 +114,7 @@ private static class UpgradeVersionCheck implements TestRule
79114 public void evaluate () throws Throwable
80115 {
81116 Assume .assumeTrue ("Test doesn't support upgrading from version: " + previousVersion ,
82- (eariestVersion == null || eariestVersion .value () <= previousVersion ) &&
83- (latestVersion == null || previousVersion <= latestVersion .value ())
117+ VersionRange .versionRange (eariestVersion , latestVersion ).contains (previousVersion )
84118 );
85119 base .evaluate ();
86120 }
0 commit comments