1 32 33 package com.jeantessier.metrics; 34 35 import junit.framework.*; 36 37 public class TestNameListMeasurement extends TestCase implements MeasurementVisitor { 38 private NameListMeasurement measurement; 39 private Measurement visited; 40 41 protected void setUp() { 42 measurement = new NameListMeasurement(null, null, null); 43 } 44 45 public void testMeasurementDescriptor() throws Exception { 46 MeasurementDescriptor descriptor = new MeasurementDescriptor(); 47 descriptor.setShortName("foo"); 48 descriptor.setLongName("bar"); 49 descriptor.setClassFor(NameListMeasurement.class); 50 51 measurement = (NameListMeasurement) descriptor.createMeasurement(); 52 53 assertNotNull(measurement.getDescriptor()); 54 assertEquals(NameListMeasurement.class, measurement.getDescriptor().getClassFor()); 55 assertEquals("foo", measurement.getShortName()); 56 assertEquals("bar", measurement.getLongName()); 57 } 58 59 public void testCreateFromMeasurementDescriptor() throws Exception { 60 MeasurementDescriptor descriptor = new MeasurementDescriptor(); 61 descriptor.setShortName("foo"); 62 descriptor.setLongName("bar"); 63 descriptor.setClassFor(NameListMeasurement.class); 64 65 measurement = (NameListMeasurement) descriptor.createMeasurement(); 66 67 assertNotNull(measurement); 68 assertEquals(descriptor, measurement.getDescriptor()); 69 assertSame(descriptor, measurement.getDescriptor()); 70 assertEquals(NameListMeasurement.class, measurement.getClass()); 71 assertEquals("foo", measurement.getShortName()); 72 assertEquals("bar", measurement.getLongName()); 73 } 74 75 public void testCreateSet() { 76 measurement = new NameListMeasurement(null, null, "SET"); 77 78 measurement.add("abc"); 79 measurement.add("abc"); 80 81 assertEquals(1, measurement.intValue()); 82 } 83 84 public void testCreateList() { 85 measurement = new NameListMeasurement(null, null, "LIST"); 86 87 measurement.add("abc"); 88 measurement.add("abc"); 89 90 assertEquals(2, measurement.intValue()); 91 } 92 93 public void testCreateDefault() { 94 measurement.add("abc"); 95 measurement.add("abc"); 96 97 assertEquals(1, measurement.intValue()); 98 } 99 100 public void testAddObject() { 101 Object o1 = new Object (); 102 Object o2 = new Object (); 103 104 assertEquals("zero", 0, measurement.intValue()); 105 assertEquals("zero", 0.0, measurement.doubleValue(), 0.01); 106 assertEquals("zero", 0, measurement.getValue().intValue()); 107 108 measurement.add(o1); 109 assertEquals("one", 1, measurement.intValue()); 110 assertEquals("one", 1.0, measurement.doubleValue(), 0.01); 111 assertEquals("one", 1, measurement.getValue().intValue()); 112 113 measurement.add(o2); 114 assertEquals("two", 2, measurement.intValue()); 115 assertEquals("two", 2.0, measurement.doubleValue(), 0.01); 116 assertEquals("two", 2, measurement.getValue().intValue()); 117 118 measurement.add(o1); 119 assertEquals("three", 2, measurement.intValue()); 120 assertEquals("three", 2.0, measurement.doubleValue(), 0.01); 121 assertEquals("three", 2, measurement.getValue().intValue()); 122 } 123 124 public void testValues() { 125 Object o1 = new Object (); 126 Object o2 = new Object (); 127 128 measurement.add(o1); 129 measurement.add(o2); 130 131 assertEquals("size", 2, measurement.getValues().size()); 132 assertTrue("Missing o1", measurement.getValues().contains(o1)); 133 assertTrue("Missing o2", measurement.getValues().contains(o2)); 134 135 try { 136 measurement.getValues().add(o2); 137 fail("Was allowed to modify the Values() collection"); 138 } catch (UnsupportedOperationException ex) { 139 } 141 } 142 143 public void testAddInt() { 144 assertEquals("zero", 0, measurement.intValue()); 145 assertEquals("zero", 0.0, measurement.doubleValue(), 0.01); 146 assertEquals("zero", 0, measurement.getValue().intValue()); 147 148 measurement.add(1); 149 assertEquals("one", 0, measurement.intValue()); 150 assertEquals("one", 0.0, measurement.doubleValue(), 0.01); 151 assertEquals("one", 0, measurement.getValue().intValue()); 152 153 measurement.add(1); 154 assertEquals("two", 0, measurement.intValue()); 155 assertEquals("two", 0.0, measurement.doubleValue(), 0.01); 156 assertEquals("two", 0, measurement.getValue().intValue()); 157 } 158 159 public void testAddFloat() { 160 assertEquals("zero", 0, measurement.intValue()); 161 assertEquals("zero", 0.0, measurement.doubleValue(), 0.01); 162 assertEquals("zero", 0, measurement.getValue().intValue()); 163 164 measurement.add(1.0); 165 assertEquals("one", 0, measurement.intValue()); 166 assertEquals("one", 0.0, measurement.doubleValue(), 0.01); 167 assertEquals("one", 0, measurement.getValue().intValue()); 168 169 measurement.add(1.0); 170 assertEquals("two", 0, measurement.intValue()); 171 assertEquals("two", 0.0, measurement.doubleValue(), 0.01); 172 assertEquals("two", 0, measurement.getValue().intValue()); 173 } 174 175 public void testInUndefinedRange() { 176 assertTrue(measurement.isInRange()); 177 178 measurement.add(new Object ()); 179 180 assertTrue(measurement.isInRange()); 181 182 measurement.add(new Object ()); 183 measurement.add(new Object ()); 184 185 assertTrue(measurement.isInRange()); 186 } 187 188 public void testInOpenRange() throws Exception { 189 MeasurementDescriptor descriptor = new MeasurementDescriptor(); 190 descriptor.setShortName("foo"); 191 descriptor.setLongName("bar"); 192 descriptor.setClassFor(NameListMeasurement.class); 193 194 measurement = (NameListMeasurement) descriptor.createMeasurement(); 195 196 assertTrue(measurement.isInRange()); 197 198 measurement.add(new Object ()); 199 200 assertTrue(measurement.isInRange()); 201 202 measurement.add(new Object ()); 203 measurement.add(new Object ()); 204 205 assertTrue(measurement.isInRange()); 206 } 207 208 public void testInLowerBoundRange() throws Exception { 209 MeasurementDescriptor descriptor = new MeasurementDescriptor(); 210 descriptor.setShortName("foo"); 211 descriptor.setLongName("bar"); 212 descriptor.setClassFor(NameListMeasurement.class); 213 descriptor.setLowerThreshold(new Integer (1)); 214 215 measurement = (NameListMeasurement) descriptor.createMeasurement(); 216 217 assertTrue(!measurement.isInRange()); 218 219 measurement.add(new Object ()); 220 221 assertTrue(measurement.isInRange()); 222 223 measurement.add(new Object ()); 224 measurement.add(new Object ()); 225 226 assertTrue(measurement.isInRange()); 227 } 228 229 public void testInUpperBoundRange() throws Exception { 230 MeasurementDescriptor descriptor = new MeasurementDescriptor(); 231 descriptor.setShortName("foo"); 232 descriptor.setLongName("bar"); 233 descriptor.setClassFor(NameListMeasurement.class); 234 descriptor.setUpperThreshold(new Float (1.5)); 235 236 measurement = (NameListMeasurement) descriptor.createMeasurement(); 237 238 assertTrue(measurement.isInRange()); 239 240 measurement.add(new Object ()); 241 242 assertTrue(measurement.isInRange()); 243 244 measurement.add(new Object ()); 245 measurement.add(new Object ()); 246 247 assertTrue(!measurement.isInRange()); 248 } 249 250 public void testInBoundRange() throws Exception { 251 MeasurementDescriptor descriptor = new MeasurementDescriptor(); 252 descriptor.setShortName("foo"); 253 descriptor.setLongName("bar"); 254 descriptor.setClassFor(NameListMeasurement.class); 255 descriptor.setLowerThreshold(new Integer (1)); 256 descriptor.setUpperThreshold(new Float (1.5)); 257 258 measurement = (NameListMeasurement) descriptor.createMeasurement(); 259 260 assertTrue(!measurement.isInRange()); 261 262 measurement.add(new Object ()); 263 264 assertTrue(measurement.isInRange()); 265 266 measurement.add(new Object ()); 267 measurement.add(new Object ()); 268 269 assertTrue(!measurement.isInRange()); 270 } 271 272 public void testAccept() { 273 visited = null; 274 measurement.accept(this); 275 assertSame(measurement, visited); 276 } 277 278 public void testEmpty() throws Exception { 279 MeasurementDescriptor descriptor = new MeasurementDescriptor(); 280 descriptor.setShortName("foo"); 281 descriptor.setLongName("bar"); 282 descriptor.setClassFor(NameListMeasurement.class); 283 284 measurement = (NameListMeasurement) descriptor.createMeasurement(); 285 286 assertTrue("Before Add()", measurement.isEmpty()); 287 288 measurement.add("foo"); 289 290 assertFalse("After Add()", measurement.isEmpty()); 291 } 292 293 public void visitStatisticalMeasurement(StatisticalMeasurement measurement) { 294 } 296 297 public void visitRatioMeasurement(RatioMeasurement measurement) { 298 } 300 301 public void visitNbSubMetricsMeasurement(NbSubMetricsMeasurement measurement) { 302 } 304 305 public void visitCounterMeasurement(CounterMeasurement measurement) { 306 } 308 309 public void visitContextAccumulatorMeasurement(ContextAccumulatorMeasurement measurement) { 310 } 312 313 public void visitNameListMeasurement(NameListMeasurement measurement) { 314 visited = measurement; 315 } 316 317 public void visitSubMetricsAccumulatorMeasurement(SubMetricsAccumulatorMeasurement measurement) { 318 } 320 321 public void visitSumMeasurement(SumMeasurement measurement) { 322 } 324 } 325 | Popular Tags |