1 package org
.codehaus
.groovy
.grails
.orm
.hibernate
;
5 import org
.codehaus
.groovy
.grails
.commons
.DefaultGrailsApplication
;
6 import org
.codehaus
.groovy
.grails
.commons
.DomainClassArtefactHandler
;
7 import org
.codehaus
.groovy
.grails
.commons
.GrailsApplication
;
8 import org
.codehaus
.groovy
.grails
.commons
.GrailsDomainClass
;
9 import org
.codehaus
.groovy
.grails
.commons
.spring
.GrailsRuntimeConfigurator
;
10 import org
.codehaus
.groovy
.grails
.orm
.hibernate
.metaclass
.FindByPersistentMethod
;
11 import org
.codehaus
.groovy
.grails
.support
.MockApplicationContext
;
12 import org
.codehaus
.groovy
.grails
.plugins
.PluginMetaManager
;
13 import org
.codehaus
.groovy
.grails
.plugins
.DefaultPluginMetaManager
;
14 import org
.codehaus
.groovy
.runtime
.InvokerInvocationException
;
15 import org
.hibernate
.SessionFactory
;
16 import org
.springframework
.context
.ApplicationContext
;
17 import org
.springframework
.context
.support
.StaticMessageSource
;
18 import org
.springframework
.mock
.web
.MockServletContext
;
19 import org
.springframework
.orm
.hibernate3
.SessionHolder
;
20 import org
.springframework
.test
.AbstractDependencyInjectionSpringContextTests
;
21 import org
.springframework
.transaction
.support
.TransactionSynchronizationManager
;
22 import org
.springframework
.validation
.Errors
;
23 import org
.springframework
.core
.io
.Resource
;
27 public class PersistentMethodTests
extends AbstractDependencyInjectionSpringContextTests
{
29 protected GrailsApplication grailsApplication
= null;
30 private GroovyClassLoader cl
= new GroovyClassLoader();
31 private SessionFactory sessionFactory
;
32 private org
.hibernate
.classic
.Session hibSession
;
36 * @param grailsApplication The grailsApplication to set.
38 public void setGrailsApplication(GrailsApplication grailsApplication
) {
39 this.grailsApplication
= grailsApplication
;
43 protected void onTearDown() throws Exception
45 grailsApplication
= null;
47 sessionFactory
= null;
52 protected void onSetUp() throws Exception
{
53 cl
.parseClass("dataSource {\n" +
54 "\t\t\tdbCreate = \"create-drop\" // one of 'create', 'create-drop','update'\n" +
55 "\t\t\turl = \"jdbc:hsqldb:mem:devDB\" \n" +
56 "\tpooled = false\n" +
57 "\tdriverClassName = \"org.hsqldb.jdbcDriver\"\n" +
58 "\tusername = \"sa\"\n" +
59 "\tpassword = \"\"\n" +
61 Class groovyClass
= cl
.parseClass("public class PersistentMethodTests {\n" +
63 "\t Long version \n" +
65 "\t String firstName \n" +
66 "\t String lastName \n" +
68 "\t boolean active = true\n" +
70 "\tstatic constraints = {\n" +
71 "\t\tfirstName(size:4..15)\n" +
72 "\t\tlastName(nullable:false)\n" +
73 "\t\tage(nullable:true)\n" +
77 Class descendentClass
= cl
.parseClass(
78 "public class PersistentMethodTestsDescendent extends PersistentMethodTests {\n" +
80 " static constraints = {\n" +
81 " gender(blank:false)\n" +
86 ExpandoMetaClass
.enableGlobally();
88 grailsApplication
= new DefaultGrailsApplication(new Class
[]{groovyClass
, descendentClass
},cl
);
91 MockApplicationContext parent
= new MockApplicationContext();
92 parent
.registerMockBean(GrailsApplication
.APPLICATION_ID
, grailsApplication
);
93 parent
.registerMockBean("messageSource", new StaticMessageSource());
94 parent
.registerMockBean(PluginMetaManager
.BEAN_ID
, new DefaultPluginMetaManager(new Resource
[0]));
95 GrailsRuntimeConfigurator configurator
= new GrailsRuntimeConfigurator(grailsApplication
,parent
);
96 ApplicationContext appCtx
= configurator
.configure( new MockServletContext( ));
97 this.sessionFactory
= (SessionFactory
)appCtx
.getBean("sessionFactory");
101 if(!TransactionSynchronizationManager
.hasResource(this.sessionFactory
)) {
102 this.hibSession
= this.sessionFactory
.openSession();
103 TransactionSynchronizationManager
.bindResource(this.sessionFactory
, new SessionHolder(hibSession
));
109 protected String
[] getConfigLocations() {
110 return new String
[] { "org/codehaus/groovy/grails/orm/hibernate/grails-persistent-method-tests.xml" };
113 public void testMethodSignatures() {
115 FindByPersistentMethod findBy
= new FindByPersistentMethod( grailsApplication
,null,new GroovyClassLoader());
116 assertTrue(findBy
.isMethodMatch("findByFirstName"));
117 assertTrue(findBy
.isMethodMatch("findByFirstNameAndLastName"));
118 assertFalse(findBy
.isMethodMatch("rubbish"));
122 public void testSavePersistentMethod() {
123 // init spring config
126 GrailsDomainClass domainClass
= (GrailsDomainClass
) this.grailsApplication
.getArtefact(DomainClassArtefactHandler
.TYPE
,
127 "PersistentMethodTests");
129 GroovyObject obj
= (GroovyObject
)domainClass
.newInstance();
130 obj
.setProperty( "id", new Long(1) );
131 obj
.setProperty( "firstName", "fred" );
132 obj
.setProperty( "lastName", "flintstone" );
134 obj
.invokeMethod("save", null);
136 // test ident method to retrieve value of id
137 Object id
= obj
.invokeMethod("ident", null);
139 assertTrue(id
instanceof Long
);
142 public void testValidatePersistentMethod() {
143 // init spring config
146 GrailsDomainClass domainClass
= (GrailsDomainClass
) this.grailsApplication
.getArtefact(DomainClassArtefactHandler
.TYPE
,
147 "PersistentMethodTests");
149 GroovyObject obj
= (GroovyObject
)domainClass
.newInstance();
150 obj
.setProperty( "id", new Long(1) );
151 obj
.setProperty( "firstName", "fr" );
152 obj
.setProperty( "lastName", "flintstone" );
154 obj
.invokeMethod("validate", null);
156 Errors errors
= (Errors
)obj
.getProperty("errors");
157 assertNotNull(errors
);
158 assertTrue(errors
.hasErrors());
161 public void testValidateMethodWithFieldList() {
162 // init spring config
165 GrailsDomainClass domainClass
= (GrailsDomainClass
) this.grailsApplication
.getArtefact(DomainClassArtefactHandler
.TYPE
,
166 "PersistentMethodTests");
168 GroovyObject obj
= (GroovyObject
)domainClass
.newInstance();
169 obj
.setProperty( "id", new Long(1) );
170 obj
.setProperty( "firstName", "fr" );
171 obj
.setProperty( "lastName", "flintstone" );
173 Object result
= obj
.invokeMethod("validate", Arrays
.asList(new Object
[]{"age"}));
174 assertEquals(Boolean
.TRUE
, result
);
176 result
= obj
.invokeMethod("validate", Arrays
.asList(new Object
[]{"firstName"}));
177 assertEquals(Boolean
.FALSE
, result
);
180 public void testValidatePersistentMethodOnDerivedClass() {
181 GrailsDomainClass domainClass
= (GrailsDomainClass
) this.grailsApplication
.getArtefact(DomainClassArtefactHandler
.TYPE
,
182 "PersistentMethodTestsDescendent");
184 GroovyObject obj
= (GroovyObject
)domainClass
.newInstance();
185 obj
.setProperty( "id", new Long(1) );
186 obj
.setProperty( "gender", "female" );
188 obj
.invokeMethod("validate", null);
190 Errors errors
= (Errors
)obj
.getProperty("errors");
191 assertNotNull(errors
);
192 assertTrue(errors
.hasErrors());
194 // Check that nullable constraints on superclass are throwing errors
195 obj
= (GroovyObject
)domainClass
.newInstance();
196 obj
.setProperty( "id", new Long(1) );
197 obj
.setProperty( "gender", "female" );
198 obj
.setProperty( "firstName", "Marc" );
200 obj
.invokeMethod("validate", null);
202 errors
= (Errors
)obj
.getProperty("errors");
204 System
.out
.println("errors = " + errors
);
206 assertNotNull(errors
);
207 assertTrue(errors
.hasErrors());
208 assertEquals(1, errors
.getFieldErrorCount("lastName"));
211 public void testFindPersistentMethods() {
212 GrailsDomainClass domainClass
= (GrailsDomainClass
) this.grailsApplication
.getArtefact(DomainClassArtefactHandler
.TYPE
,
213 "PersistentMethodTests");
215 GroovyObject obj
= (GroovyObject
)domainClass
.newInstance();
216 obj
.setProperty( "id", new Long(1) );
217 obj
.setProperty( "firstName", "fred" );
218 obj
.setProperty( "lastName", "flintstone" );
219 obj
.setProperty( "age", new Integer(45));
221 obj
.invokeMethod("save", null);
223 // test query without a method
225 obj
.getMetaClass().invokeStaticMethod(obj
, "find", new Object
[] {} );
226 fail("Should have thrown an exception");
232 // test invalid query
234 obj
.getMetaClass().invokeStaticMethod(obj
, "find", new Object
[] {"from AnotherClass"} );
235 fail("Should have thrown grails query exception");
237 catch(GroovyRuntimeException gqe
) {
241 // test find with HQL query
242 List params
= new ArrayList();
244 Object returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "find", new Object
[] { "from PersistentMethodTests where firstName like ?", params
});
245 assertNotNull(returnValue
);
247 // test find with HQL query
250 returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "find", new Object
[] { "from PersistentMethodTests where firstName like ?", params
});
251 assertNull(returnValue
);
253 // test find with HQL query and array of params
254 Object
[] paramsArray
= new Object
[] {"fre%"};
255 returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "find", new Object
[] { "from PersistentMethodTests where firstName like ?", paramsArray
});
256 assertNotNull(returnValue
);
258 // test with a GString argument
259 Binding b
= new Binding();
260 b
.setVariable("test","fre%");
261 b
.setVariable("test1", "flint%");
262 GString gs
= (GString
)new GroovyShell(b
).evaluate("\"$test\"");
263 GString gs1
= (GString
)new GroovyShell(b
).evaluate("\"$test1\"");
268 returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "find", new Object
[] { "from PersistentMethodTests where firstName like ? and lastName like ?", params
});
269 assertNotNull(returnValue
);
271 // test named params with GString parameters
272 Map namedArgs
= new HashMap();
273 namedArgs
.put("name", gs
);
274 returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "find", new Object
[] { "from PersistentMethodTests where firstName like :name", namedArgs
});
275 assertNotNull(returnValue
);
277 // test with a GString query
278 b
.setVariable("className","PersistentMethodTests");
279 gs
= (GString
)new GroovyShell(b
).evaluate("\"from ${className} where firstName like ? and lastName like ?\"");
281 returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "find", new Object
[] { gs
, params
});
282 assertNotNull(returnValue
);
284 // test find with query and named params
286 namedArgs
.put( "name", "fred" );
287 returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "find", new Object
[] { "from PersistentMethodTests as p where p.firstName = :name", namedArgs
});
288 assertNotNull(returnValue
);
290 // test find with query and named list params
292 List namesList
= new ArrayList();
293 namesList
.add("fred");
294 namesList
.add("anothername");
295 namedArgs
.put( "namesList", namesList
);
296 returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "find", new Object
[] { "from PersistentMethodTests as p where p.firstName in (:namesList)", namedArgs
});
297 assertNotNull(returnValue
);
299 // test find with query and named array params
301 namedArgs
.put( "namesList", new Object
[] {"fred","anothername"} );
302 returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "find", new Object
[] { "from PersistentMethodTests as p where p.firstName in (:namesList)", namedArgs
});
303 assertNotNull(returnValue
);
305 // test query with wrong named parameter
308 namedArgs
.put(new Long(1), "fred");
309 obj
.getMetaClass().invokeStaticMethod(obj
, "find", new Object
[] { "from PersistentMethodTests as p where p.firstName = :name", namedArgs
});
310 // new Long(1) is not valid name for named param, so exception should be thrown
311 fail("Should have thrown grails query exception");
313 catch(GroovyRuntimeException gqe
) {
317 // test find by example
318 GroovyObject example
= (GroovyObject
)domainClass
.newInstance();
319 example
.setProperty( "firstName", "fred" );
320 returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "find", new Object
[] { example
});
321 assertNotNull(returnValue
);
323 // test find by wrong example
324 example
= (GroovyObject
)domainClass
.newInstance();
325 example
.setProperty( "firstName", "someone" );
326 returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "find", new Object
[] { example
});
327 assertNull(returnValue
);
329 // test query with wrong argument type
331 obj
.getMetaClass().invokeStaticMethod(obj
, "find", new Object
[] { new Date()});
332 fail("Should have thrown an exception");
339 public void testFindByPersistentMethods() {
340 GrailsDomainClass domainClass
= (GrailsDomainClass
) this.grailsApplication
.getArtefact(DomainClassArtefactHandler
.TYPE
,
341 "PersistentMethodTests");
343 GroovyObject obj
= (GroovyObject
)domainClass
.newInstance();
344 obj
.setProperty( "id", new Long(1) );
345 obj
.setProperty( "firstName", "fred" );
346 obj
.setProperty( "lastName", "flintstone" );
347 obj
.setProperty( "age", new Integer(45));
349 obj
.invokeMethod("save", null);
351 GroovyObject obj2
= (GroovyObject
)domainClass
.newInstance();
352 obj2
.setProperty( "id", new Long(2) );
353 obj2
.setProperty( "firstName", "wilma" );
354 obj2
.setProperty( "lastName", "flintstone" );
355 obj2
.setProperty( "age", new Integer(42));
356 obj2
.invokeMethod("save", null);
358 GroovyObject obj3
= (GroovyObject
)domainClass
.newInstance();
359 obj3
.setProperty( "id", new Long(3) );
360 obj3
.setProperty( "firstName", "dino" );
361 obj3
.setProperty( "lastName", "dinosaur" );
362 obj3
.setProperty( "age", new Integer(12));
363 obj3
.invokeMethod("save", null);
365 Object returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "findAllByFirstName", new Object
[] { "fred", new Integer(10) });
366 assertNotNull(returnValue
);
367 assertTrue(returnValue
instanceof List
);
369 List returnList
= (List
)returnValue
;
370 assertEquals(1, returnList
.size());
372 returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "findAllByFirstNameAndLastName", new Object
[] { "fred", "flintstone" });
373 assertNotNull(returnValue
);
374 assertTrue(returnValue
instanceof List
);
376 returnList
= (List
)returnValue
;
377 assertEquals(1, returnList
.size());
379 /*returnValue = obj.getMetaClass().invokeStaticMethod(obj, "findByFirstNameOrLastName", new Object[] { "fred", "flintstone" });
380 assertNotNull(returnValue);
381 assertTrue(returnValue instanceof List);
383 returnList = (List)returnValue;
384 assertEquals(2, returnList.size());*/
386 returnList
= (List
)obj
.getMetaClass().invokeStaticMethod(obj
, "findAllByFirstNameNotEqual", new Object
[] { "fred" });
387 assertEquals(2, returnList
.size());
388 obj
= (GroovyObject
)returnList
.get(0);
389 obj2
= (GroovyObject
)returnList
.get(1);
390 assertFalse("fred".equals( obj
.getProperty("firstName")));
391 assertFalse("fred".equals( obj2
.getProperty("firstName")));
393 returnList
= (List
)obj
.getMetaClass().invokeStaticMethod(obj
, "findAllByAgeLessThan", new Object
[] { new Integer(20) });
394 assertEquals(1, returnList
.size());
395 obj
= (GroovyObject
)returnList
.get(0);
396 assertEquals("dino", obj
.getProperty("firstName"));
398 returnList
= (List
)obj
.getMetaClass().invokeStaticMethod(obj
, "findAllByAgeLessThanEquals", new Object
[] { new Integer(12) });
399 assertEquals(1, returnList
.size());
401 returnList
= (List
)obj
.getMetaClass().invokeStaticMethod(obj
, "findAllByAgeGreaterThan", new Object
[] { new Integer(20) });
402 assertEquals(2, returnList
.size());
404 returnList
= (List
)obj
.getMetaClass().invokeStaticMethod(obj
, "findAllByAgeGreaterThanEquals", new Object
[] { new Integer(42) });
405 assertEquals(2, returnList
.size());
407 returnList
= (List
)obj
.getMetaClass().invokeStaticMethod(obj
, "findAllByAgeGreaterThanAndLastName", new Object
[] { new Integer(20), "flintstone" });
408 assertEquals(2, returnList
.size());
410 returnList
= (List
)obj
.getMetaClass().invokeStaticMethod(obj
, "findAllByLastNameLike", new Object
[] { "flint%" });
411 assertEquals(2, returnList
.size());
413 returnList
= (List
)obj
.getMetaClass().invokeStaticMethod(obj
, "findAllByLastNameIlike", new Object
[] { "FLINT%" });
414 assertEquals(2, returnList
.size());
416 returnList
= (List
)obj
.getMetaClass().invokeStaticMethod(obj
, "findAllByAgeBetween", new Object
[] { new Integer(10), new Integer(43) });
417 assertEquals(2, returnList
.size());
420 returnList
= (List
)obj
.getMetaClass().invokeStaticMethod(obj
, "findAllByActive", new Object
[] { Boolean
.TRUE
});
421 assertEquals(3, returnList
.size());
423 Map queryMap
= new HashMap();
424 queryMap
.put("firstName", "wilma");
425 queryMap
.put("lastName", "flintstone");
426 returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "findWhere", new Object
[] { queryMap
});
427 assertNotNull(returnValue
);
429 queryMap
= new HashMap();
430 queryMap
.put("lastName", "flintstone");
431 returnList
= (List
)obj
.getMetaClass().invokeStaticMethod(obj
, "findAllWhere", new Object
[] { queryMap
});
432 assertEquals(2, returnList
.size());
434 // now lets test several automatic type conversions
435 returnList
= (List
)obj
.getMetaClass().invokeStaticMethod(obj
, "findAllById", new Object
[] { "1" });
436 assertEquals(1, returnList
.size());
438 returnList
= (List
)obj
.getMetaClass().invokeStaticMethod(obj
, "findAllById", new Object
[] { new Integer("1") });
439 assertEquals(1, returnList
.size());
441 // and case when automatic conversion cannot be applied
443 returnList
= (List
)obj
.getMetaClass().invokeStaticMethod(obj
, "findAllById", new Object
[] { "1.1" });
444 } catch(MissingMethodException iae
) {
447 catch(InvokerInvocationException iie
) {
452 // and the wrong number of arguments!
454 obj
.getMetaClass().invokeStaticMethod(obj
, "findAllByAgeBetween", new Object
[] { new Integer(10) });
455 fail("Should have thrown an exception for invalid argument count");
457 catch(MissingMethodException mme
) {
460 catch(InvokerInvocationException iie
) {
466 public void testGetPersistentMethod() {
467 GrailsDomainClass domainClass
= (GrailsDomainClass
) this.grailsApplication
.getArtefact(DomainClassArtefactHandler
.TYPE
,
468 "PersistentMethodTests");
470 GroovyObject obj
= (GroovyObject
)domainClass
.newInstance();
471 obj
.setProperty( "id", new Long(1) );
472 obj
.setProperty( "firstName", "fred" );
473 obj
.setProperty( "lastName", "flintstone" );
475 obj
.invokeMethod("save", null);
477 GroovyObject obj2
= (GroovyObject
)domainClass
.newInstance();
478 obj2
.setProperty( "id", new Long(2) );
479 obj2
.setProperty( "firstName", "wilma" );
480 obj2
.setProperty( "lastName", "flintstone" );
482 obj2
.invokeMethod("save", null);
485 Object returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "get", new Object
[] { new Long(2) });
486 assertNotNull(returnValue
);
487 assertEquals(returnValue
.getClass(),domainClass
.getClazz());
490 public void testGetAllPersistentMethod() {
491 GrailsDomainClass domainClass
= (GrailsDomainClass
) this.grailsApplication
.getArtefact(DomainClassArtefactHandler
.TYPE
,
492 "PersistentMethodTests");
494 GroovyObject obj
= (GroovyObject
)domainClass
.newInstance();
495 obj
.setProperty( "id", new Long(1) );
496 obj
.setProperty( "firstName", "fred" );
497 obj
.setProperty( "lastName", "flintstone" );
499 obj
.invokeMethod("save", null);
501 GroovyObject obj2
= (GroovyObject
)domainClass
.newInstance();
502 obj2
.setProperty( "id", new Long(2) );
503 obj2
.setProperty( "firstName", "wilma" );
504 obj2
.setProperty( "lastName", "flintstone" );
506 obj2
.invokeMethod("save", null);
508 GroovyObject obj3
= (GroovyObject
)domainClass
.newInstance();
509 obj3
.setProperty( "id", new Long(3) );
510 obj3
.setProperty( "firstName", "john" );
511 obj3
.setProperty( "lastName", "smith" );
513 obj3
.invokeMethod("save", null);
515 // get wilma and fred by ids passed as method arguments
516 List args
= new ArrayList();
517 args
.add(new Long(2));
518 args
.add(new Long(1));
520 Object returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "getAll", new Object
[] { args
});
521 assertNotNull(returnValue
);
522 assertEquals(ArrayList
.class,returnValue
.getClass());
523 List returnList
= (List
)returnValue
;
524 assertEquals(2, returnList
.size());
525 GroovyObject result
= (GroovyObject
)returnList
.get(0);
526 GroovyObject result1
= (GroovyObject
)returnList
.get(1);
527 assertEquals(new Long(2), result
.getProperty("id"));
528 assertEquals(new Long(1), result1
.getProperty("id"));
530 // get john and fred by ids passed in list
531 List param
= new ArrayList();
532 param
.add( new Long(3) );
533 param
.add( new Long(1) );
534 returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "getAll", new Object
[] { param
});
535 assertNotNull(returnValue
);
536 assertEquals(ArrayList
.class,returnValue
.getClass());
537 returnList
= (List
)returnValue
;
538 assertEquals(2, returnList
.size());
539 result
= (GroovyObject
)returnList
.get(0);
540 result1
= (GroovyObject
)returnList
.get(1);
541 assertEquals(new Long(3), result
.getProperty("id"));
542 assertEquals(new Long(1), result1
.getProperty("id"));
544 // when called without arguments should return a list of all objects
545 returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "getAll", new Object
[] {});
546 assertNotNull(returnValue
);
547 assertEquals(ArrayList
.class,returnValue
.getClass());
548 returnList
= (List
)returnValue
;
549 assertEquals(3, returnList
.size());
551 args
= new ArrayList();
552 args
.add(new Long(5));
553 args
.add(new Long(2));
554 args
.add(new Long(7));
555 args
.add(new Long(1));
556 // if there are no object with specified id - should return null on corresponding places
557 returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "getAll", new Object
[] {args
});
558 assertNotNull(returnValue
);
559 assertEquals(ArrayList
.class,returnValue
.getClass());
560 returnList
= (List
)returnValue
;
561 assertEquals(4, returnList
.size());
562 assertNull(returnList
.get(0));
563 assertNull(returnList
.get(2));
566 public void testDiscardMethod() {
567 GrailsDomainClass domainClass
= (GrailsDomainClass
) this.grailsApplication
.getArtefact(DomainClassArtefactHandler
.TYPE
,
568 "PersistentMethodTests");
570 GroovyObject obj
= (GroovyObject
)domainClass
.newInstance();
571 obj
.setProperty( "id", new Long(1) );
572 obj
.setProperty( "firstName", "fred" );
573 obj
.setProperty( "lastName", "flintstone" );
575 obj
.invokeMethod("save", null);
577 assertTrue(hibSession
.contains(obj
));
578 obj
.invokeMethod("discard", null);
579 assertFalse(hibSession
.contains(obj
));
582 public void testFindAllPersistentMethod() {
583 GrailsDomainClass domainClass
= (GrailsDomainClass
) this.grailsApplication
.getArtefact(DomainClassArtefactHandler
.TYPE
,
584 "PersistentMethodTests");
586 GroovyObject obj
= (GroovyObject
)domainClass
.newInstance();
587 obj
.setProperty( "id", new Long(1) );
588 obj
.setProperty( "firstName", "fred" );
589 obj
.setProperty( "lastName", "flintstone" );
591 obj
.invokeMethod("save", null);
593 GroovyObject obj2
= (GroovyObject
)domainClass
.newInstance();
594 obj2
.setProperty( "id", new Long(2) );
595 obj2
.setProperty( "firstName", "wilma" );
596 obj2
.setProperty( "lastName", "flintstone" );
598 obj2
.invokeMethod("save", null);
600 // test invalid query
602 obj
.getMetaClass().invokeStaticMethod(obj
, "findAll", new Object
[] {"from AnotherClass"} );
603 fail("Should have thrown grails query exception");
605 catch(GroovyRuntimeException gqe
) {
609 // test find with a query
610 Object returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "findAll", new Object
[] { "from PersistentMethodTests" });
611 assertNotNull(returnValue
);
612 assertEquals(ArrayList
.class,returnValue
.getClass());
613 List listResult
= (List
)returnValue
;
614 assertEquals(2, listResult
.size());
616 // test without a query (should return all instances)
617 returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "findAll", new Object
[] {} );
618 assertNotNull(returnValue
);
619 assertEquals(ArrayList
.class, returnValue
.getClass());
620 listResult
= (List
)returnValue
;
621 assertEquals(2, listResult
.size());
624 // test find with query and args
625 List args
= new ArrayList();
627 returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "findAll", new Object
[] { "from PersistentMethodTests as p where p.firstName = ?", args
});
628 assertNotNull(returnValue
);
629 assertEquals(ArrayList
.class,returnValue
.getClass());
630 listResult
= (List
)returnValue
;
631 assertEquals(1, listResult
.size());
633 // test find with query and array argument
634 returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "findAll", new Object
[] { "from PersistentMethodTests as p where p.firstName = ?", new Object
[] {"wilma"} });
635 assertNotNull(returnValue
);
636 assertEquals(ArrayList
.class,returnValue
.getClass());
637 listResult
= (List
)returnValue
;
638 assertEquals(1, listResult
.size());
640 // test find with query and named params
641 Map namedArgs
= new HashMap();
642 namedArgs
.put( "name", "wilma" );
643 returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "findAll", new Object
[] { "from PersistentMethodTests as p where p.firstName = :name", namedArgs
});
644 assertNotNull(returnValue
);
645 assertEquals(ArrayList
.class,returnValue
.getClass());
646 listResult
= (List
)returnValue
;
647 assertEquals(1, listResult
.size());
649 // test with a GString argument
650 Binding b
= new Binding();
651 b
.setVariable("test","fre%");
652 b
.setVariable("test1", "flint%");
653 GString gs
= (GString
)new GroovyShell(b
).evaluate("\"$test\"");
654 GString gs1
= (GString
)new GroovyShell(b
).evaluate("\"$test1\"");
658 returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "findAll", new Object
[] { "from PersistentMethodTests where firstName like ? and lastName like ?", args
});
659 assertNotNull(returnValue
);
660 assertTrue(returnValue
instanceof List
);
661 listResult
= (List
)returnValue
;
662 assertEquals(1, listResult
.size());
664 // GStrings in named params
666 namedArgs
.put("firstName", gs
);
667 namedArgs
.put("lastName", gs1
);
668 returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "findAll", new Object
[] { "from PersistentMethodTests where firstName like :firstName and lastName like :lastName", namedArgs
});
669 assertNotNull(returnValue
);
670 assertTrue(returnValue
instanceof List
);
671 listResult
= (List
)returnValue
;
672 assertEquals(1, listResult
.size());
674 // test with a GString query
675 b
.setVariable("className","PersistentMethodTests");
676 gs
= (GString
)new GroovyShell(b
).evaluate("\"from ${className} where firstName like ? and lastName like ?\"");
677 returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "findAll", new Object
[] { gs
, args
});
678 assertNotNull(returnValue
);
681 // test find with query and named list params
683 List namesList
= new ArrayList();
684 namesList
.add("wilma");
685 namesList
.add("fred");
686 namedArgs
.put( "namesList", namesList
);
687 returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "findAll", new Object
[] { "from PersistentMethodTests as p where p.firstName in (:namesList)", namedArgs
});
688 assertNotNull(returnValue
);
689 assertEquals(ArrayList
.class,returnValue
.getClass());
690 listResult
= (List
)returnValue
;
691 assertEquals(2, listResult
.size());
693 // test find with query and named array params
695 namedArgs
.put( "namesList", new Object
[] {"wilma","fred"} );
696 returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "findAll", new Object
[] { "from PersistentMethodTests as p where p.firstName in (:namesList)", namedArgs
});
697 assertNotNull(returnValue
);
698 assertEquals(ArrayList
.class,returnValue
.getClass());
699 listResult
= (List
)returnValue
;
700 assertEquals(2, listResult
.size());
702 // test find with max result
704 namedArgs
.put( "namesList", new Object
[] {"wilma","fred"} );
705 returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "findAll", new Object
[] { "from PersistentMethodTests as p where p.firstName in (:namesList)", namedArgs
, new Integer(1) });
706 assertNotNull(returnValue
);
707 assertEquals(ArrayList
.class,returnValue
.getClass());
708 listResult
= (List
)returnValue
;
709 assertEquals(1, listResult
.size());
711 // test find with max result without params
712 returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "findAll", new Object
[] { "from PersistentMethodTests as p", new Integer(1) });
713 assertNotNull(returnValue
);
714 assertEquals(ArrayList
.class,returnValue
.getClass());
715 listResult
= (List
)returnValue
;
716 assertEquals(1, listResult
.size());
718 // test with max result in Map
719 Map resultsMap
= new HashMap();
720 resultsMap
.put("max", new Integer(1));
721 returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "findAll", new Object
[] { "from PersistentMethodTests as p where p.firstName in (:namesList)", namedArgs
, resultsMap
});
722 assertNotNull(returnValue
);
723 assertEquals(ArrayList
.class,returnValue
.getClass());
724 listResult
= (List
)returnValue
;
725 assertEquals(1, listResult
.size());
726 // test with 'max' param in named parameter map - for backward compatibility
727 namedArgs
.put("max", new Integer(1));
728 returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "findAll", new Object
[] { "from PersistentMethodTests as p where p.firstName in (:namesList)", namedArgs
});
729 assertNotNull(returnValue
);
730 assertEquals(ArrayList
.class,returnValue
.getClass());
731 listResult
= (List
)returnValue
;
732 assertEquals(1, listResult
.size());
734 // test find with offset
736 namedArgs
.put( "namesList", new Object
[] {"wilma","fred"} );
737 returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "findAll", new Object
[] { "from PersistentMethodTests as p where p.firstName in (:namesList)", namedArgs
, new Integer(2), new Integer(1) });
738 assertNotNull(returnValue
);
739 assertEquals(ArrayList
.class,returnValue
.getClass());
740 listResult
= (List
)returnValue
;
741 // max results = 2 and offset = 1 => 1 of 2 result expected
742 assertEquals(1, listResult
.size());
744 // test find with offset without params
745 returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "findAll", new Object
[] { "from PersistentMethodTests as p", new Integer(2), new Integer(1) });
746 assertNotNull(returnValue
);
747 assertEquals(ArrayList
.class,returnValue
.getClass());
748 listResult
= (List
)returnValue
;
749 // max results = 2 and offset = 1 => 1 of 2 result expected
750 assertEquals(1, listResult
.size());
752 // test with offset in Map
753 resultsMap
= new HashMap();
754 resultsMap
.put("offset", new Integer(1));
755 returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "findAll", new Object
[] { "from PersistentMethodTests as p where p.firstName in (:namesList)", namedArgs
, resultsMap
});
756 assertNotNull(returnValue
);
757 assertEquals(ArrayList
.class,returnValue
.getClass());
758 listResult
= (List
)returnValue
;
759 // max results not specified and offset = 1 => 1 of 2 result expected
760 assertEquals(1, listResult
.size());
762 // test with 'offset' param in named parameter map - for backward compatibility
763 namedArgs
.put("offset", new Integer(1));
764 returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "findAll", new Object
[] { "from PersistentMethodTests as p where p.firstName in (:namesList)", namedArgs
});
765 assertNotNull(returnValue
);
766 assertEquals(ArrayList
.class,returnValue
.getClass());
767 listResult
= (List
)returnValue
;
768 assertEquals(1, listResult
.size());
770 // test query with wrong named parameter
773 namedArgs
.put(new Long(1), "wilma");
774 obj
.getMetaClass().invokeStaticMethod(obj
, "findAll", new Object
[] { "from PersistentMethodTests as p where p.firstName = :name", namedArgs
});
775 // new Long(1) is not valid name for named param, so exception should be thrown
776 fail("Should have thrown grails query exception");
778 catch(GroovyRuntimeException gqe
) {
782 // test find by example
783 GroovyObject example
= (GroovyObject
)domainClass
.newInstance();
784 example
.setProperty( "firstName", "fred" );
785 returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
, "findAll", new Object
[] { example
});
786 assertNotNull(returnValue
);
787 assertEquals(ArrayList
.class,returnValue
.getClass());
788 listResult
= (List
)returnValue
;
789 assertEquals(1, listResult
.size());
791 // test query with wrong argument type
793 obj
.getMetaClass().invokeStaticMethod(obj
, "findAll", new Object
[] { new Date()});
794 fail("Should have thrown an exception");
801 public void testListPersistentMethods() {
802 GrailsDomainClass domainClass
= (GrailsDomainClass
) this.grailsApplication
.getArtefact(DomainClassArtefactHandler
.TYPE
,
803 "PersistentMethodTests");
805 GroovyObject obj
= (GroovyObject
)domainClass
.newInstance();
806 obj
.setProperty( "id", new Long(1) );
807 obj
.setProperty( "firstName", "fred" );
808 obj
.setProperty( "lastName", "flintstone" );
810 obj
.invokeMethod("save", null);
812 GroovyObject obj2
= (GroovyObject
)domainClass
.newInstance();
813 obj2
.setProperty( "id", new Long(2) );
814 obj2
.setProperty( "firstName", "wilma" );
815 obj2
.setProperty( "lastName", "flintstone" );
817 obj2
.invokeMethod("save", null);
819 GroovyObject obj3
= (GroovyObject
)domainClass
.newInstance();
820 obj3
.setProperty( "id", new Long(3) );
821 obj3
.setProperty( "firstName", "dino" );
822 obj3
.setProperty( "lastName", "dinosaur" );
824 obj3
.invokeMethod("save", null);
827 Object returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
,"list", null);
828 assertNotNull(returnValue
);
829 assertTrue(returnValue
instanceof List
);
831 List returnList
= (List
)returnValue
;
832 assertEquals(3, returnList
.size());
833 // test list with max value
834 Map argsMap
= new HashMap();
835 argsMap
.put("max",new Integer(1));
836 returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
,"list", new Object
[]{ argsMap
});
837 assertNotNull(returnValue
);
838 assertTrue(returnValue
instanceof List
);
840 returnList
= (List
)returnValue
;
841 assertEquals(1, returnList
.size());
843 // test list with order by desc
844 argsMap
= new HashMap();
845 argsMap
.put("order", "desc");
846 argsMap
.put("sort", "firstName");
848 returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
,
849 "listOrderByFirstName", new Object
[] { argsMap
});
850 assertNotNull(returnValue
);
851 assertTrue(returnValue
instanceof List
);
853 returnList
= (List
) returnValue
;
854 obj
= (GroovyObject
) returnList
.get(0);
855 obj2
= (GroovyObject
) returnList
.get(1);
857 assertEquals("wilma", obj
.getProperty("firstName"));
858 assertEquals("fred", obj2
.getProperty("firstName"));
864 public void testExecuteQueryMethod() {
865 GrailsDomainClass domainClass
= (GrailsDomainClass
) this.grailsApplication
.getArtefact(DomainClassArtefactHandler
.TYPE
,
866 "PersistentMethodTests");
868 GroovyObject obj
= (GroovyObject
)domainClass
.newInstance();
869 obj
.setProperty( "id", new Long(1) );
870 obj
.setProperty( "firstName", "fred" );
871 obj
.setProperty( "lastName", "flintstone" );
872 obj
.invokeMethod("save", null);
874 obj
= (GroovyObject
)domainClass
.newInstance();
875 obj
.setProperty( "id", new Long(2) );
876 obj
.setProperty( "firstName", "wilma" );
877 obj
.setProperty( "lastName", "flintstone" );
878 obj
.invokeMethod("save", null);
880 MetaClass domain
= obj
.getMetaClass();
882 // test query without a method
884 domain
.invokeStaticMethod(obj
, "executeQuery", new Object
[] {} );
885 fail("Should have thrown an exception");
891 // test query with too many params
893 domain
.invokeStaticMethod(obj
, "executeQuery", new Object
[] { "query", "param", new HashMap(), "4" } );
894 fail("Should have thrown an exception");
900 // test query with wrong third param type (must be Map)
902 domain
.invokeStaticMethod(obj
, "executeQuery", new Object
[] { "query", "param", "wrong third param" } );
903 fail("Should have thrown an exception");
909 // test find with a query
910 Object returnValue
= domain
.invokeStaticMethod(obj
, "executeQuery", new Object
[] { "select distinct p from PersistentMethodTests as p" });
911 assertNotNull( returnValue
);
912 assertEquals( ArrayList
.class, returnValue
.getClass() );
913 List listResult
= (List
)returnValue
;
914 assertEquals(2, listResult
.size());
916 // test find with a query and paginate params
917 Map paginateParams
= new HashMap();
918 paginateParams
.put( "max", new Integer(1) );
919 listResult
= (List
)obj
.getMetaClass().invokeStaticMethod(obj
, "executeQuery", new Object
[] { "select distinct p from PersistentMethodTests as p order by p.firstName", paginateParams
});
920 assertEquals(1, listResult
.size());
921 assertEquals("fred", ((GroovyObject
)listResult
.get(0)).getProperty("firstName"));
922 paginateParams
.put( "offset", new Integer(1) );
923 listResult
= (List
)obj
.getMetaClass().invokeStaticMethod(obj
, "executeQuery", new Object
[] { "select distinct p from PersistentMethodTests as p order by p.firstName", paginateParams
});
924 assertEquals(1, listResult
.size());
925 assertEquals("wilma", ((GroovyObject
)listResult
.get(0)).getProperty("firstName"));
927 // test find with query and args
928 List args
= new ArrayList();
930 listResult
= (List
) domain
.invokeStaticMethod(obj
, "executeQuery", new Object
[] { "select distinct p from PersistentMethodTests as p where p.firstName = ?", args
});
931 assertEquals(1, listResult
.size());
933 // test find with query and arg
934 listResult
= (List
)domain
.invokeStaticMethod(obj
, "executeQuery", new Object
[] { "select distinct p from PersistentMethodTests as p where p.firstName = ?", "wilma" });
935 assertEquals(1, listResult
.size());
937 // test find with query and named params
938 Map namedArgs
= new HashMap();
939 namedArgs
.put( "name", "wilma" );
940 listResult
= (List
)domain
.invokeStaticMethod(obj
, "executeQuery", new Object
[] { "select distinct p from PersistentMethodTests as p where p.firstName = :name", namedArgs
});
941 assertEquals(1, listResult
.size());
943 // test find with query and named list params
945 List namesList
= new ArrayList();
946 namesList
.add("wilma");
947 namesList
.add("fred");
948 namedArgs
.put( "namesList", namesList
);
949 listResult
= (List
)domain
.invokeStaticMethod(obj
, "executeQuery", new Object
[] { "select distinct p from PersistentMethodTests as p where p.firstName in (:namesList) order by p.firstName", namedArgs
});
950 assertEquals(2, listResult
.size());
951 // test find with a query and named list params and paginate params
952 paginateParams
.clear();
953 paginateParams
.put( "max", new Integer(1) );
954 listResult
= (List
)domain
.invokeStaticMethod(obj
, "executeQuery", new Object
[] { "select distinct p from PersistentMethodTests as p where p.firstName in (:namesList) order by p.firstName", namedArgs
, paginateParams
});
955 assertEquals(1, listResult
.size());
956 assertEquals("fred", ((GroovyObject
)listResult
.get(0)).getProperty("firstName"));
957 paginateParams
.put( "offset", new Integer(1) );
958 listResult
= (List
)domain
.invokeStaticMethod(obj
, "executeQuery", new Object
[] { "select distinct p from PersistentMethodTests as p where p.firstName in (:namesList) order by p.firstName", namedArgs
, paginateParams
});
959 assertEquals(1, listResult
.size());
960 assertEquals("wilma", ((GroovyObject
)listResult
.get(0)).getProperty("firstName"));
962 // test query with wrong named parameter
965 namedArgs
.put(new Long(1), "wilma");
966 domain
.invokeStaticMethod(obj
, "executeQuery", new Object
[] { "select distinct p from PersistentMethodTests as p where p.firstName = :name", namedArgs
});
967 // new Long(1) is not valid name for named param, so exception should be thrown
968 fail("Should have thrown grails query exception");
970 catch(GroovyRuntimeException gqe
) {
975 public void testDMLOperation() throws Exception
{
976 GrailsDomainClass domainClass
= (GrailsDomainClass
) this.grailsApplication
.getArtefact(DomainClassArtefactHandler
.TYPE
,
977 "PersistentMethodTests");
979 GroovyObject obj
= (GroovyObject
)domainClass
.newInstance();
980 obj
.setProperty( "id", new Long(1) );
981 obj
.setProperty( "firstName", "fred" );
982 obj
.setProperty( "lastName", "flintstone" );
984 obj
.invokeMethod("save", null);
986 GroovyObject obj2
= (GroovyObject
)domainClass
.newInstance();
987 obj2
.setProperty( "id", new Long(2) );
988 obj2
.setProperty( "firstName", "wilma" );
989 obj2
.setProperty( "lastName", "flintstone" );
991 obj2
.invokeMethod("save", null);
993 GroovyObject obj3
= (GroovyObject
)domainClass
.newInstance();
994 obj3
.setProperty( "id", new Long(3) );
995 obj3
.setProperty( "firstName", "dino" );
996 obj3
.setProperty( "lastName", "dinosaur" );
998 obj3
.invokeMethod("save", null);
1000 Object returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
,"list", null);
1001 assertNotNull(returnValue
);
1002 assertTrue(returnValue
instanceof List
);
1004 List returnList
= (List
)returnValue
;
1005 assertEquals(3, returnList
.size());
1007 obj
.getMetaClass().invokeStaticMethod(obj
,"executeUpdate", new Object
[]{"delete PersistentMethodTests"});
1009 returnValue
= obj
.getMetaClass().invokeStaticMethod(obj
,"list", null);
1010 assertNotNull(returnValue
);
1011 assertTrue(returnValue
instanceof List
);
1013 returnList
= (List
)returnValue
;
1014 assertEquals(0, returnList
.size());