1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.math4.legacy.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 org.apache.commons.math4.legacy.TestUtils;
30 import org.apache.commons.math4.legacy.stat.descriptive.DescriptiveStatistics;
31 import org.apache.commons.math4.legacy.stat.descriptive.SummaryStatistics;
32 import org.junit.After;
33 import org.junit.Assert;
34 import org.junit.Before;
35 import org.junit.Test;
36
37
38
39 public abstract class CertifiedDataAbstractTest {
40
41 private DescriptiveStatistics descriptives;
42
43 private SummaryStatistics summaries;
44
45 private Map<String, Double> certifiedValues;
46
47 @Before
48 public void setUp() throws IOException {
49 descriptives = new DescriptiveStatistics();
50 summaries = new SummaryStatistics();
51 certifiedValues = new HashMap<>();
52
53 loadData();
54 }
55
56 private void loadData() throws IOException {
57 BufferedReader in = null;
58
59 try {
60 URL resourceURL = getClass().getClassLoader().getResource(getResourceName());
61 in = new BufferedReader(new InputStreamReader(resourceURL.openStream()));
62
63 String line = in.readLine();
64 while (line != null) {
65
66
67
68
69 line = line.trim();
70
71
72 if (!("".equals(line) || line.startsWith("#"))) {
73 int n = line.indexOf('=');
74 if (n == -1) {
75
76 double value = Double.parseDouble(line);
77 descriptives.addValue(value);
78 summaries.addValue(value);
79 } else {
80
81 String name = line.substring(0, n).trim();
82 String valueString = line.substring(n + 1).trim();
83 Double value = Double.valueOf(valueString);
84 certifiedValues.put(name, value);
85 }
86 }
87 line = in.readLine();
88 }
89 } finally {
90 if (in != null) {
91 in.close();
92 }
93 }
94 }
95
96 protected abstract String getResourceName();
97
98 protected double getMaximumAbsoluteError() {
99 return 1.0e-5;
100 }
101
102 @After
103 public void tearDown() {
104 descriptives.clear();
105 descriptives = null;
106
107 summaries.clear();
108 summaries = null;
109
110 certifiedValues.clear();
111 certifiedValues = null;
112 }
113
114 @Test
115 public void testCertifiedValues() {
116 for (String name : certifiedValues.keySet()) {
117 Double expectedValue = 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);
141 Object property = meth.invoke(bean);
142 if (meth.getReturnType().equals(Double.TYPE)) {
143 return (Double) property;
144 } else if (meth.getReturnType().equals(Long.TYPE)) {
145 return Double.valueOf(((Long) property).doubleValue());
146 } else {
147 Assert.fail("wrong type: " + meth.getReturnType().getName());
148 }
149 } catch (NoSuchMethodException nsme) {
150
151 } catch (InvocationTargetException ite) {
152 Assert.fail(ite.getMessage());
153 } catch (IllegalAccessException iae) {
154 Assert.fail(iae.getMessage());
155 }
156 return null;
157 }
158 }