"Fossies" - the Fresh Open Source Software Archive

Member "groovy-3.0.9/src/test/groovy/CategoryTest.groovy" (3 Sep 2021, 9487 Bytes) of package /linux/misc/apache-groovy-src-3.0.9.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. See also the last Fossies "Diffs" side-by-side code changes report for "CategoryTest.groovy": 3.0.9_vs_4.0.0-rc-1.

    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 groovy.test.GroovyTestCase
   22 
   23 final class CategoryTest extends GroovyTestCase {
   24 
   25     void setUp() {
   26         def dummy = null
   27         CategoryTestPropertyCategory.setSomething(dummy, 'hello')
   28         CategoryTestHelperPropertyReplacer.setaProperty(dummy, 'anotherValue')
   29     }
   30 
   31     void testCategories() {
   32         use (StringCategory) {
   33             assert "Sam".lower() == "sam";
   34             use (IntegerCategory.class) {
   35                 assert "Sam".lower() == "sam";
   36                 assert 1.inc() == 2;
   37             }
   38             shouldFail(MissingMethodException, { 1.inc() });
   39         }
   40         shouldFail(MissingMethodException, { "Sam".lower() });
   41     }
   42 
   43     void testReturnValueWithUseClass() {
   44         def returnValue = use(StringCategory) {
   45             "Green Eggs And Ham".lower()
   46         }
   47         assert "green eggs and ham" == returnValue
   48     }
   49 
   50     void testReturnValueWithUseList() {
   51         def returnValue = use([StringCategory, IntegerCategory]) {
   52             "Green Eggs And Ham".lower() + 5.inc()
   53         }
   54         assert "green eggs and ham6" == returnValue
   55     }
   56 
   57     void testCategoryDefinedProperties() {
   58         use(CategoryTestPropertyCategory) {
   59             assert getSomething() == "hello"
   60             assert something == "hello"
   61             something = "nihao"
   62             assert something == "nihao"
   63         }
   64 
   65         // test the new value again in a new block
   66         use(CategoryTestPropertyCategory) {
   67             assert something == "nihao"
   68         }
   69     }
   70 
   71     void testCategoryReplacedPropertyAccessMethod() {
   72         def cth = new CategoryTestHelper()
   73         cth.aProperty = "aValue"
   74         assert cth.aProperty == "aValue"
   75         use (CategoryTestHelperPropertyReplacer) {
   76             assert cth.aProperty == "anotherValue"
   77             cth.aProperty = "this is boring"
   78             assert cth.aProperty == "this is boring"
   79         }
   80         assert cth.aProperty == "aValue"
   81     }
   82 
   83     void testCategoryHiddenByClassMethod() {
   84       assertScript """
   85          class A{}
   86          class B extends A{def m(){1}}
   87          class Category{ static m(A a) {2}}
   88          def b = new B()
   89          use (Category) {
   90            assert b.m() == 1
   91          }
   92       """
   93     }
   94 
   95     void testCategoryOverridingClassMethod() {
   96       assertScript """
   97          class A {def m(){1}}
   98          class Category{ static m(A a) {2}}
   99          def a = new A()
  100          use (Category) {
  101            assert a.m() == 2
  102          }
  103       """
  104       assertScript """
  105          class A {def m(){1}}
  106          class B extends A{}
  107          class Category{ static m(A a) {2}}
  108          def a = new B()
  109          use (Category) {
  110            assert a.m() == 2
  111          }
  112       """
  113     }
  114 
  115     void testCategoryWithMixedOverriding() {
  116       assertScript """
  117          class A{def m(){0}}
  118          class B extends A{def m(){1}}
  119          class Category{ static m(A a) {2}}
  120          def b = new B()
  121          use (Category) {
  122            assert b.m() == 1
  123          }
  124       """
  125     }
  126 
  127     void testCategoryInheritance() {
  128       assertScript """
  129         public class Foo {
  130           static Object foo(Object obj) {
  131             "Foo.foo()"
  132           }
  133         }
  134 
  135         public class Bar extends Foo{
  136           static Object bar(Object obj) {
  137             "Bar.bar()"
  138           }
  139         }
  140 
  141         def obj = new Object()
  142 
  143         use(Foo){
  144           assert obj.foo() == "Foo.foo()"
  145         }
  146 
  147         use(Bar){
  148           assert obj.bar() == "Bar.bar()"
  149           assert obj.foo() == "Foo.foo()"
  150         }
  151       """
  152     }
  153 
  154     void testNullReceiverChangeForPOJO() {
  155         // GROOVY-5248
  156         // this test will call a method using a POJO while a category is active
  157         // in call site caching this triggers the usage of POJOMetaClassSite,
  158         // which was missing a null check for the receiver. The last foo call
  159         // uses null to exaclty check that path. I use multiple calls with foo(1)
  160         // before to ensure for example indy will do the right things as well,
  161         // since indy may need more than one call here.
  162         assertScript """
  163             class Cat {
  164               public static findAll(Integer x, Closure cl) {1}
  165             }
  166 
  167              def foo(x) {
  168                  x.findAll {}
  169              }
  170 
  171              use (Cat) {
  172                  assert foo(1) == 1
  173                  assert foo(1) == 1
  174                  assert foo(1) == 1
  175                  assert foo(null) == []
  176                  assert foo(1) == 1
  177                  assert foo(1) == 1
  178                  assert foo(1) == 1
  179              }
  180         """
  181     }
  182 
  183     def foo(x){x.bar()}
  184 
  185     void testMethodHiding1() {
  186         def x = new X()
  187         assert foo(x) == 1
  188         use (XCat) {
  189             assert foo(x) == 2
  190             def t = Thread.start {assert foo(x)==1}
  191             t.join()
  192         }
  193         assert foo(x) == 1
  194         def t = Thread.start {use (XCat2){assert foo(x)==3}}
  195         t.join()
  196         assert foo(x) == 1
  197     }
  198 
  199     void testMethodHiding2() {
  200         def x = new X()
  201         assert foo(x) == 1
  202         use (XCat) {
  203             assert foo(x) == 2
  204             def t = Thread.start {use (XCat2){assert foo(x)==3}}
  205             t.join()
  206             assert foo(x) == 2
  207             t = Thread.start {assert foo(x)==1}
  208             t.join()
  209         }
  210         assert foo(x) == 1
  211         def t = Thread.start {use (XCat2){assert foo(x)==3}}
  212         t.join()
  213         assert foo(x) == 1
  214     }
  215 
  216     void testCallToPrivateMethod1() {
  217         assertScript '''
  218             class A {
  219                 private foo() { 1 }
  220                 def baz() { foo() }
  221             }
  222 
  223             class B extends A {}
  224 
  225             class C {}
  226 
  227             use(C) {
  228                 assert new B().baz() == 1
  229             }
  230         '''
  231     }
  232 
  233     // GROOVY-6263
  234     void testCallToPrivateMethod2() {
  235         assertScript '''
  236             class A {
  237                 private foo(a) { 1 }
  238                 def baz() { foo() }
  239             }
  240 
  241             class B extends A {}
  242 
  243             class C {}
  244 
  245             use(C) {
  246                 assert new B().baz() == 1
  247             }
  248         '''
  249     }
  250 
  251     // GROOVY-5453
  252     void testOverloadedGetterMethod() {
  253         assertScript '''
  254             class Cat {
  255                 static getFoo(String s) {'String'}
  256                 static getFoo(CharSequence s) {'CharSequence'}
  257             }
  258             use (Cat) {
  259                 assert 'abc'.getFoo() == 'String'
  260                 assert 'abc'.foo      == 'String'
  261             }
  262         '''
  263     }
  264 
  265     // GROOVY-3867
  266     void testPropertyMissing() {
  267         def x = new X()
  268 
  269         shouldFail(MissingPropertyException) {
  270             assert x.baz != "works" // accessing x.baz should throw MPE
  271         }
  272 
  273         use(XCat4) {
  274             assert x.baz == "works"
  275         }
  276 
  277         shouldFail(MissingPropertyException) {
  278             assert x.baz != "works" // accessing x.baz should throw MPE
  279         }
  280     }
  281 
  282     // GROOVY-3867
  283     void testMethodMissing() {
  284         def x = new X()
  285         assert foo(x) == 1
  286         use (XCat3) {
  287             assert foo(x) == 1 // regular foo() is not affected by methodMissing in category
  288             assert x.baz() == 4 // XCat3.methodMissing is called
  289         }
  290         assert foo(x) == 1
  291         def t = Thread.start {use (XCat3){assert x.baz()==4}}
  292         t.join()
  293         assert foo(x) == 1
  294         shouldFail(MissingMethodException) {
  295             x.baz()
  296         }
  297     }
  298 
  299     // GROOVY-3867
  300     void testMethodMissingNoStatic() {
  301         def x = new X()
  302         use (XCat3) {
  303             assert x.baz() == 4 // XCat3.methodMissing is called for instance
  304             shouldFail(MissingMethodException) {
  305                 assert X.baz() != 4 // XCat3.methodMissing should not be called for static method of X
  306             }
  307         }
  308     }
  309 }
  310 
  311 class X{ def bar(){1}}
  312 class XCat{ static bar(X x){2}}
  313 class XCat2{ static bar(X x){3}}
  314 class XCat3{ static methodMissing(X x, String name, args) {4}}
  315 class XCat4{ static propertyMissing(X x, String name) {"works"}}
  316 
  317 class StringCategory {
  318     static String lower(String string) {
  319         return string.toLowerCase();
  320     }
  321 }
  322 
  323 class IntegerCategory {
  324     static Integer inc(Integer i) {
  325         return i + 1;
  326     }
  327 }
  328 
  329 class CategoryTestPropertyCategory {
  330     private static aVal = "hello"
  331     static getSomething(Object self) { return aVal }
  332     static void setSomething(Object self, newValue) { aVal = newValue }
  333 }
  334 
  335 class CategoryTestHelper {
  336     def aProperty = "aValue"
  337 }
  338 
  339 class CategoryTestHelperPropertyReplacer {
  340     private static aVal = "anotherValue"
  341     static getaProperty(CategoryTestHelper self) { return aVal }
  342     static void setaProperty(CategoryTestHelper self, newValue) { aVal = newValue }
  343 }