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