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  package org.apache.commons.math.stat.descriptive;
18  
19  
20  import junit.framework.Test;
21  import junit.framework.TestSuite;
22  
23  import org.apache.commons.math.stat.descriptive.moment.Mean;
24  import org.apache.commons.math.stat.descriptive.summary.Sum;
25  /**
26   * Test cases for the {@link SummaryStatistics} class.
27   * When SummaryStatisticsImpl is removed in math 2.0, test cases from
28   * SummaryStatisticsImplTest should be merged into this class.
29   *
30   * @version $Revision: 566833 $ $Date: 2007-08-16 13:36:33 -0700 (Thu, 16 Aug 2007) $
31   */
32  
33  public final class SummaryStatisticsTest extends SummaryStatisticsAbstractTest {
34  
35      public SummaryStatisticsTest(String name) {
36          super(name);
37      }
38      
39      public static Test suite() {
40          TestSuite suite = new TestSuite(SummaryStatisticsTest.class);
41          suite.setName("SummaryStatistics tests");
42          return suite;
43      }
44  
45      protected SummaryStatistics createSummaryStatistics() {
46          return new SummaryStatistics();
47      }
48      
49      public void testSetterInjection() throws Exception {
50          SummaryStatistics u = createSummaryStatistics();
51          u.setMeanImpl(new Sum());
52          u.setSumLogImpl(new Sum());
53          u.addValue(1);
54          u.addValue(3);
55          assertEquals(4, u.getMean(), 1E-14);
56          assertEquals(4, u.getSumOfLogs(), 1E-14);
57          assertEquals(Math.exp(2), u.getGeometricMean(), 1E-14);
58          u.clear();
59          u.addValue(1);
60          u.addValue(2);
61          assertEquals(3, u.getMean(), 1E-14);
62          u.clear();
63          u.setMeanImpl(new Mean()); // OK after clear
64      }
65      
66      public void testSetterIllegalState() throws Exception {
67          SummaryStatistics u = createSummaryStatistics();
68          u.addValue(1);
69          u.addValue(3);
70          try {
71              u.setMeanImpl(new Sum());
72              fail("Expecting IllegalStateException");
73          } catch (IllegalStateException ex) {
74              // expected
75          }
76      }
77  }