-
Notifications
You must be signed in to change notification settings - Fork 951
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GRAILS-10838 - add GrailsWebDataBindingStructuredEditorSpec
- 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.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
...est/groovy/org/codehaus/groovy/grails/orm/GrailsWebDataBindingStructuredEditorSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
|
||
} |