Coverage Report - com.jeantessier.dependencyfinder.cli.ClassCohesion
 
Classes in this File Line Coverage Branch Coverage Complexity
ClassCohesion
30%
19/63
23%
8/34
3.429
 
 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.dependencyfinder.cli;
 34  
 
 35  
 import java.util.*;
 36  
 import java.io.*;
 37  
 
 38  
 import org.apache.log4j.*;
 39  
 
 40  
 import com.jeantessier.commandline.*;
 41  
 import com.jeantessier.dependency.*;
 42  
 
 43  1
 public class ClassCohesion extends DependencyGraphCommand {
 44  
     private static final String DEFAULT_ENCODING = "utf-8";
 45  
     private static final String DEFAULT_DTD_PREFIX = "http://depfind.sourceforge.net/dtd";
 46  
     private static final String DEFAULT_INDENT_TEXT = "    ";
 47  
 
 48  
     protected void populateCommandLineSwitches()  {
 49  11
         super.populateCommandLineSwitches();
 50  11
         populateCommandLineSwitchesForXMLOutput(DEFAULT_ENCODING, DEFAULT_DTD_PREFIX, DEFAULT_INDENT_TEXT);
 51  
 
 52  11
         getCommandLine().addToggleSwitch("csv");
 53  11
         getCommandLine().addToggleSwitch("txt");
 54  11
         getCommandLine().addToggleSwitch("xml");
 55  11
         getCommandLine().addToggleSwitch("list");
 56  11
     }
 57  
 
 58  
     protected Collection<CommandLineException> parseCommandLine(String[] args) {
 59  10
         Collection<CommandLineException> exceptions = super.parseCommandLine(args);
 60  10
         int modeSwitch = 0;
 61  
 
 62  10
         if (getCommandLine().getToggleSwitch("csv")) {
 63  2
             modeSwitch++;
 64  
         }
 65  10
         if (getCommandLine().getToggleSwitch("txt")) {
 66  2
             modeSwitch++;
 67  
         }
 68  10
         if (getCommandLine().getToggleSwitch("xml")) {
 69  1
             modeSwitch++;
 70  
         }
 71  10
         if (modeSwitch != 1) {
 72  7
             exceptions.add(new CommandLineException("Must have one and only one of -csv, -txt, or -xml"));
 73  
         }
 74  
 
 75  10
         return exceptions;
 76  
     }
 77  
 
 78  
     public void doProcessing() throws Exception {
 79  0
         LCOM4Gatherer gatherer = new LCOM4Gatherer();
 80  
 
 81  0
         Logger.getLogger(OOMetrics.class).debug("Reading classes and computing metrics as we go ...");
 82  0
         getVerboseListener().print("Reading classes and computing metrics as we go ...");
 83  0
         gatherer.traverseNodes(loadGraph().getPackages().values());
 84  
 
 85  0
         Logger.getLogger(OOMetrics.class).debug("Printing results ...");
 86  0
         getVerboseListener().print("Printing results ...");
 87  
 
 88  0
         if (getCommandLine().isPresent("csv")) {
 89  0
             printCSVFiles(gatherer.getResults());
 90  0
         } else if (getCommandLine().isPresent("txt")) {
 91  0
             printTextFile(gatherer.getResults());
 92  0
         } else if (getCommandLine().isPresent("xml")) {
 93  0
             printXMLFile(gatherer.getResults());
 94  
         }
 95  
 
 96  0
         Logger.getLogger(OOMetrics.class).debug("Done.");
 97  0
     }
 98  
 
 99  
     private void printCSVFiles(Map<ClassNode, Collection<Collection<FeatureNode>>> results) throws IOException {
 100  0
         getOut().println("class, LCOM4");
 101  0
         for (Map.Entry<ClassNode, Collection<Collection<FeatureNode>>> entry : results.entrySet()) {
 102  0
             getOut().println(entry.getKey().getName() + ", " + entry.getValue().size());
 103  
         }
 104  0
     }
 105  
 
 106  
     private void printTextFile(Map<ClassNode, Collection<Collection<FeatureNode>>> results) throws IOException {
 107  0
         String indentText = getCommandLine().getSingleSwitch("indent-text");
 108  
 
 109  0
         for (Map.Entry<ClassNode, Collection<Collection<FeatureNode>>> entry : results.entrySet()) {
 110  0
             getOut().println(entry.getKey().getName() + ": " + entry.getValue().size());
 111  0
             if (entry.getValue().size() > 1 && getCommandLine().isPresent("list")) {
 112  0
                 getOut().println(indentText + "--------");
 113  0
                 for (Collection<FeatureNode> component : entry.getValue()) {
 114  0
                     for (FeatureNode feature : component) {
 115  0
                         getOut().println(indentText + feature.getName().substring(feature.getClassNode().getName().length() + 1));
 116  
                     }
 117  0
                     getOut().println(indentText + "--------");
 118  
                 }
 119  
             }
 120  
         }
 121  0
     }
 122  
 
 123  
     private void printXMLFile(Map<ClassNode, Collection<Collection<FeatureNode>>> results) throws IOException {
 124  0
         String indentText = getCommandLine().getSingleSwitch("indent-text");
 125  
 
 126  0
         getOut().println("<classes>");
 127  0
         for (Map.Entry<ClassNode, Collection<Collection<FeatureNode>>> entry : results.entrySet()) {
 128  0
             if (entry.getValue().size() > 1) {
 129  0
                 getOut().println(indentText + "<class name=\"" + entry.getKey().getName() + "\" lcom4=\"" + entry.getValue().size() + "\">");
 130  0
                 for (Collection<FeatureNode> component : entry.getValue()) {
 131  0
                     getOut().println(indentText + indentText + "<component>");
 132  0
                     for (FeatureNode feature : component) {
 133  0
                         getOut().println(indentText + indentText + indentText + "<feature name=\"" + feature.getName() + "\"/>");
 134  
                     }
 135  0
                     getOut().println(indentText + indentText + "</component>");
 136  
                 }
 137  0
                 getOut().println(indentText + "</class>");
 138  
             } else {
 139  0
                 getOut().println(indentText + "<class name=\"" + entry.getKey().getName() + "\" lcom4=\"" + entry.getValue().size() + "\"/>");
 140  
             }
 141  
         }
 142  0
         getOut().println("</classes>");
 143  0
     }
 144  
 
 145  
     public static void main(String[] args) throws Exception {
 146  0
         new ClassCohesion().run(args);
 147  0
     }
 148  
 }