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.incubator.jpackage.internal; 26 27 import java.nio.file.Path; 28 import static org.hamcrest.CoreMatchers.is; 29 import static org.hamcrest.CoreMatchers.not; 30 import static org.junit.Assert.*; 31 import org.junit.Test; 32 33 34 public class ToolValidatorTest { 35 36 @Test 37 public void testAvailable() { 38 assertNull(new ToolValidator(TOOL_JAVA).validate()); 39 } 40 41 @Test 42 public void testNotAvailable() { 43 assertValidationFailure(new ToolValidator(TOOL_UNKNOWN).validate(), true); 44 } 45 46 @Test 47 public void testVersionParserUsage() { 48 // Without minimal version configured, version parser should not be used 49 new ToolValidator(TOOL_JAVA).setVersionParser(unused -> { 50 throw new RuntimeException(); 51 }).validate(); 52 53 // Minimal version is 1, actual is 10. Should be OK. 54 assertNull(new ToolValidator(TOOL_JAVA).setMinimalVersion( 55 new DottedVersion("1")).setVersionParser(unused -> "10").validate()); 56 57 // Minimal version is 5, actual is 4.99.37. Error expected. 58 assertValidationFailure(new ToolValidator(TOOL_JAVA).setMinimalVersion( 59 new DottedVersion("5")).setVersionParser(unused -> "4.99.37").validate(), 60 false); 61 62 // Minimal version is 8, actual is 10, lexicographical comparison is used. Error expected. 63 assertValidationFailure(new ToolValidator(TOOL_JAVA).setMinimalVersion( 64 "8").setVersionParser(unused -> "10").validate(), false); 65 66 // Minimal version is 8, actual is 10, Use DottedVersion class for comparison. Should be OK. 67 assertNull(new ToolValidator(TOOL_JAVA).setMinimalVersion( 68 new DottedVersion("8")).setVersionParser(unused -> "10").validate()); 69 } 70 71 private static void assertValidationFailure(ConfigException v, 72 boolean withCause) { 73 assertNotNull(v); 74 assertThat("", is(not(v.getMessage().strip()))); 75 assertThat("", is(not(v.advice.strip()))); 76 if (withCause) { 77 assertNotNull(v.getCause()); 78 } else { 79 assertNull(v.getCause()); 80 } 81 } 82 83 private final static String TOOL_JAVA; 84 private final static String TOOL_UNKNOWN = Path.of(System.getProperty( 85 "java.home"), "bin").toString(); 86 87 static { 88 String fname = "java"; 89 if (Platform.isWindows()) { 90 fname = fname + ".exe"; 91 } 92 TOOL_JAVA = Path.of(System.getProperty("java.home"), "bin", fname).toString(); 93 } 94 }