Skip to content

Commit

Permalink
add some test coverage for passing GStrings to gorm query methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Brown committed Aug 15, 2011
1 parent 1ee3b55 commit a99b9dc
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 64 deletions.
Expand Up @@ -363,7 +363,7 @@ private static abstract class CriterionAdaptor {
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;
Expand Down
@@ -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"

}
}

This file was deleted.

0 comments on commit a99b9dc

Please sign in to comment.