1 /* 2 * Copyright (c) 2008, 2010, 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 29 * @summary Basic Test for ServiceTag.getJavaServiceTag(String) 30 * to verify that the registration.xml and servicetag files 31 * are both created correctly. 32 * @author Mandy Chung 33 * 34 * @run build JavaServiceTagTest1 SvcTagClient Util 35 * @run main JavaServiceTagTest1 36 */ 37 38 import com.sun.servicetag.*; 39 import java.io.*; 40 import java.util.*; 41 42 public class JavaServiceTagTest1 { 43 private static String registrationDir = System.getProperty("test.classes"); 44 private static String servicetagDir = System.getProperty("test.src"); 45 private static File regFile; 46 private static File svcTagFile; 47 private static Registry registry; 48 public static void main(String[] argv) throws Exception { 49 try { 50 registry = Util.getSvcTagClientRegistry(); 51 runTest(); 52 } finally { 53 // restore empty registry file 54 Util.emptyRegistryFile(); 55 } 56 } 57 58 private static void runTest() throws Exception { 59 // cleanup the registration.xml and servicetag file in the test directory 60 System.setProperty("servicetag.dir.path", registrationDir); 61 regFile = new File(registrationDir, "registration.xml"); 62 regFile.delete(); 63 64 svcTagFile = new File(registrationDir, "servicetag"); 65 svcTagFile.delete(); 66 67 // verify that only one service tag is created 68 ServiceTag st1 = testJavaServiceTag("Test1"); 69 70 // getJavaServiceTag method should create a new service tag 71 // and delete the old one 72 ServiceTag st2 = testJavaServiceTag("Test2"); 73 if (registry.getServiceTag(st1.getInstanceURN()) != null) { 74 throw new RuntimeException("instance_urn: " + st1.getInstanceURN() + 75 " exists but expected to be removed"); 76 } 77 78 // expected to have different instance_urn 79 if (st1.getInstanceURN().equals(st2.getInstanceURN())) { 80 throw new RuntimeException("instance_urn: " + st1.getInstanceURN() + 81 " == " + st2.getInstanceURN()); 82 } 83 84 // Delete the service tag from the Registry and the servicetag file 85 if (registry.removeServiceTag(st2.getInstanceURN()) == null) { 86 throw new RuntimeException("Failed to remove " + 87 st1.getInstanceURN() + " from the registry"); 88 } 89 svcTagFile.delete(); 90 91 // call the getJavaServiceTag(String) method again 92 // should create the servicetag file. 93 ServiceTag st3 = testJavaServiceTag("Test2"); 94 if (!Util.matches(st2, st3)) { 95 System.out.println(st2); 96 System.out.println(st3); 97 throw new RuntimeException("Test Failed: Expected to be the same"); 98 } 99 100 } 101 102 private static ServiceTag testJavaServiceTag(String source) throws Exception { 103 ServiceTag svctag = ServiceTag.getJavaServiceTag(source); 104 checkServiceTag(svctag, source); 105 106 // verify if registration.xml is created 107 if (!regFile.exists()) { 108 throw new RuntimeException(regFile + " not created."); 109 } 110 111 // verify the registration.xml content is the expected service tag 112 BufferedInputStream in = new BufferedInputStream(new FileInputStream(regFile)); 113 RegistrationData registration = RegistrationData.loadFromXML(in); 114 Set<ServiceTag> c = registration.getServiceTags(); 115 if (c.size() != 1) { 116 throw new RuntimeException(regFile + " has " + c.size() + 117 " service tags. Expected 1."); 118 } 119 ServiceTag st = registration.getServiceTag(svctag.getInstanceURN()); 120 if (!Util.matches(st, svctag)) { 121 throw new RuntimeException("RegistrationData ServiceTag " + 122 " doesn't match."); 123 } 124 125 // verify the service tag added in the registry 126 st = registry.getServiceTag(svctag.getInstanceURN()); 127 if (!Util.matches(st, svctag)) { 128 throw new RuntimeException("Registry ServiceTag " + 129 " doesn't match."); 130 } 131 132 // verify if servicetag file is created 133 if (!svcTagFile.exists()) { 134 throw new RuntimeException(svcTagFile + " not created."); 135 } 136 137 // verify that the servicetag file only contains one instance_urn 138 BufferedReader reader = new BufferedReader(new FileReader(svcTagFile)); 139 int count = 0; 140 try { 141 String line; 142 while ((line = reader.readLine()) != null) { 143 if (line.equals(svctag.getInstanceURN())) { 144 count++; 145 } else { 146 throw new RuntimeException("servicetag contains " + 147 " unexpected instance_urn " + line); 148 } 149 } 150 } finally { 151 reader.close(); 152 } 153 if (count != 1) { 154 throw new RuntimeException("servicetag contains unexpected " + 155 "number of instance_urn = " + count); 156 } 157 return svctag; 158 } 159 160 private static void checkServiceTag(ServiceTag st, String source) 161 throws IOException { 162 Properties props = loadSwordfishEntries(); 163 if (st.getProductURN(). 164 equals(props.getProperty("servicetag.jdk.urn"))) { 165 if (!st.getProductName(). 166 equals(props.getProperty("servicetag.jdk.name"))) { 167 throw new RuntimeException("Product URN and name don't match."); 168 } 169 } else if (st.getProductURN(). 170 equals(props.getProperty("servicetag.jre.urn"))) { 171 if (!st.getProductName(). 172 equals(props.getProperty("servicetag.jre.name"))) { 173 throw new RuntimeException("Product URN and name don't match."); 174 } 175 } else { 176 throw new RuntimeException("Unexpected product_urn: " + 177 st.getProductURN()); 178 } 179 if (!st.getProductVersion(). 180 equals(System.getProperty("java.version"))) { 181 throw new RuntimeException("Unexpected product_version: " + 182 st.getProductVersion()); 183 } 184 if (!st.getProductParent(). 185 equals(props.getProperty("servicetag.parent.name"))) { 186 throw new RuntimeException("Unexpected product_parent: " + 187 st.getProductParent()); 188 } 189 if (!st.getProductParentURN(). 190 equals(props.getProperty("servicetag.parent.urn"))) { 191 throw new RuntimeException("Unexpected product_parent_urn: " + 192 st.getProductParentURN()); 193 } 194 if (!st.getPlatformArch(). 195 equals(System.getProperty("os.arch"))) { 196 throw new RuntimeException("Unexpected platform_arch: " + 197 st.getPlatformArch()); 198 } 199 200 String vendor = System.getProperty("java.vendor"); 201 if (!st.getProductVendor(). 202 equals(vendor)) { 203 throw new RuntimeException("Unexpected product_vendor: " + 204 st.getProductVendor()); 205 } 206 if (!st.getSource(). 207 equals(source)) { 208 throw new RuntimeException("Unexpected source: " + 209 st.getSource() + " expected: " + source); 210 } 211 String[] ss = st.getProductDefinedInstanceID().split(","); 212 boolean id = false; 213 boolean dir = false; 214 for (String s : ss) { 215 String[] values = s.split("="); 216 if (values[0].equals("id")) { 217 id = true; 218 String[] sss = values[1].split(" "); 219 if (!sss[0].equals(System.getProperty("java.runtime.version"))) { 220 throw new RuntimeException("Unexpected version in id: " + 221 sss[0]); 222 } 223 if (sss.length < 2) { 224 throw new RuntimeException("Unexpected id=" + values[1]); 225 } 226 } else if (values[0].equals("dir")) { 227 dir = true; 228 } 229 } 230 if (!id || !dir) { 231 throw new RuntimeException("Unexpected product_defined_instance_id: " + 232 st.getProductDefinedInstanceID()); 233 } 234 } 235 236 private static Properties loadSwordfishEntries() 237 throws IOException { 238 int version = sun.misc.Version.jdkMinorVersion(); 239 String filename = "/com/sun/servicetag/resources/javase_" + 240 version + "_swordfish.properties"; 241 InputStream in = Installer.class.getClass().getResourceAsStream(filename); 242 Properties props = new Properties(); 243 try { 244 props.load(in); 245 } finally { 246 in.close(); 247 } 248 return props; 249 } 250 }