1 32 33 package com.jeantessier.metrics; 34 35 import junit.framework.*; 36 37 import java.io.*; 38 39 import org.xml.sax.*; 40 41 public class TestMetricsConfigurationLoader extends TestCase { 42 private MetricsConfiguration configuration; 43 private MetricsConfigurationLoader loader; 44 45 protected void setUp() throws Exception { 46 configuration = new MetricsConfiguration(); 47 loader = new MetricsConfigurationLoader(configuration, Boolean.getBoolean("DEPENDENCYFINDER_TESTS_VALIDATE")); 48 } 49 50 public void testEmptyFile() throws IOException, SAXException { 51 Reader in = new StringReader(""); 52 53 try { 54 loader.load(in); 55 fail("Read empty file"); 56 } catch (SAXParseException ex) { 57 } 59 60 assertEquals("ProjectMeasurements", 0, configuration.getProjectMeasurements().size()); 61 assertEquals("GroupMeasurements", 0, configuration.getGroupMeasurements().size()); 62 assertEquals("ClassMeasurements", 0, configuration.getClassMeasurements().size()); 63 assertEquals("MethodMeasurements", 0, configuration.getMethodMeasurements().size()); 64 } 65 66 public void testEmptyDocument() throws IOException, SAXException { 67 Reader in = new StringReader("<metrics-configuration/>"); 68 69 MetricsConfiguration configuration2 = loader.load(in); 70 assertSame(configuration, configuration2); 71 72 assertEquals("ProjectMeasurements", 0, configuration.getProjectMeasurements().size()); 73 assertEquals("GroupMeasurements", 0, configuration.getGroupMeasurements().size()); 74 assertEquals("ClassMeasurements", 0, configuration.getClassMeasurements().size()); 75 assertEquals("MethodMeasurements", 0, configuration.getMethodMeasurements().size()); 76 } 77 78 public void testNonWellFormedDocument() throws IOException, SAXException { 79 Reader in = new StringReader("<metrics-configuration>"); 80 81 try { 82 loader.load(in); 83 fail("Read non well formed file"); 84 } catch (SAXParseException ex) { 85 } 87 88 assertEquals("ProjectMeasurements", 0, configuration.getProjectMeasurements().size()); 89 assertEquals("GroupMeasurements", 0, configuration.getGroupMeasurements().size()); 90 assertEquals("ClassMeasurements", 0, configuration.getClassMeasurements().size()); 91 assertEquals("MethodMeasurements", 0, configuration.getMethodMeasurements().size()); 92 } 93 94 public void testValidation() throws IOException, SAXException { 95 StringBuffer document = new StringBuffer (); 96 97 document.append("<!DOCTYPE metrics-configuration SYSTEM \"http://depfind.sourceforge.net/dtd/metrics-configuration.dtd\">\n"); 98 document.append("<metrics-configuration>\n"); 99 document.append(" <project-measurements/>\n"); 100 document.append(" <group-measurements/>\n"); 101 document.append(" <class-measurements/>\n"); 102 document.append(" <method-measurements/>\n"); 103 document.append("</metrics-configuration>\n"); 104 105 Reader in = new StringReader(document.toString()); 106 107 MetricsConfiguration configuration2 = loader.load(in); 108 assertSame(configuration, configuration2); 109 110 assertEquals("ProjectMeasurements", 0, configuration.getProjectMeasurements().size()); 111 assertEquals("GroupMeasurements", 0, configuration.getGroupMeasurements().size()); 112 assertEquals("ClassMeasurements", 0, configuration.getClassMeasurements().size()); 113 assertEquals("MethodMeasurements", 0, configuration.getMethodMeasurements().size()); 114 } 115 116 public void testPackageMeasurement() throws IOException, SAXException { 117 StringBuffer document = new StringBuffer (); 118 119 document.append("<!DOCTYPE metrics-configuration SYSTEM \"http://depfind.sourceforge.net/dtd/metrics-configuration.dtd\">\n"); 120 document.append("<metrics-configuration>\n"); 121 document.append(" <project-measurements>\n"); 122 document.append(" <measurement>\n"); 123 document.append(" <short-name>SLOC</short-name>\n"); 124 document.append(" <long-name>Single Lines of Code</long-name>\n"); 125 document.append(" <class>com.jeantessier.metrics.StatisticalMeasurement</class>\n"); 126 document.append(" <init>\n"); 127 document.append(" SLOC\n"); 128 document.append(" DISPOSE_SUM\n"); 129 document.append(" </init>\n"); 130 document.append(" </measurement>\n"); 131 document.append(" </project-measurements>\n"); 132 document.append(" <group-measurements/>\n"); 133 document.append(" <class-measurements/>\n"); 134 document.append(" <method-measurements/>\n"); 135 document.append("</metrics-configuration>\n"); 136 137 Reader in = new StringReader(document.toString()); 138 139 MetricsConfiguration configuration2 = loader.load(in); 140 assertSame(configuration, configuration2); 141 142 assertEquals("ProjectMeasurements", 1, configuration.getProjectMeasurements().size()); 143 assertEquals("GroupMeasurements", 0, configuration.getGroupMeasurements().size()); 144 assertEquals("ClassMeasurements", 0, configuration.getClassMeasurements().size()); 145 assertEquals("MethodMeasurements", 0, configuration.getMethodMeasurements().size()); 146 147 MeasurementDescriptor descriptor = (MeasurementDescriptor) configuration.getProjectMeasurements().get(0); 148 assertEquals("SLOC", descriptor.getShortName()); 149 assertEquals("Single Lines of Code", descriptor.getLongName()); 150 assertEquals(com.jeantessier.metrics.StatisticalMeasurement.class, descriptor.getClassFor()); 151 assertEquals("SLOC\n DISPOSE_SUM", descriptor.getInitText()); 152 assertNull("descriptor.LowerThreshold()", descriptor.getLowerThreshold()); 153 assertNull("descriptor.UpperThreshold()", descriptor.getUpperThreshold()); 154 } 155 156 public void testGroupMeasurement() throws IOException, SAXException { 157 StringBuffer document = new StringBuffer (); 158 159 document.append("<!DOCTYPE metrics-configuration SYSTEM \"http://depfind.sourceforge.net/dtd/metrics-configuration.dtd\">\n"); 160 document.append("<metrics-configuration>\n"); 161 document.append(" <project-measurements/>\n"); 162 document.append(" <group-measurements>\n"); 163 document.append(" <measurement>\n"); 164 document.append(" <short-name>SLOC</short-name>\n"); 165 document.append(" <long-name>Single Lines of Code</long-name>\n"); 166 document.append(" <class>com.jeantessier.metrics.StatisticalMeasurement</class>\n"); 167 document.append(" <init>\n"); 168 document.append(" SLOC\n"); 169 document.append(" DISPOSE_SUM\n"); 170 document.append(" </init>\n"); 171 document.append(" </measurement>\n"); 172 document.append(" </group-measurements>\n"); 173 document.append(" <class-measurements/>\n"); 174 document.append(" <method-measurements/>\n"); 175 document.append("</metrics-configuration>\n"); 176 177 Reader in = new StringReader(document.toString()); 178 179 MetricsConfiguration configuration2 = loader.load(in); 180 assertSame(configuration, configuration2); 181 182 assertEquals("ProjectMeasurements", 0, configuration.getProjectMeasurements().size()); 183 assertEquals("GroupMeasurements", 1, configuration.getGroupMeasurements().size()); 184 assertEquals("ClassMeasurements", 0, configuration.getClassMeasurements().size()); 185 assertEquals("MethodMeasurements", 0, configuration.getMethodMeasurements().size()); 186 187 MeasurementDescriptor descriptor = (MeasurementDescriptor) configuration.getGroupMeasurements().get(0); 188 assertEquals("SLOC", descriptor.getShortName()); 189 assertEquals("Single Lines of Code", descriptor.getLongName()); 190 assertEquals(com.jeantessier.metrics.StatisticalMeasurement.class, descriptor.getClassFor()); 191 assertEquals("SLOC\n DISPOSE_SUM", descriptor.getInitText()); 192 assertNull("descriptor.LowerThreshold()", descriptor.getLowerThreshold()); 193 assertNull("descriptor.UpperThreshold()", descriptor.getUpperThreshold()); 194 } 195 196 public void testClassMeasurement() throws IOException, SAXException { 197 StringBuffer document = new StringBuffer (); 198 199 document.append("<!DOCTYPE metrics-configuration SYSTEM \"http://depfind.sourceforge.net/dtd/metrics-configuration.dtd\">\n"); 200 document.append("<metrics-configuration>\n"); 201 document.append(" <project-measurements/>\n"); 202 document.append(" <group-measurements/>\n"); 203 document.append(" <class-measurements>\n"); 204 document.append(" <measurement>\n"); 205 document.append(" <short-name>SLOC</short-name>\n"); 206 document.append(" <long-name>Single Lines of Code</long-name>\n"); 207 document.append(" <class>com.jeantessier.metrics.StatisticalMeasurement</class>\n"); 208 document.append(" <init>\n"); 209 document.append(" SLOC\n"); 210 document.append(" </init>\n"); 211 document.append(" </measurement>\n"); 212 document.append(" </class-measurements>\n"); 213 document.append(" <method-measurements/>\n"); 214 document.append("</metrics-configuration>\n"); 215 216 Reader in = new StringReader(document.toString()); 217 218 MetricsConfiguration configuration2 = loader.load(in); 219 assertSame(configuration, configuration2); 220 221 assertEquals("ProjectMeasurements", 0, configuration.getProjectMeasurements().size()); 222 assertEquals("GroupMeasurements", 0, configuration.getGroupMeasurements().size()); 223 assertEquals("ClassMeasurements", 1, configuration.getClassMeasurements().size()); 224 assertEquals("MethodMeasurements", 0, configuration.getMethodMeasurements().size()); 225 226 MeasurementDescriptor descriptor = (MeasurementDescriptor) configuration.getClassMeasurements().get(0); 227 assertEquals("SLOC", descriptor.getShortName()); 228 assertEquals("Single Lines of Code", descriptor.getLongName()); 229 assertEquals(com.jeantessier.metrics.StatisticalMeasurement.class, descriptor.getClassFor()); 230 assertEquals("SLOC", descriptor.getInitText()); 231 assertNull("descriptor.LowerThreshold()", descriptor.getLowerThreshold()); 232 assertNull("descriptor.UpperThreshold()", descriptor.getUpperThreshold()); 233 } 234 235 public void testMethodMeasurement() throws IOException, SAXException { 236 StringBuffer document = new StringBuffer (); 237 238 document.append("<!DOCTYPE metrics-configuration SYSTEM \"http://depfind.sourceforge.net/dtd/metrics-configuration.dtd\">\n"); 239 document.append("<metrics-configuration>\n"); 240 document.append(" <project-measurements/>\n"); 241 document.append(" <group-measurements/>\n"); 242 document.append(" <class-measurements/>\n"); 243 document.append(" <method-measurements>\n"); 244 document.append(" <measurement>\n"); 245 document.append(" <short-name>SLOC</short-name>\n"); 246 document.append(" <long-name>Single Lines of Code</long-name>\n"); 247 document.append(" <class>com.jeantessier.metrics.CounterMeasurement</class>\n"); 248 document.append(" <upper-threshold>50</upper-threshold>\n"); 249 document.append(" </measurement>\n"); 250 document.append(" </method-measurements>\n"); 251 document.append("</metrics-configuration>\n"); 252 253 Reader in = new StringReader(document.toString()); 254 255 MetricsConfiguration configuration2 = loader.load(in); 256 assertSame(configuration, configuration2); 257 258 assertEquals("ProjectMeasurements", 0, configuration.getProjectMeasurements().size()); 259 assertEquals("GroupMeasurements", 0, configuration.getGroupMeasurements().size()); 260 assertEquals("ClassMeasurements", 0, configuration.getClassMeasurements().size()); 261 assertEquals("MethodMeasurements", 1, configuration.getMethodMeasurements().size()); 262 263 MeasurementDescriptor descriptor = (MeasurementDescriptor) configuration.getMethodMeasurements().get(0); 264 assertEquals("SLOC", descriptor.getShortName()); 265 assertEquals("Single Lines of Code", descriptor.getLongName()); 266 assertEquals(com.jeantessier.metrics.CounterMeasurement.class, descriptor.getClassFor()); 267 assertNull("descriptor.Init()", descriptor.getInitText()); 268 assertNull("descriptor.LowerThreshold()", descriptor.getLowerThreshold()); 269 assertEquals("descriptor.UpperThreshold()", "50", descriptor.getUpperThreshold()); 270 } 271 272 public void testGroupDefinitions() throws IOException, SAXException { 273 StringBuffer document = new StringBuffer (); 274 275 document.append("<!DOCTYPE metrics-configuration SYSTEM \"http://depfind.sourceforge.net/dtd/metrics-configuration.dtd\">\n"); 276 document.append("<metrics-configuration>\n"); 277 document.append(" <project-measurements/>\n"); 278 document.append(" <group-definitions>\n"); 279 document.append(" <group-definition>\n"); 280 document.append(" <name>Jean Tessier</name>\n"); 281 document.append(" <pattern>/^com.jeantessier/</pattern>\n"); 282 document.append(" </group-definition>\n"); 283 document.append(" </group-definitions>\n"); 284 document.append(" <group-measurements/>\n"); 285 document.append(" <class-measurements/>\n"); 286 document.append(" <method-measurements/>\n"); 287 document.append("</metrics-configuration>\n"); 288 289 Reader in = new StringReader(document.toString()); 290 291 MetricsConfiguration configuration2 = loader.load(in); 292 assertSame(configuration, configuration2); 293 294 assertEquals("ProjectMeasurements", 0, configuration.getProjectMeasurements().size()); 295 assertEquals("GroupMeasurements", 0, configuration.getGroupMeasurements().size()); 296 assertEquals("ClassMeasurements", 0, configuration.getClassMeasurements().size()); 297 assertEquals("MethodMeasurements", 0, configuration.getMethodMeasurements().size()); 298 299 assertEquals("groups for foobar", 0, configuration.getGroups("foobar").size()); 300 assertEquals("groups for com.jeantessier.metrics", 1, configuration.getGroups("com.jeantessier.metrics").size()); 301 assertEquals("Jean Tessier", configuration.getGroups("com.jeantessier.metrics").iterator().next()); 302 } 303 } 304 | Popular Tags |