Frage

I am stuck on the following problem : I want to write a specs2 specification to assert that my to and from json transformations are symetrical. However, I get an error on joda datetime dates.

'2012-04-17T00:04:00.000+02:00' is not equal to '2012-04-17T00:04:00.000+02:00'. Values have the same string representation but possibly different types like List[Int] and List[String] (TimeSpecs.scala:18) 

Here is a minimalist specs demonstrating the problem

import org.joda.time.DateTime
import org.specs2.mutable.Specification

class TimeSpecs extends Specification {
  "joda and specs2" should {
    "play nice" in {
      val date = DateTime.parse("2012-04-17T00:04:00+0200")
      val date2 = DateTime.parse("2012-04-17T00:04:00+0200")
      date === date2
    }
    "play nice through play json transform" in {
      import play.api.libs.json._
      import play.api.libs.json.Json._
      val date = DateTime.parse("2012-04-17T00:04:00+0200")
      val jsDate= toJson(date)
      val date2= jsDate.as[DateTime]

      date === date2
    }
  }
}

how should I compare date and date2 in the second test ? they are the same but specs2 doesn't seem to see that :(

--- edit

"manually" inspecting the type at runtime with date.getClass.getCanonicalName returns org.joda.time.Datetime as expected

import org.joda.time.DateTime
import org.specs2.mutable.Specification

class TimeSpecs extends Specification {
  "joda and specs2" should {
    "play nice" in {
      val date = DateTime.parse("2012-04-17T00:04:00+0200")
      val date2 = DateTime.parse("2012-04-17T00:04:00+0200")
      date === date2
    }
    "play nice through play json transform" in {
      import play.api.libs.json._
      import play.api.libs.json.Json._
      val date:DateTime = DateTime.parse("2012-04-17T00:04:00+0200")
      val jsDate= toJson(date)
      val date2:DateTim= jsDate.as[DateTime]
      println(date.getClass.getCanonicalName) //prints org.joda.time.DateTime
      println(date2.getClass.getCanonicalName)//prints org.joda.time.DateTime
      date === date2
    }
  }
}

Using DateTime#isEqual does kind of work but I loose the benefit of fluent matchers and the useful error messages they bring. Aditionally, what I am actually trying to compare are case class instances which happen to contain dates, not the dates themselves.

Using

      date should beEqualTo(date2)

yields the same error as ===

War es hilfreich?

Lösung

The problem is that joda time defines a very strict equals which considers the date's Chronology for the equality ( DateTime#getChronology ). The isEqual method proposed by Kim Stebel does ignore the Chronology.

From there on, there are 2 possibilities: Defining custom read and writes for play, then using the same pattern to create the dates as in the following example

import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat
import org.specs2.mutable.Specification

class TimeSpecs extends Specification {
    val pattern = "yyyy-MM-dd'T'HH:mm:ssZZ"
  "joda and specs2" should {
    "play nice" in {
      val date = DateTime.parse("2012-04-17T00:04:00+0200",DateTimeFormat.forPattern(pattern))
      val date2 = DateTime.parse("2012-04-17T00:04:00+0200",DateTimeFormat.forPattern(pattern))
      date === date2
    }
    "play nice through play json transform" in {
      import play.api.libs.json.Json._

      //play2 custom write
      implicit def customJodaWrite = play.api.libs.json.Writes.jodaDateWrites(pattern)
      //play2 custom read
      implicit def customJodaRead = play.api.libs.json.Reads.jodaDateReads(pattern) 


      val date:DateTime = DateTime.parse("2012-04-17T00:04:00+0200",DateTimeFormat.forPattern(pattern)) //make sure you parse the initial date with the same pattern
      val jsDate= toJson(date)
      val date2:DateTime= jsDate.as[DateTime]

      println(date.getClass.getCanonicalName)
      println(date2.getClass.getCanonicalName)
      println(jsDate)
      date should beEqualTo(date2)
    }
  }
}

Play 2.1 defaults to parsing (and writing to json) based on the unix timestamp in milliseconds without timezone information. When parsing back from the unix timestamp, it will consider it in the local computer timezone (in my case Europe/Paris). Hence the need for a custom parser/writer

Joda uses a specific formatter when calling parse without a parser argument, it doesn't seem possible to create the same formatter with only a pattern string ( I haven't found a way to activate the DateTimeFormatter#withOffsetParsed method through a pattern string).

Another possibility may be to define a custom specs2 matcher for jodatime which would use isEqual instead of equals. Since I don't want the unix epoch in my json anyway, I'll stick with the custom play transformers

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top