Coverage Report - com.jeantessier.metrics.TestMetricsFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
TestMetricsFactory
100%
125/125
N/A
1
 
 1  
 /*
 2  
  *  Copyright (c) 2001-2009, Jean Tessier
 3  
  *  All rights reserved.
 4  
  *  
 5  
  *  Redistribution and use in source and binary forms, with or without
 6  
  *  modification, are permitted provided that the following conditions
 7  
  *  are met:
 8  
  *  
 9  
  *      * Redistributions of source code must retain the above copyright
 10  
  *        notice, this list of conditions and the following disclaimer.
 11  
  *  
 12  
  *      * Redistributions in binary form must reproduce the above copyright
 13  
  *        notice, this list of conditions and the following disclaimer in the
 14  
  *        documentation and/or other materials provided with the distribution.
 15  
  *  
 16  
  *      * Neither the name of Jean Tessier nor the names of his contributors
 17  
  *        may be used to endorse or promote products derived from this software
 18  
  *        without specific prior written permission.
 19  
  *  
 20  
  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 21  
  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 22  
  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 23  
  *  A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR
 24  
  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 25  
  *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 26  
  *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 27  
  *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 28  
  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 29  
  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 30  
  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 31  
  */
 32  
 
 33  
 package com.jeantessier.metrics;
 34  
 
 35  
 import junit.framework.*;
 36  
 
 37  
 import java.io.*;
 38  
 
 39  13
 public class TestMetricsFactory extends TestCase {
 40  
     private MetricsConfiguration configuration;
 41  
     private MetricsFactory factory;
 42  
 
 43  
     protected void setUp() throws Exception {
 44  13
         configuration = new MetricsConfigurationLoader(Boolean.getBoolean("DEPENDENCYFINDER_TESTS_VALIDATE")).load("etc" + File.separator + "MetricsConfig.xml");
 45  13
         factory = new MetricsFactory("test", configuration);
 46  13
     }
 47  
     
 48  
     public void testCreateProjectMetrics() {
 49  1
         Metrics m1 = factory.createProjectMetrics("foo");
 50  1
         assertNotNull(m1);
 51  1
         assertEquals("New metrics name", "foo", m1.getName());
 52  
 
 53  1
         Metrics m2 = factory.createProjectMetrics("foo");
 54  1
         assertSame(m1, m2);
 55  1
         assertEquals("project measurements", configuration.getProjectMeasurements().size(), m1.getMeasurementNames().size());
 56  1
     }
 57  
             
 58  
     public void testIncludeProjectMetrics() {
 59  1
         Metrics m1 = factory.createProjectMetrics("foo");
 60  
 
 61  1
         assertFalse("ProjectMetrics() contains external metrics", factory.getProjectMetrics().contains(m1));
 62  
 
 63  1
         factory.includeProjectMetrics(m1);
 64  
 
 65  1
         assertTrue("ProjectMetrics() does not contain internal metrics", factory.getProjectMetrics().contains(m1));
 66  1
     }
 67  
     
 68  
     public void testCreateGroupMetrics() {
 69  1
         Metrics m1 = factory.createGroupMetrics("foo");
 70  1
         assertNotNull(m1);
 71  1
         assertEquals("New metrics name", "foo", m1.getName());
 72  
 
 73  1
         Metrics m2 = factory.createGroupMetrics("foo");
 74  1
         assertSame(m1, m2);
 75  1
         assertEquals("group measurements", configuration.getGroupMeasurements().size(), m1.getMeasurementNames().size());
 76  1
     }
 77  
             
 78  
     public void testIncludeGroupMetrics() {
 79  1
         Metrics m1 = factory.createGroupMetrics("foo");
 80  
 
 81  1
         assertFalse("GroupMetrics() contains external metrics", factory.getGroupMetrics().contains(m1));
 82  
 
 83  1
         factory.includeGroupMetrics(m1);
 84  
 
 85  1
         assertTrue("GroupMetrics() does not contain internal metrics", factory.getGroupMetrics().contains(m1));
 86  1
     }
 87  
 
 88  
     public void testCreateClassMetrics() {
 89  1
         Metrics m1 = factory.createClassMetrics("foo");
 90  1
         assertNotNull(m1);
 91  1
         assertEquals("New metrics name", "foo", m1.getName());
 92  
 
 93  1
         Metrics m2 = factory.createClassMetrics("foo");
 94  1
         assertSame(m1, m2);
 95  1
         assertEquals("class measurements", configuration.getClassMeasurements().size(), m1.getMeasurementNames().size());
 96  1
     }
 97  
         
 98  
     public void testIncludeClassMetrics() {
 99  1
         Metrics m1 = factory.createClassMetrics("foo");
 100  
 
 101  1
         assertFalse("ClassMetrics() contains external metrics", factory.getClassMetrics().contains(m1));
 102  
 
 103  1
         factory.includeClassMetrics(m1);
 104  
 
 105  1
         assertTrue("ClassMetrics() does not contain internal metrics", factory.getClassMetrics().contains(m1));
 106  1
     }
 107  
 
 108  
     public void testCreateMethodMetrics() {
 109  1
         Metrics m1 = factory.createMethodMetrics("foo");
 110  1
         assertNotNull(m1);
 111  1
         assertEquals("New metrics name", "foo", m1.getName());
 112  
 
 113  1
         Metrics m2 = factory.createMethodMetrics("foo");
 114  1
         assertSame(m1, m2);
 115  1
         assertEquals("method measurements", configuration.getMethodMeasurements().size(), m1.getMeasurementNames().size());
 116  1
     }
 117  
     
 118  
     public void testIncludeMethodMetrics() {
 119  1
         Metrics m1 = factory.createMethodMetrics("foo");
 120  
 
 121  1
         assertFalse("MethodMetrics() contains external metrics", factory.getMethodMetrics().contains(m1));
 122  
 
 123  1
         factory.includeMethodMetrics(m1);
 124  
 
 125  1
         assertTrue("MethodMetrics() does not contain internal metrics", factory.getMethodMetrics().contains(m1));
 126  1
     }
 127  
     
 128  
     public void testCreateStaticInitializerMetrics() {
 129  1
         Metrics m = factory.createMethodMetrics("foo.static {}");
 130  
 
 131  1
         assertEquals("class name", "foo", m.getParent().getName());
 132  1
     }
 133  
 
 134  
     public void testCreateStructure() {
 135  1
         Metrics methodMetrics  = factory.createMethodMetrics("a.A.a()");
 136  1
         Metrics classMetrics   = factory.createClassMetrics("a.A");
 137  1
         Metrics packageMetrics = factory.createGroupMetrics("a");
 138  1
         Metrics projectMetrics = factory.createProjectMetrics();
 139  
 
 140  1
         factory.includeMethodMetrics(methodMetrics);
 141  
         
 142  1
         assertTrue(projectMetrics.getSubMetrics().contains(packageMetrics));
 143  1
         assertTrue(packageMetrics.getSubMetrics().contains(classMetrics));
 144  1
         assertTrue(classMetrics.getSubMetrics().contains(methodMetrics));
 145  1
     }
 146  
 
 147  
     public void testGroupDefinitionsWithInternal() {
 148  1
         configuration.addGroupDefinition("foo", "/foo/");
 149  1
         configuration.addGroupDefinition("bar", "/bar/");
 150  1
         configuration.addGroupDefinition("baz", "/baz/");
 151  
 
 152  1
         Metrics metrics = factory.createClassMetrics("com.foobar.Foobar");
 153  1
         factory.includeClassMetrics(metrics);
 154  
 
 155  1
         assertEquals("Number of groups",     3, factory.getGroupMetrics().size());
 156  1
         assertEquals("Number of all groups", 3, factory.getAllGroupMetrics().size());
 157  
 
 158  1
         assertTrue("Group foo missing",        factory.getAllGroupNames().contains("foo"));
 159  1
         assertTrue("Group bar missing",        factory.getAllGroupNames().contains("bar"));
 160  1
         assertFalse("Group baz present",       factory.getAllGroupNames().contains("baz"));
 161  1
         assertTrue("Group com.foobar missing", factory.getAllGroupNames().contains("com.foobar"));
 162  
 
 163  1
         assertTrue("Group foo missing",        factory.getGroupNames().contains("foo"));
 164  1
         assertTrue("Group bar missing",        factory.getGroupNames().contains("bar"));
 165  1
         assertFalse("Group baz present",       factory.getGroupNames().contains("baz"));
 166  1
         assertTrue("Group com.foobar missing", factory.getGroupNames().contains("com.foobar"));
 167  
 
 168  1
         assertTrue("Not in foo",        factory.createGroupMetrics("foo").getSubMetrics().contains(metrics));
 169  1
         assertTrue("Not in bar",        factory.createGroupMetrics("bar").getSubMetrics().contains(metrics));
 170  1
         assertFalse("In baz",           factory.createGroupMetrics("baz").getSubMetrics().contains(metrics));
 171  1
         assertTrue("Not in com.foobar", factory.createGroupMetrics("com.foobar").getSubMetrics().contains(metrics));
 172  
 
 173  1
         assertEquals("Wrong parent", factory.createGroupMetrics("com.foobar"), metrics.getParent());
 174  1
     }
 175  
 
 176  
     public void testGroupDefinitionsWithExternal() {
 177  1
         configuration.addGroupDefinition("foo", "/foo/");
 178  1
         configuration.addGroupDefinition("bar", "/bar/");
 179  1
         configuration.addGroupDefinition("baz", "/baz/");
 180  
 
 181  1
         Metrics metrics = factory.createClassMetrics("com.foobar.Foobar");
 182  
 
 183  1
         assertEquals("Number of groups",     0, factory.getGroupMetrics().size());
 184  1
         assertEquals("Number of all groups", 1, factory.getAllGroupMetrics().size());
 185  
 
 186  1
         assertFalse("Group foo present",       factory.getAllGroupNames().contains("foo"));
 187  1
         assertFalse("Group bar present",       factory.getAllGroupNames().contains("bar"));
 188  1
         assertFalse("Group baz present",       factory.getAllGroupNames().contains("baz"));
 189  1
         assertTrue("Group com.foobar missing", factory.getAllGroupNames().contains("com.foobar"));
 190  
 
 191  1
         assertFalse("In foo",        factory.createGroupMetrics("foo").getSubMetrics().contains(metrics));
 192  1
         assertFalse("In bar",        factory.createGroupMetrics("bar").getSubMetrics().contains(metrics));
 193  1
         assertFalse("In baz",        factory.createGroupMetrics("baz").getSubMetrics().contains(metrics));
 194  1
         assertFalse("In com.foobar", factory.createGroupMetrics("com.foobar").getSubMetrics().contains(metrics));
 195  
 
 196  1
         assertEquals("Wrong parent", factory.createGroupMetrics("com.foobar"), metrics.getParent());
 197  1
     }
 198  
 
 199  
     public void testGroupDefinitionsWithBoth() {
 200  1
         configuration.addGroupDefinition("foo", "/foo/");
 201  1
         configuration.addGroupDefinition("baz", "/baz/");
 202  
 
 203  1
         Metrics fooMetrics    = factory.createClassMetrics("com.foo.Foo");
 204  1
         Metrics foobazMetrics = factory.createClassMetrics("com.baz.Foobaz");
 205  
 
 206  1
         factory.includeClassMetrics(fooMetrics);
 207  
 
 208  1
         assertEquals("Number of groups",     2, factory.getGroupMetrics().size());
 209  
 
 210  1
         assertTrue("Group foo missing",      factory.getGroupNames().contains("foo"));
 211  1
         assertFalse("Group baz present",     factory.getGroupNames().contains("baz"));
 212  1
         assertTrue("Group com.foo missing",  factory.getGroupNames().contains("com.foo"));
 213  1
         assertFalse("Group com.baz missing", factory.getGroupNames().contains("com.baz"));
 214  
 
 215  1
         assertTrue("Not in foo",     factory.createGroupMetrics("foo").getSubMetrics().contains(fooMetrics));
 216  1
         assertTrue("Not in com.foo", factory.createGroupMetrics("com.foo").getSubMetrics().contains(fooMetrics));
 217  
 
 218  1
         assertEquals("foo.size()",     1, factory.createGroupMetrics("foo").getSubMetrics().size());
 219  1
         assertEquals("com.foo.size()", 1, factory.createGroupMetrics("com.foo").getSubMetrics().size());
 220  
 
 221  1
         assertEquals("Number of all groups", 3, factory.getAllGroupMetrics().size());
 222  
 
 223  1
         assertTrue("Group foo missing",     factory.getAllGroupNames().contains("foo"));
 224  1
         assertFalse("Group baz present",    factory.getAllGroupNames().contains("baz"));
 225  1
         assertTrue("Group com.foo missing", factory.getAllGroupNames().contains("com.foo"));
 226  1
         assertTrue("Group com.baz missing", factory.getAllGroupNames().contains("com.baz"));
 227  
 
 228  1
         assertFalse("In com.baz", factory.createGroupMetrics("com.baz").getSubMetrics().contains(foobazMetrics));
 229  
         
 230  1
         assertEquals("com.baz.size()", 0, factory.createGroupMetrics("com.baz").getSubMetrics().size());
 231  
 
 232  1
         assertEquals("Wrong parent", factory.createGroupMetrics("com.foo"), fooMetrics.getParent());
 233  1
         assertEquals("Wrong parent", factory.createGroupMetrics("com.baz"), foobazMetrics.getParent());
 234  1
     }
 235  
 }