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