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 registry class
  30  *          by replacing stclient with SvcTagClient utility
  31  * @author  Mandy Chung
  32  *
  33  * @run build SvcTagClient SystemRegistryTest Util
  34  * @run main SystemRegistryTest
  35  */
  36 
  37 import com.sun.servicetag.*;
  38 import java.io.*;
  39 import java.util.*;
  40 
  41 public class SystemRegistryTest {
  42     private static String registryDir = System.getProperty("test.classes");
  43     private static String servicetagDir = System.getProperty("test.src");
  44     private static List<ServiceTag> list = new ArrayList<ServiceTag>();
  45     private static String[] files = new String[] {
  46                                         "servicetag1.properties",
  47                                         "servicetag2.properties",
  48                                         "servicetag3.properties"
  49                                     };
  50 
  51     private static Registry registry;
  52     public static void main(String[] argv) throws Exception {
  53         try {
  54             registry = Util.getSvcTagClientRegistry();
  55             runTest();
  56         } finally {
  57             // restore empty registry file
  58             Util.emptyRegistryFile();
  59         }
  60     }
  61 
  62     private static void runTest() throws Exception {
  63         for (String filename : files) {
  64             File f = new File(servicetagDir, filename);
  65             ServiceTag svcTag = Util.newServiceTag(f);
  66             ServiceTag st = registry.addServiceTag(svcTag);
  67             list.add(st);
  68             System.out.println(st);
  69         }
  70 
  71         testDuplicate(list.get(0));
  72         testNotFound();
  73 
  74         // remove a service tag
  75         String urn = list.get(0).getInstanceURN();
  76         ServiceTag svcTag = registry.removeServiceTag(urn);
  77         if (!Util.matches(svcTag, list.get(0))) {
  78            throw new RuntimeException(urn +
  79                " deleted but does not match.");
  80         }
  81 
  82         // get a service tag
  83         svcTag = list.get(1);
  84         urn = svcTag.getInstanceURN();
  85         ServiceTag st = registry.getServiceTag(urn);
  86         if (!Util.matches(svcTag, st)) {
  87            throw new RuntimeException(urn +
  88                " returned from getServiceTag but does not match.");
  89         }
  90         // update the service tag
  91         registry.updateServiceTag(urn, "My new defined ID");
  92         st = registry.getServiceTag(urn);
  93         if (Util.matches(svcTag, st)) {
  94            throw new RuntimeException(urn +
  95                " updated but expected to be different.");
  96         }
  97 
  98         if (!st.getProductDefinedInstanceID().equals("My new defined ID")) {
  99             throw new RuntimeException("Invalid product_defined_instance_id " +
 100                 st.getProductDefinedInstanceID());
 101         }
 102         if (st.getInstallerUID() != -1) {
 103             throw new RuntimeException("Invalid installer_uid " +
 104                 st.getInstallerUID());
 105         }
 106         if (st.getTimestamp().equals(svcTag.getTimestamp())) {
 107             throw new RuntimeException("Timestamp " +
 108                 st.getTimestamp() + " == " + svcTag.getTimestamp());
 109         }
 110 
 111     }
 112     private static void testDuplicate(ServiceTag st) throws IOException {
 113         boolean dup = false;
 114         try {
 115            registry.addServiceTag(st);
 116         } catch (IllegalArgumentException e) {
 117            dup = true;
 118         }
 119         if (!dup) {
 120            throw new RuntimeException(st.getInstanceURN() +
 121                " added successfully but expected to be a duplicated.");
 122         }
 123     }
 124 
 125     private static void testNotFound() throws Exception {
 126         String instanceURN = "urn:st:721cf98a-f4d7-6231-bb1d-f2f5aa903ef7";
 127         ServiceTag svctag = registry.removeServiceTag(instanceURN);
 128         if (svctag != null) {
 129            throw new RuntimeException(instanceURN +
 130                " exists but expected not found");
 131         }
 132 
 133         svctag = registry.updateServiceTag(instanceURN, "testing");
 134         if (svctag != null) {
 135            throw new RuntimeException(instanceURN +
 136                " exists but expected not found");
 137         }
 138     }
 139 }