1 16 17 package org.apache.commons.math.stat.data; 18 19 import java.io.BufferedReader ; 20 import java.io.IOException ; 21 import java.io.InputStreamReader ; 22 import java.lang.reflect.Method ; 23 import java.net.URL ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.Map ; 27 28 import org.apache.commons.math.TestUtils; 29 import org.apache.commons.math.stat.descriptive.DescriptiveStatistics; 30 import org.apache.commons.math.stat.descriptive.SummaryStatistics; 31 32 import junit.framework.TestCase; 33 34 37 public abstract class CertifiedDataAbstractTest extends TestCase { 38 39 private DescriptiveStatistics descriptives; 40 41 private SummaryStatistics summaries; 42 43 private Map certifiedValues; 44 45 protected void setUp() throws Exception { 46 descriptives = DescriptiveStatistics.newInstance(); 47 summaries = SummaryStatistics.newInstance(); 48 certifiedValues = new HashMap (); 49 50 loadData(); 51 } 52 53 private void loadData() throws IOException { 54 BufferedReader in = null; 55 56 try { 57 URL resourceURL = getClass().getClassLoader().getResource(getResourceName()); 58 in = new BufferedReader (new InputStreamReader (resourceURL.openStream())); 59 60 String line = in.readLine(); 61 while (line != null) { 62 63 66 line = line.trim(); 67 68 if (!("".equals(line) || line.startsWith("#"))) { 70 int n = line.indexOf('='); 71 if (n == -1) { 72 double value = Double.parseDouble(line); 74 descriptives.addValue(value); 75 summaries.addValue(value); 76 } else { 77 String name = line.substring(0, n).trim(); 79 String valueString = line.substring(n + 1).trim(); 80 Double value = new Double (valueString); 81 certifiedValues.put(name, value); 82 } 83 } 84 line = in.readLine(); 85 } 86 } finally { 87 if (in != null) { 88 in.close(); 89 } 90 } 91 } 92 93 96 protected abstract String getResourceName(); 97 98 protected double getMaximumAbsoluteError() { 99 return 1.0e-5; 100 } 101 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() throws Exception { 114 Iterator iter = certifiedValues.keySet().iterator(); 115 while (iter.hasNext()) { 116 String name = iter.next().toString(); 117 Double expectedValue = (Double )certifiedValues.get(name); 118 try { 119 Double summariesValue = (Double )this.getProperty(summaries, name); 120 TestUtils.assertEquals("summary value for " + name + " is incorrect.", 121 summariesValue.doubleValue(), expectedValue.doubleValue(), getMaximumAbsoluteError()); 122 } catch (Exception ex) { 123 } 124 125 try { 126 Double descriptivesValue = (Double )this.getProperty(descriptives, name); 127 TestUtils.assertEquals("descriptive value for " + name + " is incorrect.", 128 descriptivesValue.doubleValue(), expectedValue.doubleValue(), getMaximumAbsoluteError()); 129 } catch (Exception ex) { 130 } 131 } 132 } 133 134 135 protected Object getProperty(Object bean, String name) throws Exception { 136 String prop = "get" + name.substring(0,1).toUpperCase() + name.substring(1); 138 Method meth = bean.getClass().getMethod(prop, new Class [0]); 139 return meth.invoke(bean, new Object [0]); 140 } 141 } 142 | Popular Tags |