1 /*
   2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package jdk.incubator.jpackage.internal;
  24 
  25 import java.util.ArrayList;
  26 import java.util.List;
  27 import java.util.function.Function;
  28 import org.junit.Test;
  29 import org.junit.runner.RunWith;
  30 import org.junit.runners.Parameterized;
  31 import org.junit.runners.Parameterized.Parameters;
  32 import static org.junit.Assert.*;
  33 
  34 @RunWith(Parameterized.class)
  35 public class CompareDottedVersionTest {
  36 
  37     public CompareDottedVersionTest(boolean greedy, String version1,
  38             String version2, int result) {
  39         this.version1 = version1;
  40         this.version2 = version2;
  41         this.expectedResult = result;
  42 
  43         if (greedy) {
  44             createTestee = DottedVersion::greedy;
  45         } else {
  46             createTestee = DottedVersion::lazy;
  47         }
  48     }
  49 
  50     @Parameters
  51     public static List<Object[]> data() {
  52         List<Object[]> data = new ArrayList<>();
  53         for (var greedy : List.of(true, false)) {
  54             data.addAll(List.of(new Object[][] {
  55                 { greedy, "00.0.0", "0", 0 },
  56                 { greedy, "0.035", "0.0035", 0 },
  57                 { greedy, "1", "1", 0 },
  58                 { greedy, "2", "2.0", 0 },
  59                 { greedy, "2.00", "2.0", 0 },
  60                 { greedy, "1.2.3.4", "1.2.3.4.5", -1 },
  61                 { greedy, "34", "33", 1 },
  62                 { greedy, "34.0.78", "34.1.78", -1 }
  63             }));
  64         }
  65 
  66         data.addAll(List.of(new Object[][] {
  67             { false, "", "1", -1 },
  68             { false, "1.2.4-R4", "1.2.4-R5", 0 },
  69             { false, "1.2.4.-R4", "1.2.4.R5", 0 },
  70             { false, "7+1", "7+4", 0 },
  71             { false, "2+14", "2-14", 0 },
  72             { false, "23.4.RC4", "23.3.RC10", 1 },
  73             { false, "77.0", "77.99999999999999999999999999999999999999999999999", 0 },
  74         }));
  75 
  76         return data;
  77     }
  78 
  79     @Test
  80     public void testIt() {
  81         int actualResult = compare(version1, version2);
  82         assertEquals(expectedResult, actualResult);
  83 
  84         int actualNegateResult = compare(version2, version1);
  85         assertEquals(actualResult, -1 * actualNegateResult);
  86     }
  87 
  88     private int compare(String x, String y) {
  89         int result = createTestee.apply(x).compareTo(y);
  90 
  91         if (result < 0) {
  92             return -1;
  93         }
  94 
  95         if (result > 0) {
  96             return 1;
  97         }
  98 
  99         return 0;
 100     }
 101 
 102     private final String version1;
 103     private final String version2;
 104     private final int expectedResult;
 105     private final Function<String, DottedVersion> createTestee;
 106 }