1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.codehaus.groovy.syntax.parser;
15
16 import groovy.lang.GroovyObject;
17 import groovy.lang.MissingClassException;
18 import groovy.lang.MissingPropertyException;
19
20 import java.io.ByteArrayInputStream;
21
22 import org.codehaus.groovy.classgen.RuntimeIncompleteClassException;
23 import org.codehaus.groovy.classgen.TestSupport;
24 import org.codehaus.groovy.control.CompilationFailedException;
25
26 public class AbstractMethodCheckTest extends TestSupport {
27
28
29 /***
30 * GString literals generate an anonymous inner class which
31 * adds an implementation of the abstract String[] getStrings()
32 * method.
33 */
34 public void testAnonymousGStringAbstract() throws Exception {
35 GroovyObject object =
36 assertCompileWorks(
37 "class GStringTest {\n" +
38 " public groovy.lang.GString testGStringAbstract(Integer x, Integer y) {\n" +
39 " return \"GStringTest of ${x} and ${y}\"\n" +
40 " }\n" +
41 "}\n");
42
43 }
44
45 public void testCompleteComparable() throws Exception {
46 GroovyObject object =
47 assertCompileWorks(
48 "class ComparableTest implements java.lang.Comparable {\n" +
49 " int compareTo(java.lang.Object o) {\n" +
50 " return 0;\n" +
51 " }\n" +
52 "}\n");
53 }
54
55
56 public void testIncompleteCharSequence() throws Exception {
57
58 MissingClassException e =
59 assertCompileFailed(
60 "class IncompleteCharSequenceTest implements java.lang.CharSequence {\n" +
61 " char charAt(int pos) {\n" +
62 " return 'a';\n" +
63 " }\n" +
64 "\n" +
65 " int length() {\n" +
66 " return 5;\n" +
67 " }\n" +
68 "}\n");
69 }
70
71 public void testCompleteCharSequence() throws Exception {
72 GroovyObject object =
73 assertCompileWorks(
74 "class CompleteCharSequenceTest implements java.lang.CharSequence {\n" +
75 " char charAt(int pos) {\n" +
76 " return 'a';\n" +
77 " }\n" +
78 "\n" +
79 " int length() {\n" +
80 " return 5;\n" +
81 " }\n" +
82 "\n" +
83 " java.lang.CharSequence subSequence(int start, int end) {\n" +
84 " return null;\n" +
85 " }\n" +
86 "}\n");
87 }
88
89
90 public void testIncompleteList() throws Exception {
91
92 MissingClassException e =
93 assertCompileFailed(
94 "class IncompleteList extends java.util.AbstractList {\n" +
95 " int size() { return 0; }\n" +
96 "}\n");
97 }
98
99
100 public void testCompleteList() throws Exception {
101 GroovyObject object =
102 assertCompileWorks(
103 "class CompleteList extends java.util.AbstractList {\n" +
104 " int size() { return 0; }\n" +
105 " Object get(int pos) { return null; }\n" +
106 "}\n");
107 }
108
109
110 protected GroovyObject assertCompileWorks(String code) throws Exception {
111 Class type =
112 loader.parseClass(new ByteArrayInputStream(code.getBytes()), "ValidClass_" + getMethodName() + ".groovy");
113 return (GroovyObject) type.newInstance();
114 }
115
116 protected MissingClassException assertCompileFailed(String code) throws Exception {
117 try {
118 assertCompileWorks(code);
119
120 fail("Should have thrown an exception");
121 }
122 catch( CompilationFailedException e ) {
123 Exception cause = e.getUnit().getException(0);
124 if( cause instanceof RuntimeIncompleteClassException ) {
125 System.out.println("Worked, threw: " + cause);
126
127 return null;
128 }
129 throw e;
130 }
131 return null;
132 }
133
134 }