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

Groovy example source code file (NodeGPathTest.groovy)

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

bob, bob, groovytestcase, james, james, london, london, nodegpathtest, nodegpathtest

The Groovy NodeGPathTest.groovy source code

package groovy.gpath

/**
 * Some GPath tests using trees
 */
class NodeGPathTest extends GroovyTestCase {

    void testFind() {
        def tree = createTree()
        assert tree.person.find { it['@name'] == 'James' }.location[0]['@name'] == 'London'
    }

    void testFindAll() {
        def tree = createTree()
        def peopleWithNameBob = tree.person.findAll { it['@name'] != 'Bob' }
        assert peopleWithNameBob.size() == 1
    }

    void testCollect() {
        def tree = createTree()
        def namesOfAllPeople = tree.person.collect { it['@name'] }
        assert namesOfAllPeople == ['James', 'Bob']
    }

    protected def createTree() {
        def builder = NodeBuilder.newInstance()
        def root = builder.people() {
            person(name:'James') {
                location(name:'London')
                projects {
                    project(name:'geronimo')
                }
            }
            person(name:'Bob') {
                location(name:'Atlanta')
                projects {
                    project(name:'drools')
                }
            }
        }
        assert root != null
        return root
    }
}

Other Groovy examples (source code examples)

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