1 32 33 package com.jeantessier.metrics; 34 35 import java.util.*; 36 37 import org.apache.oro.text.perl.*; 38 39 import junit.framework.*; 40 41 public class TestNbSubMetricsMeasurementSelectionCriteria extends TestCase { 42 private Metrics metrics; 43 private MeasurementDescriptor descriptor; 44 45 private Metrics m1; 46 private Metrics m2; 47 private Metrics m3; 48 private Metrics m4; 49 private Metrics m5; 50 private Metrics m6; 51 52 protected void setUp() throws Exception { 53 m1 = new Metrics("m1"); 54 m2 = new Metrics("m2"); 55 m3 = new Metrics("m3"); 56 m4 = new Metrics("m4"); 57 m5 = new Metrics("m5"); 58 m6 = new Metrics("m6"); 59 60 MeasurementDescriptor present = new MeasurementDescriptor(); 61 present.setShortName("P"); 62 present.setLongName("present"); 63 present.setClassFor(CounterMeasurement.class); 64 65 MeasurementDescriptor counter = new MeasurementDescriptor(); 66 counter.setShortName("C"); 67 counter.setLongName("counter"); 68 counter.setClassFor(CounterMeasurement.class); 69 70 m1.track(present.createMeasurement(m1)); 71 72 m2.track(present.createMeasurement(m2)); 73 m2.track(counter.createMeasurement(m2)); 74 m2.addToMeasurement("C", 0); 75 76 m3.track(counter.createMeasurement(m3)); 77 m3.addToMeasurement("C", 1); 78 79 m4.track(counter.createMeasurement(m4)); 80 m4.addToMeasurement("C", 2); 81 82 m5.track(counter.createMeasurement(m5)); 83 m5.addToMeasurement("C", 3); 84 85 m6.track(counter.createMeasurement(m6)); 86 m6.addToMeasurement("C", 4); 87 88 metrics = new Metrics("metrics"); 89 90 metrics.addSubMetrics(m1); 91 metrics.addSubMetrics(m2); 92 metrics.addSubMetrics(m3); 93 metrics.addSubMetrics(m4); 94 metrics.addSubMetrics(m5); 95 metrics.addSubMetrics(m6); 96 97 descriptor = new MeasurementDescriptor(); 98 descriptor.setShortName("Nb"); 99 descriptor.setLongName("Number"); 100 descriptor.setClassFor(NbSubMetricsMeasurement.class); 101 } 102 103 public void testDefault() throws Exception { 104 NbSubMetricsMeasurement measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics); 105 assertEquals("default", 6, measurement.intValue()); 106 } 107 108 public void testPresence() throws Exception { 109 descriptor.setInitText("P"); 110 111 NbSubMetricsMeasurement measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics); 112 assertEquals("presence", 2, measurement.intValue()); 113 } 114 115 public void testLesserThan() throws Exception { 116 descriptor.setInitText("C < 3"); 117 118 NbSubMetricsMeasurement measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics); 119 assertEquals("lesser than", 3, measurement.intValue()); 120 } 121 122 public void testLesserThanOrEqual() throws Exception { 123 descriptor.setInitText("C <= 3"); 124 125 NbSubMetricsMeasurement measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics); 126 assertEquals("lesser than or equal", 4, measurement.intValue()); 127 } 128 129 public void testGreaterThan() throws Exception { 130 descriptor.setInitText("C > 1"); 131 132 NbSubMetricsMeasurement measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics); 133 assertEquals("greater than", 3, measurement.intValue()); 134 } 135 136 public void testGreaterThanOrEqual() throws Exception { 137 descriptor.setInitText("C >= 1"); 138 139 NbSubMetricsMeasurement measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics); 140 assertEquals("greater than or equal", 4, measurement.intValue()); 141 } 142 143 public void testEqual() throws Exception { 144 descriptor.setInitText("C == 1"); 145 146 NbSubMetricsMeasurement measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics); 147 assertEquals("equal", 1, measurement.intValue()); 148 } 149 150 public void testNotEqual() throws Exception { 151 descriptor.setInitText("C != 1"); 152 153 NbSubMetricsMeasurement measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics); 154 assertEquals("not equal", 4, measurement.intValue()); 155 } 156 157 public void testAnd() throws Exception { 158 descriptor.setInitText("1 <= C <= 3"); 159 160 NbSubMetricsMeasurement measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics); 161 assertEquals("and", 3, measurement.intValue()); 162 } 163 164 public void testOr() throws Exception { 165 descriptor.setInitText("C == 1\nC == 2"); 166 167 NbSubMetricsMeasurement measurement = (NbSubMetricsMeasurement) descriptor.createMeasurement(metrics); 168 assertEquals("or", 2, measurement.intValue()); 169 } 170 171 public void testSplit() { 172 String operators = "/(<)|(<=)|(>)|(>=)|(==)|(!=)/"; 173 Perl5Util perl = new org.apache.oro.text.perl.Perl5Util(); 174 175 List list = new ArrayList(); 176 String str; 177 178 list.clear(); 179 str = ""; 180 perl.split(list, operators, str); 181 assertEquals("split(\"" + str + "\") expected [] but was " + list, 0, list.size()); 182 183 list.clear(); 184 str = "P"; 185 perl.split(list, operators, str); 186 assertEquals("split(\"" + str + "\") expected [\"P\"] but was " + list, 1, list.size()); 187 assertEquals("split(\"" + str + "\") expected [\"P\"] but was " + list, str, list.get(0)); 188 189 list.clear(); 190 str = "P > 0"; 191 perl.split(list, operators, str); 192 assertEquals("split(\"" + str + "\") expected [\"P \", \">\", \" 0\"] but was " + list, 3, list.size()); 193 assertEquals("split(\"" + str + "\") expected [\"P \", \">\", \" 0\"] but was " + list, "P ", list.get(0)); 194 assertEquals("split(\"" + str + "\") expected [\"P \", \">\", \" 0\"] but was " + list, ">", list.get(1)); 195 assertEquals("split(\"" + str + "\") expected [\"P \", \">\", \" 0\"] but was " + list, " 0", list.get(2)); 196 197 list.clear(); 198 str = "1 < P < 3"; 199 perl.split(list, operators, str); 200 assertEquals("split(\"" + str + "\") expected [\"1 \", \"<\", \" P \", \"<\", \" 3\"] but was " + list, 5, list.size()); 201 assertEquals("split(\"" + str + "\") expected [\"1 \", \"<\", \" P \", \"<\", \" 3\"] but was " + list, "1 ", list.get(0)); 202 assertEquals("split(\"" + str + "\") expected [\"1 \", \"<\", \" P \", \"<\", \" 3\"] but was " + list, "<", list.get(1)); 203 assertEquals("split(\"" + str + "\") expected [\"1 \", \"<\", \" P \", \"<\", \" 3\"] but was " + list, " P ", list.get(2)); 204 assertEquals("split(\"" + str + "\") expected [\"1 \", \"<\", \" P \", \"<\", \" 3\"] but was " + list, "<", list.get(3)); 205 assertEquals("split(\"" + str + "\") expected [\"1 \", \"<\", \" P \", \"<\", \" 3\"] but was " + list, " 3", list.get(4)); 206 207 list.clear(); 208 str = "1 < P DISPOSE_MEAN < 3"; 209 perl.split(list, operators, str); 210 assertEquals("split(\"" + str + "\") expected [\"1 \", \"<\", \" P DISPOSE_MEAN \", \"<\", \" 3\"] but was " + list, 5, list.size()); 211 assertEquals("split(\"" + str + "\") expected [\"1 \", \"<\", \" P DISPOSE_MEAN \", \"<\", \" 3\"] but was " + list, "1 ", list.get(0)); 212 assertEquals("split(\"" + str + "\") expected [\"1 \", \"<\", \" P DISPOSE_MEAN \", \"<\", \" 3\"] but was " + list, "<", list.get(1)); 213 assertEquals("split(\"" + str + "\") expected [\"1 \", \"<\", \" P DISPOSE_MEAN \", \"<\", \" 3\"] but was " + list, " P DISPOSE_MEAN ", list.get(2)); 214 assertEquals("split(\"" + str + "\") expected [\"1 \", \"<\", \" P DISPOSE_MEAN \", \"<\", \" 3\"] but was " + list, "<", list.get(3)); 215 assertEquals("split(\"" + str + "\") expected [\"1 \", \"<\", \" P DISPOSE_MEAN \", \"<\", \" 3\"] but was " + list, " 3", list.get(4)); 216 } 217 } 218 | Popular Tags |