2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.eclipse.org/org/documents/epl-v10.php
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.
17 package com.android.ide.eclipse.adt.internal.sdk;
19 import org.codehaus.groovy.control.CompilationFailedException;
20 import org.codehaus.groovy.control.MultipleCompilationErrorsException;
21 import org.eclipse.swt.graphics.Rectangle;
23 import java.io.FileNotFoundException;
24 import java.io.InputStream;
26 import groovy.lang.GroovyClassLoader;
27 import junit.framework.TestCase;
30 * Tests we can invoke a groovy script that implements a given interface.
32 public class TestGroovy extends TestCase {
35 protected void setUp() throws Exception {
40 * This is the interface that we want our Groovy script to implement.
42 public static interface AdtTestInterface {
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);
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);
56 * Loads a groovy script that defines one class that implements the {@link AdtTestInterface}.
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
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);
74 // The stream is null if the file does not exists.
75 if (myGroovyStream == null) {
76 throw new FileNotFoundException(filename);
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);
84 // Get an instance. This might throw ClassCastException.
85 return (AdtTestInterface) gClass.newInstance();
89 * Tests that a {@link FileNotFoundException} is thrown if when trying
90 * to load a missing script.
92 public void testMissingScript() throws Exception {
94 AdtTestInterface instance = loadScript("not_an_existing_script.groovy");
95 } catch (FileNotFoundException e) {
96 assertEquals("not_an_existing_script.groovy", e.getMessage());
100 fail("Script failed to throw an exception on missing groovy file.");
104 * Tests that a {@link ClassCastException} is thrown if the script does not
105 * implement our interface.
107 public void testInvalidInterface() throws Exception {
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());
117 fail("Script failed to throw a ClassCastException.");
121 * Tests that a {@link CompilationFailedException} is thrown if the script
124 public void testCompilationError() throws Exception {
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());
134 fail("Script failed to throw a compilation error.");
138 * Tests a valid script scenario with only some basic methods
140 public void testSimpleMethods() throws Exception {
141 AdtTestInterface instance = loadScript("simple_test.groovy");
143 assertTrue(instance.acceptDrag("LinearLayout"));
144 assertFalse(instance.acceptDrag("RelativeLayout"));
145 assertNull(instance.acceptDrop("none"));
147 Rectangle r = instance.acceptDrop("LinearLayout");
149 assertEquals(new Rectangle(1, 2, 3, 4), r);
153 * Tests a valid script scenario with some methods providing some callbacks.
155 public void testCallback() throws Exception {
156 AdtTestInterface instance = loadScript("simple_test.groovy");
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();
163 // Let the groovy script get back the object and play with it
164 assertTrue(instance.testGroovyObject(o));