1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.hipparchus.stat.data;
24
25 import java.io.BufferedReader;
26 import java.io.IOException;
27 import java.io.InputStreamReader;
28 import java.lang.reflect.InvocationTargetException;
29 import java.lang.reflect.Method;
30 import java.net.URL;
31 import java.util.HashMap;
32 import java.util.Map;
33
34 import org.hipparchus.UnitTestUtils;
35 import org.hipparchus.stat.descriptive.DescriptiveStatistics;
36 import org.hipparchus.stat.descriptive.StreamingStatistics;
37 import org.junit.After;
38 import org.junit.Assert;
39 import org.junit.Before;
40 import org.junit.Test;
41
42
43
44 public abstract class CertifiedDataAbstractTest {
45
46 private DescriptiveStatistics descriptives;
47
48 private StreamingStatistics summaries;
49
50 private Map<String, Double> certifiedValues;
51
52 @Before
53 public void setUp() throws IOException {
54 descriptives = new DescriptiveStatistics();
55 summaries = new StreamingStatistics();
56 certifiedValues = new HashMap<String, Double>();
57
58 loadData();
59 }
60
61 private void loadData() throws IOException {
62 BufferedReader in = null;
63
64 try {
65 URL resourceURL = getClass().getClassLoader().getResource(getResourceName());
66 in = new BufferedReader(new InputStreamReader(resourceURL.openStream()));
67
68 String line = in.readLine();
69 while (line != null) {
70
71
72
73
74 line = line.trim();
75
76
77 if (!("".equals(line) || line.startsWith("#"))) {
78 int n = line.indexOf('=');
79 if (n == -1) {
80
81 double value = Double.parseDouble(line);
82 descriptives.addValue(value);
83 summaries.addValue(value);
84 } else {
85
86 String name = line.substring(0, n).trim();
87 String valueString = line.substring(n + 1).trim();
88 Double value = Double.valueOf(valueString);
89 certifiedValues.put(name, value);
90 }
91 }
92 line = in.readLine();
93 }
94 } finally {
95 if (in != null) {
96 in.close();
97 }
98 }
99 }
100
101 protected abstract String getResourceName();
102
103 protected double getMaximumAbsoluteError() {
104 return 1.0e-5;
105 }
106
107 @After
108 public void tearDown() {
109 descriptives.clear();
110 descriptives = null;
111
112 summaries.clear();
113 summaries = null;
114
115 certifiedValues.clear();
116 certifiedValues = null;
117 }
118
119 @Test
120 public void testCertifiedValues() {
121 for (String name : certifiedValues.keySet()) {
122 Double expectedValue = certifiedValues.get(name);
123
124 Double summariesValue = getProperty(summaries, name);
125 if (summariesValue != null) {
126 UnitTestUtils.assertEquals("summary value for " + name + " is incorrect.",
127 summariesValue.doubleValue(), expectedValue.doubleValue(),
128 getMaximumAbsoluteError());
129 }
130
131 Double descriptivesValue = getProperty(descriptives, name);
132 if (descriptivesValue != null) {
133 UnitTestUtils.assertEquals("descriptive value for " + name + " is incorrect.",
134 descriptivesValue.doubleValue(), expectedValue.doubleValue(),
135 getMaximumAbsoluteError());
136 }
137 }
138 }
139
140
141 protected Double getProperty(Object bean, String name) {
142 try {
143
144 String prop = "get" + name.substring(0,1).toUpperCase() + name.substring(1);
145 Method meth = bean.getClass().getMethod(prop, new Class[0]);
146 meth.setAccessible(true);
147 Object property = meth.invoke(bean, new Object[0]);
148 if (meth.getReturnType().equals(Double.TYPE)) {
149 return (Double) property;
150 } else if (meth.getReturnType().equals(Long.TYPE)) {
151 return Double.valueOf(((Long) property).doubleValue());
152 } else {
153 Assert.fail("wrong type: " + meth.getReturnType().getName());
154 }
155 } catch (NoSuchMethodException nsme) {
156
157 } catch (InvocationTargetException ite) {
158 Assert.fail(ite.getMessage());
159 } catch (IllegalAccessException iae) {
160 Assert.fail(iae.getMessage());
161 }
162 return null;
163 }
164 }