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