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 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         registry = Util.getSvcTagClientRegistry();
  54 
  55         for (String filename : files) {
  56             File f = new File(servicetagDir, filename);
  57             ServiceTag svcTag = Util.newServiceTag(f);
  58             ServiceTag st = registry.addServiceTag(svcTag);
  59             list.add(st);
  60             System.out.println(st);
  61         }
  62 
  63         testDuplicate(list.get(0));
  64         testNotFound();
  65 
  66         // remove a service tag
  67         String urn = list.get(0).getInstanceURN();
  68         ServiceTag svcTag = registry.removeServiceTag(urn);
  69         if (!Util.matches(svcTag, list.get(0))) {
  70            throw new RuntimeException(urn +
  71                " deleted but does not match.");
  72         }
  73 
  74         // get a service tag
  75         svcTag = list.get(1);
  76         urn = svcTag.getInstanceURN();
  77         ServiceTag st = registry.getServiceTag(urn);
  78         if (!Util.matches(svcTag, st)) {
  79            throw new RuntimeException(urn +
  80                " returned from getServiceTag but does not match.");
  81         }
  82         // update the service tag
  83         registry.updateServiceTag(urn, "My new defined ID");
  84         st = registry.getServiceTag(urn);
  85         if (Util.matches(svcTag, st)) {
  86            throw new RuntimeException(urn +
  87                " updated but expected to be different.");
  88         }
  89 
  90         if (!st.getProductDefinedInstanceID().equals("My new defined ID")) {
  91             throw new RuntimeException("Invalid product_defined_instance_id " +
  92                 st.getProductDefinedInstanceID());
  93         }
  94         if (st.getInstallerUID() != -1) {
  95             throw new RuntimeException("Invalid installer_uid " +
  96                 st.getInstallerUID());
  97         }
  98         if (st.getTimestamp().equals(svcTag.getTimestamp())) {
  99             throw new RuntimeException("Timestamp " +
 100                 st.getTimestamp() + " == " + svcTag.getTimestamp());
 101         }
 102 
 103     }
 104     private static void testDuplicate(ServiceTag st) throws IOException {
 105         boolean dup = false;
 106         try {
 107            registry.addServiceTag(st);
 108         } catch (IllegalArgumentException e) {
 109            dup = true;
 110         }
 111         if (!dup) {
 112            throw new RuntimeException(st.getInstanceURN() +
 113                " added successfully but expected to be a duplicated.");
 114         }
 115     }
 116 
 117     private static void testNotFound() throws Exception {
 118         String instanceURN = "urn:st:721cf98a-f4d7-6231-bb1d-f2f5aa903ef7";
 119         ServiceTag svctag = registry.removeServiceTag(instanceURN);
 120         if (svctag != null) {
 121            throw new RuntimeException(instanceURN +
 122                " exists but expected not found");
 123         }
 124 
 125         svctag = registry.updateServiceTag(instanceURN, "testing");
 126         if (svctag != null) {
 127            throw new RuntimeException(instanceURN +
 128                " exists but expected not found");
 129         }
 130     }
 131 }