Coverage Report - com.jeantessier.metrics.TestNbSubMetricsMeasurement
 
Classes in this File Line Coverage Branch Coverage Complexity
TestNbSubMetricsMeasurement
93%
97/104
50%
4/8
1
 
 1  
 /*
 2  
  *  Copyright (c) 2001-2009, Jean Tessier
 3  
  *  All rights reserved.
 4  
  *  
 5  
  *  Redistribution and use in source and binary forms, with or without
 6  
  *  modification, are permitted provided that the following conditions
 7  
  *  are met:
 8  
  *  
 9  
  *      * Redistributions of source code must retain the above copyright
 10  
  *        notice, this list of conditions and the following disclaimer.
 11  
  *  
 12  
  *      * Redistributions in binary form must reproduce the above copyright
 13  
  *        notice, this list of conditions and the following disclaimer in the
 14  
  *        documentation and/or other materials provided with the distribution.
 15  
  *  
 16  
  *      * Neither the name of Jean Tessier nor the names of his contributors
 17  
  *        may be used to endorse or promote products derived from this software
 18  
  *        without specific prior written permission.
 19  
  *  
 20  
  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 21  
  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 22  
  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 23  
  *  A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR
 24  
  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 25  
  *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 26  
  *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 27  
  *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 28  
  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 29  
  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 30  
  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 31  
  */
 32  
 
 33  
 package com.jeantessier.metrics;
 34  
 
 35  
 import junit.framework.*;
 36  
 
 37  10
 public class TestNbSubMetricsMeasurement extends TestCase implements MeasurementVisitor {
 38  
     private MeasurementDescriptor descriptor;
 39  
     private NbSubMetricsMeasurement measurement;
 40  
     private Metrics metrics;
 41  
     private Measurement visited;
 42  
     
 43  
     protected void setUp() {
 44  10
         metrics = new Metrics("foo");
 45  
 
 46  10
         descriptor = new MeasurementDescriptor();
 47  10
         descriptor.setShortName("foo");
 48  10
         descriptor.setLongName("bar");
 49  10
         descriptor.setClassFor(NbSubMetricsMeasurement.class);
 50  10
         descriptor.setCached(false);
 51  10
     }
 52  
 
 53  
     public void testCreateFromMeasurementDescriptor() throws Exception {
 54  1
         measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement();
 55  
         
 56  1
         assertNotNull(measurement);
 57  1
         assertEquals(descriptor, measurement.getDescriptor());
 58  1
         assertSame(descriptor, measurement.getDescriptor());
 59  1
         assertEquals(NbSubMetricsMeasurement.class, measurement.getClass());
 60  1
         assertEquals("foo", measurement.getShortName());
 61  1
         assertEquals("bar", measurement.getLongName());
 62  1
     }
 63  
 
 64  
     public void testAddSubMetrics() throws Exception {
 65  1
         measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics);
 66  
 
 67  1
         assertEquals(0, measurement.getValue().intValue());
 68  1
         assertEquals(0.0, measurement.getValue().doubleValue(), 0.01);
 69  1
         assertEquals(0, measurement.getValue().intValue());
 70  
 
 71  1
         metrics.addSubMetrics(new Metrics("bar"));
 72  
 
 73  1
         assertEquals(1, measurement.getValue().intValue());
 74  1
         assertEquals(1.0, measurement.getValue().doubleValue(), 0.01);
 75  1
         assertEquals(1, measurement.getValue().intValue());
 76  
 
 77  1
         metrics.addSubMetrics(new Metrics("bar"));
 78  
 
 79  1
         assertEquals(1, measurement.getValue().intValue());
 80  1
         assertEquals(1.0, measurement.getValue().doubleValue(), 0.01);
 81  1
         assertEquals(1, measurement.getValue().intValue());
 82  
 
 83  1
         metrics.addSubMetrics(new Metrics("baz"));
 84  
 
 85  1
         assertEquals(2, measurement.getValue().intValue());
 86  1
         assertEquals(2.0, measurement.getValue().doubleValue(), 0.01);
 87  1
         assertEquals(2, measurement.getValue().intValue());
 88  1
     }
 89  
 
 90  
     public void testInUndefinedRange() throws Exception {
 91  1
         measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics);
 92  
         
 93  1
         assertTrue(measurement.isInRange());
 94  
 
 95  1
         metrics.addSubMetrics(new Metrics("foo"));
 96  
         
 97  1
         assertTrue(measurement.isInRange());
 98  
 
 99  1
         metrics.addSubMetrics(new Metrics("bar"));
 100  1
         metrics.addSubMetrics(new Metrics("baz"));
 101  
 
 102  1
         assertTrue(measurement.isInRange());
 103  1
     }
 104  
 
 105  
     public void testInOpenRange() throws Exception {
 106  1
         measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics);
 107  
         
 108  1
         assertTrue(measurement.isInRange());
 109  
 
 110  1
         metrics.addSubMetrics(new Metrics("foo"));
 111  
         
 112  1
         assertTrue(measurement.isInRange());
 113  
 
 114  1
         metrics.addSubMetrics(new Metrics("bar"));
 115  1
         metrics.addSubMetrics(new Metrics("baz"));
 116  
 
 117  1
         assertTrue(measurement.isInRange());
 118  1
     }
 119  
 
 120  
     public void testInLowerBoundRange() throws Exception {
 121  1
         descriptor.setLowerThreshold(new Integer(1));
 122  
 
 123  1
         measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics);
 124  
         
 125  1
         assertTrue(!measurement.isInRange());
 126  
 
 127  1
         metrics.addSubMetrics(new Metrics("foo"));
 128  
         
 129  1
         assertTrue(measurement.isInRange());
 130  
 
 131  1
         metrics.addSubMetrics(new Metrics("bar"));
 132  1
         metrics.addSubMetrics(new Metrics("baz"));
 133  
         
 134  1
         assertTrue(measurement.isInRange());
 135  1
     }
 136  
 
 137  
     public void testInUpperBoundRange() throws Exception {
 138  1
         descriptor.setUpperThreshold(new Float(1.5));
 139  
 
 140  1
         measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics);
 141  
         
 142  1
         assertTrue(measurement.isInRange());
 143  
 
 144  1
         metrics.addSubMetrics(new Metrics("foo"));
 145  
         
 146  1
         assertTrue(measurement.isInRange());
 147  
 
 148  1
         metrics.addSubMetrics(new Metrics("bar"));
 149  1
         metrics.addSubMetrics(new Metrics("baz"));
 150  
         
 151  1
         assertTrue(!measurement.isInRange());
 152  1
     }
 153  
 
 154  
     public void testInBoundRange() throws Exception {
 155  1
         descriptor.setLowerThreshold(new Integer(1));
 156  1
         descriptor.setUpperThreshold(new Float(1.5));
 157  
 
 158  1
         measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics);
 159  
         
 160  1
         assertTrue(!measurement.isInRange());
 161  
 
 162  1
         metrics.addSubMetrics(new Metrics("foo"));
 163  
         
 164  1
         assertTrue(measurement.isInRange());
 165  
 
 166  1
         metrics.addSubMetrics(new Metrics("bar"));
 167  1
         metrics.addSubMetrics(new Metrics("baz"));
 168  
         
 169  1
         assertTrue(!measurement.isInRange());
 170  1
     }
 171  
 
 172  
     public void testCachedValue() throws Exception {
 173  1
         descriptor.setCached(true);
 174  
 
 175  1
         measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics);
 176  
 
 177  1
         assertEquals("empty metrics", 0, measurement.getValue().intValue());
 178  
         
 179  1
         metrics.addSubMetrics(new Metrics("foo"));
 180  1
         metrics.addSubMetrics(new Metrics("bar"));
 181  1
         metrics.addSubMetrics(new Metrics("baz"));
 182  
 
 183  1
         assertEquals("empty metrics", 0, measurement.getValue().intValue());
 184  1
     }
 185  
 
 186  
     public void testAccept() throws Exception {
 187  1
         measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics);
 188  
         
 189  1
         visited = null;
 190  1
         measurement.accept(this);
 191  1
         assertSame(measurement, visited);
 192  1
     }
 193  
 
 194  
     public void testEmpty() throws Exception {
 195  1
         measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics);
 196  
 
 197  1
         assertTrue("Before AddSubMetrics()", measurement.isEmpty());
 198  
         
 199  1
         metrics.addSubMetrics(new Metrics("foo"));
 200  
 
 201  1
         assertFalse("After AddSubMetrics()", measurement.isEmpty());
 202  1
     }
 203  
     
 204  
     public void visitStatisticalMeasurement(StatisticalMeasurement measurement) {
 205  
         // Do nothing
 206  0
     }
 207  
     
 208  
     public void visitRatioMeasurement(RatioMeasurement measurement) {
 209  
         // Do nothing
 210  0
     }
 211  
     
 212  
     public void visitNbSubMetricsMeasurement(NbSubMetricsMeasurement measurement) {
 213  1
         visited = measurement;
 214  1
     }
 215  
     
 216  
     public void visitCounterMeasurement(CounterMeasurement measurement) {
 217  
         // Do nothing
 218  0
     }
 219  
     
 220  
     public void visitContextAccumulatorMeasurement(ContextAccumulatorMeasurement measurement) {
 221  
         // Do nothing
 222  0
     }
 223  
     
 224  
     public void visitNameListMeasurement(NameListMeasurement measurement) {
 225  
         // Do nothing
 226  0
     }
 227  
     
 228  
     public void visitSubMetricsAccumulatorMeasurement(SubMetricsAccumulatorMeasurement measurement) {
 229  
         // Do nothing
 230  0
     }
 231  
     
 232  
     public void visitSumMeasurement(SumMeasurement measurement) {
 233  
         // Do nothing
 234  0
     }
 235  
 }