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 ServiceTag.newServiceTag() to test invalid fields.
  30  * @author  Mandy Chung
  31  *
  32  * @run build InvalidServiceTag
  33  * @run main InvalidServiceTag
  34  */
  35 
  36 import com.sun.servicetag.*;
  37 import java.io.*;
  38 import java.util.*;
  39 
  40 public class InvalidServiceTag {
  41     private final static int MAX_CONTAINER_LEN = 64 - 1;
  42     public static void main(String[] argv) throws Exception {
  43         // all fields valid
  44         ServiceTag st1 = ServiceTag.newInstance("product name",
  45                                                 "product version",
  46                                                 "product urn",
  47                                                 "product parent",
  48                                                 "product parent urn",
  49                                                 "product defined instance ID",
  50                                                 "product vendor",
  51                                                 "platform arch",
  52                                                 "container",
  53                                                 "source");
  54         // empty optional field
  55         ServiceTag st2 = ServiceTag.newInstance("product name",
  56                                                 "product version",
  57                                                 "product urn",
  58                                                 "product parent",
  59                                                 "",
  60                                                 "",
  61                                                 "product vendor",
  62                                                 "platform arch",
  63                                                 "container",
  64                                                 "source");
  65         // Invalid - empty required field
  66         setInvalidContainer("");
  67         // Invalid - required field exceeds max length.
  68         StringBuilder sb = new StringBuilder();
  69         for (int i = 0; i <= MAX_CONTAINER_LEN; i++) {
  70             sb.append('x');
  71         }
  72         setInvalidContainer(sb.toString());
  73         System.out.println("Test passed.");
  74     }
  75     private static void setInvalidContainer(String container) {
  76         boolean exceptionThrown = false;
  77         try {
  78             ServiceTag st2 = ServiceTag.newInstance("product name",
  79                                                     "product version",
  80                                                     "product urn",
  81                                                     "product parent",
  82                                                     "product parent urn",
  83                                                     "product defined instance ID",
  84                                                     "product vendor",
  85                                                     "platform arch",
  86                                                     container,
  87                                                     "source");
  88         } catch (IllegalArgumentException iae) {
  89             iae.printStackTrace();
  90             exceptionThrown = true;
  91         }
  92         if (!exceptionThrown) {
  93             throw new RuntimeException("IllegalArgumentException not thrown");
  94         }
  95     }
  96 }