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 VmIdentifier 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 VmIdentifierCreateResolve {
  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 VmIdentifierTestHandler();
  47         sp.parse(testcases, dh);
  48     }
  49 }
  50 
  51 class VmIdentifierTestHandler extends DefaultHandler {
  52     private static final boolean debug = false;
  53     private static final int START                     = 0;
  54     private static final int VMIDENTIFIER_TESTS        = 1;
  55     private static final int TESTCASE                  = 2;
  56     private static final int DESCRIPTION               = 3;
  57     private static final int VMIDENTIFIER              = 4;
  58     private static final int HOSTIDENTIFIER            = 5;
  59     private static final int RESOLVED                  = 6;
  60 
  61     private TestCase test;
  62     private String value = null;
  63     private int state;
  64     private Attributes attributes;
  65 
  66     public VmIdentifierTestHandler() {
  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 VMIDENTIFIER_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(e);
 123             }
 124             state = VMIDENTIFIER_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 VMIDENTIFIER:
 136             test.setExpectedVmIdentifier(value);
 137             state = TESTCASE;
 138             value = null;
 139             break;
 140 
 141         case HOSTIDENTIFIER:
 142             test.setExpectedHostIdentifier(value);
 143             state = TESTCASE;
 144             value = null;
 145             break;
 146 
 147         case RESOLVED:
 148             test.setResolvedVmIdentifier(value);
 149             state = TESTCASE;
 150             value = null;
 151             break;
 152 
 153         default:
 154             throw new RuntimeException("Unexpected state: " + state);
 155         }
 156     }
 157 
 158     public void endPrefixMapping(String prefix) {
 159         if (debug) {
 160             System.out.println("endPrefixMapping(): prefix = " + prefix);
 161         }
 162     }
 163 
 164     public void ignorableWhitespace(char[] ch, int start, int length) {
 165         if (debug) {
 166             System.out.println("ignoreableWhitespace():"
 167                                + " ch = " + new String(ch, start, length)
 168                                + " start = " + start
 169                                + " length = " + length);
 170         }
 171     }
 172 
 173     public void processingInstruction(String target, String data) {
 174         if (debug) {
 175             System.out.println("processingInstruction():"
 176                                + " target = " + target
 177                                + " data = " + data);
 178         }
 179     }
 180 
 181     public void setDocumentLocator(Locator locator) {
 182         if (debug) {
 183             System.out.println("setDocumentLocator(): locator = " + locator);
 184         }
 185     }
 186 
 187     public void skippedEntity(String name) {
 188         if (debug) {
 189             System.out.println("skippedEntity(): name = " + name);
 190         }
 191     }
 192 
 193     public void startDocument() {
 194         if (debug) {
 195             System.out.println("startDocument():");
 196         }
 197     }
 198 
 199     public void startElement(String namespaceURI, String localName,
 200                              String qName, Attributes attributes) {
 201         if (debug) {
 202             System.out.println("startElement():"
 203                                + " namespaceURI = " + namespaceURI
 204                                + " localName = " + localName
 205                                + " qName = " + qName
 206                                + " state = " + state);
 207 
 208             System.out.println("   Attributes(" + attributes.getLength() + ")");
 209             for (int i = 0; i < attributes.getLength(); i++) {
 210                 System.out.println("     name = " + attributes.getQName(i)
 211                                    + " value = " + attributes.getValue(i));
 212             }
 213         }
 214 
 215         this.attributes = attributes;
 216 
 217         switch (state) {
 218         case START:
 219             if (qName.compareTo("VmIdentifierTests") == 0) {
 220                 state = VMIDENTIFIER_TESTS;
 221             } else {
 222                 System.err.println("unexpected input: state = " + state
 223                                    + " input = " + qName);
 224             }
 225             break;
 226 
 227         case VMIDENTIFIER_TESTS:
 228             if (qName.compareTo("testcase") == 0) {
 229                 state = TESTCASE;
 230                 int id_n = attributes.getIndex("id");
 231 
 232                 if (id_n == -1) {
 233                     throw new RuntimeException("id attribute expected");
 234                 }
 235 
 236                 int vmid_n = attributes.getIndex("VmIdentifierInput");
 237                 if (vmid_n == -1) {
 238                     throw new RuntimeException(
 239                             "VmIdentifier attribute expected");
 240                 }
 241 
 242                 String hostid_input = null;
 243                 int hostid_n = attributes.getIndex("HostIdentifierInput");
 244                 if (hostid_n != -1) {
 245                     hostid_input = attributes.getValue(hostid_n);
 246                 }
 247 
 248                 String vmid_input = attributes.getValue(vmid_n);
 249                 String id = attributes.getValue(id_n);
 250 
 251                 test = new TestCase(id, vmid_input, hostid_input);
 252             } else {
 253                 System.err.println("unexpected input: state = " + state
 254                                    + " input = " + qName);
 255             }
 256             break;
 257 
 258         case TESTCASE:
 259             if (test == null) {
 260                 throw new RuntimeException("TestCase null");
 261             }
 262             value = null;
 263             if (qName.compareTo("description") == 0) {
 264                 state = DESCRIPTION;
 265 
 266             } else if (qName.compareTo("VmIdentifier") == 0) {
 267                 state = VMIDENTIFIER;
 268 
 269             } else if (qName.compareTo("HostIdentifier") == 0) {
 270                 state = HOSTIDENTIFIER;
 271 
 272             } else if (qName.compareTo("Resolved") == 0) {
 273                 state = RESOLVED;
 274 
 275             } else {
 276                 System.err.println("unexpected input: state = " + state
 277                                    + " input = " + qName);
 278             }
 279             break;
 280 
 281         case DESCRIPTION:
 282         case VMIDENTIFIER:
 283         case HOSTIDENTIFIER:
 284         case RESOLVED:
 285             if (test == null) {
 286                 throw new RuntimeException("TestCase null");
 287             }
 288             break;
 289 
 290         default:
 291             System.err.println("Unexpected state: " + state);
 292             break;
 293         }
 294     }
 295 
 296     public void startPrefixMapping(String prefix, String uri) {
 297         if (debug) {
 298             System.out.println("startPrefixMapping():"
 299                                + " prefix = " + prefix
 300                                + " uri = " + uri);
 301         }
 302     }
 303 }
 304 
 305 class VmIdentifierException extends Exception {
 306     String result;
 307     TestCase test;
 308 
 309     VmIdentifierException(TestCase test, String result) {
 310         this.test = test;
 311         this.result = result;
 312     }
 313 
 314     public String getMessage() {
 315         return "Test " + test.id + " " + "Failed: "
 316                + "Expected = " + test.expectedVmIdentifier + " "
 317                + "Actual = " + result;
 318     }
 319 }
 320 
 321 class HostIdentifierException extends Exception {
 322     String result;
 323     TestCase test;
 324 
 325     HostIdentifierException(TestCase test, String result) {
 326         this.test = test;
 327         this.result = result;
 328     }
 329 
 330     public String getMessage() {
 331         return "Test " + test.id + " " + "Failed: "
 332                + "Expected = " + test.expectedHostIdentifier + " "
 333                + "Actual = " + result;
 334     }
 335 }
 336 
 337 class ResolvedVmIdentifierException extends Exception {
 338     String result;
 339     TestCase test;
 340 
 341     ResolvedVmIdentifierException(TestCase test, String result) {
 342         this.test = test;
 343         this.result = result;
 344     }
 345     public String getMessage() {
 346         return "Test " + test.id + " " + "Failed: "
 347                + "Expected = " + test.resolvedVmIdentifier + " "
 348                + "Actual = " + result;
 349     }
 350 }
 351 
 352 class TestCase {
 353     private static final boolean debug = false;
 354 
 355     String id;
 356     String vmid;
 357     String hostid;
 358     String expectedVmIdentifier;
 359     String expectedHostIdentifier;
 360     String resolvedVmIdentifier;
 361     String description;
 362 
 363     public TestCase(String id, String vmid, String hostid) {
 364         this.id = id;
 365         this.vmid = vmid;
 366         this.hostid = hostid;
 367     }
 368 
 369     public void run() throws Exception {
 370         if (expectedVmIdentifier == null || expectedHostIdentifier == null
 371                 || resolvedVmIdentifier == null) {
 372             throw new IllegalArgumentException(
 373                     "expected values not initialized");
 374         }
 375 
 376         VmIdentifier test_vmid = null;
 377         HostIdentifier test_hostid = null;
 378         VmIdentifier resolved_vmid =  null;
 379 
 380         if (debug) {
 381             System.out.println("creating VmIdentifier");
 382         }
 383 
 384         test_vmid = new VmIdentifier(vmid);
 385 
 386         if (debug) {
 387             System.out.println("creating HostIdentifier");
 388         }
 389 
 390         if (hostid != null) {
 391             test_hostid = new HostIdentifier(hostid);
 392         } else {
 393             test_hostid = new HostIdentifier(test_vmid);
 394         }
 395 
 396         if (debug) {
 397             System.out.println("resolving VmIdentifier");
 398         }
 399 
 400         resolved_vmid =  test_hostid.resolve(test_vmid);
 401 
 402         String test_vmid_str = test_vmid.toString();
 403         String test_hostid_str = test_hostid.toString();
 404         String resolved_vmid_str = resolved_vmid.toString();
 405 
 406         if (debug) {
 407             System.out.println("comparing VmIdentifier result");
 408         }
 409 
 410         if (test_vmid_str.compareTo(expectedVmIdentifier) != 0) {
 411             throw new VmIdentifierException(this, test_vmid_str);
 412         }
 413 
 414         if (debug) {
 415             System.out.println("comparing HostIdentifier result");
 416         }
 417 
 418         if (test_hostid_str.compareTo(expectedHostIdentifier) != 0) {
 419             throw new HostIdentifierException(this, test_hostid_str);
 420         }
 421 
 422         if (debug) {
 423             System.out.println("comparing VmIdentifier result");
 424         }
 425 
 426         if (resolved_vmid_str.compareTo(resolvedVmIdentifier) != 0) {
 427             throw new ResolvedVmIdentifierException(this, resolved_vmid_str);
 428         }
 429     }
 430 
 431     public void setDescription(String description) {
 432         this.description = description;
 433     }
 434 
 435     public void setExpectedVmIdentifier(String expectedVmIdentifier) {
 436         if (debug) {
 437             System.out.println("setting vmidentifier string to " + vmid);
 438         }
 439         this.expectedVmIdentifier = expectedVmIdentifier;
 440     }
 441 
 442     public void setExpectedHostIdentifier(String expectedHostIdentifier) {
 443         this.expectedHostIdentifier = expectedHostIdentifier;
 444     }
 445 
 446     public void setResolvedVmIdentifier(String resolvedVmIdentifier) {
 447         this.resolvedVmIdentifier = resolvedVmIdentifier;
 448     }
 449 }