1 32 33 package com.jeantessier.metrics; 34 35 import junit.framework.*; 36 37 public class TestSubMetricsAccumulatorMeasurement 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(SubMetricsAccumulatorMeasurement.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(SubMetricsAccumulatorMeasurement.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.addSubMetrics(m1); 148 metrics.addSubMetrics(m2); 149 metrics.addSubMetrics(m3); 150 151 assertEquals(4, measurement.intValue()); 152 assertTrue("\"abc\" not in " + measurement.getValues(), measurement.getValues().contains("abc")); 153 assertTrue("\"def\" not in " + measurement.getValues(), measurement.getValues().contains("def")); 154 assertTrue("\"ghi\" not in " + measurement.getValues(), measurement.getValues().contains("ghi")); 155 assertTrue("\"jkl\" not in " + measurement.getValues(), measurement.getValues().contains("jkl")); 156 } 157 158 public void testCachedValues() throws Exception { 159 descriptor.setInitText("NL"); 160 descriptor.setCached(true); 161 162 measurement = (AccumulatorMeasurement) descriptor.createMeasurement(metrics); 163 assertEquals(0, measurement.intValue()); 164 assertTrue(measurement.getValues().isEmpty()); 165 166 metrics.addSubMetrics(m1); 167 metrics.addSubMetrics(m2); 168 metrics.addSubMetrics(m3); 169 170 assertEquals(0, measurement.intValue()); 171 assertTrue(measurement.getValues().isEmpty()); 172 } 173 174 public void testSingleFiltered() throws Exception { 175 descriptor.setInitText("NL /a/"); 176 177 measurement = (AccumulatorMeasurement) descriptor.createMeasurement(metrics); 178 assertEquals(0, measurement.intValue()); 179 assertTrue(measurement.getValues().isEmpty()); 180 181 metrics.addSubMetrics(m1); 182 metrics.addSubMetrics(m2); 183 metrics.addSubMetrics(m3); 184 185 assertEquals(1, measurement.intValue()); 186 assertTrue("\"abc\" not in " + measurement.getValues(), measurement.getValues().contains("abc")); 187 } 188 189 public void testMultiFilterFiltered() throws Exception { 190 descriptor.setInitText("NL /a/\nNL /k/"); 191 192 measurement = (AccumulatorMeasurement) descriptor.createMeasurement(metrics); 193 assertEquals(0, measurement.intValue()); 194 assertTrue(measurement.getValues().isEmpty()); 195 196 metrics.addSubMetrics(m1); 197 metrics.addSubMetrics(m2); 198 metrics.addSubMetrics(m3); 199 200 assertEquals(2, measurement.intValue()); 201 assertTrue("\"abc\" not in " + measurement.getValues(), measurement.getValues().contains("abc")); 202 assertTrue("\"jkl\" not in " + measurement.getValues(), measurement.getValues().contains("jkl")); 203 } 204 205 public void testModifiedValues() throws Exception { 206 descriptor.setInitText("NL /(a)/"); 207 208 measurement = (AccumulatorMeasurement) descriptor.createMeasurement(metrics); 209 assertEquals(0, measurement.intValue()); 210 assertTrue(measurement.getValues().isEmpty()); 211 212 metrics.addSubMetrics(m1); 213 metrics.addSubMetrics(m2); 214 metrics.addSubMetrics(m3); 215 216 assertEquals(1, measurement.intValue()); 217 assertTrue("\"a\" not in " + measurement.getValues(), measurement.getValues().contains("a")); 218 } 219 220 public void testMultiMeasurements() throws Exception { 221 descriptor.setInitText("NL /a/\nNbL /2/"); 222 223 measurement = (AccumulatorMeasurement) descriptor.createMeasurement(metrics); 224 assertEquals(0, measurement.intValue()); 225 assertTrue(measurement.getValues().isEmpty()); 226 227 metrics.addSubMetrics(m1); 228 metrics.addSubMetrics(m2); 229 metrics.addSubMetrics(m3); 230 231 assertEquals(3, measurement.intValue()); 232 assertTrue("\"abc\" not in " + measurement.getValues(), measurement.getValues().contains("abc")); 233 assertTrue("\"123\" not in " + measurement.getValues(), measurement.getValues().contains("123")); 234 assertTrue("\"248\" not in " + measurement.getValues(), measurement.getValues().contains("248")); 235 } 236 237 public void testAccept() throws Exception { 238 measurement = (AccumulatorMeasurement) descriptor.createMeasurement(metrics); 239 240 visited = null; 241 measurement.accept(this); 242 assertSame(measurement, visited); 243 } 244 245 public void testEmpty() throws Exception { 246 descriptor.setInitText("NL"); 247 248 measurement = (AccumulatorMeasurement) descriptor.createMeasurement(metrics); 249 metrics.track(nameList.createMeasurement(metrics)); 250 251 Metrics submetrics = new Metrics("submetrics"); 252 submetrics.track(nameList.createMeasurement(submetrics)); 253 metrics.addSubMetrics(submetrics); 254 255 assertTrue("Before Add()", measurement.isEmpty()); 256 257 submetrics.addToMeasurement("NL", "foo"); 258 259 assertFalse("After Add()", measurement.isEmpty()); 260 } 261 262 public void visitStatisticalMeasurement(StatisticalMeasurement measurement) { 263 } 265 266 public void visitRatioMeasurement(RatioMeasurement measurement) { 267 } 269 270 public void visitNbSubMetricsMeasurement(NbSubMetricsMeasurement measurement) { 271 } 273 274 public void visitCounterMeasurement(CounterMeasurement measurement) { 275 } 277 278 public void visitContextAccumulatorMeasurement(ContextAccumulatorMeasurement measurement) { 279 } 281 282 public void visitNameListMeasurement(NameListMeasurement measurement) { 283 } 285 286 public void visitSubMetricsAccumulatorMeasurement(SubMetricsAccumulatorMeasurement measurement) { 287 visited = measurement; 288 } 289 290 public void visitSumMeasurement(SumMeasurement measurement) { 291 } 293 } 294 | Popular Tags |