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.Collections;
  26 import java.util.List;
  27 import java.util.function.Function;
  28 import java.util.stream.Stream;
  29 import org.junit.Rule;
  30 import org.junit.Test;
  31 import org.junit.rules.ExpectedException;
  32 import static org.junit.Assert.*;
  33 import org.junit.runner.RunWith;
  34 import org.junit.runners.Parameterized;
  35 
  36 @RunWith(Parameterized.class)
  37 public class DottedVersionTest {
  38 
  39     public DottedVersionTest(boolean greedy) {
  40         this.greedy = greedy;
  41         if (greedy) {
  42             createTestee = DottedVersion::greedy;
  43         } else {
  44             createTestee = DottedVersion::lazy;
  45         }
  46     }
  47 
  48     @Parameterized.Parameters
  49     public static List<Object[]> data() {
  50         return List.of(new Object[] { true }, new Object[] { false });
  51     }
  52 
  53     @Rule
  54     public ExpectedException exceptionRule = ExpectedException.none();
  55 
  56     @Test
  57     public void testValid() {
  58         final List<String> validStrings = List.of(
  59             "1.0",
  60             "1",
  61             "2.234.045",
  62             "2.234.0",
  63             "0",
  64             "0.1"
  65         );
  66 
  67         final List<String> validLazyStrings;
  68         if (greedy) {
  69             validLazyStrings = Collections.emptyList();
  70         } else {
  71             validLazyStrings = List.of(
  72                 "1.-1",
  73                 "5.",
  74                 "4.2.",
  75                 "3..2",
  76                 "2.a",
  77                 "0a",
  78                 ".",
  79                 " ",
  80                 " 1",
  81                 "1. 2",
  82                 "+1",
  83                 "-1",
  84                 "-0",
  85                 "1234567890123456789012345678901234567890"
  86             );
  87         }
  88 
  89         Stream.concat(validStrings.stream(), validLazyStrings.stream())
  90         .forEach(value -> {
  91             DottedVersion version = createTestee.apply(value);
  92             assertEquals(version.toString(), value);
  93         });
  94     }
  95 
  96     @Test
  97     public void testNull() {
  98         exceptionRule.expect(NullPointerException.class);
  99         createTestee.apply(null);
 100     }
 101 
 102     @Test
 103     public void testEmpty() {
 104         if (greedy) {
 105             exceptionRule.expect(IllegalArgumentException.class);
 106             exceptionRule.expectMessage("Version may not be empty string");
 107             createTestee.apply("");
 108         } else {
 109             assertTrue(0 == createTestee.apply("").compareTo(""));
 110             assertTrue(0 == createTestee.apply("").compareTo("0"));
 111         }
 112     }
 113 
 114     private final boolean greedy;
 115     private final Function<String, DottedVersion> createTestee;
 116 }