Coverage Report - com.jeantessier.dependencyfinder.cli.DiffCommand
 
Classes in this File Line Coverage Branch Coverage Complexity
DiffCommand
37%
22/58
16%
2/12
2.714
 
 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.io.*;
 36  
 import java.lang.reflect.*;
 37  
 import java.util.*;
 38  
 
 39  
 import org.apache.log4j.*;
 40  
 
 41  
 import com.jeantessier.commandline.*;
 42  
 import com.jeantessier.diff.*;
 43  
 
 44  2
 public abstract class DiffCommand extends Command {
 45  
     public static final String API_STRATEGY = "api";
 46  
     public static final String INCOMPATIBLE_STRATEGY = "incompatible";
 47  
 
 48  
     public static final String DEFAULT_LEVEL = API_STRATEGY;
 49  
 
 50  
     protected void showSpecificUsage(PrintStream out) {
 51  2
         out.println();
 52  2
         out.println("Defaults is text output to the console.");
 53  2
         out.println();
 54  2
     }
 55  
 
 56  
     protected void populateCommandLineSwitches() {
 57  16
         super.populateCommandLineSwitches();
 58  16
         populateCommandLineSwitchesForXMLOutput(Report.DEFAULT_ENCODING, Report.DEFAULT_DTD_PREFIX, Report.DEFAULT_INDENT_TEXT);
 59  
 
 60  16
         getCommandLine().addSingleValueSwitch("name");
 61  16
         getCommandLine().addMultipleValuesSwitch("old", true);
 62  16
         getCommandLine().addSingleValueSwitch("old-label");
 63  16
         getCommandLine().addMultipleValuesSwitch("new", true);
 64  16
         getCommandLine().addSingleValueSwitch("new-label");
 65  16
         getCommandLine().addSingleValueSwitch("filter");
 66  16
         getCommandLine().addToggleSwitch("code");
 67  16
         getCommandLine().addSingleValueSwitch("level", DEFAULT_LEVEL);
 68  16
     }
 69  
 
 70  
     protected Collection<CommandLineException> parseCommandLine(String[] args) {
 71  14
         Collection<CommandLineException> exceptions = super.parseCommandLine(args);
 72  
 
 73  14
         if (!getCommandLine().isPresent("old-label")) {
 74  14
             getCommandLine().getSwitch("old-label").setValue(getCommandLine().getMultipleSwitch("old").toString());
 75  
         }
 76  
 
 77  14
         if (!getCommandLine().isPresent("new-label")) {
 78  14
             getCommandLine().getSwitch("new-label").setValue(getCommandLine().getMultipleSwitch("new").toString());
 79  
         }
 80  
 
 81  14
         return exceptions;
 82  
     }
 83  
 
 84  
     protected DifferencesFactory getDifferencesFactory() throws IOException {
 85  0
         DifferenceStrategy baseStrategy = getBaseStrategy(getCommandLine().getToggleSwitch("code"));
 86  0
         DifferenceStrategy strategy = getStrategy(getCommandLine().getSingleSwitch("level"), baseStrategy);
 87  
 
 88  0
         if (getCommandLine().isPresent("filter")) {
 89  0
             strategy = new ListBasedDifferenceStrategy(strategy, getCommandLine().getSingleSwitch("filter"));
 90  
         }
 91  
 
 92  0
         return new DifferencesFactory(strategy);
 93  
     }
 94  
 
 95  
     private DifferenceStrategy getBaseStrategy(boolean useCode) {
 96  
         DifferenceStrategy result;
 97  
 
 98  0
         if (useCode) {
 99  0
             result = new CodeDifferenceStrategy();
 100  
         } else {
 101  0
             result = new NoDifferenceStrategy();
 102  
         }
 103  
 
 104  0
         return result;
 105  
     }
 106  
 
 107  
     private DifferenceStrategy getStrategy(String level, DifferenceStrategy baseStrategy) {
 108  
         DifferenceStrategy result;
 109  
 
 110  0
         if (API_STRATEGY.equals(level)) {
 111  0
             result = new APIDifferenceStrategy(baseStrategy);
 112  0
         } else if (INCOMPATIBLE_STRATEGY.equals(level)) {
 113  0
             result = new IncompatibleDifferenceStrategy(baseStrategy);
 114  
         } else {
 115  
             try {
 116  
                 Constructor constructor;
 117  
                 try {
 118  0
                     constructor = Class.forName(level).getConstructor(DifferenceStrategy.class);
 119  0
                     result = (DifferenceStrategy) constructor.newInstance(baseStrategy);
 120  0
                 } catch (NoSuchMethodException ex) {
 121  0
                     result = (DifferenceStrategy) Class.forName(level).newInstance();
 122  0
                 }
 123  0
             } catch (InvocationTargetException ex) {
 124  0
                 Logger.getLogger(getClass()).error("Unknown level \"" + level + "\", using default level \"" + DEFAULT_LEVEL + "\"", ex);
 125  0
                 result = getDefaultStrategy(baseStrategy);
 126  0
             } catch (InstantiationException ex) {
 127  0
                 Logger.getLogger(getClass()).error("Unknown level \"" + level + "\", using default level \"" + DEFAULT_LEVEL + "\"", ex);
 128  0
                 result = getDefaultStrategy(baseStrategy);
 129  0
             } catch (IllegalAccessException ex) {
 130  0
                 Logger.getLogger(getClass()).error("Unknown level \"" + level + "\", using default level \"" + DEFAULT_LEVEL + "\"", ex);
 131  0
                 result = getDefaultStrategy(baseStrategy);
 132  0
             } catch (ClassNotFoundException ex) {
 133  0
                 Logger.getLogger(getClass()).error("Unknown level \"" + level + "\", using default level \"" + DEFAULT_LEVEL + "\"", ex);
 134  0
                 result = getDefaultStrategy(baseStrategy);
 135  0
             } catch (ClassCastException ex) {
 136  0
                 Logger.getLogger(getClass()).error("Unknown level \"" + level + "\", using default level \"" + DEFAULT_LEVEL + "\"", ex);
 137  0
                 result = getDefaultStrategy(baseStrategy);
 138  0
             }
 139  
         }
 140  
 
 141  0
         return result;
 142  
     }
 143  
 
 144  
     private DifferenceStrategy getDefaultStrategy(DifferenceStrategy strategy) {
 145  0
         return new APIDifferenceStrategy(strategy);
 146  
     }
 147  
 }