alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (DynamicMemberTest.groovy)

This example Groovy source code file (DynamicMemberTest.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

dynamicmembertest, dynamicmembertest, groovytestcase, groovytestcase, string, string, testmethod, testmethod

The Groovy DynamicMemberTest.groovy source code

package groovy

class DynamicMemberTest extends GroovyTestCase {
  def aTestMethod(o){o}
  def aProperty
  
  public void testGStringMethodCall(){
    def name = "aTestMethod"
    assert this."$name"(1) == 1
    assert this."${name}"(2) == 2
    assert "$name"(3) == 3
    assert "${name}"(4) == 4
    name = "TestMethod"
    assert this.("a"+"TestMethod")(5) == 5
    assert this.("a"+name)(6) == 6
  }
  
  public void testGStringPropertyAccess(){
    def name = "aProperty"
    this.aProperty = "foo"
    assert this."$name" == "foo"
    assert this."${name}" == "foo"
    assert "$name" == "aProperty"
    assert "${name}" == "aProperty"
  }
  
  public void testStringMethodCallAndAttributeAccess() {
    this.aProperty = "String"
    assert this."aProperty" == "String"
    assert this."aTestMethod"("String") == "String"
    assert "aTestMethod"("String") == "String"
  }
  
  public void testDynamicAttributeAccess() {
    this.aProperty = "tada"
    def name = "aProperty"
    assert this.@"$name" == "tada"
    assert this.@"${name}" == "tada"
  }
  
  public void testDynamicMethodClosure() {
    def cl = this.&"aTestMethod"
    assert cl("String") == "String"
    def name ="aTestMethod"
    cl = this.&"$name"
    assert cl("String") == "String"
  }
  
}

Other Groovy examples (source code examples)

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

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

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

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.