1 32 33 package com.jeantessier.metrics; 34 35 import junit.framework.*; 36 37 public class TestContextAccumulatorMeasurement extends TestCase implements MeasurementVisitor { 38 private MeasurementDescriptor descriptor; 39 private AccumulatorMeasurement measurement; 40 private Metrics metrics; 41 private Measurement visited; 42 43 private MeasurementDescriptor nameList; 44 private MeasurementDescriptor numberList; 45 private MeasurementDescriptor counter; 46 47 private Metrics m1; 48 private Metrics m2; 49 private Metrics m3; 50 51 protected void setUp() throws Exception { 52 m1 = new Metrics("m1"); 53 m2 = new Metrics("m2"); 54 m3 = new Metrics("m3"); 55 56 nameList = new MeasurementDescriptor(); 57 nameList.setShortName("NL"); 58 nameList.setLongName("name list"); 59 nameList.setClassFor(NameListMeasurement.class); 60 61 numberList = new MeasurementDescriptor(); 62 numberList.setShortName("NbL"); 63 numberList.setLongName("number list"); 64 numberList.setClassFor(NameListMeasurement.class); 65 66 counter = new MeasurementDescriptor(); 67 counter.setShortName("NL"); 68 counter.setLongName("counter"); 69 counter.setClassFor(CounterMeasurement.class); 70 71 m1.track(nameList.createMeasurement(m1)); 72 m1.addToMeasurement("NL", "abc"); 73 m1.addToMeasurement("NL", "def"); 74 m1.addToMeasurement("NL", "ghi"); 75 76 m1.track(numberList.createMeasurement(m1)); 77 m1.addToMeasurement("NbL", "123"); 78 m1.addToMeasurement("NbL", "456"); 79 m1.addToMeasurement("NbL", "789"); 80 81 m2.track(nameList.createMeasurement(m2)); 82 m2.addToMeasurement("NL", "jkl"); 83 m2.addToMeasurement("NL", "abc"); 84 85 m2.track(numberList.createMeasurement(m2)); 86 m2.addToMeasurement("NbL", "159"); 87 m2.addToMeasurement("NbL", "248"); 88 89 m3.track(counter.createMeasurement(m3)); 90 m3.addToMeasurement("NL", 1); 91 92 metrics = new Metrics("metrics"); 93 94 descriptor = new MeasurementDescriptor(); 95 descriptor.setShortName("foo"); 96 descriptor.setLongName("bar"); 97 descriptor.setClassFor(ContextAccumulatorMeasurement.class); 98 descriptor.setCached(false); 99 } 100 101 public void testCreateFromMeasurementDescriptor() throws Exception { 102 measurement = (AccumulatorMeasurement) descriptor.createMeasurement(metrics); 103 104 assertNotNull(measurement); 105 assertEquals(descriptor, measurement.getDescriptor()); 106 assertSame(descriptor, measurement.getDescriptor()); 107 assertEquals(ContextAccumulatorMeasurement.class, measurement.getClass()); 108 assertEquals("foo", measurement.getShortName()); 109 assertEquals("bar", measurement.getLongName()); 110 } 111 112 public void testNullInit() throws Exception { 113 measurement = (AccumulatorMeasurement) descriptor.createMeasurement(metrics); 114 assertEquals(0, measurement.intValue()); 115 assertTrue(measurement.getValues().isEmpty()); 116 117 metrics.addSubMetrics(m1); 118 metrics.addSubMetrics(m2); 119 metrics.addSubMetrics(m3); 120 121 assertEquals(0, measurement.intValue()); 122 assertTrue(measurement.getValues().isEmpty()); 123 } 124 125 public void testEmptyInit() throws Exception { 126 descriptor.setInitText(""); 127 128 measurement = (AccumulatorMeasurement) descriptor.createMeasurement(metrics); 129 assertEquals(0, measurement.intValue()); 130 assertTrue(measurement.getValues().isEmpty()); 131 132 metrics.addSubMetrics(m1); 133 metrics.addSubMetrics(m2); 134 metrics.addSubMetrics(m3); 135 136 assertEquals(0, measurement.intValue()); 137 assertTrue(measurement.getValues().isEmpty()); 138 } 139 140 public void testRawValues() throws Exception { 141 descriptor.setInitText("NL"); 142 143 measurement = (AccumulatorMeasurement) descriptor.createMeasurement(metrics); 144 assertEquals(0, measurement.intValue()); 145 assertTrue(measurement.getValues().isEmpty()); 146 147 metrics.track(nameList.createMeasurement(metrics)); 148 metrics.addToMeasurement("NL", "foo"); 149 metrics.addToMeasurement("NL", "bar"); 150 151 metrics.addSubMetrics(m1); 152 metrics.addSubMetrics(m2); 153 metrics.addSubMetrics(m3); 154 155 assertEquals(2, measurement.intValue()); 156 assertTrue("\"foo\" not in " + measurement.getValues(), measurement.getValues().contains("foo")); 157 assertTrue("\"bar\" not in " + measurement.getValues(), measurement.getValues().contains("bar")); 158 } 159 160 public void testCachedValues() throws Exception { 161 descriptor.setInitText("NL"); 162 descriptor.setCached(true); 163 164 measurement = (AccumulatorMeasurement) descriptor.createMeasurement(metrics); 165 assertEquals(0, measurement.intValue()); 166 assertTrue(measurement.getValues().isEmpty()); 167 168 metrics.track(nameList.createMeasurement(metrics)); 169 metrics.addToMeasurement("NL", "foo"); 170 metrics.addToMeasurement("NL", "bar"); 171 172 metrics.addSubMetrics(m1); 173 metrics.addSubMetrics(m2); 174 metrics.addSubMetrics(m3); 175 176 assertEquals(0, measurement.intValue()); 177 assertTrue(measurement.getValues().isEmpty()); 178 } 179 180 public void testSingleFiltered() throws Exception { 181 descriptor.setInitText("NL /a/"); 182 183 measurement = (AccumulatorMeasurement) descriptor.createMeasurement(metrics); 184 assertEquals(0, measurement.intValue()); 185 assertTrue(measurement.getValues().isEmpty()); 186 187 metrics.track(nameList.createMeasurement(metrics)); 188 metrics.addToMeasurement("NL", "foo"); 189 metrics.addToMeasurement("NL", "bar"); 190 191 metrics.addSubMetrics(m1); 192 metrics.addSubMetrics(m2); 193 metrics.addSubMetrics(m3); 194 195 assertEquals(1, measurement.intValue()); 196 assertTrue("\"bar\" not in " + measurement.getValues(), measurement.getValues().contains("bar")); 197 } 198 199 public void testMultiFilterFiltered() throws Exception { 200 descriptor.setInitText("NL /a/\nNL /o/"); 201 202 measurement = (AccumulatorMeasurement) descriptor.createMeasurement(metrics); 203 assertEquals(0, measurement.intValue()); 204 assertTrue(measurement.getValues().isEmpty()); 205 206 metrics.track(nameList.createMeasurement(metrics)); 207 metrics.addToMeasurement("NL", "foo"); 208 metrics.addToMeasurement("NL", "bar"); 209 210 metrics.addSubMetrics(m1); 211 metrics.addSubMetrics(m2); 212 metrics.addSubMetrics(m3); 213 214 assertEquals(2, measurement.intValue()); 215 assertTrue("\"foo\" not in " + measurement.getValues(), measurement.getValues().contains("foo")); 216 assertTrue("\"bar\" not in " + measurement.getValues(), measurement.getValues().contains("bar")); 217 } 218 219 public void testModifiedValues() throws Exception { 220 descriptor.setInitText("NL /(a)/"); 221 222 measurement = (AccumulatorMeasurement) descriptor.createMeasurement(metrics); 223 assertEquals(0, measurement.intValue()); 224 assertTrue(measurement.getValues().isEmpty()); 225 226 metrics.track(nameList.createMeasurement(metrics)); 227 metrics.addToMeasurement("NL", "foo"); 228 metrics.addToMeasurement("NL", "bar"); 229 230 metrics.addSubMetrics(m1); 231 metrics.addSubMetrics(m2); 232 metrics.addSubMetrics(m3); 233 234 assertEquals(1, measurement.intValue()); 235 assertTrue("\"a\" not in " + measurement.getValues(), measurement.getValues().contains("a")); 236 } 237 238 public void testMultiMeasurements() throws Exception { 239 descriptor.setInitText("NL /a/\nNbL /2/"); 240 241 measurement = (AccumulatorMeasurement) descriptor.createMeasurement(metrics); 242 assertEquals(0, measurement.intValue()); 243 assertTrue(measurement.getValues().isEmpty()); 244 245 metrics.track(nameList.createMeasurement(metrics)); 246 metrics.addToMeasurement("NL", "foo"); 247 metrics.addToMeasurement("NL", "bar"); 248 249 metrics.track(numberList.createMeasurement(metrics)); 250 metrics.addToMeasurement("NbL", "1234"); 251 metrics.addToMeasurement("NbL", "5678"); 252 253 metrics.addSubMetrics(m1); 254 metrics.addSubMetrics(m2); 255 metrics.addSubMetrics(m3); 256 257 assertEquals(2, measurement.intValue()); 258 assertTrue("\"bar\" not in " + measurement.getValues(), measurement.getValues().contains("bar")); 259 assertTrue("\"1234\" not in " + measurement.getValues(), measurement.getValues().contains("1234")); 260 } 261 262 public void testAccept() throws Exception { 263 measurement = (AccumulatorMeasurement) descriptor.createMeasurement(metrics); 264 265 visited = null; 266 measurement.accept(this); 267 assertSame(measurement, visited); 268 } 269 270 public void testEmpty() throws Exception { 271 descriptor.setInitText("NL"); 272 273 measurement = (AccumulatorMeasurement) descriptor.createMeasurement(metrics); 274 metrics.track(nameList.createMeasurement(metrics)); 275 276 assertTrue("Before Add()", measurement.isEmpty()); 277 278 metrics.addToMeasurement("NL", "foo"); 279 280 assertFalse("After Add()", measurement.isEmpty()); 281 } 282 283 public void visitStatisticalMeasurement(StatisticalMeasurement measurement) { 284 } 286 287 public void visitRatioMeasurement(RatioMeasurement measurement) { 288 } 290 291 public void visitNbSubMetricsMeasurement(NbSubMetricsMeasurement measurement) { 292 } 294 295 public void visitCounterMeasurement(CounterMeasurement measurement) { 296 } 298 299 public void visitContextAccumulatorMeasurement(ContextAccumulatorMeasurement measurement) { 300 visited = measurement; 301 } 302 303 public void visitNameListMeasurement(NameListMeasurement measurement) { 304 } 306 307 public void visitSubMetricsAccumulatorMeasurement(SubMetricsAccumulatorMeasurement measurement) { 308 } 310 311 public void visitSumMeasurement(SumMeasurement measurement) { 312 } 314 } 315 | Popular Tags |