Skip to content

Commit

Permalink
GRAILS-10838 - add GrailsWebDataBindingStructuredEditorSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Scott Brown committed Nov 26, 2013
1 parent 4ebda77 commit 891a81a
Showing 1 changed file with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.codehaus.groovy.grails.orm

import org.codehaus.groovy.grails.web.binding.GrailsWebDataBinder
import org.grails.databinding.DataBindingSource
import org.grails.databinding.SimpleMapDataBindingSource
import org.grails.databinding.converters.AbstractStructuredBindingEditor

import spock.lang.Specification

class GrailsWebDataBindingStructuredEditorSpec extends Specification {

void 'Test structured editor'() {
given: 'A binder with a structured editor registered'
def binder = new GrailsWebDataBinder()
binder.setStructuredBindingEditors(new StucturedEmployeeEditor())
def company = new Company()

when: 'binding happens'
binder.bind company, [president: 'struct', president_firstName: 'Tim', president_lastName: 'Cook',
vicePresident: 'struct', vicePresident_firstName: 'Eddy', vicePresident_lastName: 'Cue'] as SimpleMapDataBindingSource

then: 'the expected values were bound'
'Tim' == company.president.firstName
'Cook' == company.president.lastName
'Eddy' == company.vicePresident.firstName
'Cue' == company.vicePresident.lastName
}
}

class Company {
Employee president
Employee vicePresident
}

class Employee {
String firstName
String lastName
}

class StucturedEmployeeEditor extends AbstractStructuredBindingEditor<Employee> {

@Override
public Employee getPropertyValue(Object obj, String propertyName, DataBindingSource bindingSource) {
def employee = new Employee()
employee.firstName = bindingSource.getPropertyValue(propertyName + '_firstName')
employee.lastName = bindingSource.getPropertyValue(propertyName + '_lastName')
employee
}

@Override
Class<? extends Employee> getTargetType() {
Employee
}


}

0 comments on commit 891a81a

Please sign in to comment.