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    *      http://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  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.Map;
28  
29  import junit.framework.TestCase;
30  
31  import org.apache.commons.math.TestUtils;
32  import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
33  import org.apache.commons.math.stat.descriptive.SummaryStatistics;
34  
35  /**
36   * @version $Revision: 762118 $ $Date: 2009-04-05 12:55:59 -0400 (Sun, 05 Apr 2009) $
37   */
38  public abstract class CertifiedDataAbstractTest extends TestCase {
39      
40      private DescriptiveStatistics descriptives;
41      
42      private SummaryStatistics summaries;
43      
44      private Map<String, Double> certifiedValues;
45  
46      @Override
47      protected void setUp() throws Exception {
48          descriptives = new DescriptiveStatistics();
49          summaries = new SummaryStatistics();
50          certifiedValues = new HashMap<String, Double>();
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                  /* this call to StringUtils did little for the 
66                   * following conditional structure 
67                   */
68                  line = line.trim();
69  
70                  // not empty line or comment
71                  if (!("".equals(line) || line.startsWith("#"))) {
72                      int n = line.indexOf('=');
73                      if (n == -1) {
74                          // data value
75                          double value = Double.parseDouble(line);
76                          descriptives.addValue(value);
77                          summaries.addValue(value);
78                      } else {
79                          // certified value
80                          String name = line.substring(0, n).trim();
81                          String valueString = line.substring(n + 1).trim();
82                          Double value = Double.valueOf(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     @Override
102     protected void tearDown() throws Exception {
103         descriptives.clear();
104         descriptives = null;
105         
106         summaries.clear();
107         summaries = null;
108         
109         certifiedValues.clear();
110         certifiedValues = null;
111     }
112     
113     public void testCertifiedValues() {
114         for (String name : certifiedValues.keySet()) {
115             Double expectedValue = certifiedValues.get(name);
116 
117             Double summariesValue = getProperty(summaries, name);
118             if (summariesValue != null) {
119                 TestUtils.assertEquals("summary value for " + name + " is incorrect.",
120                                        summariesValue.doubleValue(), expectedValue.doubleValue(),
121                                        getMaximumAbsoluteError());
122             }
123 
124             Double descriptivesValue = getProperty(descriptives, name);
125             if (descriptivesValue != null) {
126                 TestUtils.assertEquals("descriptive value for " + name + " is incorrect.",
127                                        descriptivesValue.doubleValue(), expectedValue.doubleValue(),
128                                        getMaximumAbsoluteError());
129             }
130         }
131     }
132     
133     
134     protected Double getProperty(Object bean, String name) {
135         try {
136             // Get the value of prop
137             String prop = "get" + name.substring(0,1).toUpperCase() + name.substring(1); 
138             Method meth = bean.getClass().getMethod(prop, new Class[0]);
139             Object property = meth.invoke(bean, new Object[0]);
140             if (meth.getReturnType().equals(Double.TYPE)) {
141                 return (Double) property;
142             } else if (meth.getReturnType().equals(Long.TYPE)) {
143                 return Double.valueOf(((Long) property).doubleValue());
144             } else {
145                 fail("wrong type: " + meth.getReturnType().getName());
146             }
147         } catch (NoSuchMethodException nsme) {
148             // ignored
149         } catch (InvocationTargetException ite) {
150             fail(ite.getMessage());
151         } catch (IllegalAccessException iae) {
152             fail(iae.getMessage());
153         }
154         return null;
155     }
156 }