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;
20  import java.io.IOException;
21  import java.io.InputStreamReader;
22  import java.lang.reflect.Method;
23  import java.net.URL;
24  import java.util.HashMap;
25  import java.util.Iterator;
26  import java.util.Map;
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: 1.8 $ $Date: 2004/12/06 05:49:44 $
36   */
37  public abstract class CertifiedDataAbstractTest extends TestCase {
38  	
39  	private DescriptiveStatistics descriptives;
40  	
41  	private SummaryStatistics summaries;
42  	
43  	private Map certifiedValues;
44  	
45  	protected void setUp() throws Exception {
46  		descriptives = DescriptiveStatistics.newInstance();
47  		summaries = SummaryStatistics.newInstance();
48  		certifiedValues = new HashMap();
49  		
50  		loadData();
51  	}
52  
53  	private void loadData() throws IOException {
54  		BufferedReader in = null;
55  
56  		try {
57  			URL resourceURL = getClass().getClassLoader().getResource(getResourceName());
58  			in = new BufferedReader(new InputStreamReader(resourceURL.openStream()));
59  			
60  			String 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 name = line.substring(0, n).trim();
79  						String valueString = line.substring(n + 1).trim();
80  						Double value = new Double(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 getResourceName();
97  
98  	protected double getMaximumAbsoluteError() {
99  		return 1.0e-5;
100 	}
101 	
102 	protected void tearDown() throws Exception {
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 {
114 		Iterator iter = certifiedValues.keySet().iterator();
115 		while (iter.hasNext()) {
116 			String name = iter.next().toString();
117 			Double expectedValue = (Double)certifiedValues.get(name);
118 			try {
119 				Double summariesValue = (Double)this.getProperty(summaries, name);
120 				TestUtils.assertEquals("summary value for " + name + " is incorrect.",
121 						summariesValue.doubleValue(), expectedValue.doubleValue(), getMaximumAbsoluteError());
122 			} catch (Exception ex) {
123 			}
124 			
125 			try {
126 				Double descriptivesValue = (Double)this.getProperty(descriptives, name);
127 				TestUtils.assertEquals("descriptive value for " + name + " is incorrect.",
128 						descriptivesValue.doubleValue(), expectedValue.doubleValue(), getMaximumAbsoluteError());
129 			} catch (Exception ex) {
130 			}
131 		}
132  	}
133 	
134 	
135 	protected Object getProperty(Object bean, String name) throws Exception{
136 	    // Get the value of prop
137 	    String prop = "get" + name.substring(0,1).toUpperCase() + name.substring(1); 
138         Method meth = bean.getClass().getMethod(prop, new Class[0]);
139         return meth.invoke(bean, new Object[0]);
140 	}
141 }