OSDN Git Service

android-2.1_r1 snapshot
[android-x86/sdk.git] / eclipse / plugins / com.android.ide.eclipse.tests / src / com / android / ide / eclipse / adt / internal / sdk / TestGroovy.java
1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.eclipse.org/org/documents/epl-v10.php
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.android.ide.eclipse.adt.internal.sdk;
18
19 import org.codehaus.groovy.control.CompilationFailedException;
20 import org.codehaus.groovy.control.MultipleCompilationErrorsException;
21 import org.eclipse.swt.graphics.Rectangle;
22
23 import java.io.FileNotFoundException;
24 import java.io.InputStream;
25
26 import groovy.lang.GroovyClassLoader;
27 import junit.framework.TestCase;
28
29 /**
30  * Tests we can invoke a groovy script that implements a given interface.
31  */
32 public class TestGroovy extends TestCase {
33
34     @Override
35     protected void setUp() throws Exception {
36         super.setUp();
37     }
38
39     /**
40      * This is the interface that we want our Groovy script to implement.
41      */
42     public static interface AdtTestInterface {
43
44         /** Method that returns a boolean. */
45         public boolean acceptDrag(String xmlName);
46         /** Method that returns an SWT Rectangle. */
47         public Rectangle acceptDrop(String xmlName);
48
49         /** Method that returns some Groovy object (opaque for us). */
50         public Object returnGroovyObject();
51         /** Method that accepts the Groovy object back. */
52         public boolean testGroovyObject(Object o);
53     }
54
55     /**
56      * Loads a groovy script that defines one class that implements the {@link AdtTestInterface}.
57      *
58      * @param filename The name of the script to load, that must be located in this Java package.
59      * @return A non-null instance of the groovy class on success.
60      * @throws CompilationFailedException if the groovy script failed to compile (e.g. syntax
61      *             errors or it doesn't completely implement the interface.)
62      * @throws ClassCastException if the groovy script does not implement our interface.
63      * @throws FileNotFoundException if the groovy script does not exist.
64      * @throws InstantiationException
65      * @throws IllegalAccessException
66      */
67     public AdtTestInterface loadScript(String filename)
68         throws CompilationFailedException, ClassCastException,
69                InstantiationException, IllegalAccessException,
70                FileNotFoundException {
71         // Get the input source from the sources or the JAR.
72         InputStream myGroovyStream = getClass().getResourceAsStream(filename);
73
74         // The stream is null if the file does not exists.
75         if (myGroovyStream == null) {
76             throw new FileNotFoundException(filename);
77         }
78
79         // Create a groovy class from it. Can fail to compile.
80         ClassLoader cl = getClass().getClassLoader();
81         GroovyClassLoader gcl = new GroovyClassLoader(cl);
82         Class gClass = gcl.parseClass(myGroovyStream, filename);
83
84         // Get an instance. This might throw ClassCastException.
85         return (AdtTestInterface) gClass.newInstance();
86     }
87
88     /**
89      * Tests that a {@link FileNotFoundException} is thrown if when trying
90      * to load a missing script.
91      */
92     public void testMissingScript() throws Exception {
93         try {
94             AdtTestInterface instance = loadScript("not_an_existing_script.groovy");
95         } catch (FileNotFoundException e) {
96             assertEquals("not_an_existing_script.groovy", e.getMessage());
97             return; // succeed
98         }
99
100         fail("Script failed to throw an exception on missing groovy file.");
101     }
102
103     /**
104      * Tests that a {@link ClassCastException} is thrown if the script does not
105      * implement our interface.
106      */
107     public void testInvalidInterface() throws Exception {
108         try {
109             AdtTestInterface instance = loadScript("invalid_interface.groovy");
110         } catch(ClassCastException e) {
111             // This has to fail because the script does not implement our interface
112             // The message explains why but we're not harcoding the message in the test.
113             assertNotNull(e.getMessage());
114             return; // succeed
115         }
116
117         fail("Script failed to throw a ClassCastException.");
118     }
119
120     /**
121      * Tests that a {@link CompilationFailedException} is thrown if the script
122      * is not valid.
123      */
124     public void testCompilationError() throws Exception {
125         try {
126             AdtTestInterface instance = loadScript("compile_error.groovy");
127         } catch (CompilationFailedException e) {
128             // This script does not compile, the message explains why but we're not
129             // harcoding the message in the test.
130             assertNotNull(e.getMessage());
131             return; // succeed
132         }
133
134         fail("Script failed to throw a compilation error.");
135     }
136
137     /**
138      * Tests a valid script scenario with only some basic methods
139      */
140     public void testSimpleMethods() throws Exception {
141         AdtTestInterface instance = loadScript("simple_test.groovy");
142
143         assertTrue(instance.acceptDrag("LinearLayout"));
144         assertFalse(instance.acceptDrag("RelativeLayout"));
145         assertNull(instance.acceptDrop("none"));
146
147         Rectangle r = instance.acceptDrop("LinearLayout");
148         assertNotNull(r);
149         assertEquals(new Rectangle(1, 2, 3, 4), r);
150     }
151
152     /**
153      * Tests a valid script scenario with some methods providing some callbacks.
154      */
155     public void testCallback() throws Exception {
156         AdtTestInterface instance = loadScript("simple_test.groovy");
157
158         // The groovy method returns an object. We should treat it as an opaque object
159         // which purpose is just to give it back to groovy later.
160         Object o = instance.returnGroovyObject();
161         assertNotNull(o);
162
163         // Let the groovy script get back the object and play with it
164         assertTrue(instance.testGroovyObject(o));
165     }
166
167
168 }