KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > math > stat > data > CertifiedDataAbstractTest


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.math.stat.data;
18
19 import java.io.BufferedReader JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStreamReader JavaDoc;
22 import java.lang.reflect.Method JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import org.apache.commons.math.TestUtils;
29 import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
30 import org.apache.commons.math.stat.descriptive.SummaryStatistics;
31
32 import junit.framework.TestCase;
33
34 /**
35  * @version $Revision$ $Date: 2005-06-26 15:25:41 -0700 (Sun, 26 Jun 2005) $
36  */

37 public abstract class CertifiedDataAbstractTest extends TestCase {
38     
39     private DescriptiveStatistics descriptives;
40     
41     private SummaryStatistics summaries;
42     
43     private Map JavaDoc certifiedValues;
44     
45     protected void setUp() throws Exception JavaDoc {
46         descriptives = DescriptiveStatistics.newInstance();
47         summaries = SummaryStatistics.newInstance();
48         certifiedValues = new HashMap JavaDoc();
49         
50         loadData();
51     }
52
53     private void loadData() throws IOException JavaDoc {
54         BufferedReader JavaDoc in = null;
55
56         try {
57             URL JavaDoc resourceURL = getClass().getClassLoader().getResource(getResourceName());
58             in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(resourceURL.openStream()));
59             
60             String JavaDoc line = in.readLine();
61             while (line != null) {
62                 
63                 /* this call to StringUtils did little for the
64                  * following conditional structure
65                  */

66                 line = line.trim();
67
68                 // not empty line or comment
69
if (!("".equals(line) || line.startsWith("#"))) {
70                     int n = line.indexOf('=');
71                     if (n == -1) {
72                         // data value
73
double value = Double.parseDouble(line);
74                         descriptives.addValue(value);
75                         summaries.addValue(value);
76                     } else {
77                         // certified value
78
String JavaDoc name = line.substring(0, n).trim();
79                         String JavaDoc valueString = line.substring(n + 1).trim();
80                         Double JavaDoc value = new Double JavaDoc(valueString);
81                         certifiedValues.put(name, value);
82                     }
83                 }
84                 line = in.readLine();
85             }
86         } finally {
87             if (in != null) {
88                 in.close();
89             }
90         }
91     }
92
93     /**
94      * @return
95      */

96     protected abstract String JavaDoc getResourceName();
97
98     protected double getMaximumAbsoluteError() {
99         return 1.0e-5;
100     }
101     
102     protected void tearDown() throws Exception JavaDoc {
103         descriptives.clear();
104         descriptives = null;
105         
106         summaries.clear();
107         summaries = null;
108         
109         certifiedValues.clear();
110         certifiedValues = null;
111     }
112     
113     public void testCertifiedValues() throws Exception JavaDoc {
114         Iterator JavaDoc iter = certifiedValues.keySet().iterator();
115         while (iter.hasNext()) {
116             String JavaDoc name = iter.next().toString();
117             Double JavaDoc expectedValue = (Double JavaDoc)certifiedValues.get(name);
118             try {
119                 Double JavaDoc summariesValue = (Double JavaDoc)this.getProperty(summaries, name);
120                 TestUtils.assertEquals("summary value for " + name + " is incorrect.",
121                         summariesValue.doubleValue(), expectedValue.doubleValue(), getMaximumAbsoluteError());
122             } catch (Exception JavaDoc ex) {
123             }
124             
125             try {
126                 Double JavaDoc descriptivesValue = (Double JavaDoc)this.getProperty(descriptives, name);
127                 TestUtils.assertEquals("descriptive value for " + name + " is incorrect.",
128                         descriptivesValue.doubleValue(), expectedValue.doubleValue(), getMaximumAbsoluteError());
129             } catch (Exception JavaDoc ex) {
130             }
131         }
132     }
133     
134     
135     protected Object JavaDoc getProperty(Object JavaDoc bean, String JavaDoc name) throws Exception JavaDoc{
136         // Get the value of prop
137
String JavaDoc prop = "get" + name.substring(0,1).toUpperCase() + name.substring(1);
138         Method JavaDoc meth = bean.getClass().getMethod(prop, new Class JavaDoc[0]);
139         return meth.invoke(bean, new Object JavaDoc[0]);
140     }
141 }
142
Popular Tags