Home | History | Annotate | Download | only in groovytests
      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.tests.groovytests;
     18 
     19 import org.codehaus.groovy.control.CompilationFailedException;
     20 import org.eclipse.swt.graphics.Rectangle;
     21 
     22 import groovy.lang.GroovyClassLoader;
     23 
     24 import java.io.FileNotFoundException;
     25 import java.io.InputStream;
     26 
     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     @SuppressWarnings("unchecked")
     68     private AdtTestInterface loadScript(String filename)
     69         throws CompilationFailedException, ClassCastException,
     70                InstantiationException, IllegalAccessException,
     71                FileNotFoundException {
     72         // Get the input source from the sources or the JAR.
     73         InputStream myGroovyStream = getClass().getResourceAsStream(filename);
     74 
     75         // The stream is null if the file does not exists.
     76         if (myGroovyStream == null) {
     77             throw new FileNotFoundException(filename);
     78         }
     79 
     80         // Create a groovy class from it. Can fail to compile.
     81         ClassLoader cl = getClass().getClassLoader();
     82         GroovyClassLoader gcl = new GroovyClassLoader(cl);
     83         Class gClass = gcl.parseClass(myGroovyStream, filename);
     84 
     85         // Get an instance. This might throw ClassCastException.
     86         return (AdtTestInterface) gClass.newInstance();
     87     }
     88 
     89     /**
     90      * Tests that a {@link FileNotFoundException} is thrown if when trying
     91      * to load a missing script.
     92      */
     93     public void testMissingScript() throws Exception {
     94         try {
     95             @SuppressWarnings("unused")
     96             AdtTestInterface instance = loadScript("not_an_existing_script.groovy");
     97             fail("loadScript should not succeed, FileNotFoundException expected.");
     98         } catch (FileNotFoundException e) {
     99             assertEquals("not_an_existing_script.groovy", e.getMessage());
    100             return; // succeed
    101         }
    102 
    103         fail("Script failed to throw an exception on missing groovy file.");
    104     }
    105 
    106     /**
    107      * Tests that a {@link ClassCastException} is thrown if the script does not
    108      * implement our interface.
    109      */
    110     public void testInvalidInterface() throws Exception {
    111         try {
    112             @SuppressWarnings("unused")
    113             AdtTestInterface instance = loadScript("invalid_interface.groovy");
    114             fail("loadScript should not succeed, ClassCastException expected.");
    115         } catch(ClassCastException e) {
    116             // This has to fail because the script does not implement our interface
    117             // The message explains why but we're not harcoding the message in the test.
    118             assertNotNull(e.getMessage());
    119             return; // succeed
    120         }
    121 
    122         fail("Script failed to throw a ClassCastException.");
    123     }
    124 
    125     /**
    126      * Tests that a {@link CompilationFailedException} is thrown if the script
    127      * is not valid.
    128      */
    129     public void testCompilationError() throws Exception {
    130         try {
    131             @SuppressWarnings("unused")
    132             AdtTestInterface instance = loadScript("compile_error.groovy");
    133             fail("loadScript should not succeed, CompilationFailedException expected.");
    134         } catch (CompilationFailedException e) {
    135             // This script does not compile, the message explains why but we're not
    136             // harcoding the message in the test.
    137             assertNotNull(e.getMessage());
    138             return; // succeed
    139         }
    140 
    141         fail("Script failed to throw a compilation error.");
    142     }
    143 
    144     /**
    145      * Tests a valid script scenario with only some basic methods
    146      */
    147     public void testSimpleMethods() throws Exception {
    148         AdtTestInterface instance = loadScript("simple_test.groovy");
    149 
    150         assertTrue(instance.acceptDrag("LinearLayout"));
    151         assertFalse(instance.acceptDrag("RelativeLayout"));
    152         assertNull(instance.acceptDrop("none"));
    153 
    154         Rectangle r = instance.acceptDrop("LinearLayout");
    155         assertNotNull(r);
    156         assertEquals(new Rectangle(1, 2, 3, 4), r);
    157     }
    158 
    159     /**
    160      * Tests a valid script scenario with some methods providing some callbacks.
    161      */
    162     public void testCallback() throws Exception {
    163         AdtTestInterface instance = loadScript("simple_test.groovy");
    164 
    165         // The groovy method returns an object. We should treat it as an opaque object
    166         // which purpose is just to give it back to groovy later.
    167         Object o = instance.returnGroovyObject();
    168         assertNotNull(o);
    169 
    170         // Let the groovy script get back the object and play with it
    171         assertTrue(instance.testGroovyObject(o));
    172     }
    173 
    174 
    175 }
    176