Coverage Report - org.owasp.dependencycheck.exception.ExceptionCollection
 
Classes in this File Line Coverage Branch Coverage Complexity
ExceptionCollection
6%
4/60
0%
0/10
1.333
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  *
 16  
  * Copyright (c) 2016 Jeremy Long. All Rights Reserved.
 17  
  */
 18  
 package org.owasp.dependencycheck.exception;
 19  
 
 20  
 import java.io.PrintStream;
 21  
 import java.io.PrintWriter;
 22  
 import java.util.ArrayList;
 23  
 import java.util.List;
 24  
 
 25  
 /**
 26  
  * A collection of several exceptions.
 27  
  *
 28  
  * @author Jeremy Lomg
 29  
  */
 30  
 public class ExceptionCollection extends Exception {
 31  
 
 32  
     /**
 33  
      * Instantiates a new exception collection.
 34  
      *
 35  
      * @param exceptions a list of exceptions
 36  
      */
 37  
     public ExceptionCollection(List<Throwable> exceptions) {
 38  0
         super();
 39  0
         this.exceptions = exceptions;
 40  0
     }
 41  
 
 42  
     /**
 43  
      * Instantiates a new exception collection.
 44  
      *
 45  
      * @param msg the exception message
 46  
      * @param exceptions a list of exceptions
 47  
      */
 48  
     public ExceptionCollection(String msg, List<Throwable> exceptions) {
 49  1
         super(msg);
 50  1
         this.exceptions = exceptions;
 51  1
     }
 52  
 
 53  
     /**
 54  
      * Instantiates a new exception collection.
 55  
      *
 56  
      * @param exceptions a list of exceptions
 57  
      * @param fatal indicates if the exception that occurred is fatal - meaning
 58  
      * that no analysis was performed.
 59  
      */
 60  
     public ExceptionCollection(List<Throwable> exceptions, boolean fatal) {
 61  0
         super();
 62  0
         this.exceptions = exceptions;
 63  0
         this.fatal = fatal;
 64  0
     }
 65  
 
 66  
     /**
 67  
      * Instantiates a new exception collection.
 68  
      *
 69  
      * @param msg the exception message
 70  
      * @param exceptions a list of exceptions
 71  
      * @param fatal indicates if the exception that occurred is fatal - meaning
 72  
      * that no analysis was performed.
 73  
      */
 74  
     public ExceptionCollection(String msg, List<Throwable> exceptions, boolean fatal) {
 75  0
         super(msg);
 76  0
         this.exceptions = exceptions;
 77  0
         this.fatal = fatal;
 78  0
     }
 79  
 
 80  
     /**
 81  
      * Instantiates a new exception collection.
 82  
      *
 83  
      * @param exceptions a list of exceptions
 84  
      * @param fatal indicates if the exception that occurred is fatal - meaning
 85  
      * that no analysis was performed.
 86  
      */
 87  
     public ExceptionCollection(Throwable exceptions, boolean fatal) {
 88  0
         super();
 89  0
         this.exceptions = new ArrayList<Throwable>();
 90  0
         this.exceptions.add(exceptions);
 91  0
         this.fatal = fatal;
 92  0
     }
 93  
 
 94  
     /**
 95  
      * Instantiates a new exception collection.
 96  
      *
 97  
      * @param msg the exception message
 98  
      * @param exception a list of exceptions
 99  
      */
 100  
     public ExceptionCollection(String msg, Throwable exception) {
 101  0
         super(msg);
 102  0
         this.exceptions = new ArrayList<Throwable>();
 103  0
         this.exceptions.add(exception);
 104  0
         this.fatal = false;
 105  0
     }
 106  
 
 107  
     /**
 108  
      * Instantiates a new exception collection.
 109  
      */
 110  
     public ExceptionCollection() {
 111  0
         super();
 112  0
         this.exceptions = new ArrayList<Throwable>();
 113  0
     }
 114  
     /**
 115  
      * The serial version uid.
 116  
      */
 117  
     private static final long serialVersionUID = 1L;
 118  
 
 119  
     /**
 120  
      * A collection of exceptions.
 121  
      */
 122  
     private List<Throwable> exceptions;
 123  
 
 124  
     /**
 125  
      * Get the value of exceptions.
 126  
      *
 127  
      * @return the value of exceptions
 128  
      */
 129  
     public List<Throwable> getExceptions() {
 130  0
         return exceptions;
 131  
     }
 132  
 
 133  
     /**
 134  
      * Adds an exception to the collection.
 135  
      *
 136  
      * @param ex the exception to add
 137  
      */
 138  
     public void addException(Throwable ex) {
 139  0
         this.exceptions.add(ex);
 140  0
     }
 141  
 
 142  
     /**
 143  
      * Adds an exception to the collection.
 144  
      *
 145  
      * @param ex the exception to add
 146  
      * @param fatal flag indicating if this is a fatal error
 147  
      */
 148  
     public void addException(Throwable ex, boolean fatal) {
 149  0
         addException(ex);
 150  0
         this.fatal = fatal;
 151  0
     }
 152  
 
 153  
     /**
 154  
      * Flag indicating if a fatal exception occurred that would prevent the
 155  
      * attempt at completing the analysis even if exceptions occurred.
 156  
      */
 157  1
     private boolean fatal = false;
 158  
 
 159  
     /**
 160  
      * Get the value of fatal.
 161  
      *
 162  
      * @return the value of fatal
 163  
      */
 164  
     public boolean isFatal() {
 165  0
         return fatal;
 166  
     }
 167  
 
 168  
     /**
 169  
      * Set the value of fatal.
 170  
      *
 171  
      * @param fatal new value of fatal
 172  
      */
 173  
     public void setFatal(boolean fatal) {
 174  0
         this.fatal = fatal;
 175  0
     }
 176  
 
 177  
     /**
 178  
      * Prints the stack trace.
 179  
      *
 180  
      * @param s the writer to print to
 181  
      */
 182  
     @Override
 183  
     public void printStackTrace(PrintWriter s) {
 184  0
         s.println("Multiple Exceptions Occured");
 185  0
         super.printStackTrace(s);
 186  0
         for (Throwable t : this.exceptions) {
 187  0
             s.println("Next Exception:");
 188  0
             t.printStackTrace(s);
 189  0
         }
 190  0
     }
 191  
 
 192  
     /**
 193  
      * Prints the stack trace.
 194  
      *
 195  
      * @param s the stream to write the stack trace to
 196  
      */
 197  
     @Override
 198  
     public void printStackTrace(PrintStream s) {
 199  0
         s.println("Multiple Exceptions Occurred");
 200  0
         super.printStackTrace(s);
 201  0
         for (Throwable t : this.exceptions) {
 202  0
             s.println("Next Exception:");
 203  0
             t.printStackTrace(s);
 204  0
         }
 205  0
     }
 206  
 
 207  
     /**
 208  
      * Returns the error message, including the message from all contained
 209  
      * exceptions.
 210  
      *
 211  
      * @return the error message
 212  
      */
 213  
     @Override
 214  
     public String getMessage() {
 215  0
         final StringBuilder sb = new StringBuilder();
 216  0
         final String msg = super.getMessage();
 217  0
         if (msg == null || msg.isEmpty()) {
 218  0
             sb.append("One or more exceptions occured during analysis:");
 219  
         } else {
 220  0
             sb.append(msg);
 221  
         }
 222  0
         for (Throwable t : this.exceptions) {
 223  0
             sb.append("\n\t").append(t.getMessage());
 224  0
         }
 225  0
         return sb.toString();
 226  
     }
 227  
 }