1 /*
   2  * Copyright (c) 2004, 2017, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 4990825
  27  * @summary test that HostIdentifier objects get created as expected
  28  */
  29 
  30 import java.io.*;
  31 import java.net.*;
  32 import javax.xml.parsers.*;
  33 import org.xml.sax.*;
  34 import org.xml.sax.helpers.DefaultHandler;
  35 import sun.jvmstat.monitor.*;
  36 
  37 public class HostIdentifierCreate {
  38 
  39     public static void main(String args[]) throws Exception {
  40         File testcases =
  41                 new File(System.getProperty("test.src", "."), "testcases");
  42 
  43         SAXParserFactory spf = SAXParserFactory.newInstance();
  44         SAXParser sp = spf.newSAXParser();
  45         DefaultHandler dh = new HostIdentifierTestHandler();
  46         sp.parse(testcases, dh);
  47     }
  48 }
  49 
  50 class HostIdentifierTestHandler extends DefaultHandler {
  51     private static final boolean debug = false;
  52     private static final int START                     = 0;
  53     private static final int HOSTIDENTIFIER_TESTS      = 1;
  54     private static final int TESTCASE                  = 2;
  55     private static final int DESCRIPTION               = 3;
  56     private static final int HOSTIDENTIFIER            = 4;
  57 
  58     private TestCase test;
  59     private String value = null;
  60     private int state;
  61     private Attributes attributes;
  62 
  63     public HostIdentifierTestHandler() {
  64         super();
  65     }
  66 
  67     public void characters(char[] ch, int start, int length) {
  68         String s = new String(ch, start, length);
  69         if (debug) {
  70             System.out.println("characters: start = " + start +
  71                                " length = " + length +
  72                                " chars = " + s);
  73         }
  74 
  75         if (value == null) {
  76             value = s.trim();
  77         } else {
  78             value = value + s.trim();
  79             if (debug) {
  80                 System.out.println("characters: appended characters to "
  81                                    + "previous value: new value = " + value);
  82             }
  83         }
  84     }
  85 
  86     public void endDocument() {
  87         if (debug) {
  88             System.out.println("endDocument()");
  89         }
  90     }
  91 
  92     public void endElement(String namespaceURI, String localName,
  93                            String qName)
  94                 throws SAXException {
  95         if (debug) {
  96             System.out.println("endElement(): namespaceURI = " + namespaceURI
  97                                + " localName = " + localName
  98                                + " qName = " + qName
  99                                + " state = " + state);
 100         }
 101 
 102         switch (state) {
 103         case START:
 104             throw new RuntimeException("Unexpected state: " + state);
 105 
 106         case HOSTIDENTIFIER_TESTS:
 107             state = START;
 108             break;
 109 
 110         case TESTCASE:
 111             if (test == null) {
 112                 throw new RuntimeException("Unexpected thread state");
 113             }
 114             try {
 115               System.out.println("running test case " + test.id);
 116               test.run();            // run the test
 117             }
 118             catch (Exception e) {
 119               throw new SAXException("Testcase id = " + test.id, e);
 120             }
 121             state = HOSTIDENTIFIER_TESTS;
 122             test = null;
 123             value = null;
 124             break;
 125 
 126         case DESCRIPTION:
 127             test.setDescription(value);
 128             state = TESTCASE;
 129             value = null;
 130             break;
 131 
 132         case HOSTIDENTIFIER:
 133             test.setExpectedHostIdentifier(value);
 134             state = TESTCASE;
 135             value = null;
 136             break;
 137 
 138         default:
 139             throw new RuntimeException("Unexpected state: " + state);
 140         }
 141     }
 142 
 143     public void endPrefixMapping(String prefix) {
 144         if (debug) {
 145             System.out.println("endPrefixMapping(): prefix = " + prefix);
 146         }
 147     }
 148 
 149     public void ignorableWhitespace(char[] ch, int start, int length) {
 150         if (debug) {
 151             System.out.println("ignoreableWhitespace():"
 152                                + " ch = " + new String(ch, start, length)
 153                                + " start = " + start
 154                                + " length = " + length);
 155         }
 156     }
 157 
 158     public void processingInstruction(String target, String data) {
 159         if (debug) {
 160             System.out.println("processingInstruction():"
 161                                + " target = " + target
 162                                + " data = " + data);
 163         }
 164     }
 165 
 166     public void setDocumentLocator(Locator locator) {
 167         if (debug) {
 168             System.out.println("setDocumentLocator(): locator = " + locator);
 169         }
 170     }
 171 
 172     public void skippedEntity(String name) {
 173         if (debug) {
 174             System.out.println("skippedEntity(): name = " + name);
 175         }
 176     }
 177 
 178     public void startDocument() {
 179         if (debug) {
 180             System.out.println("startDocument():");
 181         }
 182     }
 183 
 184     public void startElement(String namespaceURI, String localName,
 185                              String qName, Attributes attributes) {
 186         if (debug) {
 187             System.out.println("startElement():"
 188                                + " namespaceURI = " + namespaceURI
 189                                + " localName = " + localName
 190                                + " qName = " + qName
 191                                + " state = " + state);
 192 
 193             System.out.println("   Attributes(" + attributes.getLength() + ")");
 194             for (int i = 0; i < attributes.getLength(); i++) {
 195                 System.out.println("     name = " + attributes.getQName(i)
 196                                    + " value = " + attributes.getValue(i));
 197             }
 198         }
 199 
 200         this.attributes = attributes;
 201 
 202         switch (state) {
 203         case START:
 204             if (qName.compareTo("HostIdentifierTests") == 0) {
 205                 state = HOSTIDENTIFIER_TESTS;
 206             } else {
 207                 System.err.println("unexpected input: state = " + state
 208                                    + " input = " + qName);
 209             }
 210             break;
 211 
 212         case HOSTIDENTIFIER_TESTS:
 213             if (qName.compareTo("testcase") == 0) {
 214                 state = TESTCASE;
 215                 int id_n = attributes.getIndex("id");
 216 
 217                 if (id_n == -1) {
 218                     throw new RuntimeException("id attribute expected");
 219                 }
 220 
 221                 String hostid_input = null;
 222                 int hostid_n = attributes.getIndex("HostIdentifierInput");
 223                 if (hostid_n != -1) {
 224                     hostid_input = attributes.getValue(hostid_n);
 225                     if (hostid_input.length() == 0) {
 226                         hostid_input = null;
 227                     }
 228                 }
 229 
 230                 String id = attributes.getValue(id_n);
 231 
 232                 test = new TestCase(id, hostid_input);
 233             } else {
 234                 System.err.println("unexpected input: state = " + state
 235                                    + " input = " + qName);
 236             }
 237             break;
 238 
 239         case TESTCASE:
 240             if (test == null) {
 241                 throw new RuntimeException("TestCase null");
 242             }
 243             value = null;
 244             if (qName.compareTo("description") == 0) {
 245                 state = DESCRIPTION;
 246 
 247             } else if (qName.compareTo("HostIdentifier") == 0) {
 248                 state = HOSTIDENTIFIER;
 249 
 250             } else {
 251                 System.err.println("unexpected input: state = " + state
 252                                    + " input = " + qName);
 253             }
 254             break;
 255 
 256         case DESCRIPTION:
 257         case HOSTIDENTIFIER:
 258             if (test == null) {
 259                 throw new RuntimeException("TestCase null");
 260             }
 261             break;
 262 
 263         default:
 264             System.err.println("Unexpected state: " + state);
 265             break;
 266         }
 267     }
 268 
 269     public void startPrefixMapping(String prefix, String uri) {
 270         if (debug) {
 271             System.out.println("startPrefixMapping():"
 272                                + " prefix = " + prefix
 273                                + " uri = " + uri);
 274         }
 275     }
 276 }
 277 
 278 class HostIdentifierException extends Exception {
 279     String result;
 280     TestCase test;
 281 
 282     HostIdentifierException(TestCase test, String result) {
 283         this.test = test;
 284         this.result = result;
 285     }
 286 
 287     public String getMessage() {
 288         return "Test " + test.id + " " + "Failed: "
 289                + "Expected = " + test.expectedHostIdentifier + " "
 290                + "Actual = " + result;
 291     }
 292 }
 293 
 294 class TestCase {
 295     private static final boolean debug = false;
 296 
 297     String id;
 298     String hostid;
 299     String expectedHostIdentifier;
 300     String description;
 301 
 302     public TestCase(String id, String hostid) {
 303         this.id = id;
 304         this.hostid = hostid;
 305     }
 306 
 307     public void run() throws Exception {
 308         if (expectedHostIdentifier == null) {
 309             throw new IllegalArgumentException(
 310                     "HostIdentifier not initialized");
 311         }
 312 
 313         HostIdentifier test_hostid = new HostIdentifier(hostid);
 314 
 315         String test_hostid_str = test_hostid.toString();
 316 
 317         if (debug) {
 318             System.out.println("comparing HostIdentifier result");
 319         }
 320 
 321         if (test_hostid_str.compareTo(expectedHostIdentifier) != 0) {
 322             throw new HostIdentifierException(this, test_hostid_str);
 323         }
 324     }
 325 
 326     public void setDescription(String description) {
 327         this.description = description;
 328     }
 329 
 330     public void setExpectedHostIdentifier(String expectedHostIdentifier) {
 331         this.expectedHostIdentifier = expectedHostIdentifier;
 332     }
 333 }