[ERA-CVS] src/org/jdaemon/test/util/attribute TestAttributePackage.java, NONE, 1.1
Brought to you by:
jessex
|
From: <je...@23...> - 2008-10-06 10:26:36
|
Update of /cvsroot/era/src/org/jdaemon/test/util/attribute In directory 23jxhf1.ch3.sourceforge.com:/tmp/cvs-serv12559/test/util/attribute Added Files: TestAttributePackage.java Log Message: Various changes for ERA v2 - 'This time it's Generic...' --- NEW FILE: TestAttributePackage.java --- /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package org.jdaemon.test.util.attribute; import org.jdaemon.util.attribute.*; import org.jdaemon.util.functional.*; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; import java.util.LinkedList; import java.util.List; /** Test class for Attributes. * * This is a Test container for attributes. str_attr and int_attr are two test attributes which * address values of stringv and intv respectively. * * @author jonathan */ class TestObject { public String stringv; public Integer intv; public TestObject(String a, Integer i) { stringv = a; intv = i; } /** The accessor object for stringv. * * Subclasses ComparableAttribute, which provides requires that the value type * is comparable. Otherwise we would have to implemnet getComparator() ourselves. * */ public static Attribute<String,TestObject> str_attr = new ComparableAttribute<String,TestObject>() { /** Return the value of member stringv in an instance of TestObject. * * @param o An instance of TestObject * @return the value of member variable stringv in o */ public String getValue(TestObject o) { return o.stringv; } /** Set the value of member stringv in an instance of TestObject. * * @param o An instance of TestObject * @param s a new value for the member variable stringv in o */ public void setValue(String s, TestObject o) { o.stringv = s; } }; /** The accessor object for intv. * * Subclasses ComparableAttribute, which provides requires that the value type * is comparable. Otherwise we would have to implement getComparator() ourselves. * */ public static NumericAttribute<Integer, TestObject> int_attr = new NumericAttribute<Integer,TestObject>() { /** Return the value of member stringv in an instance of TestObject. * * @param o An instance of TestObject * @return the value of member variable intv in o */ public Integer getValue(TestObject o) { return o.intv; } /** Set the value of member intv in an instance of TestObject. * * @param o An instance of TestObject * @param i a new value for the member variable intv in o */ public void setValue(Integer i, TestObject o) { o.intv = i; } }; } /** Test cases for the Attribute package. * * We define a class (TestObject) and two attributes int_attr and string_attr, and * perform various test operations thereon. * * @author jonathan */ public class TestAttributePackage extends TestCase { /** Create a list full of TestObjects. * * @return a list of test data. All data fields are defined. */ private LinkedList<TestObject> newTestList() { LinkedList<TestObject> result = new LinkedList<TestObject>(); result.add(new TestObject("one", 1)); result.add(new TestObject("two", 2)); result.add(new TestObject("three", 3)); result.add(new TestObject("four", 4)); result.add(new TestObject("five", 5)); result.add(new TestObject("six", 6)); result.add(new TestObject("seven", 7)); result.add(new TestObject("eight", 8)); return result; } /** Create a list full of TestObjects. * * @return a list of test data. Some data fields are null. */ private LinkedList<TestObject> newTestListWithNulls() { LinkedList<TestObject> list = newTestList(); list.addFirst(new TestObject("nullint",null)); list.addLast(new TestObject(null, new Integer(9))); list.add(4,new TestObject(null,null)); return list; } /** Tests getting the value of intv out of a TestObject using an attribute. */ public void testGetAttributeInt() { TestObject o = new TestObject("one",1); assertEquals(new Integer(1), TestObject.int_attr.getValue(o)); } /** Tests getting the value of stringv out of a TestObject using an attribute. */ public void testGetAttributeString() { TestObject o = new TestObject("one",1); assertEquals("one", TestObject.str_attr.getValue(o)); } /** Tests setting the value of intv in a TestObject using an attribute. */ public void testSetAttributeInt() { TestObject o = new TestObject("one",2); TestObject.int_attr.setValue(1,o); assertEquals(new Integer(1), TestObject.int_attr.getValue(o)); } /** Tests setting the value of stringv in a TestObject using an attribute. */ public void testSetAttributeString() { TestObject o = new TestObject("two",1); TestObject.str_attr.setValue("one",o); assertEquals("one", TestObject.str_attr.getValue(o)); } /** Tests the use of the Attributes.max function on string and integer attributes */ public void testMax() { LinkedList<TestObject> list = newTestList(); assertEquals(new Integer(8), Attributes.max(list, TestObject.int_attr)); assertEquals("two", Attributes.max(list, TestObject.str_attr)); } /** Tests the use of the Attributes.max function on string and integer attributes */ public void testMaxWithNulls() { LinkedList<TestObject> list = newTestListWithNulls(); assertEquals(new Integer(9), Attributes.max(list, TestObject.int_attr)); assertEquals("two", Attributes.max(list, TestObject.str_attr)); } /** Tests the use of the Attributes.min function on string and integer attributes */ public void testMin() { LinkedList<TestObject> list = newTestList(); assertEquals(new Integer(1), Attributes.min(list, TestObject.int_attr)); assertEquals("eight", Attributes.min(list, TestObject.str_attr)); } /** Tests the use of the Attributes.min function on string and integer attributes. * * Note: This is where we have to start thinking. If values can be null, we want * the min function to return the lowest non-null value. However, if we are sorting * a list, the null values should be treated as 'lowest'. */ public void testMinWithNulls() { LinkedList<TestObject> list = newTestListWithNulls(); assertEquals(new Integer(1), Attributes.min(list, TestObject.int_attr)); assertEquals("eight", Attributes.min(list, TestObject.str_attr)); } /** Tests the use of the Attributes.count function on string and integer attributes */ public void testCount() { LinkedList<TestObject> list = newTestList(); assertEquals(8, Attributes.count(list, TestObject.int_attr)); assertEquals(8, Attributes.count(list, TestObject.str_attr)); } public void testSort() { LinkedList<TestObject> list = newTestList(); Attributes.sort(list, TestObject.str_attr); assertEquals(list.getFirst().intv, new Integer(8)); assertEquals(list.getLast().intv, new Integer(2)); } private void doTestAggregates(LinkedList<TestObject> list) { Accumulator<TestObject,String> max_str = TestObject.str_attr.newMax(); Accumulator<TestObject,String> min_str = TestObject.str_attr.newMin(); Accumulator<TestObject,Long> count_str = TestObject.str_attr.newCount(); Accumulator<TestObject,Number> sum_int = TestObject.int_attr.newSum(); LinkedList<Accumulator<TestObject,?>> aggs = new LinkedList<Accumulator<TestObject,?>>(); aggs.add(max_str); aggs.add(min_str); aggs.add(count_str); aggs.add(sum_int); Attributes.aggregates(list, aggs); assertEquals(max_str.getResult(), Attributes.max(list, TestObject.str_attr)); assertEquals(min_str.getResult(), Attributes.min(list, TestObject.str_attr)); // figure out what the count and sum are supposed to be long count = 0; double sum = 0; for (TestObject o : list) { if (o.stringv != null) count++; if (o.intv != null) sum+=o.intv.doubleValue(); } assertEquals(count_str.getResult(), new Long(count)); assertEquals(sum_int.getResult(), new Double(sum)); } public void testAggregates() { doTestAggregates(newTestList()); } public void testAggregatesWithNulls() { doTestAggregates(newTestListWithNulls()); } public void testFilter() { LinkedList<TestObject> test = newTestList(); List<TestObject> result; result = Operations.filter(test, TestObject.int_attr.isLessThan(4)); assertEquals(result.size(), 3); result = Operations.filter(test, TestObject.int_attr.isGreaterThan(6)); assertEquals(result.size(), 2); // expand this. } public static TestSuite suite() { return new TestSuite(TestAttributePackage.class); } public static void main(String[] args) { junit.textui.TestRunner.run(suite()); } } |