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