Permalink
Browse files

add some test coverage for passing GStrings to gorm query methods

  • Loading branch information...
jeffbrown committed Aug 15, 2011
1 parent 1ee3b55 commit a99b9dc5cba8c20fa6ffc19913c7f6cbe93effc2
@@ -363,7 +363,7 @@ public HibernateCriterionAdaptor(Criterion criterion) {
public abstract org.hibernate.criterion.Criterion toHibernateCriterion(Criterion criterion);
protected Object convertStringValue(Object o) {
- if(o instanceof StreamCharBuffer) {
+ if((!(o instanceof String)) && (o instanceof StreamCharBuffer || o instanceof CharSequence)) {
o = o.toString();
}
return o;
@@ -0,0 +1,88 @@
+package org.codehaus.groovy.grails.orm.hibernate
+
+import grails.util.GrailsWebUtil
+
+import org.codehaus.groovy.grails.web.util.StreamCharBuffer
+import org.springframework.web.context.request.RequestContextHolder
+
+/**
+ * @author Graeme Rocher
+ * @since 1.1
+ */
+class CharSequenceAndGormTests extends AbstractGrailsHibernateTests {
+
+ protected void onSetUp() {
+ gcl.parseClass '''
+import grails.persistence.*
+
+@Entity
+class SomeDomainClass {
+ String name
+}
+'''
+
+ gcl.parseClass '''
+class StreamCharTagLib {
+ Closure callMe = { attrs, body ->
+ out << "hello"
+ }
+}
+'''
+ }
+
+ void testGormWithStreamCharBuffer() {
+ GrailsWebUtil.bindMockWebRequest(appCtx)
+
+ try {
+ def someDomainClass = ga.getDomainClass('SomeDomainClass').clazz
+
+ assertNotNull "should have saved instance", someDomainClass.newInstance(name:"hello").save(flush:true)
+ session.clear()
+
+ def taglib = appCtx.getBean("StreamCharTagLib")
+ def result = taglib.callMe()
+ assertTrue result instanceof StreamCharBuffer
+
+ testQueryMethods result
+ }
+ finally {
+ RequestContextHolder.setRequestAttributes(null)
+ }
+ }
+
+ void testGormWithGString() {
+ GrailsWebUtil.bindMockWebRequest(appCtx)
+
+ try {
+ def someDomainClass = ga.getDomainClass('SomeDomainClass').clazz
+
+ assertNotNull "should have saved instance", someDomainClass.newInstance(name:"hello").save(flush:true)
+ session.clear()
+
+ def value = 'hello'
+ def queryArg = "${value}"
+ assertTrue queryArg instanceof GString
+ testQueryMethods queryArg
+ }
+ finally {
+ RequestContextHolder.setRequestAttributes(null)
+ }
+ }
+
+ private testQueryMethods(queryArg) {
+ def someDomainClass = ga.getDomainClass('SomeDomainClass').clazz
+ assert someDomainClass.findByName(queryArg) : "should have found a result when passing a ${queryArg.getClass()} value"
+ assert someDomainClass.findByNameLike(queryArg) : "should have found a result when passing a ${queryArg.getClass()} value"
+ assert someDomainClass.countByName(queryArg) : "should have found a result when passing a ${queryArg.getClass()} value"
+ assert someDomainClass.countByNameLike(queryArg) : "should have found a result when passing a ${queryArg.getClass()} value"
+ assert someDomainClass.findAllByName(queryArg) : "should have found a result when passing a ${queryArg.getClass()} value"
+ assert someDomainClass.findAllByNameLike(queryArg) : "should have found a result when passing a ${queryArg.getClass()} value"
+ assert someDomainClass.findWhere(name:queryArg) : "should have found a result when passing a ${queryArg.getClass()} value"
+ assert someDomainClass.findAllWhere(name:queryArg) : "should have found a result when passing a ${queryArg.getClass()} value"
+ assert someDomainClass.withCriteria{ eq 'name',queryArg } : "should have found a result when passing a ${queryArg.getClass()} value"
+ assert someDomainClass.find("from SomeDomainClass s where s.name = ?", [queryArg]) : "should have found a result when passing a ${queryArg.getClass()} value"
+ assert someDomainClass.findAll("from SomeDomainClass s where s.name = ?", [queryArg]) : "should have found a result when passing a ${queryArg.getClass()} value"
+ assert someDomainClass.executeQuery("from SomeDomainClass s where s.name = ?", [queryArg]) : "should have found a result when passing a ${queryArg.getClass()} value"
+
+ }
+}
@@ -1,63 +0,0 @@
-package org.codehaus.groovy.grails.orm.hibernate
-
-import grails.util.GrailsWebUtil
-
-import org.codehaus.groovy.grails.web.util.StreamCharBuffer
-import org.springframework.web.context.request.RequestContextHolder
-
-/**
- * @author Graeme Rocher
- * @since 1.1
- */
-class StreamCharBufferAndGormTests extends AbstractGrailsHibernateTests {
-
- protected void onSetUp() {
- gcl.parseClass '''
-import grails.persistence.*
-
-@Entity
-class StreamCharMe {
- String name
-}
-'''
-
- gcl.parseClass '''
-class StreamCharTagLib {
- Closure callMe = { attrs, body ->
- out << "hello"
- }
-}
-'''
- }
-
- void testGormWithStreamCharBuffer() {
- GrailsWebUtil.bindMockWebRequest(appCtx)
-
- try {
- def StreamCharMe = ga.getDomainClass('StreamCharMe').clazz
-
- assertNotNull "should have saved instance", StreamCharMe.newInstance(name:"hello").save(flush:true)
- session.clear()
-
- def taglib = appCtx.getBean("StreamCharTagLib")
- def result = taglib.callMe()
- assertTrue result instanceof StreamCharBuffer
-
- assert StreamCharMe.findByName(result) : "should have found a result when passing a StreamCharBuffer value"
- assert StreamCharMe.findByNameLike(result) : "should have found a result when passing a StreamCharBuffer value"
- assert StreamCharMe.countByName(result) : "should have found a result when passing a StreamCharBuffer value"
- assert StreamCharMe.countByNameLike(result) : "should have found a result when passing a StreamCharBuffer value"
- assert StreamCharMe.findAllByName(result) : "should have found a result when passing a StreamCharBuffer value"
- assert StreamCharMe.findAllByNameLike(result) : "should have found a result when passing a StreamCharBuffer value"
- assert StreamCharMe.findWhere(name:result) : "should have found a result when passing a StreamCharBuffer value"
- assert StreamCharMe.findAllWhere(name:result) : "should have found a result when passing a StreamCharBuffer value"
- assert StreamCharMe.withCriteria{ eq 'name',result } : "should have found a result when passing a StreamCharBuffer value"
- assert StreamCharMe.find("from StreamCharMe s where s.name = ?", [result]) : "should have found a result when passing a StreamCharBuffer value"
- assert StreamCharMe.findAll("from StreamCharMe s where s.name = ?", [result]) : "should have found a result when passing a StreamCharBuffer value"
- assert StreamCharMe.executeQuery("from StreamCharMe s where s.name = ?", [result]) : "should have found a result when passing a StreamCharBuffer value"
- }
- finally {
- RequestContextHolder.setRequestAttributes(null)
- }
- }
-}

0 comments on commit a99b9dc

Please sign in to comment.