1 32 33 package com.jeantessier.metrics; 34 35 import junit.framework.*; 36 37 public class TestSumMeasurement extends TestCase implements MeasurementVisitor { 38 private MeasurementDescriptor descriptor; 39 private Metrics metrics; 40 private SumMeasurement measurement; 41 private Measurement visited; 42 43 protected void setUp() { 44 descriptor = new MeasurementDescriptor(); 45 descriptor.setShortName("foo"); 46 descriptor.setLongName("FOO"); 47 descriptor.setClassFor(SumMeasurement.class); 48 descriptor.setCached(false); 49 50 metrics = new Metrics("foobar"); 51 } 52 53 public void testMeasurementDescriptor() throws Exception { 54 measurement = (SumMeasurement) descriptor.createMeasurement(); 55 56 assertNotNull(measurement.getDescriptor()); 57 assertEquals(SumMeasurement.class, measurement.getDescriptor().getClassFor()); 58 assertEquals("foo", measurement.getShortName()); 59 assertEquals("FOO", measurement.getLongName()); 60 } 61 62 public void testCreateFromMeasurementDescriptor() throws Exception { 63 measurement = (SumMeasurement) descriptor.createMeasurement(); 64 65 assertNotNull(measurement); 66 assertEquals(descriptor, measurement.getDescriptor()); 67 assertSame(descriptor, measurement.getDescriptor()); 68 assertEquals(SumMeasurement.class, measurement.getClass()); 69 assertEquals("foo", measurement.getShortName()); 70 assertEquals("FOO", measurement.getLongName()); 71 } 72 73 public void testCreateDefault() { 74 measurement = new SumMeasurement(descriptor, null, null); 75 76 assertEquals(0, measurement.doubleValue(), 0.01); 77 } 78 79 public void testEmptyInitText() throws Exception { 80 descriptor.setInitText(""); 81 82 measurement = (SumMeasurement) descriptor.createMeasurement(); 83 84 assertEquals(0, measurement.doubleValue(), 0.01); 85 } 86 87 public void testEmptyLineInitText() throws Exception { 88 descriptor.setInitText("\n"); 89 90 measurement = (SumMeasurement) descriptor.createMeasurement(); 91 92 assertEquals(0, measurement.doubleValue(), 0.01); 93 } 94 95 public void testDashInitText() throws Exception { 96 descriptor.setInitText("-"); 97 98 measurement = (SumMeasurement) descriptor.createMeasurement(); 99 100 assertEquals(0, measurement.doubleValue(), 0.01); 101 } 102 103 public void testConstant() throws Exception { 104 descriptor.setInitText("2"); 105 106 measurement = (SumMeasurement) descriptor.createMeasurement(); 107 108 assertEquals(2, measurement.doubleValue(), 0.01); 109 } 110 111 public void testConstantAndEmptyLine() throws Exception { 112 descriptor.setInitText("\n2\n"); 113 114 measurement = (SumMeasurement) descriptor.createMeasurement(); 115 116 assertEquals(2, measurement.doubleValue(), 0.01); 117 } 118 119 public void testAddition() throws Exception { 120 descriptor.setInitText("1\n1"); 121 122 measurement = (SumMeasurement) descriptor.createMeasurement(); 123 124 assertEquals(2, measurement.doubleValue(), 0.01); 125 } 126 127 public void testNegative() throws Exception { 128 descriptor.setInitText("-2"); 129 130 measurement = (SumMeasurement) descriptor.createMeasurement(); 131 132 assertEquals(-2, measurement.doubleValue(), 0.01); 133 } 134 135 public void testSubstraction() throws Exception { 136 descriptor.setInitText("2\n-1"); 137 138 measurement = (SumMeasurement) descriptor.createMeasurement(); 139 140 assertEquals(1, measurement.doubleValue(), 0.01); 141 142 descriptor.setInitText("1\n-2"); 143 144 measurement = (SumMeasurement) descriptor.createMeasurement(); 145 146 assertEquals(-1, measurement.doubleValue(), 0.01); 147 } 148 149 public void testSubMeasurement() throws Exception { 150 descriptor.setInitText("bar"); 151 152 metrics.track("bar", new CounterMeasurement(null, metrics, "2")); 153 154 measurement = (SumMeasurement) descriptor.createMeasurement(metrics); 155 156 assertEquals(2, measurement.doubleValue(), 0.01); 157 } 158 159 public void testStatisticalMeasurement() throws Exception { 160 descriptor.setInitText("bar DISPOSE_SUM"); 161 162 metrics.track("bar", new StatisticalMeasurement(null, metrics, "bar")); 163 164 Metrics submetrics = new Metrics("submetrics"); 165 submetrics.track("bar", new CounterMeasurement(null, submetrics, "2")); 166 metrics.addSubMetrics(submetrics); 167 168 measurement = (SumMeasurement) descriptor.createMeasurement(metrics); 169 170 assertEquals(2, measurement.doubleValue(), 0.01); 171 } 172 173 public void testAddMeasurements() throws Exception { 174 descriptor.setInitText("bar\nbaz"); 175 176 metrics.track("bar", new CounterMeasurement(null, metrics, "1")); 177 metrics.track("baz", new CounterMeasurement(null, metrics, "1")); 178 179 measurement = (SumMeasurement) descriptor.createMeasurement(metrics); 180 181 assertEquals(2, measurement.doubleValue(), 0.01); 182 } 183 184 public void testSubstractMeasurements() throws Exception { 185 descriptor.setInitText("bar\n-baz"); 186 187 metrics.track("bar", new CounterMeasurement(null, metrics, "1")); 188 metrics.track("baz", new CounterMeasurement(null, metrics, "2")); 189 190 measurement = (SumMeasurement) descriptor.createMeasurement(metrics); 191 192 assertEquals(-1, measurement.doubleValue(), 0.01); 193 } 194 195 public void testInUndefinedRange() throws Exception { 196 descriptor.setInitText("bar"); 197 198 metrics.track("bar", new CounterMeasurement(null, null, null)); 199 200 measurement = (SumMeasurement) descriptor.createMeasurement(metrics); 201 202 assertTrue(measurement.isInRange()); 203 204 metrics.addToMeasurement("bar", 1); 205 206 assertTrue(measurement.isInRange()); 207 208 metrics.addToMeasurement("bar", 1); 209 210 assertTrue(measurement.isInRange()); 211 } 212 213 public void testInOpenRange() throws Exception { 214 descriptor.setInitText("bar"); 215 216 metrics.track("bar", new CounterMeasurement(null, null, null)); 217 218 measurement = (SumMeasurement) descriptor.createMeasurement(metrics); 219 220 assertTrue(measurement.isInRange()); 221 222 metrics.addToMeasurement("bar", 1); 223 224 assertTrue(measurement.isInRange()); 225 226 metrics.addToMeasurement("bar", 1); 227 228 assertTrue(measurement.isInRange()); 229 } 230 231 public void testInLowerBoundRange() throws Exception { 232 descriptor.setInitText("bar"); 233 descriptor.setLowerThreshold(new Integer (1)); 234 235 metrics.track("bar", new CounterMeasurement(null, null, null)); 236 237 measurement = (SumMeasurement) descriptor.createMeasurement(metrics); 238 239 assertEquals(0, measurement.intValue()); 240 assertTrue(!measurement.isInRange()); 241 242 metrics.addToMeasurement("bar", 1); 243 244 assertEquals(1, measurement.intValue()); 245 assertTrue(measurement.isInRange()); 246 247 metrics.addToMeasurement("bar", 1); 248 249 assertEquals(2, measurement.intValue()); 250 assertTrue(measurement.isInRange()); 251 } 252 253 public void testInUpperBoundRange() throws Exception { 254 descriptor.setInitText("bar"); 255 descriptor.setUpperThreshold(new Float (1.5)); 256 257 metrics.track("bar", new CounterMeasurement(null, null, null)); 258 259 measurement = (SumMeasurement) descriptor.createMeasurement(metrics); 260 261 assertTrue(measurement.isInRange()); 262 263 metrics.addToMeasurement("bar", 1); 264 265 assertTrue(measurement.isInRange()); 266 267 metrics.addToMeasurement("bar", 1); 268 269 assertTrue(!measurement.isInRange()); 270 } 271 272 public void testInBoundRange() throws Exception { 273 descriptor.setInitText("bar"); 274 descriptor.setLowerThreshold(new Integer (1)); 275 descriptor.setUpperThreshold(new Float (1.5)); 276 277 metrics.track("bar", new CounterMeasurement(null, null, null)); 278 279 measurement = (SumMeasurement) descriptor.createMeasurement(metrics); 280 281 assertTrue(!measurement.isInRange()); 282 283 metrics.addToMeasurement("bar", 1); 284 285 assertTrue(measurement.isInRange()); 286 287 metrics.addToMeasurement("bar", 1); 288 289 assertTrue(!measurement.isInRange()); 290 } 291 292 public void testCachedValue() throws Exception { 293 descriptor.setInitText("bar"); 294 descriptor.setCached(true); 295 296 metrics.track("bar", new CounterMeasurement(null, null, null)); 297 298 measurement = (SumMeasurement) descriptor.createMeasurement(metrics); 299 300 assertEquals(0, measurement.doubleValue(), 0.01); 301 302 metrics.addToMeasurement("bar", 1); 303 304 assertEquals(0, measurement.doubleValue(), 0.01); 305 } 306 307 public void testAccept() { 308 measurement = new SumMeasurement(null, null, null); 309 310 visited = null; 311 measurement.accept(this); 312 assertSame(measurement, visited); 313 } 314 315 public void testEmptyWithOneMeasurement() throws Exception { 316 descriptor.setInitText("bar"); 317 318 metrics.track("bar", new CounterMeasurement(null, null, null)); 319 320 measurement = (SumMeasurement) descriptor.createMeasurement(metrics); 321 322 assertTrue("Before Add()", measurement.isEmpty()); 323 324 metrics.addToMeasurement("bar", 1); 325 326 assertFalse("After Add(1)", measurement.isEmpty()); 327 328 metrics.addToMeasurement("bar", -1); 329 330 assertFalse("After Add(-1)", measurement.isEmpty()); 331 } 332 333 public void testEmptyWithTwoMeasurements() throws Exception { 334 descriptor.setInitText("bar\nbaz"); 335 336 metrics.track("bar", new CounterMeasurement(null, null, null)); 337 metrics.track("baz", new CounterMeasurement(null, null, null)); 338 339 measurement = (SumMeasurement) descriptor.createMeasurement(metrics); 340 341 assertTrue("bar is not empty", metrics.getMeasurement("bar").isEmpty()); 342 assertTrue("baz is not empty", metrics.getMeasurement("baz").isEmpty()); 343 assertTrue("Before Add()", measurement.isEmpty()); 344 345 metrics.addToMeasurement("bar", 1); 346 347 assertFalse("bar is empty", metrics.getMeasurement("bar").isEmpty()); 348 assertTrue("baz is not empty", metrics.getMeasurement("baz").isEmpty()); 349 assertFalse("After Add(1)", measurement.isEmpty()); 350 351 metrics.addToMeasurement("bar", -1); 352 353 assertFalse("bar is empty", metrics.getMeasurement("bar").isEmpty()); 354 assertTrue("baz is not empty", metrics.getMeasurement("baz").isEmpty()); 355 assertFalse("After Add(-1)", measurement.isEmpty()); 356 } 357 358 public void testEmptyWithConstant() throws Exception { 359 descriptor.setInitText("2"); 360 361 measurement = (SumMeasurement) descriptor.createMeasurement(metrics); 362 363 assertTrue("with constants", measurement.isEmpty()); 364 } 365 366 public void visitStatisticalMeasurement(StatisticalMeasurement measurement) { 367 } 369 370 public void visitRatioMeasurement(RatioMeasurement measurement) { 371 } 373 374 public void visitNbSubMetricsMeasurement(NbSubMetricsMeasurement measurement) { 375 } 377 378 public void visitCounterMeasurement(CounterMeasurement measurement) { 379 } 381 382 public void visitContextAccumulatorMeasurement(ContextAccumulatorMeasurement measurement) { 383 } 385 386 public void visitNameListMeasurement(NameListMeasurement measurement) { 387 } 389 390 public void visitSubMetricsAccumulatorMeasurement(SubMetricsAccumulatorMeasurement measurement) { 391 } 393 394 public void visitSumMeasurement(SumMeasurement measurement) { 395 visited = measurement; 396 } 397 } 398 | Popular Tags |