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 Registration Data
  30  * @author  Mandy Chung
  31  *
  32  * @run build NewRegistrationData Util
  33  * @run main NewRegistrationData
  34  */
  35 
  36 import com.sun.servicetag.*;
  37 import java.io.*;
  38 import java.util.*;
  39 
  40 public class NewRegistrationData {
  41     private static RegistrationData regData;
  42     private static Map<String, ServiceTag> stMap = new LinkedHashMap<String, ServiceTag>();
  43     private static String[] files = new String[] {
  44                                         "servicetag1.properties",
  45                                         "servicetag2.properties",
  46                                         "servicetag3.properties"
  47                                     };
  48 
  49     public static void main(String[] argv) throws Exception {
  50         String regDataDir = System.getProperty("test.classes");
  51         String servicetagDir = System.getProperty("test.src");
  52 
  53         File reg = new File(regDataDir, "registration.xml");
  54         // Make sure a brand new file is created
  55         reg.delete();
  56 
  57         regData = new RegistrationData();
  58         if (regData.getRegistrationURN().isEmpty()) {
  59             throw new RuntimeException("Empty registration urn");
  60         }
  61 
  62         int count = 0;
  63         for (String f : files) {
  64             addServiceTag(servicetagDir, f, ++count);
  65         }
  66 
  67         // check if the registration data contains all service tags
  68         Set<ServiceTag> c = regData.getServiceTags();
  69         for (ServiceTag st : c) {
  70             if (!Util.matches(st, regData.getServiceTag(st.getInstanceURN()))) {
  71                 throw new RuntimeException("ServiceTag added in the regData " +
  72                     " doesn't match.");
  73             }
  74         }
  75 
  76         // store the service tag to a file
  77         BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(reg));
  78         try {
  79             regData.storeToXML(out);
  80         } finally {
  81             out.close();
  82         }
  83 
  84         Util.checkRegistrationData(reg.getCanonicalPath(), stMap);
  85         System.out.println("Test passed: " + count + " service tags added");
  86     }
  87 
  88     private static void addServiceTag(String parent, String filename, int count) throws Exception {
  89         File f = new File(parent, filename);
  90         ServiceTag svcTag = Util.newServiceTag(f);
  91         regData.addServiceTag(svcTag);
  92         stMap.put(svcTag.getInstanceURN(), svcTag);
  93 
  94         Set<ServiceTag> c = regData.getServiceTags();
  95         if (c.size() != count) {
  96             throw new RuntimeException("Invalid service tag count= " +
  97                 c.size() + " expected=" + count);
  98         }
  99         ServiceTag st = regData.getServiceTag(svcTag.getInstanceURN());
 100         if (!Util.matches(st, svcTag)) {
 101             throw new RuntimeException("ServiceTag added in the regData " +
 102                 " doesn't match.");
 103         }
 104     }
 105 }