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

Groovy example source code file (GroovyObjectInheritanceTest.groovy)

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

bar, bar, compilabletestsupport, compilabletestsupport, foo, foo, groovyobjectinheritancetest, groovyobjectinheritancetest

The Groovy GroovyObjectInheritanceTest.groovy source code

/*
 * Copyright 2003-2009 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 gls.invocation

import gls.CompilableTestSupport

class GroovyObjectInheritanceTest extends CompilableTestSupport {
  void testInheritanceWithGetProperty() {
    assertScript """
        class Foo {
          def getProperty(String name) {1}
        }
        class Bar extends Foo{}
        def bar = new Bar()
        assert bar.foo==1
    """
    assertScript """
        class Foo {}
        class Bar extends Foo{
          def getProperty(String name) {1}
        }
        def bar = new Bar()
        assert bar.foo==1
    """
  }
  
  void testInheritanceWithSetProperty() {
    assertScript """
        class Foo {
          def foo
          void setProperty(String name, x) {this.foo=1}
        }
        class Bar extends Foo{}
        def bar = new Bar()
        bar.foo = 2
        assert bar.foo == 1
    """
    assertScript """
        class Foo {}
        class Bar extends Foo{
          def foo
          void setProperty(String name, x) {this.foo=1}
        }
        def bar = new Bar()
        bar.foo = 2
        assert bar.foo == 1
    """
  }
  
  void testInheritanceWithInvokeMethod() {
    assertScript """
        class Foo {
          def invokeMethod(String name, args) {1}
        }
        class Bar extends Foo{}
        def bar = new Bar()
        assert bar.foo() == 1
    """
    assertScript """
        class Foo {}
        class Bar extends Foo{
          def invokeMethod(String name, args) {1}
        }
        def bar = new Bar()
        assert bar.foo() == 1
    """
  }
  
  void testMetaClassFieldInheritance() {
    assertScript """
        class Foo {}
        class Bar extends Foo{}
        assert Foo.class.declaredFields.find{it.name=="metaClass"}!=null
        assert Bar.class.declaredFields.find{it.name=="metaClass"}==null
    """
  }
  
  void testStandardInhgeritance() {
    assertScript """
        class Foo{}
        class Bar extends Foo{}
        
        assert Foo.class.declaredFields.find{it.name=="metaClass"}!=null
        assert Bar.class.declaredFields.find{it.name=="metaClass"}==null
        
        assert Foo.class.declaredMethods.find{it.name=="getMetaClass"}!=null
        assert Bar.class.declaredMethods.find{it.name=="getMetaClass"}==null
        
        assert Foo.class.declaredMethods.find{it.name=="setMetaClass"}!=null
        assert Bar.class.declaredMethods.find{it.name=="setMetaClass"}==null
        
        assert Foo.class.declaredMethods.find{it.name=="getProperty"}!=null
        assert Bar.class.declaredMethods.find{it.name=="getProperty"}==null
        
        assert Foo.class.declaredMethods.find{it.name=="setProperty"}!=null
        assert Bar.class.declaredMethods.find{it.name=="setProperty"}==null
        
        assert Foo.class.declaredMethods.find{it.name=="invokeMethod"}!=null
        assert Bar.class.declaredMethods.find{it.name=="invokeMethod"}==null
    """
  }
}

Other Groovy examples (source code examples)

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

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

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 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.