Skip to content

Commit

Permalink
GRAILS-11044 - fix command object error handling for the legacy sprin…
Browse files Browse the repository at this point in the history
…g data binder
  • Loading branch information
Jeff Scott Brown committed Feb 6, 2014
1 parent 0c5dd05 commit 74504da
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.codehaus.groovy.grails.web.binding

import grails.test.mixin.TestMixin
import grails.test.mixin.support.GrailsUnitTestMixin
import grails.util.GrailsWebUtil
import grails.validation.Validateable

import org.codehaus.groovy.grails.commons.GrailsApplication
import org.codehaus.groovy.grails.support.MockApplicationContext
import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes
import org.springframework.web.context.request.RequestContextHolder

import spock.lang.Issue
import spock.lang.Specification


@TestMixin(GrailsUnitTestMixin)
class SpringDataBinderBindingErrorSpec extends Specification {

@Issue('GRAILS-11044')
void 'Test binding errors when using the Spring binder'() {
given: 'The spring binder is being used'
def appCtx = new MockApplicationContext()
appCtx.registerMockBean(GrailsApplication.APPLICATION_ID, grailsApplication)
appCtx.getServletContext().setAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT, appCtx)
GrailsWebUtil.bindMockWebRequest(appCtx)
grailsApplication.config.grails.databinding.useSpringBinder=true
grailsApplication.configChanged()

when: 'a binding error occurs'
RequestContextHolder.getRequestAttributes().currentRequest.setParameter 'number', 'forty two'
def house = new House()
DataBindingUtils.bindObjectToInstance(house, null)

then: ' the non-domain class object has the expected binding error associated with it'
house.hasErrors()
house.errors.errorCount == 1
house.errors.getFieldError('number').code == 'typeMismatch'
}
}

@Validateable
class House {
Integer number
}
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ public static BindingResult bindObjectToDomainInstance(GrailsDomainClass domain,
GrailsDataBinder binder = createDataBinder(object, include, exclude, webRequest.getCurrentRequest());
HttpServletRequest request = webRequest.getCurrentRequest();
performBindFromRequest(binder, request, filter);
bindingResult = binder.getBindingResult();
}
}
}
Expand Down

0 comments on commit 74504da

Please sign in to comment.