Coverage Report - org.owasp.dependencycheck.utils.DependencyVersion
 
Classes in this File Line Coverage Branch Coverage Complexity
DependencyVersion
90%
80/88
76%
49/64
5.273
 
 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) 2013 Jeremy Long. All Rights Reserved.
 17  
  */
 18  
 package org.owasp.dependencycheck.utils;
 19  
 
 20  
 import java.util.ArrayList;
 21  
 import java.util.Iterator;
 22  
 import java.util.List;
 23  
 import java.util.regex.Matcher;
 24  
 import java.util.regex.Pattern;
 25  
 import org.apache.commons.lang.StringUtils;
 26  
 
 27  
 /**
 28  
  * <p>
 29  
  * Simple object to track the parts of a version number. The parts are contained in a List such that version 1.2.3 will
 30  
  * be stored as:  <code>versionParts[0] = 1;
 31  
  * versionParts[1] = 2;
 32  
  * versionParts[2] = 3;
 33  
  * </code></p>
 34  
  * <p>
 35  
  * Note, the parser contained in this class expects the version numbers to be separated by periods. If a different
 36  
  * separator is used the parser will likely fail.</p>
 37  
  *
 38  
  * @author Jeremy Long <jeremy.long@owasp.org>
 39  
  */
 40  14
 public class DependencyVersion implements Iterable, Comparable<DependencyVersion> {
 41  
 
 42  
     /**
 43  
      * Constructor for a empty DependencyVersion.
 44  
      */
 45  2
     public DependencyVersion() {
 46  2
     }
 47  
 
 48  
     /**
 49  
      * Constructor for a DependencyVersion that will parse a version string.
 50  
      * <b>Note</b>, this should only be used when the version passed in is already known to be a well formatted version
 51  
      * number. Otherwise, DependencyVersionUtil.parseVersion() should be used instead.
 52  
      *
 53  
      * @param version the well formatted version number to parse
 54  
      */
 55  786
     public DependencyVersion(String version) {
 56  786
         parseVersion(version);
 57  786
     }
 58  
 
 59  
     /**
 60  
      * Parses a version string into its sub parts: major, minor, revision, build, etc. <b>Note</b>, this should only be
 61  
      * used to parse something that is already known to be a version number.
 62  
      *
 63  
      * @param version the version string to parse
 64  
      */
 65  
     public final void parseVersion(String version) {
 66  788
         versionParts = new ArrayList<String>();
 67  788
         if (version != null) {
 68  788
             final Pattern rx = Pattern.compile("(\\d+[a-z]{1,3}$|[a-z]+\\d+|\\d+|(release|beta|alpha)$)");
 69  788
             final Matcher matcher = rx.matcher(version.toLowerCase());
 70  3334
             while (matcher.find()) {
 71  2546
                 versionParts.add(matcher.group());
 72  
             }
 73  788
             if (versionParts.isEmpty()) {
 74  5
                 versionParts.add(version);
 75  
             }
 76  
         }
 77  788
     }
 78  
     /**
 79  
      * A list of the version parts.
 80  
      */
 81  
     private List<String> versionParts;
 82  
 
 83  
     /**
 84  
      * Get the value of versionParts.
 85  
      *
 86  
      * @return the value of versionParts
 87  
      */
 88  
     public List<String> getVersionParts() {
 89  1340
         return versionParts;
 90  
     }
 91  
 
 92  
     /**
 93  
      * Set the value of versionParts.
 94  
      *
 95  
      * @param versionParts new value of versionParts
 96  
      */
 97  
     public void setVersionParts(List<String> versionParts) {
 98  1
         this.versionParts = versionParts;
 99  1
     }
 100  
 
 101  
     /**
 102  
      * Retrieves an iterator for the version parts.
 103  
      *
 104  
      * @return an iterator for the version parts
 105  
      */
 106  
     public Iterator iterator() {
 107  1
         return versionParts.iterator();
 108  
     }
 109  
 
 110  
     /**
 111  
      * Reconstructs the version string from the split version parts.
 112  
      *
 113  
      * @return a string representing the version.
 114  
      */
 115  
     @Override
 116  
     public String toString() {
 117  256
         return StringUtils.join(versionParts.toArray(), ".");
 118  
     }
 119  
 
 120  
     /**
 121  
      * Compares the equality of this object to the one passed in as a parameter.
 122  
      *
 123  
      * @param obj the object to compare equality
 124  
      * @return returns true only if the two objects are equal, otherwise false
 125  
      */
 126  
     @Override
 127  
     public boolean equals(Object obj) {
 128  480
         if (obj == null) {
 129  0
             return false;
 130  
         }
 131  480
         if (getClass() != obj.getClass()) {
 132  0
             return false;
 133  
         }
 134  480
         final DependencyVersion other = (DependencyVersion) obj;
 135  480
         final int max = (this.versionParts.size() < other.versionParts.size())
 136  
                 ? this.versionParts.size() : other.versionParts.size();
 137  
         //TODO steal better version of code from compareTo
 138  1133
         for (int i = 0; i < max; i++) {
 139  1002
             final String thisPart = this.versionParts.get(i);
 140  1002
             final String otherPart = other.versionParts.get(i);
 141  1002
             if (!thisPart.equals(otherPart)) {
 142  349
                 return false;
 143  
             }
 144  
         }
 145  131
         if (this.versionParts.size() > max) {
 146  0
             for (int i = max; i < this.versionParts.size(); i++) {
 147  0
                 if (!"0".equals(this.versionParts.get(i))) {
 148  0
                     return false;
 149  
                 }
 150  
             }
 151  
         }
 152  
 
 153  131
         if (other.versionParts.size() > max) {
 154  107
             for (int i = max; i < other.versionParts.size(); i++) {
 155  107
                 if (!"0".equals(other.versionParts.get(i))) {
 156  107
                     return false;
 157  
                 }
 158  
             }
 159  
         }
 160  
 
 161  
         /*
 162  
          *  if (this.versionParts != other.versionParts && (this.versionParts == null || !this.versionParts.equals(other.versionParts))) {
 163  
          *      return false;
 164  
          *  }
 165  
          */
 166  24
         return true;
 167  
     }
 168  
 
 169  
     /**
 170  
      * Calculates the hashCode for this object.
 171  
      *
 172  
      * @return the hashCode
 173  
      */
 174  
     @Override
 175  
     public int hashCode() {
 176  1
         int hash = 5;
 177  1
         hash = 71 * hash + (this.versionParts != null ? this.versionParts.hashCode() : 0);
 178  1
         return hash;
 179  
     }
 180  
 
 181  
     /**
 182  
      * Determines if the three most major major version parts are identical. For instances, if version 1.2.3.4 was
 183  
      * compared to 1.2.3 this function would return true.
 184  
      *
 185  
      * @param version the version number to compare
 186  
      * @return true if the first three major parts of the version are identical
 187  
      */
 188  
     public boolean matchesAtLeastThreeLevels(DependencyVersion version) {
 189  304
         if (version == null) {
 190  0
             return false;
 191  
         }
 192  304
         if (Math.abs(this.versionParts.size() - version.versionParts.size()) >= 3) {
 193  1
             return false;
 194  
         }
 195  
 
 196  303
         final int max = (this.versionParts.size() < version.versionParts.size())
 197  
                 ? this.versionParts.size() : version.versionParts.size();
 198  
 
 199  303
         boolean ret = true;
 200  652
         for (int i = 0; i < max; i++) {
 201  586
             final String thisVersion = this.versionParts.get(i);
 202  586
             final String otherVersion = version.getVersionParts().get(i);
 203  586
             if (i >= 3) {
 204  2
                 if (thisVersion.compareToIgnoreCase(otherVersion) >= 0) {
 205  1
                     ret = false;
 206  1
                     break;
 207  
                 }
 208  584
             } else if (!thisVersion.equals(otherVersion)) {
 209  236
                 ret = false;
 210  236
                 break;
 211  
             }
 212  
         }
 213  
 
 214  303
         return ret;
 215  
     }
 216  
 
 217  
     @Override
 218  
     public int compareTo(DependencyVersion version) {
 219  30
         if (version == null) {
 220  0
             return 1;
 221  
         }
 222  30
         final List<String> left = this.getVersionParts();
 223  30
         final List<String> right = version.getVersionParts();
 224  30
         final int max = left.size() < right.size() ? left.size() : right.size();
 225  
 
 226  79
         for (int i = 0; i < max; i++) {
 227  69
             final String lStr = left.get(i);
 228  69
             final String rStr = right.get(i);
 229  69
             if (lStr.equals(rStr)) {
 230  49
                 continue;
 231  
             }
 232  
             try {
 233  20
                 final int l = Integer.parseInt(lStr);
 234  13
                 final int r = Integer.parseInt(rStr);
 235  12
                 if (l < r) {
 236  9
                     return -1;
 237  3
                 } else if (l > r) {
 238  3
                     return 1;
 239  
                 }
 240  8
             } catch (NumberFormatException ex) {
 241  8
                 final int comp = left.get(i).compareTo(right.get(i));
 242  8
                 if (comp < 0) {
 243  6
                     return -1;
 244  2
                 } else if (comp > 0) {
 245  2
                     return 1;
 246  
                 }
 247  0
             }
 248  
         }
 249  10
         if (left.size() < right.size()) {
 250  3
             return -1;
 251  7
         } else if (left.size() > right.size()) {
 252  3
             return 1;
 253  
         } else {
 254  4
             return 0;
 255  
         }
 256  
     }
 257  
 }