"Fossies" - the Fresh Open Source Software Archive

Member "groovy-3.0.9/src/test/groovy/lang/InterceptorTest.groovy" (3 Sep 2021, 3866 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 "InterceptorTest.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.lang
   20 
   21 import groovy.test.GroovyTestCase
   22 import org.codehaus.groovy.runtime.StringBufferWriter
   23 
   24 /**
   25  * Test for the Interceptor Interface usage as implemented by the
   26  * {@link TracingInterceptor}. Makes use of the {@link ProxyMetaClass} and
   27  * shows the collaboration.
   28  * <p>
   29  * As a side effect, the {@code ProxyMetaClass} is also partly tested.
   30  */
   31 final class InterceptorTest extends GroovyTestCase {
   32 
   33     private final Interceptor interceptor = new TracingInterceptor() // class under test
   34     private final String interceptable = 'Interceptable String' // the object to observe
   35     private final ProxyMetaClass proxy = ProxyMetaClass.getInstance(interceptable.class)
   36     private final StringBuffer log = new StringBuffer('\n')
   37 
   38     void setUp() {
   39         interceptor.writer = new StringBufferWriter(log)
   40         proxy.interceptor = interceptor
   41     }
   42 
   43     void testSimpleInterception() {
   44         proxy.use {
   45             assertEquals 20, interceptable.size()
   46             assertEquals 20, interceptable.length()
   47             assertTrue interceptable.startsWith('I', 0)
   48         }
   49         assertEquals("""
   50 before java.lang.String.size()
   51 after  java.lang.String.size()
   52 before java.lang.String.length()
   53 after  java.lang.String.length()
   54 before java.lang.String.startsWith(java.lang.String, java.lang.Integer)
   55 after  java.lang.String.startsWith(java.lang.String, java.lang.Integer)
   56 """, log.toString())
   57     }
   58 
   59     void testNoInterceptionWithNullInterceptor() {
   60         proxy.setInterceptor(null)
   61         proxy.use {
   62             interceptable.size()
   63         }
   64     }
   65 
   66     void testConstructorInterception() {
   67         proxy.use {
   68             new String('some string')
   69         }
   70         assertEquals("""
   71 before java.lang.String.ctor(java.lang.String)
   72 after  java.lang.String.ctor(java.lang.String)
   73 """, log.toString())
   74     }
   75 
   76     void testStaticMethodInterception() {
   77         proxy.use {
   78             assertEquals 'true', String.valueOf(true)
   79         }
   80         assertEquals("""
   81 before java.lang.String.valueOf(java.lang.Boolean)
   82 after  java.lang.String.valueOf(java.lang.Boolean)
   83 """, log.toString())
   84     }
   85 
   86     void testInterceptionOfGroovyClasses() {
   87         def slicer = new groovy.mock.example.CheeseSlicer()
   88         def proxy = ProxyMetaClass.getInstance(slicer.class)
   89         proxy.setInterceptor(interceptor)
   90         proxy.use(slicer) {
   91             slicer.coffeeBreak('')
   92         }
   93         assertEquals("""
   94 before groovy.mock.example.CheeseSlicer.coffeeBreak(java.lang.String)
   95 after  groovy.mock.example.CheeseSlicer.coffeeBreak(java.lang.String)
   96 """, log.toString())
   97     }
   98 
   99     void testProxyMetaClassUseMethodShouldReturnTheResultOfClosure() {
  100         assertTrue proxy.use { true }
  101     }
  102 
  103     // GROOVY-10009
  104     void testNullArgumentToMethodCall() {
  105         interceptable.metaClass = proxy
  106         interceptable.equals(null)
  107 
  108         assertEquals '''
  109             |before java.lang.String.equals(java.lang.Object)
  110             |after  java.lang.String.equals(java.lang.Object)
  111             |'''.stripMargin(), log.toString()
  112     }
  113 }