001: /*
002: * Copyright 2003-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.math.stat.data;
018:
019: import java.io.BufferedReader;
020: import java.io.IOException;
021: import java.io.InputStreamReader;
022: import java.lang.reflect.Method;
023: import java.net.URL;
024: import java.util.HashMap;
025: import java.util.Iterator;
026: import java.util.Map;
027:
028: import org.apache.commons.math.TestUtils;
029: import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
030: import org.apache.commons.math.stat.descriptive.SummaryStatistics;
031:
032: import junit.framework.TestCase;
033:
034: /**
035: * @version $Revision: 201916 $ $Date: 2005-06-26 15:25:41 -0700 (Sun, 26 Jun 2005) $
036: */
037: public abstract class CertifiedDataAbstractTest extends TestCase {
038:
039: private DescriptiveStatistics descriptives;
040:
041: private SummaryStatistics summaries;
042:
043: private Map certifiedValues;
044:
045: protected void setUp() throws Exception {
046: descriptives = DescriptiveStatistics.newInstance();
047: summaries = SummaryStatistics.newInstance();
048: certifiedValues = new HashMap();
049:
050: loadData();
051: }
052:
053: private void loadData() throws IOException {
054: BufferedReader in = null;
055:
056: try {
057: URL resourceURL = getClass().getClassLoader().getResource(
058: getResourceName());
059: in = new BufferedReader(new InputStreamReader(resourceURL
060: .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)
082: .trim();
083: Double value = new Double(valueString);
084: certifiedValues.put(name, value);
085: }
086: }
087: line = in.readLine();
088: }
089: } finally {
090: if (in != null) {
091: in.close();
092: }
093: }
094: }
095:
096: /**
097: * @return
098: */
099: protected abstract String getResourceName();
100:
101: protected double getMaximumAbsoluteError() {
102: return 1.0e-5;
103: }
104:
105: protected void tearDown() throws Exception {
106: descriptives.clear();
107: descriptives = null;
108:
109: summaries.clear();
110: summaries = null;
111:
112: certifiedValues.clear();
113: certifiedValues = null;
114: }
115:
116: public void testCertifiedValues() throws Exception {
117: Iterator iter = certifiedValues.keySet().iterator();
118: while (iter.hasNext()) {
119: String name = iter.next().toString();
120: Double expectedValue = (Double) certifiedValues.get(name);
121: try {
122: Double summariesValue = (Double) this .getProperty(
123: summaries, name);
124: TestUtils.assertEquals("summary value for " + name
125: + " is incorrect.", summariesValue
126: .doubleValue(), expectedValue.doubleValue(),
127: getMaximumAbsoluteError());
128: } catch (Exception ex) {
129: }
130:
131: try {
132: Double descriptivesValue = (Double) this .getProperty(
133: descriptives, name);
134: TestUtils.assertEquals("descriptive value for " + name
135: + " is incorrect.", descriptivesValue
136: .doubleValue(), expectedValue.doubleValue(),
137: getMaximumAbsoluteError());
138: } catch (Exception ex) {
139: }
140: }
141: }
142:
143: protected Object getProperty(Object bean, String name)
144: throws Exception {
145: // Get the value of prop
146: String prop = "get" + name.substring(0, 1).toUpperCase()
147: + name.substring(1);
148: Method meth = bean.getClass().getMethod(prop, new Class[0]);
149: return meth.invoke(bean, new Object[0]);
150: }
151: }
|