1+ package org.grails.web.converters.marshaller.json
2+
3+ import grails.converters.JSON
4+ import grails.converters.XML
5+ import grails.core.DefaultGrailsApplication
6+ import grails.persistence.Entity
7+ import org.grails.web.converters.configuration.ConvertersConfigurationInitializer
8+ import spock.lang.Specification
9+
10+ class DomainClassMarshallerSpec extends Specification {
11+ void setup () {
12+ final initializer = new ConvertersConfigurationInitializer ()
13+ def grailsApplication = new DefaultGrailsApplication (Author , Book )
14+ grailsApplication. initialise()
15+ initializer. grailsApplication = grailsApplication
16+ initializer. initialize()
17+ }
18+
19+ void " Test DomainClassMarshaller's should maintain order of relations" () {
20+ def json, xml
21+
22+ when :
23+ def book = new Book (
24+ id : 1 , version : 2 ,
25+ authorsSet : authors as Set ,
26+ authorsMap : authors. inject([:]) { acc , val ->
27+ acc[val. name] = val
28+ acc
29+ }
30+ )
31+ JSON . use(' deep' ) {
32+ json = book as JSON
33+ }
34+ XML . use(' deep' ) {
35+ xml = book as XML
36+ }
37+
38+ then :
39+ json. toString() == expectedJson
40+ xml. toString() == expectedXml
41+
42+ where :
43+ authors | expectedJson | expectedXml
44+ [new Author (id : 1 , name : ' a' ), new Author (id : 2 , name : ' b' )] | ' {"class":"org.grails.web.converters.marshaller.json.Book","id":1,"authorsMap":{"a":{"class":"org.grails.web.converters.marshaller.json.Author","id":1,"name":"a"},"b":{"class":"org.grails.web.converters.marshaller.json.Author","id":2,"name":"b"}},"authorsSet":[{"class":"org.grails.web.converters.marshaller.json.Author","id":1,"name":"a"},{"class":"org.grails.web.converters.marshaller.json.Author","id":2,"name":"b"}]}' | ' <?xml version="1.0" encoding="UTF-8"?><book id="1"><authorsMap><entry key="a" id="1"><name>a</name></entry><entry key="b" id="2"><name>b</name></entry></authorsMap><authorsSet><author id="1"><name>a</name></author><author id="2"><name>b</name></author></authorsSet></book>'
45+ [new Author (id : 2 , name : ' b' ), new Author (id : 1 , name : ' a' )] | ' {"class":"org.grails.web.converters.marshaller.json.Book","id":1,"authorsMap":{"b":{"class":"org.grails.web.converters.marshaller.json.Author","id":2,"name":"b"},"a":{"class":"org.grails.web.converters.marshaller.json.Author","id":1,"name":"a"}},"authorsSet":[{"class":"org.grails.web.converters.marshaller.json.Author","id":2,"name":"b"},{"class":"org.grails.web.converters.marshaller.json.Author","id":1,"name":"a"}]}' | ' <?xml version="1.0" encoding="UTF-8"?><book id="1"><authorsMap><entry key="b" id="2"><name>b</name></entry><entry key="a" id="1"><name>a</name></entry></authorsMap><authorsSet><author id="2"><name>b</name></author><author id="1"><name>a</name></author></authorsSet></book>'
46+ }
47+ }
48+
49+ @Entity
50+ class Author {
51+ Long id
52+ Long version
53+ String name
54+ }
55+
56+ @Entity
57+ class Book {
58+ static hasMany = [authorsSet : Author , authorsMap : Author ]
59+ Long id
60+ Long version
61+ Set authorsSet
62+ Map authorsMap
63+ }
0 commit comments