1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.math.stat.data;
19
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;
25 import java.net.URL;
26 import java.util.HashMap;
27 import java.util.Map;
28
29 import junit.framework.TestCase;
30
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;
34
35
36
37
38 public abstract class CertifiedDataAbstractTest extends TestCase {
39
40 private DescriptiveStatistics descriptives;
41
42 private SummaryStatistics summaries;
43
44 private Map<String, Double> certifiedValues;
45
46 @Override
47 protected void setUp() throws Exception {
48 descriptives = new DescriptiveStatistics();
49 summaries = new SummaryStatistics();
50 certifiedValues = new HashMap<String, Double>();
51
52 loadData();
53 }
54
55 private void loadData() throws IOException {
56 BufferedReader in = null;
57
58 try {
59 URL resourceURL = getClass().getClassLoader().getResource(getResourceName());
60 in = new BufferedReader(new InputStreamReader(resourceURL.openStream()));
61
62 String line = in.readLine();
63 while (line != null) {
64
65
66
67
68 line = line.trim();
69
70
71 if (!("".equals(line) || line.startsWith("#"))) {
72 int n = line.indexOf('=');
73 if (n == -1) {
74
75 double value = Double.parseDouble(line);
76 descriptives.addValue(value);
77 summaries.addValue(value);
78 } else {
79
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);
84 }
85 }
86 line = in.readLine();
87 }
88 } finally {
89 if (in != null) {
90 in.close();
91 }
92 }
93 }
94
95 protected abstract String getResourceName();
96
97 protected double getMaximumAbsoluteError() {
98 return 1.0e-5;
99 }
100
101 @Override
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() {
114 for (String name : certifiedValues.keySet()) {
115 Double expectedValue = certifiedValues.get(name);
116
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());
122 }
123
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());
129 }
130 }
131 }
132
133
134 protected Double getProperty(Object bean, String name) {
135 try {
136
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());
144 } else {
145 fail("wrong type: " + meth.getReturnType().getName());
146 }
147 } catch (NoSuchMethodException nsme) {
148
149 } catch (InvocationTargetException ite) {
150 fail(ite.getMessage());
151 } catch (IllegalAccessException iae) {
152 fail(iae.getMessage());
153 }
154 return null;
155 }
156 }