diff options
author | Yohann Roussel <yroussel@google.com> | 2015-06-16 16:58:51 +0200 |
---|---|---|
committer | Jean-Marie Henaff <jmhenaff@google.com> | 2015-06-25 11:39:13 +0200 |
commit | f589bf111e928b35733bfebf63613ee0a5434611 (patch) | |
tree | 113164aeee73b7ee9c568e0575c1cc139cc1d968 | |
parent | dce5a4a097596892f3d23a048f52295be80989a1 (diff) | |
download | toolchain_jack-f589bf111e928b35733bfebf63613ee0a5434611.zip toolchain_jack-f589bf111e928b35733bfebf63613ee0a5434611.tar.gz toolchain_jack-f589bf111e928b35733bfebf63613ee0a5434611.tar.bz2 |
Move Version to schedlib
(cherry picked from commit 5328d72913fb1f702143db28e19bb54e22466149)
Change-Id: Iccd6843cc56a6a2484105e9c6690bf8a04fddc7e
-rw-r--r-- | jack/src/com/android/jack/Jack.java | 13 | ||||
-rw-r--r-- | jack/src/com/android/jack/Version.java | 126 | ||||
-rw-r--r-- | jack/tests/com/android/jack/VersionTest.java | 5 | ||||
-rw-r--r-- | sched/src/com/android/sched/util/SubReleaseKind.java | 40 | ||||
-rw-r--r-- | sched/src/com/android/sched/util/UncomparableSubReleaseKind.java | 31 | ||||
-rw-r--r-- | sched/src/com/android/sched/util/UncomparableVersion.java (renamed from jack/src/com/android/jack/SubReleaseKind.java) | 22 | ||||
-rw-r--r-- | sched/src/com/android/sched/util/Version.java | 232 | ||||
-rw-r--r-- | sched/tests/com/android/sched/AllTests.java | 2 | ||||
-rw-r--r-- | sched/tests/com/android/sched/util/AllTests.java | 30 | ||||
-rw-r--r-- | sched/tests/com/android/sched/util/SubReleaseKindTest.java | 144 | ||||
-rw-r--r-- | sched/tests/com/android/sched/util/VersionTest.java | 97 |
11 files changed, 595 insertions, 147 deletions
diff --git a/jack/src/com/android/jack/Jack.java b/jack/src/com/android/jack/Jack.java index d96c71a..554905c 100644 --- a/jack/src/com/android/jack/Jack.java +++ b/jack/src/com/android/jack/Jack.java @@ -253,6 +253,7 @@ import com.android.sched.scheduler.Scheduler; import com.android.sched.scheduler.SubPlanBuilder; import com.android.sched.scheduler.TagOrMarkerOrComponentSet; import com.android.sched.util.RunnableHooks; +import com.android.sched.util.Version; import com.android.sched.util.config.Config; import com.android.sched.util.config.ConfigPrinterFactory; import com.android.sched.util.config.ConfigurationException; @@ -272,7 +273,6 @@ import org.antlr.runtime.RecognitionException; import java.io.File; import java.io.IOException; -import java.io.InputStream; import java.util.ArrayList; import java.util.List; import java.util.Set; @@ -1257,19 +1257,16 @@ public abstract class Jack { } } - @Nonnull - private static final String PROPERTIES_FILE = "jack-version.properties"; @CheckForNull private static Version version = null; @Nonnull public static Version getVersion() { if (version == null) { - InputStream is = Jack.class.getClassLoader().getResourceAsStream(PROPERTIES_FILE); - if (is != null) { - version = new Version(is); - } else { - logger.log(Level.SEVERE, "Failed to open Jack properties file " + PROPERTIES_FILE); + try { + version = new Version("jack", Jack.class.getClassLoader()); + } catch (IOException e) { + logger.log(Level.SEVERE, "Failed to open Jack version file", e); throw new AssertionError(); } } diff --git a/jack/src/com/android/jack/Version.java b/jack/src/com/android/jack/Version.java deleted file mode 100644 index f53a3af..0000000 --- a/jack/src/com/android/jack/Version.java +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Copyright (C) 2015 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.jack; - -import com.android.sched.util.log.LoggerFactory; - -import java.io.IOException; -import java.io.InputStream; -import java.util.Properties; -import java.util.logging.Level; -import java.util.logging.Logger; - -import javax.annotation.CheckForNull; -import javax.annotation.Nonnegative; -import javax.annotation.Nonnull; - -/** - * A class describing version, release, build & code. - */ -public class Version { - @Nonnull - private static final Logger logger = LoggerFactory.getLogger(); - - @Nonnull - private String version; - @Nonnull - private String releaseName; - @Nonnegative - private int releaseCode; - @Nonnull - private SubReleaseKind subReleaseKind; - @Nonnegative - private int subReleaseCode; - @CheckForNull - private String buildId; - @CheckForNull - private String codeBase; - - public Version(@Nonnull InputStream is) { - Properties prop = new Properties(); - try { - prop.load(is); - - version = prop.getProperty("version"); - assert version != null; - - releaseName = prop.getProperty("version.release.name"); - assert releaseName != null; - - releaseCode = Integer.parseInt(prop.getProperty("version.release.code")); - assert releaseCode >= 1; - - subReleaseCode = Integer.parseInt(prop.getProperty("version.sub-release.code")); - assert subReleaseCode >= 0; - - subReleaseKind = - SubReleaseKind.valueOf(SubReleaseKind.class, - prop.getProperty("version.sub-release.kind")); - buildId = prop.getProperty("version.buildid"); - codeBase = prop.getProperty("version.sha"); - - if (codeBase == null || buildId == null) { - subReleaseKind = SubReleaseKind.ENGINEERING; - } - } catch (IOException e) { - logger.log(Level.SEVERE, "Failed to read Jack properties", e); - throw new AssertionError(); - } - } - - @Nonnull - public String getVersion() { - return version; - } - - @Nonnull - public String getReleaseName() { - return releaseName; - } - - @Nonnegative - public int getReleaseCode() { - return releaseCode; - } - - @Nonnull - public SubReleaseKind getSubReleaseKind() { - return subReleaseKind; - } - - @Nonnegative - public int getSubReleaseCode() { - return subReleaseCode; - } - - @CheckForNull - public String getBuildId() { - return buildId; - } - - @CheckForNull - public String getCodeBase() { - return codeBase; - } - - @Nonnull - public String getVerboseVersion() { - return version + " '" + releaseName + "' (" - + (buildId != null ? buildId : "engineering") - + (codeBase != null ? (' ' + codeBase) : "") + ")"; - } -}
\ No newline at end of file diff --git a/jack/tests/com/android/jack/VersionTest.java b/jack/tests/com/android/jack/VersionTest.java index 73a1336..56ba09f 100644 --- a/jack/tests/com/android/jack/VersionTest.java +++ b/jack/tests/com/android/jack/VersionTest.java @@ -19,6 +19,9 @@ package com.android.jack; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import com.android.sched.util.SubReleaseKind; +import com.android.sched.util.Version; + import org.junit.Test; public class VersionTest { @@ -31,8 +34,6 @@ public class VersionTest { assertNotNull(version.getVersion()); assertNotNull(version.getVerboseVersion()); assertNotNull(version.getReleaseName()); - assertTrue(version.getReleaseCode() > 0); - assertTrue(version.getSubReleaseCode() > 0); if (version.getBuildId() == null || version.getCodeBase() == null) { assertTrue(version.getSubReleaseKind() == SubReleaseKind.ENGINEERING); diff --git a/sched/src/com/android/sched/util/SubReleaseKind.java b/sched/src/com/android/sched/util/SubReleaseKind.java new file mode 100644 index 0000000..3d1415b --- /dev/null +++ b/sched/src/com/android/sched/util/SubReleaseKind.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.sched.util; + +import javax.annotation.Nonnull; + +/** + * The kind of sub-release. + */ +public enum SubReleaseKind { + ENGINEERING, + PRE_ALPHA, + ALPHA, + BETA, + CANDIDATE, + RELEASE; + + public boolean isMoreStableThan(@Nonnull SubReleaseKind other) throws UncomparableSubReleaseKind { + if ((this == ENGINEERING && other != ENGINEERING) + || (this != ENGINEERING && other == ENGINEERING)) { + throw new UncomparableSubReleaseKind(this + " is not comparable with " + other); + } + return ordinal() > other.ordinal(); + } + +}
\ No newline at end of file diff --git a/sched/src/com/android/sched/util/UncomparableSubReleaseKind.java b/sched/src/com/android/sched/util/UncomparableSubReleaseKind.java new file mode 100644 index 0000000..5d6647e --- /dev/null +++ b/sched/src/com/android/sched/util/UncomparableSubReleaseKind.java @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.sched.util; + +import javax.annotation.Nonnull; + +/** + * Exception that means that sub release kind are not comparable. + */ +public class UncomparableSubReleaseKind extends Exception { + + private static final long serialVersionUID = 1L; + + public UncomparableSubReleaseKind(@Nonnull String message) { + super(message); + } +} diff --git a/jack/src/com/android/jack/SubReleaseKind.java b/sched/src/com/android/sched/util/UncomparableVersion.java index c7c0c59..8de26a1 100644 --- a/jack/src/com/android/jack/SubReleaseKind.java +++ b/sched/src/com/android/sched/util/UncomparableVersion.java @@ -14,16 +14,18 @@ * limitations under the License. */ -package com.android.jack; +package com.android.sched.util; + +import javax.annotation.Nonnull; /** - * The kind of sub-release. + * Exception that means that versions are not comparable. */ -public enum SubReleaseKind { - ENGINEERING, - PRE_ALPHA, - ALPHA, - BETA, - CANDIDATE, - RELEASE; -}
\ No newline at end of file +public class UncomparableVersion extends Exception { + + private static final long serialVersionUID = 1L; + + public UncomparableVersion(@Nonnull String message) { + super(message); + } +} diff --git a/sched/src/com/android/sched/util/Version.java b/sched/src/com/android/sched/util/Version.java new file mode 100644 index 0000000..c1b7746 --- /dev/null +++ b/sched/src/com/android/sched/util/Version.java @@ -0,0 +1,232 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.sched.util; + +import com.android.sched.util.findbugs.SuppressFBWarnings; +import com.android.sched.util.log.LoggerFactory; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; +import java.util.logging.Logger; + +import javax.annotation.CheckForNull; +import javax.annotation.Nonnull; + +/** + * A class describing version, release, build & code. + */ +public class Version { + @Nonnull + private static final String FILE_SUFFIX = "-version.properties"; + @Nonnull + private static final Logger logger = LoggerFactory.getLogger(); + + @Nonnull + private String version; + @Nonnull + private String releaseName; + private int releaseCode; + @Nonnull + private SubReleaseKind subReleaseKind; + private int subReleaseCode; + @CheckForNull + private String buildId; + @CheckForNull + private String codeBase; + + // FINDBUGS Fields are initialized by init() + @SuppressFBWarnings("NP_NONNULL_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR") + public Version(@Nonnull String name, @Nonnull ClassLoader loader) + throws IOException { + String resourceName = name + FILE_SUFFIX; + InputStream resourceStream = loader.getResourceAsStream(resourceName); + if (resourceStream == null) { + throw new FileNotFoundException(resourceName); + } + try { + init(resourceStream); + } finally { + try { + resourceStream.close(); + } catch (IOException e) { + // + } + } + } + + // FINDBUGS Fields are initialized by init() + @SuppressFBWarnings("NP_NONNULL_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR") + public Version(@Nonnull InputStream is) throws IOException { + init(is); + } + + public Version(int releaseCode, int subReleaseCode, @Nonnull SubReleaseKind subReleaseKind) { + this.version = "Unknown"; + this.releaseName = "Unknown"; + this.releaseCode = releaseCode; + this.subReleaseCode = subReleaseCode; + this.subReleaseKind = subReleaseKind; + } + + private void init(InputStream is) throws IOException { + Properties prop = new Properties(); + prop.load(is); + + long versionFileVersion = Long.parseLong(prop.getProperty("version-file.version.code")); + assert versionFileVersion >= 1; + + version = prop.getProperty("version"); + assert version != null; + + releaseName = prop.getProperty("version.release.name"); + assert releaseName != null; + + releaseCode = Integer.parseInt(prop.getProperty("version.release.code")); + + subReleaseCode = Integer.parseInt(prop.getProperty("version.sub-release.code")); + + subReleaseKind = + SubReleaseKind.valueOf(SubReleaseKind.class, prop.getProperty("version.sub-release.kind")); + + buildId = prop.getProperty("version.buildid"); + if (buildId != null && buildId.isEmpty()) { + buildId = null; + } + codeBase = prop.getProperty("version.sha"); + if (codeBase != null && codeBase.isEmpty()) { + codeBase = null; + } + + if (codeBase == null || buildId == null) { + subReleaseKind = SubReleaseKind.ENGINEERING; + } + } + + @Nonnull + public String getVersion() { + return version; + } + + @Nonnull + public String getReleaseName() { + return releaseName; + } + + public int getReleaseCode() { + return releaseCode; + } + + @Nonnull + public SubReleaseKind getSubReleaseKind() { + return subReleaseKind; + } + + public int getSubReleaseCode() { + return subReleaseCode; + } + + @CheckForNull + public String getBuildId() { + return buildId; + } + + @CheckForNull + public String getCodeBase() { + return codeBase; + } + + @Nonnull + public String getVerboseVersion() { + return version + " '" + releaseName + "' (" + + (buildId != null ? buildId : "engineering") + + (codeBase != null ? (' ' + codeBase) : "") + ")"; + } + + public boolean isOlderThan(@Nonnull Version other) throws UncomparableVersion { + return compareTo(other) < 0; + } + + public boolean isOlderOrEqualsThan(@Nonnull Version other) throws UncomparableVersion { + return compareTo(other) <= 0; + } + + public boolean isNewerThan(@Nonnull Version other) throws UncomparableVersion { + return compareTo(other) > 0; + } + + public boolean isNewerOrEqualsThan(@Nonnull Version other) throws UncomparableVersion { + return compareTo(other) >= 0; + } + + @Override + public final boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj instanceof Version) { + Version other = (Version) obj; + return version.equals(other.version) + && releaseName.equals(other.releaseName) + && releaseCode == other.releaseCode + && subReleaseCode == other.subReleaseCode + && subReleaseKind == other.subReleaseKind + && ((buildId != null && buildId.equals(other.buildId)) + || (buildId == null && other.buildId == null)) + && ((codeBase != null && codeBase.equals(other.codeBase)) + || (codeBase == null && other.codeBase == null)); + } + return false; + } + + @Override + public final int hashCode() { + return version.hashCode() ^ releaseName.hashCode() ^ (releaseCode * 7) ^ (subReleaseCode * 17) + ^ subReleaseKind.hashCode() ^ (buildId != null ? buildId.hashCode() : 0) + ^ (codeBase != null ? codeBase.hashCode() : 0); + } + + int compareTo(@Nonnull Version other) throws UncomparableVersion { + if ((subReleaseKind == SubReleaseKind.ENGINEERING + && other.getSubReleaseKind() != SubReleaseKind.ENGINEERING) + || releaseCode < 0 + || subReleaseCode < 0 + || (subReleaseKind != SubReleaseKind.ENGINEERING + && other.getSubReleaseKind() == SubReleaseKind.ENGINEERING) + || other.getReleaseCode() < 0 + || other.getSubReleaseCode() < 0) { + throw new UncomparableVersion( + getVerboseVersion() + " is not comparable with " + other.getVerboseVersion()); + } + + if (this.releaseCode > other.getReleaseCode() || ( + this.releaseCode == other.getReleaseCode() + && this.subReleaseCode > other.getSubReleaseCode())) { + return 1; + } + + + if (this.releaseCode < other.getReleaseCode() || ( + this.releaseCode == other.getReleaseCode() + && this.subReleaseCode < other.getSubReleaseCode())) { + return -1; + } + + return 0; + } +}
\ No newline at end of file diff --git a/sched/tests/com/android/sched/AllTests.java b/sched/tests/com/android/sched/AllTests.java index 76cab8a..916105f 100644 --- a/sched/tests/com/android/sched/AllTests.java +++ b/sched/tests/com/android/sched/AllTests.java @@ -26,7 +26,7 @@ com.android.sched.item.AllTests.class, com.android.sched.marker.AllTests.class, com.android.sched.scheduler.AllTests.class, com.android.sched.config.AllTests.class, - com.android.sched.util.config.cli.AllTests.class, + com.android.sched.util.AllTests.class, com.android.sched.util.file.AllTests.class, com.android.sched.vfs.VFSTest.class}) public class AllTests { diff --git a/sched/tests/com/android/sched/util/AllTests.java b/sched/tests/com/android/sched/util/AllTests.java new file mode 100644 index 0000000..7802e53 --- /dev/null +++ b/sched/tests/com/android/sched/util/AllTests.java @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.sched.util; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; + +@RunWith(Suite.class) +@SuiteClasses(value = { + VersionTest.class, + SubReleaseKindTest.class, + com.android.sched.util.config.cli.AllTests.class, +}) +public class AllTests { +} diff --git a/sched/tests/com/android/sched/util/SubReleaseKindTest.java b/sched/tests/com/android/sched/util/SubReleaseKindTest.java new file mode 100644 index 0000000..3420667 --- /dev/null +++ b/sched/tests/com/android/sched/util/SubReleaseKindTest.java @@ -0,0 +1,144 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.sched.util; + +import junit.framework.Assert; + +import org.junit.Test; + +public class SubReleaseKindTest { + + @Test + public void testIsMoreStable() { + try { + Assert.assertFalse(SubReleaseKind.ENGINEERING.isMoreStableThan(SubReleaseKind.ENGINEERING)); + } catch (UncomparableSubReleaseKind e) { + Assert.fail(); + } + try { + Assert.assertFalse(SubReleaseKind.ENGINEERING.isMoreStableThan(SubReleaseKind.PRE_ALPHA)); + Assert.fail(); + } catch (UncomparableSubReleaseKind e) { + // Ok, sub release kind are not comparable + } + try { + Assert.assertFalse(SubReleaseKind.ENGINEERING.isMoreStableThan(SubReleaseKind.ALPHA)); + Assert.fail(); + } catch (UncomparableSubReleaseKind e) { + // Ok, sub release kind are not comparable + } + try { + Assert.assertFalse(SubReleaseKind.ENGINEERING.isMoreStableThan(SubReleaseKind.BETA)); + Assert.fail(); + } catch (UncomparableSubReleaseKind e) { + // Ok, sub release kind are not comparable + } + try { + Assert.assertFalse(SubReleaseKind.ENGINEERING.isMoreStableThan(SubReleaseKind.CANDIDATE)); + Assert.fail(); + } catch (UncomparableSubReleaseKind e) { + // Ok, sub release kind are not comparable + } + try { + Assert.assertFalse(SubReleaseKind.ENGINEERING.isMoreStableThan(SubReleaseKind.RELEASE)); + Assert.fail(); + } catch (UncomparableSubReleaseKind e) { + // Ok, sub release kind are not comparable + } + + try { + Assert.assertTrue(SubReleaseKind.PRE_ALPHA.isMoreStableThan(SubReleaseKind.ENGINEERING)); + Assert.fail(); + } catch (UncomparableSubReleaseKind e) { + // Ok, sub release kind are not comparable + } + try { + Assert.assertFalse(SubReleaseKind.PRE_ALPHA.isMoreStableThan(SubReleaseKind.PRE_ALPHA)); + Assert.assertFalse(SubReleaseKind.PRE_ALPHA.isMoreStableThan(SubReleaseKind.ALPHA)); + Assert.assertFalse(SubReleaseKind.PRE_ALPHA.isMoreStableThan(SubReleaseKind.BETA)); + Assert.assertFalse(SubReleaseKind.PRE_ALPHA.isMoreStableThan(SubReleaseKind.CANDIDATE)); + Assert.assertFalse(SubReleaseKind.PRE_ALPHA.isMoreStableThan(SubReleaseKind.RELEASE)); + } catch (UncomparableSubReleaseKind e) { + Assert.fail(); + } + + try { + Assert.assertTrue(SubReleaseKind.ALPHA.isMoreStableThan(SubReleaseKind.ENGINEERING)); + Assert.fail(); + } catch (UncomparableSubReleaseKind e) { + // Ok, sub release kind are not comparable + } + try { + Assert.assertTrue(SubReleaseKind.ALPHA.isMoreStableThan(SubReleaseKind.PRE_ALPHA)); + Assert.assertFalse(SubReleaseKind.ALPHA.isMoreStableThan(SubReleaseKind.ALPHA)); + Assert.assertFalse(SubReleaseKind.ALPHA.isMoreStableThan(SubReleaseKind.BETA)); + Assert.assertFalse(SubReleaseKind.ALPHA.isMoreStableThan(SubReleaseKind.CANDIDATE)); + Assert.assertFalse(SubReleaseKind.ALPHA.isMoreStableThan(SubReleaseKind.RELEASE)); + } catch (UncomparableSubReleaseKind e) { + Assert.fail(); + } + + try { + Assert.assertTrue(SubReleaseKind.BETA.isMoreStableThan(SubReleaseKind.ENGINEERING)); + Assert.fail(); + } catch (UncomparableSubReleaseKind e) { + // Ok, sub release kind are not comparable + } + try { + Assert.assertTrue(SubReleaseKind.BETA.isMoreStableThan(SubReleaseKind.PRE_ALPHA)); + Assert.assertTrue(SubReleaseKind.BETA.isMoreStableThan(SubReleaseKind.ALPHA)); + Assert.assertFalse(SubReleaseKind.BETA.isMoreStableThan(SubReleaseKind.BETA)); + Assert.assertFalse(SubReleaseKind.BETA.isMoreStableThan(SubReleaseKind.CANDIDATE)); + Assert.assertFalse(SubReleaseKind.BETA.isMoreStableThan(SubReleaseKind.RELEASE)); + } catch (UncomparableSubReleaseKind e) { + Assert.fail(); + } + + try { + Assert.assertTrue(SubReleaseKind.CANDIDATE.isMoreStableThan(SubReleaseKind.ENGINEERING)); + Assert.fail(); + } catch (UncomparableSubReleaseKind e) { + // Ok, sub release kind are not comparable + } + try { + Assert.assertTrue(SubReleaseKind.CANDIDATE.isMoreStableThan(SubReleaseKind.PRE_ALPHA)); + Assert.assertTrue(SubReleaseKind.CANDIDATE.isMoreStableThan(SubReleaseKind.ALPHA)); + Assert.assertTrue(SubReleaseKind.CANDIDATE.isMoreStableThan(SubReleaseKind.BETA)); + Assert.assertFalse(SubReleaseKind.CANDIDATE.isMoreStableThan(SubReleaseKind.CANDIDATE)); + Assert.assertFalse(SubReleaseKind.CANDIDATE.isMoreStableThan(SubReleaseKind.RELEASE)); + } catch (UncomparableSubReleaseKind e) { + Assert.fail(); + } + + try { + Assert.assertTrue(SubReleaseKind.RELEASE.isMoreStableThan(SubReleaseKind.ENGINEERING)); + Assert.fail(); + } catch (UncomparableSubReleaseKind e) { + // Ok, sub release kind are not comparable + } + try { + Assert.assertTrue(SubReleaseKind.RELEASE.isMoreStableThan(SubReleaseKind.PRE_ALPHA)); + Assert.assertTrue(SubReleaseKind.RELEASE.isMoreStableThan(SubReleaseKind.ALPHA)); + Assert.assertTrue(SubReleaseKind.RELEASE.isMoreStableThan(SubReleaseKind.BETA)); + Assert.assertTrue(SubReleaseKind.RELEASE.isMoreStableThan(SubReleaseKind.CANDIDATE)); + Assert.assertFalse(SubReleaseKind.RELEASE.isMoreStableThan(SubReleaseKind.RELEASE)); + } catch (UncomparableSubReleaseKind e) { + Assert.fail(); + } + } + +} diff --git a/sched/tests/com/android/sched/util/VersionTest.java b/sched/tests/com/android/sched/util/VersionTest.java new file mode 100644 index 0000000..6e7751f --- /dev/null +++ b/sched/tests/com/android/sched/util/VersionTest.java @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.sched.util; + +import junit.framework.Assert; + +import org.junit.Test; + +public class VersionTest { + + @Test + public void testCompare() { + Version v31a = new Version(3, 1, SubReleaseKind.ALPHA); + Version v31e = new Version(3, 1, SubReleaseKind.ENGINEERING); + try { + Assert.assertTrue(v31a.compareTo(v31a) == 0); + } catch (UncomparableVersion e) { + Assert.fail(); + } + Version v32e = new Version(3, 2, SubReleaseKind.ENGINEERING); + try { + Assert.assertTrue(v31a.compareTo(v32e) < 0); + Assert.fail(); + } catch (UncomparableVersion e) { + // Ok, versions are not comparable + } + try { + Assert.assertTrue(v31e.compareTo(v32e) < 0); + } catch (UncomparableVersion e) { + // Two ENGINEERING version are comparable + Assert.fail(); + } + + Version v33p = new Version(3, 3, SubReleaseKind.PRE_ALPHA); + Version v41b = new Version(4, 1, SubReleaseKind.BETA); + try { + Assert.assertTrue(v33p.compareTo(v41b) < 0); + Assert.assertTrue(v41b.compareTo(v33p) > 0); + } catch (UncomparableVersion e) { + Assert.fail(); + } + + Version v_11b = new Version(-1, 1, SubReleaseKind.BETA); + try { + Assert.assertTrue(v31e.compareTo(v_11b) > 0); + Assert.fail(); + } catch (UncomparableVersion e) { + // Ok, versions are not comparable + } + Version v1_1b = new Version(1, -1, SubReleaseKind.BETA); + try { + Assert.assertTrue(v31e.compareTo(v1_1b) > 0); + Assert.fail(); + } catch (UncomparableVersion e) { + // Ok, versions are not comparable + } + } + + @Test + public void testNewerOlder() { + Version v31a = new Version(3, 1, SubReleaseKind.ALPHA); + Version v30a = new Version(3, 0, SubReleaseKind.ALPHA); + Version v25a = new Version(2, 5, SubReleaseKind.ALPHA); + Version v40a = new Version(4, 0, SubReleaseKind.ALPHA); + Version v32a = new Version(3, 2, SubReleaseKind.ALPHA); + + try { + Assert.assertTrue(v31a.isNewerOrEqualsThan(v31a)); + Assert.assertTrue(v31a.isNewerOrEqualsThan(v30a)); + Assert.assertTrue(v31a.isNewerOrEqualsThan(v25a)); + Assert.assertFalse(v31a.isNewerOrEqualsThan(v40a)); + Assert.assertFalse(v31a.isNewerOrEqualsThan(v32a)); + + Assert.assertFalse(v31a.isNewerThan(v31a)); + Assert.assertTrue(v31a.isNewerThan(v30a)); + Assert.assertTrue(v31a.isNewerThan(v25a)); + Assert.assertFalse(v31a.isNewerThan(v40a)); + Assert.assertFalse(v31a.isNewerThan(v32a)); + } catch (UncomparableVersion e) { + Assert.fail(); + } + } +} |