4141public class AddRelatedDynamicMethodTests extends TestCase {
4242
4343 public void testInvoke () throws Exception {
44- GroovyClassLoader gcl = new GroovyClassLoader ();
45- gcl .parseClass ("class Book {\n " +
46- "\t Long id\n " +
47- "\t Long version\n " +
48- "\n " +
49- "\t Author author\n " +
50- "}\n " +
51- "class Author {\n " +
52- "\t Long id\n " +
53- "\t Long version\n " +
54- "\t def relatesToMany = [books:Book]\n " +
55- "\t Set books\n " +
56- "}" );
57-
58-
59- GroovyObject book = (GroovyObject )gcl .loadClass ("Book" ,false ,true ).newInstance ();
60- GroovyObject author = (GroovyObject )gcl .loadClass ("Author" ,false ,true ).newInstance ();
61-
62- GrailsApplication ga = new DefaultGrailsApplication (new Class []{book .getClass (),author .getClass ()},gcl );
63- GrailsDomainClass authorDC = ga .getGrailsDomainClass ("Author" );
64-
65- AbstractAddRelatedDynamicMethod ardm = new AddRelatedDynamicMethod (authorDC .getPropertyByName ("books" ));
66-
67- try {
68- ardm .invoke (author , "addBook" , new Object [0 ]);
69- fail ("Should have thrown missing method exception!" );
70- } catch (MissingMethodException e ) {
71- // expected
72- }
73-
74- try {
75- ardm .invoke (author , "addBook" , new Object []{"blah" });
76- fail ("Should have thrown missing method exception!" );
77- } catch (MissingMethodException e ) {
78- // expected
79- }
80-
81- ardm .invoke (author , "addBook" , new Object []{book });
82-
83- assertEquals (author ,book .getProperty ("author" ));
84- Set books = (Set )author .getProperty ("books" );
85- assertNotNull (books );
86- assertEquals (1 ,books .size ());
87- assertTrue (books .contains (book ));
88- }
89-
90- public void testInvokeWithSortedSet () throws Exception {
9144 GroovyClassLoader gcl = new GroovyClassLoader ();
92- gcl .parseClass ("class Book implements Comparable {\n " +
45+ gcl .parseClass ("class Book {\n " +
9346 "\t Long id\n " +
9447 "\t Long version\n " +
9548 "\n " +
9649 "\t Author author\n " +
97- "int compareTo(other) { 1 }" +
9850 "}\n " +
9951 "class Author {\n " +
10052 "\t Long id\n " +
10153 "\t Long version\n " +
10254 "\t def relatesToMany = [books:Book]\n " +
103- "\t SortedSet books\n " +
104- "}" );
55+ "\t Set books\n " +
56+ "}\n " +
57+ "class SubBook extends Book{\n " +
58+ "\t String subtitle\n " +
59+ "\n " +
60+ "}"
61+ );
10562
10663
10764 GroovyObject book = (GroovyObject )gcl .loadClass ("Book" ,false ,true ).newInstance ();
65+ GroovyObject subbook = (GroovyObject )gcl .loadClass ("SubBook" ,false ,true ).newInstance ();
10866 GroovyObject author = (GroovyObject )gcl .loadClass ("Author" ,false ,true ).newInstance ();
10967
11068 GrailsApplication ga = new DefaultGrailsApplication (new Class []{book .getClass (),author .getClass ()},gcl );
@@ -113,24 +71,74 @@ public void testInvokeWithSortedSet() throws Exception {
11371 AbstractAddRelatedDynamicMethod ardm = new AddRelatedDynamicMethod (authorDC .getPropertyByName ("books" ));
11472
11573 try {
116- ardm .invoke (author , "addBook" , new Object [0 ]);
74+ ardm .invoke (author ,"addAuthor" , new Object [0 ]);
11775 fail ("Should have thrown missing method exception!" );
11876 } catch (MissingMethodException e ) {
11977 // expected
12078 }
12179
12280 try {
123- ardm .invoke (author , "addBook" , new Object []{"blah" });
81+ ardm .invoke (author ,"addAuthor" , new Object []{"blah" });
12482 fail ("Should have thrown missing method exception!" );
12583 } catch (MissingMethodException e ) {
12684 // expected
12785 }
12886
129- ardm .invoke (author , "addBook" , new Object []{book });
130-
87+ ardm .invoke (author ,"addAuthor" , new Object []{book });
88+ ardm .invoke (author ,"addAuthor" , new Object []{subbook });
89+
13190 assertEquals (author ,book .getProperty ("author" ));
132- SortedSet books = (SortedSet )author .getProperty ("books" );
91+ Set books = (Set )author .getProperty ("books" );
13392 assertNotNull (books );
93+ assertEquals (2 ,books .size ());
94+ assertTrue (books .contains (book ));
95+ }
96+
97+ public void testInvokeWithSortedSet () throws Exception {
98+ GroovyClassLoader gcl = new GroovyClassLoader ();
99+ gcl .parseClass ("class Book implements Comparable {\n " +
100+ "\t Long id\n " +
101+ "\t Long version\n " +
102+ "\n " +
103+ "\t Author author\n " +
104+ "int compareTo(other) { 1 }" +
105+ "}\n " +
106+ "class Author {\n " +
107+ "\t Long id\n " +
108+ "\t Long version\n " +
109+ "\t def relatesToMany = [books:Book]\n " +
110+ "\t SortedSet books\n " +
111+ "}" );
112+
113+
114+ GroovyObject book = (GroovyObject )gcl .loadClass ("Book" ,false ,true ).newInstance ();
115+ GroovyObject author = (GroovyObject )gcl .loadClass ("Author" ,false ,true ).newInstance ();
116+
117+ GrailsApplication ga = new DefaultGrailsApplication (new Class []{book .getClass (),author .getClass ()},gcl );
118+ GrailsDomainClass authorDC = ga .getGrailsDomainClass ("Author" );
119+
120+ AbstractAddRelatedDynamicMethod ardm = new AddRelatedDynamicMethod (authorDC .getPropertyByName ("books" ));
121+
122+ try {
123+ ardm .invoke (author ,"addAuthor" ,new Object [0 ]);
124+ fail ("Should have thrown missing method exception!" );
125+ } catch (MissingMethodException e ) {
126+ // expected
127+ }
128+
129+ try {
130+ ardm .invoke (author ,"addAuthor" ,new Object []{"blah" });
131+ fail ("Should have thrown missing method exception!" );
132+ } catch (MissingMethodException e ) {
133+ // expected
134+ }
135+
136+ ardm .invoke (author , "addAuthor" ,new Object []{book });
137+
138+ assertEquals (author ,book .getProperty ("author" ));
139+ SortedSet books = (SortedSet )author .getProperty ("books" );
140+ assertNotNull (books );
141+
142+ }
134143
135- }
136144}
0 commit comments