home | career | drupal | java | mac | mysql | perl | scala | uml | unix

Groovy example source code file (WithMethodTest.groovy)

This example Groovy source code file (WithMethodTest.groovy) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

groovytestcase, groovytestcase, stringbuffer, stringbuffer, withmethodtest, withmethodtest

The Groovy WithMethodTest.groovy source code

/*
 * Copyright 2003-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package groovy.lang

/**
 * Tests the .with method
 *
 */
 class WithMethodTest extends GroovyTestCase {
     
     void testDelegateGetsFirstOpportunity() {
         def sb = new StringBuffer()

         sb.with {
             // this should call append() on the 
             // delegate not, the owner
             append 'some text'
         }
         
         assertEquals 'delegate had wrong value', 'some text', sb.toString()
     }
     
     void testOwnerGetsOpportunityIfDelegateCannotRespond() {
         def sb = new StringBuffer()
         
         def returnValue
         
         sb.with {
             // this should call ownerMethod() on the owner
             returnValue = ownerMethod()
         }
         
         assertEquals 'owner should have responded to method call', 
                      42, 
                      returnValue
     }
     
     void testCallingNonExistentMethod() {
         def sb = new StringBuffer()
         
         shouldFail(MissingMethodException) {
             sb.with {
                 someNoneExistentMethod()
             }
         }
     }
     
     void testClosureWithResolveStrategyExplicitlySet() {
         def closure = {
             append 'some text'
         }
         closure.resolveStrategy = Closure.OWNER_ONLY
         
         def sb = new StringBuffer()
         
         // .with should use DELEGATE_FIRST, even though
         // the closure has another strategy set
         sb.with closure
         
         assertEquals 'delegate had wrong value', 'some text', sb.toString()
     }
     
     def ownerMethod() {
         42
     }
     
     void append(String s) {
         fail 'this should never have been called'
     }
 }

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy WithMethodTest.groovy source code file:

new blog posts

 

Copyright 1998-2014 Alvin Alexander, alvinalexander.com
All Rights Reserved.