"Fossies" - the Fresh Open Source Software Archive

Member "groovy-3.0.9/src/test/groovy/GroovyInterceptableTest.groovy" (3 Sep 2021, 4688 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 "GroovyInterceptableTest.groovy": 3.0.8_vs_3.0.9.

    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 import org.codehaus.groovy.runtime.ReflectionMethodInvoker
   23 
   24 final class GroovyInterceptableTest extends GroovyTestCase {
   25 
   26     void testMethodIntercept1() {
   27         def g = new GI()
   28         assert g.someInt() == 2806
   29         assert g.someUnexistingMethod() == 1
   30         assert g.toString() == "invokeMethodToString"
   31     }
   32 
   33     void testMethodIntercept2() {
   34         def g = new GI()
   35         assert g.foo == 89
   36         g.foo = 90
   37         assert g.foo == 90
   38         // Should this be 1 or 90?
   39         assert g.getFoo() == 1
   40     }
   41 
   42     // GROOVY-3015
   43     void testMethodIntercept3() {
   44         String shared = '''\
   45             import org.codehaus.groovy.runtime.InvokerHelper
   46             import org.codehaus.groovy.runtime.StringBufferWriter
   47             import static groovy.test.GroovyTestCase.assertEquals
   48 
   49             class Traceable implements GroovyInterceptable {
   50                 private static int indent = 1
   51                 Writer writer = new PrintWriter(System.out)
   52                 Object invokeMethod(String name, Object args) {
   53                     writer.write('\\n' + ('  ' * indent) + 'Enter ' + name)
   54                     indent += 1
   55                     def result = InvokerHelper.getMetaClass(this).invokeMethod(this, name, args)
   56                     indent -= 1
   57                     writer.write('\\n' + ('  ' * indent) + 'Leave ' + name)
   58                     return result
   59                 }
   60             }
   61 
   62             class Whatever extends Traceable {
   63                 int inner() { return 1 }
   64                 int outer() { return inner() }
   65                 int shouldTraceOuterAndInnerMethod() { return outer() }
   66                 def shouldTraceOuterAndInnerClosure = { -> return outer() }
   67             }
   68 
   69             def log = new StringBuffer()
   70             def obj = new Whatever(writer: new StringBufferWriter(log))
   71         '''
   72 
   73         assertScript shared + '''
   74             obj.shouldTraceOuterAndInnerMethod()
   75 
   76             assertEquals """
   77             |  Enter shouldTraceOuterAndInnerMethod
   78             |    Enter outer
   79             |      Enter inner
   80             |      Leave inner
   81             |    Leave outer
   82             |  Leave shouldTraceOuterAndInnerMethod""".stripMargin(), log.toString()
   83         '''
   84 
   85         assertScript shared + '''
   86             obj.shouldTraceOuterAndInnerClosure()
   87 
   88             assertEquals """
   89             |  Enter shouldTraceOuterAndInnerClosure
   90             |    Enter outer
   91             |      Enter inner
   92             |      Leave inner
   93             |    Leave outer
   94             |  Leave shouldTraceOuterAndInnerClosure""".stripMargin(), log.toString()
   95         '''
   96     }
   97 
   98     void testMissingMethod1() {
   99         def obj = new GI2()
  100         shouldFail { obj.notAMethod() }
  101         assert 'missing' == obj.result
  102     }
  103 
  104     void testMissingMethod2() {
  105         def obj = new GI2()
  106         shouldFail { obj.method() }
  107         assert 'missing' == obj.result
  108     }
  109 }
  110 
  111 //------------------------------------------------------------------------------
  112 
  113 class GI implements GroovyInterceptable {
  114     def foo = 89
  115     int someInt() { 2806 }
  116     @Override
  117     String toString() { "originalToString" }
  118     @Override
  119     Object invokeMethod(String name, Object args) {
  120         if ("toString" == name)
  121             return "invokeMethodToString"
  122         else if ("someInt" == name)
  123             return ReflectionMethodInvoker.invoke(this, name, args)
  124         else
  125             return 1
  126     }
  127 }
  128 
  129 class GI2 implements GroovyInterceptable {
  130     def result = ""
  131     @Override
  132     def invokeMethod(String name, args) {
  133         def metaMethod = Foo.metaClass.getMetaMethod(name, args)
  134         if (metaMethod != null) return metaMethod.invoke(this, args)
  135         result += "missing"
  136         throw new MissingMethodException(name, Foo.class, args)
  137     }
  138     def method() {
  139         notAMethod()
  140     }
  141 }