1 /*
2  * Copyright (c) 2014, 2016, 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 
24 package jdk.test.lib;
25 
26 import java.io.File;
27 import java.io.FileReader;
28 import java.util.Properties;
29 
30 public class BuildHelper {
31 
32     /**
33      * Commercial builds should have the BUILD_TYPE set to commercial
34      * within the release file, found at the root of the JDK.
35      */
isCommercialBuild()36     public static boolean isCommercialBuild() throws Exception {
37         String buildType = getReleaseProperty("BUILD_TYPE","notFound");
38         return buildType.equals("commercial");
39     }
40 
41 
42     /**
43      * Return the value for property key, or defaultValue if no property not found.
44      * If present, double quotes are trimmed.
45      */
getReleaseProperty(String key, String defaultValue)46     public static String getReleaseProperty(String key, String defaultValue) throws Exception {
47         Properties properties = getReleaseProperties();
48         String value = properties.getProperty(key, defaultValue);
49         return trimDoubleQuotes(value);
50     }
51 
52     /**
53      * Return the value for property key, or null if no property not found.
54      * If present, double quotes are trimmed.
55      */
getReleaseProperty(String key)56     public static String getReleaseProperty(String key) throws Exception {
57         return getReleaseProperty(key, null);
58     }
59 
60     /**
61      * Get properties from the release file
62      */
getReleaseProperties()63     public static Properties getReleaseProperties() throws Exception {
64         Properties properties = new Properties();
65         properties.load(new FileReader(getReleaseFile()));
66         return properties;
67     }
68 
69     /**
70      * Every JDK has a release file in its root.
71      * @return A handler to the release file.
72      */
getReleaseFile()73     public static File getReleaseFile() throws Exception {
74         String jdkPath = getJDKRoot();
75         File releaseFile = new File(jdkPath,"release");
76         if ( ! releaseFile.canRead() ) {
77             throw new Exception("Release file is not readable, or it is absent: " +
78                     releaseFile.getCanonicalPath());
79         }
80         return releaseFile;
81     }
82 
83     /**
84      * Returns path to the JDK under test.
85      * This path is obtained through the test.jdk property, usually set by JTREG.
86      */
getJDKRoot()87     public static String getJDKRoot() {
88         String jdkPath = System.getProperty("test.jdk");
89         if (jdkPath == null) {
90             throw new RuntimeException("System property 'test.jdk' not set. This property is normally set by jtreg. "
91                     + "When running test separately, set this property using '-Dtest.jdk=/path/to/jdk'.");
92         }
93         return jdkPath;
94     }
95 
96     /**
97      * Trim double quotes from the beginning and the end of the given string.
98      * @param original string to trim.
99      * @return a new trimmed string.
100      */
trimDoubleQuotes(String original)101     public static String trimDoubleQuotes(String original) {
102         if (original == null) { return null; }
103         String trimmed = original.replaceAll("^\"+|\"+$", "");
104         return trimmed;
105     }
106 }
107