Bug 1509120 - Fix NullPointerException on OS version normalization

This commit is contained in:
Arkadiusz Komarzewski 2018-11-22 18:28:11 +01:00 коммит произвёл Arkadiusz Komarzewski
Родитель cb2003c843
Коммит d8ecec07d9
2 изменённых файлов: 15 добавлений и 15 удалений

Просмотреть файл

@ -191,18 +191,18 @@ case class EnvironmentBuild(version: Option[String],
case class System(os: SystemOs, isWow64: Option[Boolean], memoryMB: Option[Double]) case class System(os: SystemOs, isWow64: Option[Boolean], memoryMB: Option[Double])
case class SystemOs(name: String, version: String) { case class SystemOs(name: String, version: String) {
val normalizedVersion: String = OS(Option(name), Option(version)).normalizedVersion val normalizedVersion: Option[String] = OS(Option(name), Option(version)).normalizedVersion
} }
case class OS(name: Option[String], version: Option[String]) { case class OS(name: Option[String], version: Option[String]) {
val versionRegex = "(\\d+(\\.\\d+)?(\\.\\d+)?)?.*".r val versionRegex = "(\\d+(\\.\\d+)?(\\.\\d+)?)?.*".r
val normalizedVersion: String = { val normalizedVersion: Option[String] = {
version match { version match {
case Some(v) => case Some(v) =>
val versionRegex(normalized, _, _) = v val versionRegex(normalized, _, _) = v
normalized Option(normalized)
case None => case None =>
null None
} }
} }
} }
@ -263,7 +263,7 @@ trait HasEnvironment {
override def getOsName: Option[String] = meta.`environment.system`.map(_.os.name) override def getOsName: Option[String] = meta.`environment.system`.map(_.os.name)
override def getOsVersion: Option[String] = meta.`environment.system`.map(_.os.normalizedVersion) override def getOsVersion: Option[String] = meta.`environment.system`.flatMap(_.os.normalizedVersion)
override def getArchitecture: Option[String] = meta.`environment.build`.flatMap(_.architecture) override def getArchitecture: Option[String] = meta.`environment.build`.flatMap(_.architecture)

Просмотреть файл

@ -107,16 +107,16 @@ class PingsTest extends FlatSpec with Matchers{
} }
"An OS instance" should "normalize the version" in { "An OS instance" should "normalize the version" in {
OS(Some("linux"), Some("1.1.1-ignore")).normalizedVersion should be ("1.1.1") OS(Some("linux"), Some("1.1.1-ignore")).normalizedVersion shouldBe Some("1.1.1")
OS(Some("linux"), Some("1.1.1ignore")).normalizedVersion should be ("1.1.1") OS(Some("linux"), Some("1.1.1ignore")).normalizedVersion shouldBe Some("1.1.1")
OS(Some("linux"), Some("1.1")).normalizedVersion should be ("1.1") OS(Some("linux"), Some("1.1")).normalizedVersion shouldBe Some("1.1")
OS(Some("linux"), Some("1.1-ignore")).normalizedVersion should be ("1.1") OS(Some("linux"), Some("1.1-ignore")).normalizedVersion shouldBe Some("1.1")
OS(Some("linux"), Some("1.1ignore")).normalizedVersion should be ("1.1") OS(Some("linux"), Some("1.1ignore")).normalizedVersion shouldBe Some("1.1")
OS(Some("linux"), Some("1")).normalizedVersion should be ("1") OS(Some("linux"), Some("1")).normalizedVersion shouldBe Some("1")
OS(Some("linux"), Some("1-ignore")).normalizedVersion should be ("1") OS(Some("linux"), Some("1-ignore")).normalizedVersion shouldBe Some("1")
OS(Some("linux"), Some("1ignore")).normalizedVersion should be ("1") OS(Some("linux"), Some("1ignore")).normalizedVersion shouldBe Some("1")
OS(Some("linux"), Some("non-numeric")).normalizedVersion should be (null) OS(Some("linux"), Some("non-numeric")).normalizedVersion shouldBe None
OS(Some("linux"), Some("nonnumeric1.1")).normalizedVersion should be (null) OS(Some("linux"), Some("nonnumeric1.1")).normalizedVersion shouldBe None
} }
"Main Ping" can "read events" in { "Main Ping" can "read events" in {