Coverage Report - com.jeantessier.dependency.TestLCOM4Gatherer
 
Classes in this File Line Coverage Branch Coverage Complexity
TestLCOM4Gatherer
100%
126/126
85%
12/14
1.286
 
 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.dependency;
 34  
 
 35  
 import junit.framework.TestCase;
 36  
 
 37  
 import java.util.Arrays;
 38  
 import java.util.Collection;
 39  
 import java.util.Map;
 40  
 
 41  11
 public class TestLCOM4Gatherer extends TestCase {
 42  
     private NodeFactory factory;
 43  
 
 44  
     private LCOM4Gatherer sut;
 45  
 
 46  
     protected void setUp() throws Exception {
 47  11
         super.setUp();
 48  
 
 49  11
         factory = new NodeFactory();
 50  
 
 51  11
         sut = new LCOM4Gatherer();
 52  11
     }
 53  
 
 54  
     public void testNothing() {
 55  1
         sut.traverseNodes(factory.getPackages().values());
 56  
 
 57  1
         Map<ClassNode, Collection<Collection<FeatureNode>>> actualResults = sut.getResults();
 58  1
         assertTrue(actualResults.isEmpty());
 59  1
     }
 60  
 
 61  
     public void testEmptyPackage() {
 62  1
         factory.createPackage("", true);
 63  
 
 64  1
         sut.traverseNodes(factory.getPackages().values());
 65  
 
 66  1
         Map<ClassNode, Collection<Collection<FeatureNode>>> actualResults = sut.getResults();
 67  1
         assertTrue(actualResults.isEmpty());
 68  1
     }
 69  
 
 70  
     public void testEmptyClass() {
 71  1
         ClassNode classNode = factory.createClass("Empty", true);
 72  
 
 73  1
         sut.traverseNodes(factory.getPackages().values());
 74  
 
 75  1
         Map<ClassNode, Collection<Collection<FeatureNode>>> actualResults = sut.getResults();
 76  1
         assertEquals("nb results", 1, actualResults.keySet().size());
 77  1
         assertTrue(classNode.getName() + " is missing from " + actualResults, actualResults.containsKey(classNode));
 78  
 
 79  1
         Collection<Collection<FeatureNode>> components = actualResults.get(classNode);
 80  1
         assertEquals("LCOM4 of empty class", 0, components.size());
 81  1
     }
 82  
 
 83  
     /**
 84  
      * Tests One.one, where class One will have an LCOM4 of 1 and
 85  
      * the component will be [One.one].
 86  
      */
 87  
     public void testOneFeature() {
 88  1
         FeatureNode featureNode = factory.createFeature("One.one", true);
 89  
 
 90  1
         sut.traverseNodes(factory.getPackages().values());
 91  
 
 92  1
         Map<ClassNode, Collection<Collection<FeatureNode>>> actualResults = sut.getResults();
 93  1
         assertEquals("nb results", 1, actualResults.keySet().size());
 94  1
         assertTrue(featureNode.getClassNode().getName() + " is missing from " + actualResults, actualResults.containsKey(featureNode.getClassNode()));
 95  
 
 96  1
         Collection<Collection<FeatureNode>> components = actualResults.get(featureNode.getClassNode());
 97  1
         assertEquals("LCOM4 of class w/ one feature", 1, components.size());
 98  
 
 99  1
         Collection<FeatureNode> component = components.iterator().next();
 100  1
         assertEquals("Size of first component", 1, component.size());
 101  1
         assertTrue(component.contains(featureNode));
 102  1
     }
 103  
 
 104  
     /**
 105  
      * Tests One.one and One.two, where class One will have an LCOM4 of 2 and
 106  
      * the components will be [One.one] and [One.two].
 107  
      */
 108  
     public void testTwoDisjointFeatures() {
 109  1
         FeatureNode featureNode1 = factory.createFeature("Two.one", true);
 110  1
         FeatureNode featureNode2 = factory.createFeature("Two.two", true);
 111  
 
 112  1
         sut.traverseNodes(factory.getPackages().values());
 113  
 
 114  1
         Map<ClassNode, Collection<Collection<FeatureNode>>> actualResults = sut.getResults();
 115  1
         assertEquals("nb results", 1, actualResults.keySet().size());
 116  1
         assertTrue(featureNode1.getClassNode().getName() + " is missing from " + actualResults, actualResults.containsKey(featureNode1.getClassNode()));
 117  
 
 118  1
         Collection<Collection<FeatureNode>> components = actualResults.get(featureNode1.getClassNode());
 119  1
         assertEquals("LCOM4 of class w/ two disjoint features", 2, components.size());
 120  
 
 121  1
         assertAtLeastOneComponentEquals(components, featureNode1);
 122  1
         assertAtLeastOneComponentEquals(components, featureNode2);
 123  1
     }
 124  
 
 125  
     /**
 126  
      * Tests One.one --> One.two, where class One will have an LCOM4 of 1 and
 127  
      * the component will be [One.one, One.two].
 128  
      */
 129  
     public void testTwoConnectedFeatures() {
 130  1
         FeatureNode featureNode1 = factory.createFeature("Two.one", true);
 131  1
         FeatureNode featureNode2 = factory.createFeature("Two.two", true);
 132  
 
 133  1
         featureNode1.addDependency(featureNode2);
 134  
 
 135  1
         sut.traverseNodes(factory.getPackages().values());
 136  
 
 137  1
         Map<ClassNode, Collection<Collection<FeatureNode>>> actualResults = sut.getResults();
 138  1
         assertEquals("nb results", 1, actualResults.keySet().size());
 139  1
         assertTrue(featureNode1.getClassNode().getName() + " is missing from " + actualResults, actualResults.containsKey(featureNode1.getClassNode()));
 140  
 
 141  1
         Collection<Collection<FeatureNode>> components = actualResults.get(featureNode1.getClassNode());
 142  1
         assertEquals("LCOM4 of class w/ two connected features", 1, components.size());
 143  
 
 144  1
         assertAtLeastOneComponentEquals(components, featureNode1, featureNode2);
 145  1
     }
 146  
 
 147  
     /**
 148  
      * Tests One.one --> One.two --> One.three, where class One will have an
 149  
      * LCOM4 of 1 and the component will be [One.one, One.two, One.three].
 150  
      */
 151  
     public void testThreeConnectedFeatures() {
 152  1
         FeatureNode featureNode1 = factory.createFeature("Three.one", true);
 153  1
         FeatureNode featureNode2 = factory.createFeature("Three.two", true);
 154  1
         FeatureNode featureNode3 = factory.createFeature("Three.three", true);
 155  
 
 156  1
         featureNode1.addDependency(featureNode2);
 157  1
         featureNode2.addDependency(featureNode3);
 158  
 
 159  1
         sut.traverseNodes(factory.getPackages().values());
 160  
 
 161  1
         Map<ClassNode, Collection<Collection<FeatureNode>>> actualResults = sut.getResults();
 162  1
         assertEquals("nb results", 1, actualResults.keySet().size());
 163  1
         assertTrue(featureNode1.getClassNode().getName() + " is missing from " + actualResults, actualResults.containsKey(featureNode1.getClassNode()));
 164  
 
 165  1
         Collection<Collection<FeatureNode>> components = actualResults.get(featureNode1.getClassNode());
 166  1
         assertEquals("LCOM4 of class w/ three connected features", 1, components.size());
 167  
 
 168  1
         assertAtLeastOneComponentEquals(components, featureNode1, featureNode2, featureNode3);
 169  1
     }
 170  
 
 171  
     /**
 172  
      * Tests One.one --> Two.two, where class One will have an LCOM4 of 1 and
 173  
      * the component will be [One.one].  Two.two is not included because it is
 174  
      * in another class.
 175  
      */
 176  
     public void testTwoConnectedFeaturesInSeparateClasses() {
 177  1
         FeatureNode featureNode1 = factory.createFeature("One.one", true);
 178  1
         FeatureNode featureNode2 = factory.createFeature("Two.two", true);
 179  
 
 180  1
         featureNode1.addDependency(featureNode2);
 181  
 
 182  1
         sut.traverseNodes(factory.getPackages().values());
 183  
 
 184  1
         Map<ClassNode, Collection<Collection<FeatureNode>>> actualResults = sut.getResults();
 185  1
         assertEquals("nb results", 2, actualResults.keySet().size());
 186  1
         assertTrue(featureNode1.getClassNode().getName() + " is missing from " + actualResults, actualResults.containsKey(featureNode1.getClassNode()));
 187  
 
 188  1
         Collection<Collection<FeatureNode>> components = actualResults.get(featureNode1.getClassNode());
 189  1
         assertEquals("LCOM4 of class w/ feature connected to other class", 1, components.size());
 190  
 
 191  1
         assertAtLeastOneComponentEquals(components, featureNode1);
 192  1
     }
 193  
 
 194  
     /**
 195  
      * Tests One.one --> Two.two --> One.two, where class One will have an
 196  
      * LCOM4 of 2 and the components will be [One.one] and [One.two].  There
 197  
      * are two components because the link between the methods is outside
 198  
      * class One.
 199  
      */
 200  
     public void testTwoIndirectlyConnectedFeatures() {
 201  1
         FeatureNode featureNode1 = factory.createFeature("One.one", true);
 202  1
         FeatureNode featureNode2 = factory.createFeature("Two.two", true);
 203  1
         FeatureNode featureNode3 = factory.createFeature("One.two", true);
 204  
 
 205  1
         featureNode1.addDependency(featureNode2);
 206  1
         featureNode2.addDependency(featureNode3);
 207  
 
 208  1
         sut.traverseNodes(factory.getPackages().values());
 209  
 
 210  1
         Map<ClassNode, Collection<Collection<FeatureNode>>> actualResults = sut.getResults();
 211  1
         assertEquals("nb results", 2, actualResults.keySet().size());
 212  1
         assertTrue(featureNode1.getClassNode().getName() + " is missing from " + actualResults, actualResults.containsKey(featureNode1.getClassNode()));
 213  
 
 214  1
         Collection<Collection<FeatureNode>> components = actualResults.get(featureNode1.getClassNode());
 215  1
         assertEquals("LCOM4 of class w/ features connected through other class", 2, components.size());
 216  
 
 217  1
         assertAtLeastOneComponentEquals(components, featureNode1);
 218  1
         assertAtLeastOneComponentEquals(components, featureNode3);
 219  1
     }
 220  
 
 221  
     public void testIgnoreConstructor() {
 222  1
         ClassNode classNode = factory.createClass("One", true);
 223  1
         factory.createFeature("One.One()", true);
 224  
 
 225  1
         sut.traverseNodes(factory.getPackages().values());
 226  
 
 227  1
         Map<ClassNode, Collection<Collection<FeatureNode>>> actualResults = sut.getResults();
 228  1
         assertEquals("nb results", 1, actualResults.keySet().size());
 229  1
         assertTrue(classNode.getName() + " is missing from " + actualResults, actualResults.containsKey(classNode));
 230  
 
 231  1
         Collection<Collection<FeatureNode>> components = actualResults.get(classNode);
 232  1
         assertEquals("LCOM4 of class w/ only constructors", 0, components.size());
 233  1
     }
 234  
 
 235  
     public void testIgnoreConstructor_WithinGraph() {
 236  1
         ClassNode classNode = factory.createClass("Three", true);
 237  1
         FeatureNode constructorNode = factory.createFeature("Three.Three()", true);
 238  1
         FeatureNode featureNode1 = factory.createFeature("Three.one", true);
 239  1
         FeatureNode featureNode2 = factory.createFeature("Three.two", true);
 240  
 
 241  1
         constructorNode.addDependency(featureNode1);
 242  1
         constructorNode.addDependency(featureNode2);
 243  
 
 244  1
         sut.traverseNodes(factory.getPackages().values());
 245  
 
 246  1
         Map<ClassNode, Collection<Collection<FeatureNode>>> actualResults = sut.getResults();
 247  1
         assertEquals("nb results", 1, actualResults.keySet().size());
 248  1
         assertTrue(classNode.getName() + " is missing from " + actualResults, actualResults.containsKey(classNode));
 249  
 
 250  1
         Collection<Collection<FeatureNode>> components = actualResults.get(classNode);
 251  1
         assertEquals("LCOM4 of class w/ two features connected through the constructor " + components, 2, components.size());
 252  
 
 253  1
         assertAtLeastOneComponentEquals(components, featureNode1);
 254  1
         assertAtLeastOneComponentEquals(components, featureNode2);
 255  1
     }
 256  
 
 257  
     private void assertAtLeastOneComponentEquals(Collection<Collection<FeatureNode>> components, FeatureNode ... expectedNodes) {
 258  9
         boolean found = false;
 259  
 
 260  9
         for (Collection<FeatureNode> component : components) {
 261  15
             found = found || checkComponentEquals(component, expectedNodes);
 262  
         }
 263  
 
 264  9
         assertTrue(Arrays.asList(expectedNodes) + " not in " + components, found);
 265  9
     }
 266  
 
 267  
     private boolean checkComponentEquals(Collection<FeatureNode> component, FeatureNode ... expectedNodes) {
 268  12
         boolean result = expectedNodes.length == component.size();
 269  
 
 270  27
         for (FeatureNode expectedNode : expectedNodes) {
 271  15
             result = result && component.contains(expectedNode);
 272  
         }
 273  
 
 274  12
         return result;
 275  
     }
 276  
 }