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.Iterator;
28 import java.util.Map;
29
30 import org.apache.commons.math.TestUtils;
31 import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
32 import org.apache.commons.math.stat.descriptive.SummaryStatistics;
33
34 import junit.framework.TestCase;
35
36
37
38
39 public abstract class CertifiedDataAbstractTest extends TestCase {
40
41 private DescriptiveStatistics descriptives;
42
43 private SummaryStatistics summaries;
44
45 private Map certifiedValues;
46
47 protected void setUp() throws Exception {
48 descriptives = new DescriptiveStatistics();
49 summaries = new SummaryStatistics();
50 certifiedValues = new HashMap();
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 = new Double(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 protected void tearDown() throws Exception {
102 descriptives.clear();
103 descriptives = null;
104
105 summaries.clear();
106 summaries = null;
107
108 certifiedValues.clear();
109 certifiedValues = null;
110 }
111
112 public void testCertifiedValues() {
113 Iterator iter = certifiedValues.keySet().iterator();
114
115 while (iter.hasNext()) {
116 String name = iter.next().toString();
117 Double expectedValue = (Double)certifiedValues.get(name);
118
119 Double summariesValue = getProperty(summaries, name);
120 if (summariesValue != null) {
121 TestUtils.assertEquals("summary value for " + name + " is incorrect.",
122 summariesValue.doubleValue(), expectedValue.doubleValue(),
123 getMaximumAbsoluteError());
124 }
125
126 Double descriptivesValue = getProperty(descriptives, name);
127 if (descriptivesValue != null) {
128 TestUtils.assertEquals("descriptive value for " + name + " is incorrect.",
129 descriptivesValue.doubleValue(), expectedValue.doubleValue(),
130 getMaximumAbsoluteError());
131 }
132 }
133 }
134
135
136 protected Double getProperty(Object bean, String name) {
137 try {
138
139 String prop = "get" + name.substring(0,1).toUpperCase() + name.substring(1);
140 Method meth = bean.getClass().getMethod(prop, new Class[0]);
141 Object property = meth.invoke(bean, new Object[0]);
142 if (meth.getReturnType().equals(Double.TYPE)) {
143 return (Double) property;
144 } else if (meth.getReturnType().equals(Long.TYPE)) {
145 return new Double(((Long) property).doubleValue());
146 } else {
147 fail("wrong type: " + meth.getReturnType().getName());
148 }
149 } catch (NoSuchMethodException nsme) {
150
151 } catch (InvocationTargetException ite) {
152 fail(ite.getMessage());
153 } catch (IllegalAccessException iae) {
154 fail(iae.getMessage());
155 }
156 return null;
157 }
158 }