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

Groovy example source code file (MinMaxTest.groovy)

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

chris, chris, expected, groovytestcase, joe, joe, leicester, minmaxtest, orderby, people, person, person, string, string

The Groovy MinMaxTest.groovy source code

package groovy

/** 
 * Tests the min() and max() functions
 * 
 * @author <a href="mailto:james@coredevelopers.net">James Strachan
 * @version $Revision: 11087 $
 */
class MinMaxTest extends GroovyTestCase {

    void testSimpleMinMax() {
        def list = [5, 2, 6, 1, 9, 8]
        
        def n = list.min()
        assert n == 1
        
        n = list.max()
        assert n == 9
    }
    
    void testMinMaxWithComparator() {
        def people = getPeople()

        // let's find the maximum by name

        def order = new OrderBy( { it.get('@cheese') } )

        println("People ${people}")

        def p = people.min(order)

        println("Found ${p}")

        assert p.get("@name") == "Joe" , "found person ${p}"

        p = people.max(order)
        assert p.get("@name") == "Chris" , "found person ${p}"
    }
    
    void testMinMaxOnArraysWithComparator() {
        Person[] people = [
            new Person(name:'James', cheese:'Edam', location:'London'),
            new Person(name:'Bob', cheese:'Cheddar', location:'Atlanta'),
            new Person(name:'Chris', cheese:'Red Leicester', location:'London'),
            new Person(name:'Joe', cheese:'Brie', location:'London')
        ]

        // let's find the maximum by name
        def order = new OrderBy( { it.cheese } )

        def p = people.min(order)
        assert p.name == "Joe", "Expected to find Joe but found person ${p}"

        p = people.max(order)
        assert p.name == "Chris", "Expected to find Chris but found person ${p}"
    }

    def getPeople() {
        def builder = new NodeBuilder()
        def tree = builder.people {
            person(name:'James', cheese:'Edam', location:'London')
            person(name:'Bob', cheese:'Cheddar', location:'Atlanta')
            person(name:'Chris', cheese:'Red Leicester', location:'London')
            person(name:'Joe', cheese:'Brie', location:'London')
        }
        
        return tree.children()
    }

}

class Person {
    String name
    String cheese
    String location
}

Other Groovy examples (source code examples)

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