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

Groovy example source code file (Property2Test.groovy)

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

colorado, expando, expando, foo, foo, groovytestcase, groovytestcase, james, john, john, london, property2test, propertyvalue

The Groovy Property2Test.groovy source code

/*
 * Copyright 2003-2010 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

/**
 * Tests the use of getMetaPropertyValues() and getProperties() for Beans and Expandos.
 *
 * @author john stump
 * @author dierk koenig
 * @version $Revision: 20434 $
 */
class Property2Test extends GroovyTestCase {

    void testGetPropertiesBeanCheckingValues() {
        def foo = new Foo()

        // these are the properties and their values that should be there
        def props = ['name': 'James', 'count': 1, 'location': 'London', 'blah': 9]
        foo.properties.each { name, value ->
            // todo: GROOVY-996
            // We should see protected properties, but not  private ones.
            assert name != "invisible"

            def pvalue = props[name]
            if (pvalue != null)
                assert pvalue == value

            // remove this one from the map
            props.remove(name)
        }

        // make sure there are none left over
        assert props.size() == 0
    }

    void testMetaPropertyValuesFromObject() {
        def foo = new Foo()
        def metaProps = foo.metaPropertyValues
        assert metaProps[0] instanceof PropertyValue
        assertNotNull metaProps[0].name
        assertNotNull metaProps[0].value
        assertNotNull metaProps[0].type
    }

    void testGetPropertiesExpando() {
        def foo = new Expando()

        foo.name = 'John'
        foo.location = 'Colorado'
        foo.count = 23
        foo.blah = true

        // these are the properties that should be there
        def props = ['name', 'count', 'location', 'blah']
        foo.properties.each { name, value -> props -= [name] }

        // there should be none left
        assert props.size() == 0
    }
}

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy Property2Test.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.