Version number parser can now handle patch versions. (#516)

This commit is contained in:
Clinton 2017-04-11 12:08:42 +01:00 committed by Patrick Kuo
parent d795132e7f
commit 35fa9d2e59
4 changed files with 25 additions and 8 deletions

View File

@ -11,18 +11,25 @@ import java.util.regex.Pattern
* builds of the node. [NodeVersionInfo.revision] would be required to differentiate the two.
*/
@CordaSerializable
data class Version(val major: Int, val minor: Int, val snapshot: Boolean) {
data class Version(val major: Int, val minor: Int, val patch: Int?, val snapshot: Boolean) {
companion object {
private val pattern = Pattern.compile("""(\d+)\.(\d+)(-SNAPSHOT)?""")
private val pattern = Pattern.compile("""(\d+)\.(\d+)(.(\d+))?(-SNAPSHOT)?""")
fun parse(string: String): Version {
val matcher = pattern.matcher(string)
require(matcher.matches())
return Version(matcher.group(1).toInt(), matcher.group(2).toInt(), matcher.group(3) != null)
val patch = matcher.group(4)?.toInt()
return Version(matcher.group(1).toInt(), matcher.group(2).toInt(), patch, matcher.group(5) != null)
}
}
override fun toString(): String = if (snapshot) "$major.$minor-SNAPSHOT" else "$major.$minor"
override fun toString(): String {
val sb = StringBuilder()
sb.append(major, ".", minor)
if(patch != null) sb.append(".", patch)
if(snapshot) sb.append("-SNAPSHOT")
return sb.toString()
}
}
data class NodeVersionInfo(val version: Version, val revision: String, val vendor: String)

View File

@ -7,12 +7,12 @@ import org.junit.Test
class VersionTest {
@Test
fun `parse valid non-SNAPSHOT string`() {
assertThat(Version.parse("1.2")).isEqualTo(Version(1, 2, false))
assertThat(Version.parse("1.2")).isEqualTo(Version(1, 2, null, false))
}
@Test
fun `parse valid SNAPSHOT string`() {
assertThat(Version.parse("2.23-SNAPSHOT")).isEqualTo(Version(2, 23, true))
assertThat(Version.parse("2.23-SNAPSHOT")).isEqualTo(Version(2, 23, null, true))
}
@Test
@ -28,4 +28,14 @@ class VersionTest {
Version.parse("2.3-TEST")
}.isInstanceOf(IllegalArgumentException::class.java)
}
@Test
fun `parses patch version`() {
assertThat(Version.parse("0.1.2")).isEqualTo(Version(0, 1, 2, false))
}
@Test
fun `parses snapshot patch version`() {
assertThat(Version.parse("0.1.2-SNAPSHOT")).isEqualTo(Version(0, 1, 2, true))
}
}

View File

@ -59,7 +59,7 @@ fun main(args: Array<String>) {
fun manifestValue(name: String): String? = if (Manifests.exists(name)) Manifests.read(name) else null
val nodeVersionInfo = NodeVersionInfo(
manifestValue("Corda-Version")?.let { Version.parse(it) } ?: Version(0, 0, false),
manifestValue("Corda-Version")?.let { Version.parse(it) } ?: Version(0, 0, 0, false),
manifestValue("Corda-Revision") ?: "Unknown",
manifestValue("Corda-Vendor") ?: "Unknown"
)

View File

@ -91,7 +91,7 @@ val ALL_TEST_KEYS: List<KeyPair> get() = listOf(MEGA_CORP_KEY, MINI_CORP_KEY, AL
val MOCK_IDENTITY_SERVICE: MockIdentityService get() = MockIdentityService(listOf(MEGA_CORP, MINI_CORP, DUMMY_NOTARY))
val MOCK_VERSION = Version(0, 0, false)
val MOCK_VERSION = Version(0, 0, 0, false)
val MOCK_NODE_VERSION_INFO = NodeVersionInfo(MOCK_VERSION, "Mock revision", "Mock Vendor")
fun generateStateRef() = StateRef(SecureHash.randomSHA256(), 0)