"Fossies" - the Fresh Open Source Software Archive

Member "groovy-4.0.12/src/test/groovy/AbstractClassAndInterfaceTest.groovy" (31 Jan 1980, 9409 Bytes) of package /linux/misc/apache-groovy-src-4.0.12.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Java source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2  *  Licensed to the Apache Software Foundation (ASF) under one
    3  *  or more contributor license agreements.  See the NOTICE file
    4  *  distributed with this work for additional information
    5  *  regarding copyright ownership.  The ASF licenses this file
    6  *  to you under the Apache License, Version 2.0 (the
    7  *  "License"); you may not use this file except in compliance
    8  *  with the License.  You may obtain a copy of the License at
    9  *
   10  *    http://www.apache.org/licenses/LICENSE-2.0
   11  *
   12  *  Unless required by applicable law or agreed to in writing,
   13  *  software distributed under the License is distributed on an
   14  *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15  *  KIND, either express or implied.  See the License for the
   16  *  specific language governing permissions and limitations
   17  *  under the License.
   18  */
   19 package groovy
   20 
   21 import gls.CompilableTestSupport
   22 
   23 class AbstractClassAndInterfaceTest extends CompilableTestSupport {
   24 
   25     void testInterface() {
   26         def shell = new GroovyShell()
   27         def text = """
   28             interface A {
   29                 void methodOne(Object o)
   30                 Object methodTwo()
   31             }
   32 
   33             class B implements A {
   34                 void methodOne(Object o){assert true}
   35                 Object methodTwo(){
   36                     assert true
   37                     methodOne(null)
   38                     return new Object()
   39                 }
   40             }
   41 
   42             def b = new B();
   43             return b.methodTwo()
   44             """
   45         def retVal = shell.evaluate(text)
   46         assert retVal.class == Object
   47     }
   48 
   49     void testClassImplementingAnInterfaceButMissesMethod() {
   50         shouldNotCompile """
   51             interface A {
   52                 void methodOne(Object o)
   53                 Object methodTwo()
   54             }
   55 
   56             class B implements A {
   57                 void methodOne(Object o){assert true}
   58             }
   59 
   60             def b = new B();
   61             return b.methodTwo()
   62             """
   63 
   64         shouldNotCompile """
   65             interface A {
   66                 Object methodTwo()
   67             }
   68             interface B extends A{
   69                 void methodOne(Object o)
   70             }
   71 
   72             class C implements A {
   73                 void methodOne(Object o){assert true}
   74             }
   75 
   76             def b = new C();
   77             return b.methodTwo()
   78             """
   79     }
   80 
   81     void testClassImplementingNestedInterfaceShouldContainMethodsFromSuperInterfaces() {
   82         shouldNotCompile """
   83             interface A { def a() }
   84             interface B extends A { def b() }
   85             class BImpl implements B {
   86                 def b(){ println 'foo' }
   87             }
   88             new BImpl().b()
   89             """
   90     }
   91 
   92     void testAbstractClass() {
   93         def shell = new GroovyShell()
   94         def text = """
   95             abstract class A {
   96                 abstract void methodOne(Object o)
   97                 Object methodTwo(){
   98                     assert true
   99                     methodOne(null)
  100                     return new Object()
  101                 }
  102             }
  103 
  104             class B extends A {
  105                 void methodOne(Object o){assert true}
  106             }
  107 
  108             def b = new B();
  109             return b.methodTwo()
  110             """
  111         def retVal = shell.evaluate(text)
  112         assert retVal.class == Object
  113     }    
  114 
  115     void testClassExtendingAnAbstractClassButMissesMethod() {
  116         shouldNotCompile """
  117             abstract class A {
  118                 abstract void methodOne(Object o)
  119                 Object methodTwo(){
  120                     assert true
  121                     methodOne(null)
  122                     return new Object()
  123                 }
  124                 abstract void MethodThree()
  125             }
  126 
  127             abstract class B extends A {
  128                 void methodOne(Object o){assert true}
  129             }
  130 
  131             class C extends B{}
  132 
  133             def b = new C();
  134             return b.methodTwo()
  135             """    
  136 
  137        shouldNotCompile """
  138             abstract class A {
  139                 abstract void methodOne(Object o)
  140                 Object methodTwo(){
  141                     assert true
  142                     methodOne(null)
  143                     return new Object()
  144                 }
  145                 abstract void MethodThree()
  146             }
  147 
  148             class B extends A {
  149                 void methodOne(Object o){assert true}
  150             }
  151 
  152             def b = new B();
  153             return b.methodTwo()
  154             """
  155     }
  156 
  157     void testInterfaceAbstractClassCombination() {
  158         def shell = new GroovyShell()
  159         def text = """
  160             interface A {
  161                 void methodOne()
  162             }
  163 
  164             abstract class B implements A{
  165                 abstract void methodTwo()
  166             }
  167 
  168             class C extends B {
  169                 void methodOne(){assert true}
  170                 void methodTwo(){
  171                   methodOne()
  172                 }
  173             }
  174             def c = new C()
  175             c.methodTwo()
  176             """
  177         shell.evaluate(text)
  178 
  179         shouldNotCompile """
  180             interface A {
  181                 void methodOne()
  182             }
  183 
  184             abstract class B implements A{
  185                 abstract void methodTwo()
  186             }
  187 
  188             class C extends B {}
  189             def c = new c()
  190             c.methodTwo()
  191             """
  192     }
  193 
  194     void testDefaultModifiersForInterfaces() {
  195         def shell = new GroovyShell()
  196         def text = """
  197             import java.lang.reflect.Modifier
  198 
  199             interface A {
  200                 def foo
  201             }
  202 
  203             def fields = A.class.declaredFields
  204             assert fields.length==1
  205             assert fields[0].name == "foo"
  206             assert Modifier.isPublic (fields[0].modifiers)
  207             assert Modifier.isStatic (fields[0].modifiers)
  208             assert Modifier.isFinal  (fields[0].modifiers)
  209             """
  210         shell.evaluate(text)
  211     }
  212 
  213     void testAccessToInterfaceField() {
  214         def shell = new GroovyShell()
  215         def text = """
  216             interface A {
  217                 def foo=1
  218             }
  219             class B implements A {
  220               def foo(){foo}
  221             }
  222             assert new B().foo()==1
  223        """
  224        shell.evaluate(text)
  225     }
  226 
  227     void testImplementsDuplicateInterface() {
  228         shouldCompile """
  229         interface I {}
  230         class C implements I {}
  231         """
  232         shouldNotCompile """
  233         interface I {}
  234         class C implements I, I {}
  235         """
  236     }
  237 
  238     void testDefaultMethodParamsNotAllowedInInterface() {
  239         shouldCompile """
  240         interface Foo {
  241            def doit( String param, int o )
  242         }
  243         """
  244         shouldNotCompile """
  245         interface Foo {
  246            def doit( String param = "Groovy", int o )
  247         }
  248         """
  249     }
  250 
  251     void testClassImplementsItselfCreatingACycle() {
  252         def scriptStr = """
  253             package p1
  254             class XXX implements XXX {}
  255         """
  256         shouldNotCompile scriptStr
  257 
  258         scriptStr = """
  259             class YYY implements YYY {}
  260         """
  261         shouldNotCompile scriptStr
  262     }
  263 
  264     void testAbstractClassWithPrivateAbstractMethod() {
  265         def msg = shouldNotCompile """
  266             abstract class X {
  267                 private abstract void y()
  268             }
  269         """
  270         assert msg.contains("Method 'y' from class 'X' must not be private as it is declared as an abstract method.")
  271     }
  272 
  273     void testAbstractClassWithPrivateAbstractMethods() {
  274         def msg = shouldNotCompile """
  275             abstract class X {
  276                 private abstract void y()
  277                 private abstract void z()
  278             }
  279         """
  280         assert msg.contains("Method 'y' from class 'X' must not be private as it is declared as an abstract method.")
  281         assert msg.contains("Method 'z' from class 'X' must not be private as it is declared as an abstract method.")
  282     }
  283 
  284     void testAbstractNestedClassWithPrivateAbstractMethod() {
  285         def msg = shouldNotCompile """
  286             class Z {
  287                 abstract class X {
  288                     private abstract void y()
  289                 }
  290             }
  291         """
  292         assert msg.contains("Method 'y' from class 'Z\$X' must not be private as it is declared as an abstract method.")
  293     }
  294 
  295     void testClassWithPrivateAbstractMethod() {
  296         def msg = shouldNotCompile """
  297             class X {
  298                 private abstract void y()
  299             }
  300         """
  301         assert !msg.contains("Method 'y' from class 'X' must not be private as it is declared as an abstract method.")
  302         assert msg.contains("Can't have an abstract method in a non-abstract class. The class 'X' must be declared abstract or the method 'void y()' must be implemented.")
  303     }
  304 
  305     void testEnumWithPrivateAbstractMethod() {
  306         def msg = shouldNotCompile """
  307             enum X {
  308                 CONST {
  309                     private void y() { }
  310                 }
  311 
  312                 private abstract void y()
  313             }
  314         """
  315         assert msg.contains("Method 'y' from class 'X' must not be private as it is declared as an abstract method.")
  316     }
  317 
  318     void testInterfaceWithPrivateAbstractMethod() {
  319         def msg = shouldNotCompile """
  320             interface X {
  321                 private abstract void y()
  322             }
  323         """
  324         assert msg.contains("Method 'y' is private but should be public in interface 'X'.")
  325     }
  326 }