2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
18
package org.apache.commons.math.stat.data;
20
import java.io.BufferedReader;
21
import java.io.IOException;
22
import java.io.InputStreamReader;
23
import java.lang.reflect.InvocationTargetException;
24
import java.lang.reflect.Method;
26
import java.util.HashMap;
29
import junit.framework.TestCase;
31
import org.apache.commons.math.TestUtils;
32
import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
33
import org.apache.commons.math.stat.descriptive.SummaryStatistics;
36
* @version $Revision: 762118 $ $Date: 2009-04-05 12:55:59 -0400 (Sun, 05 Apr 2009) $
38
public abstract class CertifiedDataAbstractTest extends TestCase {
40
private DescriptiveStatistics descriptives;
42
private SummaryStatistics summaries;
44
private Map<String, Double> certifiedValues;
47
protected void setUp() throws Exception {
48
descriptives = new DescriptiveStatistics();
49
summaries = new SummaryStatistics();
50
certifiedValues = new HashMap<String, Double>();
55
private void loadData() throws IOException {
56
BufferedReader in = null;
59
URL resourceURL = getClass().getClassLoader().getResource(getResourceName());
60
in = new BufferedReader(new InputStreamReader(resourceURL.openStream()));
62
String line = in.readLine();
63
while (line != null) {
65
/* this call to StringUtils did little for the
66
* following conditional structure
70
// not empty line or comment
71
if (!("".equals(line) || line.startsWith("#"))) {
72
int n = line.indexOf('=');
75
double value = Double.parseDouble(line);
76
descriptives.addValue(value);
77
summaries.addValue(value);
80
String name = line.substring(0, n).trim();
81
String valueString = line.substring(n + 1).trim();
82
Double value = Double.valueOf(valueString);
83
certifiedValues.put(name, value);
95
protected abstract String getResourceName();
97
protected double getMaximumAbsoluteError() {
102
protected void tearDown() throws Exception {
103
descriptives.clear();
109
certifiedValues.clear();
110
certifiedValues = null;
113
public void testCertifiedValues() {
114
for (String name : certifiedValues.keySet()) {
115
Double expectedValue = certifiedValues.get(name);
117
Double summariesValue = getProperty(summaries, name);
118
if (summariesValue != null) {
119
TestUtils.assertEquals("summary value for " + name + " is incorrect.",
120
summariesValue.doubleValue(), expectedValue.doubleValue(),
121
getMaximumAbsoluteError());
124
Double descriptivesValue = getProperty(descriptives, name);
125
if (descriptivesValue != null) {
126
TestUtils.assertEquals("descriptive value for " + name + " is incorrect.",
127
descriptivesValue.doubleValue(), expectedValue.doubleValue(),
128
getMaximumAbsoluteError());
134
protected Double getProperty(Object bean, String name) {
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
Object property = meth.invoke(bean, new Object[0]);
140
if (meth.getReturnType().equals(Double.TYPE)) {
141
return (Double) property;
142
} else if (meth.getReturnType().equals(Long.TYPE)) {
143
return Double.valueOf(((Long) property).doubleValue());
145
fail("wrong type: " + meth.getReturnType().getName());
147
} catch (NoSuchMethodException nsme) {
149
} catch (InvocationTargetException ite) {
150
fail(ite.getMessage());
151
} catch (IllegalAccessException iae) {
152
fail(iae.getMessage());