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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package jdk.jpackage.internal;
  26 
  27 import java.nio.file.Path;
  28 import java.util.Collections;
  29 import java.util.List;
  30 import java.util.Map;
  31 import static org.hamcrest.CoreMatchers.equalTo;
  32 import static org.hamcrest.CoreMatchers.not;
  33 import static org.junit.Assert.*;
  34 import org.junit.Test;
  35 
  36 
  37 public class PathGroupTest {
  38     public PathGroupTest() {
  39     }
  40 
  41     @Test(expected = NullPointerException.class)
  42     public void testNullId() {
  43          new PathGroup(Map.of()).getPath(null);
  44     }
  45 
  46     @Test
  47     public void testEmptyPathGroup() {
  48         PathGroup pg = new PathGroup(Map.of());
  49 
  50         assertNull(pg.getPath("foo"));
  51 
  52         assertEquals(0, pg.paths().size());
  53         assertEquals(0, pg.roots().size());
  54     }
  55 
  56     @Test
  57     public void testRootsSinglePath() {
  58         final PathGroup pg = new PathGroup(Map.of("main", PATH_FOO));
  59 
  60         List<Path> paths = pg.paths();
  61         assertEquals(1, paths.size());
  62         assertEquals(PATH_FOO, paths.iterator().next());
  63 
  64         List<Path> roots = pg.roots();
  65         assertEquals(1, roots.size());
  66         assertEquals(PATH_FOO, roots.iterator().next());
  67     }
  68 
  69     @Test
  70     public void testDuplicatedRoots() {
  71         final PathGroup pg = new PathGroup(Map.of("main", PATH_FOO, "another",
  72                 PATH_FOO, "root", PATH_EMPTY));
  73 
  74         List<Path> paths = pg.paths();
  75         Collections.sort(paths);
  76 
  77         assertEquals(3, paths.size());
  78         assertEquals(PATH_EMPTY, paths.get(0));
  79         assertEquals(PATH_FOO, paths.get(1));
  80         assertEquals(PATH_FOO, paths.get(2));
  81 
  82         List<Path> roots = pg.roots();
  83         assertEquals(1, roots.size());
  84         assertEquals(PATH_EMPTY, roots.get(0));
  85     }
  86 
  87     @Test
  88     public void testRoots() {
  89         final PathGroup pg = new PathGroup(Map.of(1, Path.of("foo"), 2, Path.of(
  90                 "foo", "bar"), 3, Path.of("foo", "bar", "buz")));
  91 
  92         List<Path> paths = pg.paths();
  93         assertEquals(3, paths.size());
  94         assertTrue(paths.contains(Path.of("foo")));
  95         assertTrue(paths.contains(Path.of("foo", "bar")));
  96         assertTrue(paths.contains(Path.of("foo", "bar", "buz")));
  97 
  98         List<Path> roots = pg.roots();
  99         assertEquals(1, roots.size());
 100         assertEquals(Path.of("foo"), roots.get(0));
 101     }
 102 
 103     @Test
 104     public void testResolveAt() {
 105         final PathGroup pg = new PathGroup(Map.of(0, PATH_FOO, 1, PATH_BAR, 2,
 106                 PATH_EMPTY));
 107 
 108         final Path aPath = Path.of("a");
 109 
 110         final PathGroup pg2 = pg.resolveAt(aPath);
 111         assertThat(pg, not(equalTo(pg2)));
 112 
 113         List<Path> paths = pg.paths();
 114         assertEquals(3, paths.size());
 115         assertTrue(paths.contains(PATH_EMPTY));
 116         assertTrue(paths.contains(PATH_FOO));
 117         assertTrue(paths.contains(PATH_BAR));
 118         assertEquals(PATH_EMPTY, pg.roots().get(0));
 119 
 120         paths = pg2.paths();
 121         assertEquals(3, paths.size());
 122         assertTrue(paths.contains(aPath.resolve(PATH_EMPTY)));
 123         assertTrue(paths.contains(aPath.resolve(PATH_FOO)));
 124         assertTrue(paths.contains(aPath.resolve(PATH_BAR)));
 125         assertEquals(aPath, pg2.roots().get(0));
 126     }
 127 
 128     private final static Path PATH_FOO = Path.of("foo");
 129     private final static Path PATH_BAR = Path.of("bar");
 130     private final static Path PATH_EMPTY = Path.of("");
 131 }