1 /* 2 * Copyright (c) 2008, 2011, 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 26 /* 27 * @test 28 * @bug 6622366 7078024 29 * @summary Basic Test for ServiceTag.getJavaServiceTag() 30 * Disable creating the service tag in the system registry. 31 * Verify the existence of registration.xml file and the 32 * content of the service tag. 33 * @author Mandy Chung 34 * 35 * @run build JavaServiceTagTest 36 * @run main JavaServiceTagTest 37 */ 38 39 import com.sun.servicetag.*; 40 import java.io.*; 41 import java.util.*; 42 43 public class JavaServiceTagTest { 44 public static void main(String[] argv) throws Exception { 45 String registrationDir = System.getProperty("test.classes"); 46 47 // disable calling to stclient 48 System.setProperty("servicetag.sthelper.supported", "false"); 49 50 if (Registry.isSupported()) { 51 throw new RuntimeException("Registry.isSupported() should " + 52 "return false"); 53 } 54 // For debugging 55 // System.setProperty("servicetag.verbose", ""); 56 57 // cleanup the registration.xml and servicetag file in the test directory 58 System.setProperty("servicetag.dir.path", registrationDir); 59 File regFile = new File(registrationDir, "registration.xml"); 60 regFile.delete(); 61 File svcTagFile = new File(registrationDir, "servicetag"); 62 svcTagFile.delete(); 63 64 ServiceTag svctag = ServiceTag.getJavaServiceTag("JavaServiceTagTest"); 65 checkServiceTag(svctag); 66 67 if (svcTagFile.exists()) { 68 throw new RuntimeException(svcTagFile + " should not exist."); 69 } 70 71 // registration.xml should be created 72 if (!regFile.exists()) { 73 throw new RuntimeException(regFile + " not created."); 74 } 75 BufferedInputStream in = new BufferedInputStream(new FileInputStream(regFile)); 76 RegistrationData registration = RegistrationData.loadFromXML(in); 77 Set<ServiceTag> c = registration.getServiceTags(); 78 if (c.size() != 1) { 79 throw new RuntimeException(regFile + " has " + c.size() + 80 " service tags. Expected 1."); 81 } 82 ServiceTag st = registration.getServiceTag(svctag.getInstanceURN()); 83 if (!Util.matches(st, svctag)) { 84 throw new RuntimeException("ServiceTag " + 85 " doesn't match."); 86 } 87 } 88 89 /** 90 * Tests if the running platform is a JDK. 91 */ 92 static boolean isJDK() { 93 // Determine the JRE path by checking the existence of 94 // <HOME>/jre/lib and <HOME>/lib. 95 String javaHome = System.getProperty("java.home"); 96 String jrepath = javaHome + File.separator + "jre"; 97 File f = new File(jrepath, "lib"); 98 if (!f.exists()) { 99 // java.home usually points to the JRE path 100 jrepath = javaHome; 101 } 102 103 return jrepath.endsWith(File.separator + "jre"); 104 } 105 106 private static void checkServiceTag(ServiceTag st) throws IOException { 107 Properties props = loadServiceTagProps(); 108 // jdk 8 and later, JDK and JRE have the same product URN. 109 String jdkUrn = props.getProperty("servicetag.jdk.urn"); 110 String jreUrn = props.getProperty("servicetag.jre.urn"); 111 boolean isJdk = isJDK(); 112 113 if (isJdk) { 114 if (!st.getProductURN().equals(jdkUrn) || 115 !st.getProductName().equals( 116 props.getProperty("servicetag.jdk.name"))) { 117 throw new RuntimeException("Product URN and name don't match."); 118 } 119 } else { 120 if (!st.getProductURN().equals(jreUrn) || 121 !st.getProductName().equals( 122 props.getProperty("servicetag.jre.name"))) { 123 throw new RuntimeException("Product URN and name don't match."); 124 } 125 } 126 if (!st.getProductVersion(). 127 equals(System.getProperty("java.version"))) { 128 throw new RuntimeException("Unexpected product_version: " + 129 st.getProductVersion()); 130 } 131 if (!st.getProductParent(). 132 equals(props.getProperty("servicetag.parent.name"))) { 133 throw new RuntimeException("Unexpected product_parent: " + 134 st.getProductParent()); 135 } 136 if (!st.getProductParentURN(). 137 equals(props.getProperty("servicetag.parent.urn"))) { 138 throw new RuntimeException("Unexpected product_parent_urn: " + 139 st.getProductParentURN()); 140 } 141 if (!st.getPlatformArch(). 142 equals(System.getProperty("os.arch"))) { 143 throw new RuntimeException("Unexpected platform_arch: " + 144 st.getPlatformArch()); 145 } 146 String vendor = System.getProperty("java.vendor"); 147 if (!st.getProductVendor(). 148 equals(vendor)) { 149 throw new RuntimeException("Unexpected product_vendor: " + 150 st.getProductVendor()); 151 } 152 if (!st.getSource(). 153 equals("JavaServiceTagTest")) { 154 throw new RuntimeException("Unexpected source: " + 155 st.getSource()); 156 } 157 String[] ss = st.getProductDefinedInstanceID().split(","); 158 boolean id = false; 159 boolean dir = false; 160 for (String s : ss) { 161 String[] values = s.split("="); 162 if (values[0].equals("id")) { 163 id = true; 164 String[] sss = values[1].split(" "); 165 if (!sss[0].equals(System.getProperty("java.runtime.version"))) { 166 throw new RuntimeException("Unexpected version in id: " + 167 sss[0]); 168 } 169 if (sss.length < 2) { 170 throw new RuntimeException("Unexpected id=" + values[1]); 171 } 172 } else if (values[0].equals("dir")) { 173 dir = true; 174 } 175 } 176 if (!id || !dir) { 177 throw new RuntimeException("Unexpected product_defined_instance_id: " + 178 st.getProductDefinedInstanceID()); 179 } 180 } 181 182 private static Properties loadServiceTagProps() 183 throws IOException { 184 String filename = "/com/sun/servicetag/resources/javase_servicetag.properties"; 185 try (InputStream in = Installer.class.getClass().getResourceAsStream(filename)) { 186 Properties props = new Properties(); 187 props.load(in); 188 return props; 189 } 190 } 191 }