1 32 33 package com.jeantessier.metrics; 34 35 import junit.framework.*; 36 37 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 metrics = new Metrics("foo"); 45 46 descriptor = new MeasurementDescriptor(); 47 descriptor.setShortName("foo"); 48 descriptor.setLongName("bar"); 49 descriptor.setClassFor(NbSubMetricsMeasurement.class); 50 descriptor.setCached(false); 51 } 52 53 public void testCreateFromMeasurementDescriptor() throws Exception { 54 measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(); 55 56 assertNotNull(measurement); 57 assertEquals(descriptor, measurement.getDescriptor()); 58 assertSame(descriptor, measurement.getDescriptor()); 59 assertEquals(NbSubMetricsMeasurement.class, measurement.getClass()); 60 assertEquals("foo", measurement.getShortName()); 61 assertEquals("bar", measurement.getLongName()); 62 } 63 64 public void testAddSubMetrics() throws Exception { 65 measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics); 66 67 assertEquals(0, measurement.intValue()); 68 assertEquals(0.0, measurement.doubleValue(), 0.01); 69 assertEquals(0, measurement.getValue().intValue()); 70 71 metrics.addSubMetrics(new Metrics("bar")); 72 73 assertEquals(1, measurement.intValue()); 74 assertEquals(1.0, measurement.doubleValue(), 0.01); 75 assertEquals(1, measurement.getValue().intValue()); 76 77 metrics.addSubMetrics(new Metrics("bar")); 78 79 assertEquals(1, measurement.intValue()); 80 assertEquals(1.0, measurement.doubleValue(), 0.01); 81 assertEquals(1, measurement.getValue().intValue()); 82 83 metrics.addSubMetrics(new Metrics("baz")); 84 85 assertEquals(2, measurement.intValue()); 86 assertEquals(2.0, measurement.doubleValue(), 0.01); 87 assertEquals(2, measurement.getValue().intValue()); 88 } 89 90 public void testInUndefinedRange() throws Exception { 91 measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics); 92 93 assertTrue(measurement.isInRange()); 94 95 metrics.addSubMetrics(new Metrics("foo")); 96 97 assertTrue(measurement.isInRange()); 98 99 metrics.addSubMetrics(new Metrics("bar")); 100 metrics.addSubMetrics(new Metrics("baz")); 101 102 assertTrue(measurement.isInRange()); 103 } 104 105 public void testInOpenRange() throws Exception { 106 measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics); 107 108 assertTrue(measurement.isInRange()); 109 110 metrics.addSubMetrics(new Metrics("foo")); 111 112 assertTrue(measurement.isInRange()); 113 114 metrics.addSubMetrics(new Metrics("bar")); 115 metrics.addSubMetrics(new Metrics("baz")); 116 117 assertTrue(measurement.isInRange()); 118 } 119 120 public void testInLowerBoundRange() throws Exception { 121 descriptor.setLowerThreshold(new Integer (1)); 122 123 measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics); 124 125 assertTrue(!measurement.isInRange()); 126 127 metrics.addSubMetrics(new Metrics("foo")); 128 129 assertTrue(measurement.isInRange()); 130 131 metrics.addSubMetrics(new Metrics("bar")); 132 metrics.addSubMetrics(new Metrics("baz")); 133 134 assertTrue(measurement.isInRange()); 135 } 136 137 public void testInUpperBoundRange() throws Exception { 138 descriptor.setUpperThreshold(new Float (1.5)); 139 140 measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics); 141 142 assertTrue(measurement.isInRange()); 143 144 metrics.addSubMetrics(new Metrics("foo")); 145 146 assertTrue(measurement.isInRange()); 147 148 metrics.addSubMetrics(new Metrics("bar")); 149 metrics.addSubMetrics(new Metrics("baz")); 150 151 assertTrue(!measurement.isInRange()); 152 } 153 154 public void testInBoundRange() throws Exception { 155 descriptor.setLowerThreshold(new Integer (1)); 156 descriptor.setUpperThreshold(new Float (1.5)); 157 158 measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics); 159 160 assertTrue(!measurement.isInRange()); 161 162 metrics.addSubMetrics(new Metrics("foo")); 163 164 assertTrue(measurement.isInRange()); 165 166 metrics.addSubMetrics(new Metrics("bar")); 167 metrics.addSubMetrics(new Metrics("baz")); 168 169 assertTrue(!measurement.isInRange()); 170 } 171 172 public void testCachedValue() throws Exception { 173 descriptor.setCached(true); 174 175 measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics); 176 177 assertEquals("empty metrics", 0, measurement.intValue()); 178 179 metrics.addSubMetrics(new Metrics("foo")); 180 metrics.addSubMetrics(new Metrics("bar")); 181 metrics.addSubMetrics(new Metrics("baz")); 182 183 assertEquals("empty metrics", 0, measurement.intValue()); 184 } 185 186 public void testAccept() throws Exception { 187 measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics); 188 189 visited = null; 190 measurement.accept(this); 191 assertSame(measurement, visited); 192 } 193 194 public void testEmpty() throws Exception { 195 measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics); 196 197 assertTrue("Before AddSubMetrics()", measurement.isEmpty()); 198 199 metrics.addSubMetrics(new Metrics("foo")); 200 201 assertFalse("After AddSubMetrics()", measurement.isEmpty()); 202 } 203 204 public void visitStatisticalMeasurement(StatisticalMeasurement measurement) { 205 } 207 208 public void visitRatioMeasurement(RatioMeasurement measurement) { 209 } 211 212 public void visitNbSubMetricsMeasurement(NbSubMetricsMeasurement measurement) { 213 visited = measurement; 214 } 215 216 public void visitCounterMeasurement(CounterMeasurement measurement) { 217 } 219 220 public void visitContextAccumulatorMeasurement(ContextAccumulatorMeasurement measurement) { 221 } 223 224 public void visitNameListMeasurement(NameListMeasurement measurement) { 225 } 227 228 public void visitSubMetricsAccumulatorMeasurement(SubMetricsAccumulatorMeasurement measurement) { 229 } 231 232 public void visitSumMeasurement(SumMeasurement measurement) { 233 } 235 } 236 | Popular Tags |