Coverage Report - org.owasp.dependencycheck.dependency.Evidence
 
Classes in this File Line Coverage Branch Coverage Complexity
Evidence
66%
35/53
36%
27/74
3.381
 
 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) 2012 Jeremy Long. All Rights Reserved.
 17  
  */
 18  
 package org.owasp.dependencycheck.dependency;
 19  
 
 20  
 import java.io.Serializable;
 21  
 
 22  
 /**
 23  
  * Evidence is a piece of information about a Dependency.
 24  
  *
 25  
  * @author Jeremy Long <jeremy.long@owasp.org>
 26  
  */
 27  37400
 public class Evidence implements Serializable, Comparable<Evidence> {
 28  
 
 29  
     /**
 30  
      * Creates a new Evidence object.
 31  
      */
 32  
     public Evidence() {
 33  
     }
 34  
 
 35  
     /**
 36  
      * Creates a new Evidence objects.
 37  
      *
 38  
      * @param source the source of the evidence.
 39  
      * @param name the name of the evidence.
 40  
      * @param value the value of the evidence.
 41  
      * @param confidence the confidence of the evidence.
 42  
      */
 43  
     public Evidence(String source, String name, String value, Confidence confidence) {
 44  
         this.source = source;
 45  
         this.name = name;
 46  
         this.value = value;
 47  
         this.confidence = confidence;
 48  
     }
 49  
     /**
 50  
      * The name of the evidence.
 51  
      */
 52  
     private String name;
 53  
 
 54  
     /**
 55  
      * Get the value of name.
 56  
      *
 57  
      * @return the value of name
 58  
      */
 59  
     public String getName() {
 60  
         return name;
 61  
     }
 62  
 
 63  
     /**
 64  
      * Set the value of name.
 65  
      *
 66  
      * @param name new value of name
 67  
      */
 68  
     public void setName(String name) {
 69  
         this.name = name;
 70  
     }
 71  
     /**
 72  
      * The source of the evidence.
 73  
      */
 74  
     private String source;
 75  
 
 76  
     /**
 77  
      * Get the value of source.
 78  
      *
 79  
      * @return the value of source
 80  
      */
 81  
     public String getSource() {
 82  
         return source;
 83  
     }
 84  
 
 85  
     /**
 86  
      * Set the value of source.
 87  
      *
 88  
      * @param source new value of source
 89  
      */
 90  
     public void setSource(String source) {
 91  
         this.source = source;
 92  
     }
 93  
     /**
 94  
      * The value of the evidence.
 95  
      */
 96  
     private String value;
 97  
 
 98  
     /**
 99  
      * Get the value of value.
 100  
      *
 101  
      * @return the value of value
 102  
      */
 103  
     public String getValue() {
 104  377
         used = true;
 105  377
         return value;
 106  
     }
 107  
 
 108  
     /**
 109  
      * Get the value of value. If setUsed is set to false this call to get will not mark the evidence as used.
 110  
      *
 111  
      * @param setUsed whether or not this call to getValue should cause the used flag to be updated
 112  
      * @return the value of value
 113  
      */
 114  
     public String getValue(Boolean setUsed) {
 115  54
         used = used || setUsed;
 116  54
         return value;
 117  
     }
 118  
 
 119  
     /**
 120  
      * Set the value of value.
 121  
      *
 122  
      * @param value new value of value
 123  
      */
 124  
     public void setValue(String value) {
 125  
         this.value = value;
 126  
     }
 127  
     /**
 128  
      * A value indicating if the Evidence has been "used" (aka read).
 129  
      */
 130  
     private boolean used;
 131  
 
 132  
     /**
 133  
      * Get the value of used.
 134  
      *
 135  
      * @return the value of used
 136  
      */
 137  
     public boolean isUsed() {
 138  
         return used;
 139  
     }
 140  
 
 141  
     /**
 142  
      * Set the value of used.
 143  
      *
 144  
      * @param used new value of used
 145  
      */
 146  
     public void setUsed(boolean used) {
 147  
         this.used = used;
 148  
     }
 149  
     /**
 150  
      * The confidence level for the evidence.
 151  
      */
 152  
     private Confidence confidence;
 153  
 
 154  
     /**
 155  
      * Get the value of confidence.
 156  
      *
 157  
      * @return the value of confidence
 158  
      */
 159  
     public Confidence getConfidence() {
 160  
         return confidence;
 161  
     }
 162  
 
 163  
     /**
 164  
      * Set the value of confidence.
 165  
      *
 166  
      * @param confidence new value of confidence
 167  
      */
 168  
     public void setConfidence(Confidence confidence) {
 169  
         this.confidence = confidence;
 170  
     }
 171  
 
 172  
     /**
 173  
      * Implements the hashCode for Evidence.
 174  
      *
 175  
      * @return hash code.
 176  
      */
 177  
     @Override
 178  
     public int hashCode() {
 179  5
         int hash = 3;
 180  5
         hash = 67 * hash + (this.name != null ? this.name.hashCode() : 0);
 181  5
         hash = 67 * hash + (this.source != null ? this.source.hashCode() : 0);
 182  5
         hash = 67 * hash + (this.value != null ? this.value.hashCode() : 0);
 183  5
         hash = 67 * hash + (this.confidence != null ? this.confidence.hashCode() : 0);
 184  5
         return hash;
 185  
     }
 186  
 
 187  
     /**
 188  
      * Implements equals for Evidence.
 189  
      *
 190  
      * @param that an object to check the equality of.
 191  
      * @return whether the two objects are equal.
 192  
      */
 193  
     @Override
 194  
     public boolean equals(Object that) {
 195  0
         if (this == that) {
 196  0
             return true;
 197  
         }
 198  0
         if (!(that instanceof Evidence)) {
 199  0
             return false;
 200  
         }
 201  0
         final Evidence e = (Evidence) that;
 202  
 
 203  0
         return testEquality(name, e.name) && testEquality(source, e.source) && testEquality(value, e.value)
 204  
                 && (confidence == null ? e.confidence == null : confidence == e.confidence);
 205  
     }
 206  
 
 207  
     /**
 208  
      * Simple equality test for use within the equals method. This does a case insensitive compare.
 209  
      *
 210  
      * @param l a string to compare.
 211  
      * @param r another string to compare.
 212  
      * @return whether the two strings are the same.
 213  
      */
 214  
     private boolean testEquality(String l, String r) {
 215  0
         return l == null ? r == null : l.equalsIgnoreCase(r);
 216  
     }
 217  
 
 218  
     /**
 219  
      * Implementation of the comparable interface.
 220  
      *
 221  
      * @param o the evidence being compared
 222  
      * @return an integer indicating the ordering of the two objects
 223  
      */
 224  
     public int compareTo(Evidence o) {
 225  37400
         if (o == null) {
 226  0
             return 1;
 227  
         }
 228  37400
         if (equalsWithNullCheck(source, o.source)) {
 229  23688
             if (equalsWithNullCheck(name, o.name)) {
 230  23581
                 if (equalsWithNullCheck(value, o.value)) {
 231  14656
                     if (equalsWithNullCheck(confidence, o.confidence)) {
 232  14643
                         return 0; //they are equal
 233  
                     } else {
 234  13
                         return compareToWithNullCheck(confidence, o.confidence);
 235  
                     }
 236  
                 } else {
 237  8925
                     return compareToIgnoreCaseWithNullCheck(value, o.value);
 238  
                 }
 239  
             } else {
 240  107
                 return compareToIgnoreCaseWithNullCheck(name, o.name);
 241  
             }
 242  
         } else {
 243  13712
             return compareToIgnoreCaseWithNullCheck(source, o.source);
 244  
         }
 245  
     }
 246  
 
 247  
     /**
 248  
      * Equality check with an exhaustive, possibly duplicative, check against nulls.
 249  
      *
 250  
      * @param me the value to be compared
 251  
      * @param other the other value to be compared
 252  
      * @return true if the values are equal; otherwise false
 253  
      */
 254  
     private boolean equalsWithNullCheck(String me, String other) {
 255  84669
         if (me == null && other == null) {
 256  0
             return true;
 257  84669
         } else if (me == null || other == null) {
 258  0
             return false;
 259  
         }
 260  84669
         return me.equals(other);
 261  
     }
 262  
 
 263  
     /**
 264  
      * Equality check with an exhaustive, possibly duplicative, check against nulls.
 265  
      *
 266  
      * @param me the value to be compared
 267  
      * @param other the other value to be compared
 268  
      * @return true if the values are equal; otherwise false
 269  
      */
 270  
     private boolean equalsWithNullCheck(Confidence me, Confidence other) {
 271  14656
         if (me == null && other == null) {
 272  0
             return true;
 273  14656
         } else if (me == null || other == null) {
 274  0
             return false;
 275  
         }
 276  14656
         return me.equals(other);
 277  
     }
 278  
 
 279  
     /**
 280  
      * Wrapper around {@link java.lang.String#compareToIgnoreCase(java.lang.String) String.compareToIgnoreCase} with an
 281  
      * exhaustive, possibly duplicative, check against nulls.
 282  
      *
 283  
      * @param me the value to be compared
 284  
      * @param other the other value to be compared
 285  
      * @return true if the values are equal; otherwise false
 286  
      */
 287  
     private int compareToIgnoreCaseWithNullCheck(String me, String other) {
 288  22744
         if (me == null && other == null) {
 289  0
             return 0;
 290  22744
         } else if (me == null) {
 291  0
             return -1; //the other string is greater then me
 292  22744
         } else if (other == null) {
 293  0
             return 1; //me is greater then the other string
 294  
         }
 295  22744
         return me.compareToIgnoreCase(other);
 296  
     }
 297  
 
 298  
     /**
 299  
      * Wrapper around {@link java.lang.Enum#compareTo(java.lang.Enum) Enum.compareTo} with an exhaustive, possibly
 300  
      * duplicative, check against nulls.
 301  
      *
 302  
      * @param me the value to be compared
 303  
      * @param other the other value to be compared
 304  
      * @return true if the values are equal; otherwise false
 305  
      */
 306  
     private int compareToWithNullCheck(Confidence me, Confidence other) {
 307  13
         if (me == null && other == null) {
 308  0
             return 0;
 309  13
         } else if (me == null) {
 310  0
             return -1; //the other string is greater then me
 311  13
         } else if (other == null) {
 312  0
             return 1; //me is greater then the other string
 313  
         }
 314  13
         return me.compareTo(other);
 315  
     }
 316  
 }