Skip to content

Commit cd28c51

Browse files
author
graeme
committed
fix for GRAILS-670
git-svn-id: https://svn.codehaus.org/grails/trunk@3087 1cfb16fd-6d17-0410-8ff1-b7e8e1e2867d
1 parent 7996aea commit cd28c51

File tree

2 files changed

+66
-58
lines changed

2 files changed

+66
-58
lines changed

src/persistence/org/codehaus/groovy/grails/metaclass/AddRelatedDynamicMethod.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public Object invoke(Object target, String methodName, Object[] arguments) {
5757
if(arguments[0] == null) {
5858
throw new IllegalArgumentException("Argument to ["+ this.methodName +"] cannot be null");
5959
}
60-
if(!arguments[0].getClass().equals(property.getReferencedPropertyType())) {
60+
if(!property.getReferencedPropertyType().isAssignableFrom(arguments[0].getClass())) {
6161
throw new MissingMethodException(this.methodName,target.getClass(),arguments);
6262
}
6363

test/persistence/org/codehaus/groovy/grails/domain/AddRelatedDynamicMethodTests.java

Lines changed: 65 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -41,70 +41,28 @@
4141
public class AddRelatedDynamicMethodTests extends TestCase {
4242

4343
public void testInvoke() throws Exception {
44-
GroovyClassLoader gcl = new GroovyClassLoader();
45-
gcl.parseClass("class Book {\n" +
46-
"\tLong id\n" +
47-
"\tLong version\n" +
48-
"\n" +
49-
"\tAuthor author\n" +
50-
"}\n" +
51-
"class Author {\n" +
52-
"\tLong id\n" +
53-
"\tLong version\n" +
54-
"\tdef relatesToMany = [books:Book]\n" +
55-
"\tSet 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
"\tLong id\n" +
9447
"\tLong version\n" +
9548
"\n" +
9649
"\tAuthor author\n" +
97-
"int compareTo(other) { 1 }" +
9850
"}\n" +
9951
"class Author {\n" +
10052
"\tLong id\n" +
10153
"\tLong version\n" +
10254
"\tdef relatesToMany = [books:Book]\n" +
103-
"\tSortedSet books\n" +
104-
"}");
55+
"\tSet books\n" +
56+
"}\n"+
57+
"class SubBook extends Book{\n" +
58+
"\tString 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+
"\tLong id\n" +
101+
"\tLong version\n" +
102+
"\n" +
103+
"\tAuthor author\n" +
104+
"int compareTo(other) { 1 }" +
105+
"}\n" +
106+
"class Author {\n" +
107+
"\tLong id\n" +
108+
"\tLong version\n" +
109+
"\tdef relatesToMany = [books:Book]\n" +
110+
"\tSortedSet 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

Comments
 (0)