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