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 reading a valid registration
  30  * @author  Mandy Chung
  31  *
  32  * @run build ValidRegistrationData
  33  * @run main ValidRegistrationData
  34  */
  35 
  36 import com.sun.servicetag.*;
  37 import java.io.*;
  38 import java.util.*;
  39 
  40 public class ValidRegistrationData {
  41     private static String registrationDir = System.getProperty("test.classes");
  42     private static String servicetagDir = System.getProperty("test.src");
  43     private static RegistrationData registration;
  44     private static Map<String, ServiceTag> stMap =
  45         new LinkedHashMap<String, ServiceTag>();
  46     private static String[] files = new String[] {
  47                                         "servicetag1.properties",
  48                                         "servicetag2.properties",
  49                                         "servicetag3.properties"
  50                                     };
  51     private static String URN = "urn:st:9543ffaa-a4f1-4f77-b2d1-f561922d4e4a";
  52 
  53     public static void main(String[] argv) throws Exception {
  54         File f = new File(servicetagDir, "registration.xml");
  55 
  56         // load the registration data with all service tags
  57         BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));
  58         registration = RegistrationData.loadFromXML(in);
  59         if (!registration.getRegistrationURN().equals(URN)){
  60             throw new RuntimeException("Invalid URN=" +
  61                 registration.getRegistrationURN());
  62         }
  63         Map<String,String> environMap = registration.getEnvironmentMap();
  64         checkEnvironmentMap(environMap);
  65 
  66         // set environment
  67         setInvalidEnvironment("hostname", "");
  68         setInvalidEnvironment("osName", "");
  69         setInvalidEnvironment("invalid", "");
  70     }
  71 
  72     private static void checkEnvironmentMap(Map<String,String> envMap)
  73             throws Exception {
  74         Properties props = new Properties();
  75         File f = new File(servicetagDir, "environ.properties");
  76         FileReader reader = new FileReader(f);
  77         try {
  78             props.load(reader);
  79         } finally {
  80             reader.close();
  81         }
  82         for (Map.Entry<String,String> entry : envMap.entrySet()) {
  83             String name = entry.getKey();
  84             String value = entry.getValue();
  85             String expected = props.getProperty(name);
  86             if (expected == null || !value.equals(expected)) {
  87                 throw new RuntimeException("Invalid environment " +
  88                     name + "=" + value);
  89             }
  90             props.remove(name);
  91         }
  92         if (!props.isEmpty()) {
  93             System.out.println("Environment missing: ");
  94             for (String s : props.stringPropertyNames()) {
  95                 System.out.println("   " + s + "=" + props.getProperty(s));
  96             }
  97             throw new RuntimeException("Invalid environment read");
  98         }
  99     }
 100     private static void setInvalidEnvironment(String name, String value) {
 101         boolean invalid = false;
 102         try {
 103             registration.setEnvironment(name, value);
 104         } catch (IllegalArgumentException e) {
 105             invalid = true;
 106         }
 107         if (!invalid) {
 108            throw new RuntimeException(name + "=" + value +
 109                " set but expected to fail.");
 110         }
 111     }
 112 }