com.nwea.samples.apachecommons.collections.SampleCollectionUtilsTest.java Source code

Java tutorial

Introduction

Here is the source code for com.nwea.samples.apachecommons.collections.SampleCollectionUtilsTest.java

Source

/**
 *******************************************************************************
 * Licensed Materials - Property of NWEA *
 *  Copyright NWEA 2009 All Rights Reserved *
 * Created on Oct 5, 2009 *
 *******************************************************************************
 */
package com.nwea.samples.apachecommons.collections;

import java.util.ArrayList;
import java.util.Collection;

import org.apache.commons.collections.Closure;
import org.apache.commons.collections.CollectionUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import static java.lang.System.*;

/**
 * @author ben.berman
 *
 */
public class SampleCollectionUtilsTest {

    private Collection<Student> students;

    @Test
    public void findMaleStudentsTest() {
        Collection<Student> maleStudents = SampleCollectionUtils.findStudentsBySex(students, "M");

        printStudents(maleStudents);
    }

    /**
     * @param maleStudents
     */
    private void printStudents(Collection<Student> students) {
        CollectionUtils.forAllDo(students, new Closure() {
            public void execute(Object arg0) {
                out.println(arg0.toString());
            }
        });
    }

    @Test
    public void setDefaultHomeroomsBySexTest() {
        // print before operation
        out.println("before setting homerooms:");
        printStudents(students);

        SampleCollectionUtils.setDefaultHomeroomsBySex(students, "English", "History");

        // print after homeroom assignment:
        out.println("after setting homerooms:");
        printStudents(students);
    }

    @Before
    public void setUp() {
        students = new ArrayList<Student>();
        students.add(new Student("Jimmy", 10, "M", "Franklin Elementary"));
        students.add(new Student("Michael", 8, "M", "Franklin Elementary"));
        students.add(new Student("Emma", 9, "F", "Franklin Elementary"));
        students.add(new Student("Nicole", 8, "F", "Franklin Elementary"));
    }

}