001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     * 
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.math.stat.data;
019    
020    import java.io.BufferedReader;
021    import java.io.IOException;
022    import java.io.InputStreamReader;
023    import java.lang.reflect.InvocationTargetException;
024    import java.lang.reflect.Method;
025    import java.net.URL;
026    import java.util.HashMap;
027    import java.util.Map;
028    
029    import junit.framework.TestCase;
030    
031    import org.apache.commons.math.TestUtils;
032    import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
033    import org.apache.commons.math.stat.descriptive.SummaryStatistics;
034    
035    /**
036     * @version $Revision: 762118 $ $Date: 2009-04-05 12:55:59 -0400 (Sun, 05 Apr 2009) $
037     */
038    public abstract class CertifiedDataAbstractTest extends TestCase {
039        
040        private DescriptiveStatistics descriptives;
041        
042        private SummaryStatistics summaries;
043        
044        private Map<String, Double> certifiedValues;
045    
046        @Override
047        protected void setUp() throws Exception {
048            descriptives = new DescriptiveStatistics();
049            summaries = new SummaryStatistics();
050            certifiedValues = new HashMap<String, Double>();
051            
052            loadData();
053        }
054    
055        private void loadData() throws IOException {
056            BufferedReader in = null;
057    
058            try {
059                URL resourceURL = getClass().getClassLoader().getResource(getResourceName());
060                in = new BufferedReader(new InputStreamReader(resourceURL.openStream()));
061                
062                String line = in.readLine();
063                while (line != null) {
064                    
065                    /* this call to StringUtils did little for the 
066                     * following conditional structure 
067                     */
068                    line = line.trim();
069    
070                    // not empty line or comment
071                    if (!("".equals(line) || line.startsWith("#"))) {
072                        int n = line.indexOf('=');
073                        if (n == -1) {
074                            // data value
075                            double value = Double.parseDouble(line);
076                            descriptives.addValue(value);
077                            summaries.addValue(value);
078                        } else {
079                            // certified value
080                            String name = line.substring(0, n).trim();
081                            String valueString = line.substring(n + 1).trim();
082                            Double value = Double.valueOf(valueString);
083                            certifiedValues.put(name, value);
084                        }
085                    }
086                    line = in.readLine();
087                }
088            } finally {
089                if (in != null) {
090                    in.close();
091                }
092            }
093        }
094    
095        protected abstract String getResourceName();
096    
097        protected double getMaximumAbsoluteError() {
098            return 1.0e-5;
099        }
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    }