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 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                  /* this call to StringUtils did little for the
72                   * following conditional structure
73                   */
74                  line = line.trim();
75  
76                  // not empty line or comment
77                  if (!("".equals(line) || line.startsWith("#"))) {
78                      int n = line.indexOf('=');
79                      if (n == -1) {
80                          // data value
81                          double value = Double.parseDouble(line);
82                          descriptives.addValue(value);
83                          summaries.addValue(value);
84                      } else {
85                          // certified value
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             // Get the value of prop
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             // ignored
157         } catch (InvocationTargetException ite) {
158             Assert.fail(ite.getMessage());
159         } catch (IllegalAccessException iae) {
160             Assert.fail(iae.getMessage());
161         }
162         return null;
163     }
164 }