jdk/test/com/sun/servicetag/Util.java
changeset 16802 ea3325542aa8
parent 16801 e2de240b437f
parent 16575 d7ad0dfaa411
child 16803 3bdc22a32b0e
equal deleted inserted replaced
16801:e2de240b437f 16802:ea3325542aa8
     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  * @bug     6622366
       
    28  * @summary Utility class used by other jtreg tests
       
    29  */
       
    30 
       
    31 import com.sun.servicetag.RegistrationData;
       
    32 import com.sun.servicetag.ServiceTag;
       
    33 import com.sun.servicetag.Registry;
       
    34 
       
    35 import java.util.Set;
       
    36 import java.util.Date;
       
    37 import java.util.Map;
       
    38 import java.util.Properties;
       
    39 import java.util.TimeZone;
       
    40 import java.text.ParseException;
       
    41 import java.text.SimpleDateFormat;
       
    42 import java.io.*;
       
    43 
       
    44 public class Util {
       
    45     public static ServiceTag newServiceTag(File f)
       
    46             throws FileNotFoundException, IOException, NumberFormatException {
       
    47         return newServiceTag(f, false /* with instance_urn */);
       
    48     }
       
    49 
       
    50     public static ServiceTag newServiceTag(File f, boolean noInstanceURN)
       
    51             throws FileNotFoundException, IOException, NumberFormatException {
       
    52         Properties props = new Properties();
       
    53         FileReader reader = new FileReader(f);
       
    54         try {
       
    55             props.load(reader);
       
    56         } finally {
       
    57             reader.close();
       
    58         }
       
    59         if (noInstanceURN) {
       
    60             return ServiceTag.newInstance(
       
    61                             props.getProperty("product_name"),
       
    62                             props.getProperty("product_version"),
       
    63                             props.getProperty("product_urn"),
       
    64                             props.getProperty("product_parent"),
       
    65                             props.getProperty("product_parent_urn"),
       
    66                             props.getProperty("product_defined_inst_id"),
       
    67                             props.getProperty("product_vendor"),
       
    68                             props.getProperty("platform_arch"),
       
    69                             props.getProperty("container"),
       
    70                             props.getProperty("source"));
       
    71         } else {
       
    72             return ServiceTag.newInstance(
       
    73                             props.getProperty("instance_urn"),
       
    74                             props.getProperty("product_name"),
       
    75                             props.getProperty("product_version"),
       
    76                             props.getProperty("product_urn"),
       
    77                             props.getProperty("product_parent"),
       
    78                             props.getProperty("product_parent_urn"),
       
    79                             props.getProperty("product_defined_inst_id"),
       
    80                             props.getProperty("product_vendor"),
       
    81                             props.getProperty("platform_arch"),
       
    82                             props.getProperty("container"),
       
    83                             props.getProperty("source"));
       
    84         }
       
    85     }
       
    86 
       
    87     public static boolean matches(ServiceTag st1, ServiceTag st2) {
       
    88         if (!st1.getInstanceURN().equals(st2.getInstanceURN())) {
       
    89             System.out.println("instance_urn: " + st1.getInstanceURN() +
       
    90                 " != " + st2.getInstanceURN());
       
    91             return false;
       
    92         }
       
    93         return matchesNoInstanceUrn(st1, st2);
       
    94     }
       
    95 
       
    96     public static boolean matchesNoInstanceUrn(ServiceTag st1, ServiceTag st2) {
       
    97         if (!st1.getProductName().equals(st2.getProductName())) {
       
    98             System.out.println("product_name: " + st1.getProductName() +
       
    99                 " != " + st2.getProductName());
       
   100             return false;
       
   101         }
       
   102 
       
   103         if (!st1.getProductVersion().equals(st2.getProductVersion())) {
       
   104             System.out.println("product_version: " + st1.getProductVersion() +
       
   105                 " != " + st2.getProductVersion());
       
   106             return false;
       
   107         }
       
   108         if (!st1.getProductURN().equals(st2.getProductURN())) {
       
   109             System.out.println("product_urn: " + st1.getProductURN() +
       
   110                 " != " + st2.getProductURN());
       
   111             return false;
       
   112         }
       
   113         if (!st1.getProductParentURN().equals(st2.getProductParentURN())) {
       
   114             System.out.println("product_parent_urn: " + st1.getProductParentURN() +
       
   115                 " != " + st2.getProductParentURN());
       
   116             return false;
       
   117         }
       
   118         if (!st1.getProductParent().equals(st2.getProductParent())) {
       
   119             System.out.println("product_parent: " + st1.getProductParent() +
       
   120                 " != " + st2.getProductParent());
       
   121             return false;
       
   122         }
       
   123         if (!st1.getProductDefinedInstanceID().equals(st2.getProductDefinedInstanceID())) {
       
   124             System.out.println("product_defined_inst_id: " +
       
   125                 st1.getProductDefinedInstanceID() +
       
   126                 " != " + st2.getProductDefinedInstanceID());
       
   127             return false;
       
   128         }
       
   129         if (!st1.getProductVendor().equals(st2.getProductVendor())) {
       
   130             System.out.println("product_vendor: " + st1.getProductVendor() +
       
   131                 " != " + st2.getProductVendor());
       
   132             return false;
       
   133         }
       
   134         if (!st1.getPlatformArch().equals(st2.getPlatformArch())) {
       
   135             System.out.println("platform_arch: " + st1.getPlatformArch() +
       
   136                 " != " + st2.getPlatformArch());
       
   137             return false;
       
   138         }
       
   139         if (!st1.getContainer().equals(st2.getContainer())) {
       
   140             System.out.println("container: " + st1.getContainer() +
       
   141                 " != " + st2.getContainer());
       
   142             return false;
       
   143         }
       
   144         if (!st1.getSource().equals(st2.getSource())) {
       
   145             System.out.println("source: " + st1.getSource() +
       
   146                 " != " + st2.getSource());
       
   147             return false;
       
   148         }
       
   149         return true;
       
   150     }
       
   151 
       
   152     public static void checkRegistrationData(String regFile,
       
   153                                              Map<String, ServiceTag> stMap)
       
   154             throws IOException {
       
   155         BufferedInputStream in = new BufferedInputStream(new FileInputStream(regFile));
       
   156         RegistrationData registration = RegistrationData.loadFromXML(in);
       
   157         Set<ServiceTag> svcTags = registration.getServiceTags();
       
   158         if (svcTags.size() != stMap.size()) {
       
   159             throw new RuntimeException("Invalid service tag count= " +
       
   160                 svcTags.size() + " expected=" + stMap.size());
       
   161         }
       
   162         for (ServiceTag st : svcTags) {
       
   163             ServiceTag st1 = stMap.get(st.getInstanceURN());
       
   164             if (!matches(st, st1)) {
       
   165                 System.err.println(st);
       
   166                 System.err.println(st1);
       
   167                 throw new RuntimeException("ServiceTag in the registry " +
       
   168                     "does not match the one in the map");
       
   169             }
       
   170         }
       
   171     }
       
   172 
       
   173 
       
   174     /**
       
   175      * Formats the Date into a timestamp string in YYYY-MM-dd HH:mm:ss GMT.
       
   176      * @param timestamp Date
       
   177      * @return a string representation of the timestamp in the YYYY-MM-dd HH:mm:ss GMT format.
       
   178      */
       
   179     static String formatTimestamp(Date timestamp) {
       
   180         SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
       
   181         df.setTimeZone(TimeZone.getTimeZone("GMT"));
       
   182         return df.format(timestamp);
       
   183     }
       
   184 
       
   185     /**
       
   186      * Parses a timestamp string in YYYY-MM-dd HH:mm:ss GMT format.
       
   187      * @param timestamp Timestamp in the YYYY-MM-dd HH:mm:ss GMT format.
       
   188      * @return Date
       
   189      */
       
   190     static Date parseTimestamp(String timestamp) {
       
   191         SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
       
   192         df.setTimeZone(TimeZone.getTimeZone("GMT"));
       
   193         try {
       
   194             return df.parse(timestamp);
       
   195         } catch (ParseException e) {
       
   196             // should not reach here
       
   197             e.printStackTrace();
       
   198             return new Date();
       
   199         }
       
   200     }
       
   201 
       
   202     /**
       
   203      * Returns the command simulating stclient behavior.
       
   204      */
       
   205     static String getSvcClientCommand(String stclientRegistry) {
       
   206         String regDir = System.getProperty("test.classes");
       
   207 
       
   208         StringBuilder sb = new StringBuilder();
       
   209         // wrap each argument to the command with double quotes
       
   210         sb.append("\"");
       
   211         sb.append(System.getProperty("java.home"));
       
   212         sb.append(File.separator).append("bin");
       
   213         sb.append(File.separator).append("java");
       
   214         sb.append("\"");
       
   215         sb.append(" -cp ");
       
   216         sb.append("\"").append(regDir).append("\"");
       
   217         sb.append(" \"-Dstclient.registry.path=");
       
   218         sb.append(stclientRegistry).append("\"");
       
   219         sb.append(" SvcTagClient");
       
   220         return sb.toString();
       
   221     }
       
   222 
       
   223     private static Registry registry = null;
       
   224     private static File registryFile = null;
       
   225     /**
       
   226      * Returns the Registry processed by SvcTagClient that simulates
       
   227      * stclient.
       
   228      */
       
   229     static synchronized Registry getSvcTagClientRegistry() throws IOException {
       
   230         String regDir = System.getProperty("test.classes");
       
   231         File f = new File(regDir, "registry.xml");
       
   232         if (registry != null) {
       
   233             if (!f.equals(registryFile) && f.length() != 0) {
       
   234                 throw new AssertionError("Has to be empty registry.xml to run in samevm");
       
   235             }
       
   236             return registry;
       
   237         }
       
   238 
       
   239         // System.setProperty("servicetag.verbose", "true");
       
   240         // enable the helper class
       
   241         System.setProperty("servicetag.sthelper.supported", "true");
       
   242         registryFile = f;
       
   243 
       
   244         String stclientCmd = Util.getSvcClientCommand(registryFile.getCanonicalPath());
       
   245         System.out.println("stclient cmd: " + stclientCmd);
       
   246         System.setProperty("servicetag.stclient.cmd", stclientCmd);
       
   247 
       
   248         // get the Registry object after the system properties are set
       
   249         registry = Registry.getSystemRegistry();
       
   250         return registry;
       
   251     }
       
   252 
       
   253     static void emptyRegistryFile() throws IOException {
       
   254         if (registryFile.exists()) {
       
   255             BufferedOutputStream out = new BufferedOutputStream(
       
   256                 new FileOutputStream(registryFile));
       
   257             try {
       
   258                 RegistrationData data = new RegistrationData();
       
   259                 data.storeToXML(out);
       
   260             } finally {
       
   261                 out.close();
       
   262             }
       
   263         }
       
   264     }
       
   265 }