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.findServiceTags()
  30  * @author  Mandy Chung
  31  *
  32  * @run build FindServiceTags SvcTagClient Util
  33  * @run main FindServiceTags
  34  */
  35 
  36 import com.sun.servicetag.*;
  37 import java.io.*;
  38 import java.util.*;
  39 
  40 // This test creates a few service tags in the Registry.
  41 // Check if the findServiceTags method returns the expected ones.
  42 public class FindServiceTags {
  43     private static String registryDir = System.getProperty("test.classes");
  44     private static String servicetagDir = System.getProperty("test.src");
  45     private static String[] files = new String[] {
  46                                         "servicetag1.properties",
  47                                         "servicetag2.properties",
  48                                         "servicetag3.properties",
  49                                         "servicetag4.properties",
  50                                         "servicetag5.properties"
  51                                     };
  52 
  53     private static Registry registry;
  54     private static Set<ServiceTag> set = new HashSet<ServiceTag>();
  55     private static Set<String> productUrns = new HashSet<String>();
  56     private static int expectedUrnCount = 3;
  57 
  58     public static void main(String[] argv) throws Exception {
  59         try {
  60             registry = Util.getSvcTagClientRegistry();
  61             runTest();
  62         } finally {
  63             // restore empty registry file
  64             Util.emptyRegistryFile();
  65         }
  66         System.out.println("Test passed.");
  67     }
  68 
  69     public static void runTest() throws Exception {
  70         for (String filename : files) {
  71             File f = new File(servicetagDir, filename);
  72             ServiceTag svcTag = Util.newServiceTag(f);
  73             ServiceTag st = registry.addServiceTag(svcTag);
  74 
  75             set.add(st);
  76             productUrns.add(st.getProductURN());
  77         }
  78         if (productUrns.size() != expectedUrnCount) {
  79             throw new RuntimeException("Unexpected number of product URNs = " +
  80                 productUrns.size() + " expected " + expectedUrnCount);
  81         }
  82         if (set.size() != files.length) {
  83             throw new RuntimeException("Unexpected number of service tags = " +
  84                 set.size() + " expected " + files.length);
  85         }
  86         String purn = null;
  87         for (String urn : productUrns) {
  88             if (purn == null) {
  89                 // save the first product_urn for later use
  90                 purn = urn;
  91             }
  92             findServiceTags(urn);
  93         }
  94 
  95         // remove all service tags of purn
  96         Set<ServiceTag> tags = registry.findServiceTags(purn);
  97         for (ServiceTag st : tags) {
  98             System.out.println("Removing service tag " + st.getInstanceURN());
  99             registry.removeServiceTag(st.getInstanceURN());
 100         }
 101         tags = registry.findServiceTags(purn);
 102         if (tags.size() != 0) {
 103             throw new RuntimeException("Unexpected service tag count = " +
 104                 tags.size());
 105         }
 106 
 107     }
 108 
 109     private static void findServiceTags(String productUrn) throws Exception {
 110         Set<ServiceTag> found = registry.findServiceTags(productUrn);
 111         Set<ServiceTag> matched = new HashSet<ServiceTag>();
 112         System.out.println("Finding service tags of product_urn=" +
 113             productUrn);
 114         for (ServiceTag st : set) {
 115             if (st.getProductURN().equals(productUrn)) {
 116                 System.out.println(st.getInstanceURN());
 117                 matched.add(st);
 118             }
 119         }
 120         if (found.size() != matched.size()) {
 121             throw new RuntimeException("Unmatched service tag count = " +
 122                 found.size() + " expected " + matched.size());
 123         }
 124 
 125         for (ServiceTag st0 : found) {
 126             ServiceTag st = null;
 127             for (ServiceTag st1 : matched) {
 128                 if (Util.matches(st0, st1)) {
 129                     st = st1;
 130                     break;
 131                 }
 132             }
 133             if (st == null) {
 134                 System.out.println("product_urn=" + st0.getProductURN());
 135                 System.out.println("instance_urn=" + st0.getInstanceURN() );
 136                 throw new RuntimeException(st0.getInstanceURN() +
 137                     " not expected in the returned list");
 138             }
 139         }
 140     }
 141 }