Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/jvmstat/monitor/VmIdentifier/VmIdentifierCreateResolve.java
38853 views
/*1* Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 499082526* @summary test that VmIdentifier objects get created as expected27*/2829import java.io.*;30import java.net.*;31import javax.xml.parsers.*;32import org.xml.sax.*;33import org.xml.sax.helpers.DefaultHandler;34import sun.jvmstat.monitor.*;3536public class VmIdentifierCreateResolve {3738public static void main(String args[]) throws Exception {39File testcases =40new File(System.getProperty("test.src", "."), "testcases");4142SAXParserFactory spf = SAXParserFactory.newInstance();43SAXParser sp = spf.newSAXParser();44DefaultHandler dh = new VmIdentifierTestHandler();45sp.parse(testcases, dh);46}47}4849class VmIdentifierTestHandler extends DefaultHandler {50private static final boolean debug = false;51private static final int START = 0;52private static final int VMIDENTIFIER_TESTS = 1;53private static final int TESTCASE = 2;54private static final int DESCRIPTION = 3;55private static final int VMIDENTIFIER = 4;56private static final int HOSTIDENTIFIER = 5;57private static final int RESOLVED = 6;5859private TestCase test;60private String value = null;61private int state;62private Attributes attributes;6364public VmIdentifierTestHandler() {65super();66}6768public void characters(char[] ch, int start, int length) {69String s = new String(ch, start, length);70if (debug) {71System.out.println("characters: start = " + start +72" length = " + length +73" chars = " + s);74}7576if (value == null) {77value = s.trim();78} else {79value = value + s.trim();80if (debug) {81System.out.println("characters: appended characters to "82+ "previous value: new value = " + value);83}84}85}8687public void endDocument() {88if (debug) {89System.out.println("endDocument()");90}91}9293public void endElement(String namespaceURI, String localName,94String qName)95throws SAXException {96if (debug) {97System.out.println("endElement(): namespaceURI = " + namespaceURI98+ " localName = " + localName99+ " qName = " + qName100+ " state = " + state);101}102103switch (state) {104case START:105throw new RuntimeException("Unexpected state: " + state);106107case VMIDENTIFIER_TESTS:108state = START;109break;110111case TESTCASE:112if (test == null) {113throw new RuntimeException("Unexpected thread state");114}115try {116System.out.println("running test case " + test.id);117test.run(); // run the test118}119catch (Exception e) {120throw new SAXException(e);121}122state = VMIDENTIFIER_TESTS;123test = null;124value = null;125break;126127case DESCRIPTION:128test.setDescription(value);129state = TESTCASE;130value = null;131break;132133case VMIDENTIFIER:134test.setExpectedVmIdentifier(value);135state = TESTCASE;136value = null;137break;138139case HOSTIDENTIFIER:140test.setExpectedHostIdentifier(value);141state = TESTCASE;142value = null;143break;144145case RESOLVED:146test.setResolvedVmIdentifier(value);147state = TESTCASE;148value = null;149break;150151default:152throw new RuntimeException("Unexpected state: " + state);153}154}155156public void endPrefixMapping(String prefix) {157if (debug) {158System.out.println("endPrefixMapping(): prefix = " + prefix);159}160}161162public void ignorableWhitespace(char[] ch, int start, int length) {163if (debug) {164System.out.println("ignoreableWhitespace():"165+ " ch = " + new String(ch, start, length)166+ " start = " + start167+ " length = " + length);168}169}170171public void processingInstruction(String target, String data) {172if (debug) {173System.out.println("processingInstruction():"174+ " target = " + target175+ " data = " + data);176}177}178179public void setDocumentLocator(Locator locator) {180if (debug) {181System.out.println("setDocumentLocator(): locator = " + locator);182}183}184185public void skippedEntity(String name) {186if (debug) {187System.out.println("skippedEntity(): name = " + name);188}189}190191public void startDocument() {192if (debug) {193System.out.println("startDocument():");194}195}196197public void startElement(String namespaceURI, String localName,198String qName, Attributes attributes) {199if (debug) {200System.out.println("startElement():"201+ " namespaceURI = " + namespaceURI202+ " localName = " + localName203+ " qName = " + qName204+ " state = " + state);205206System.out.println(" Attributes(" + attributes.getLength() + ")");207for (int i = 0; i < attributes.getLength(); i++) {208System.out.println(" name = " + attributes.getQName(i)209+ " value = " + attributes.getValue(i));210}211}212213this.attributes = attributes;214215switch (state) {216case START:217if (qName.compareTo("VmIdentifierTests") == 0) {218state = VMIDENTIFIER_TESTS;219} else {220System.err.println("unexpected input: state = " + state221+ " input = " + qName);222}223break;224225case VMIDENTIFIER_TESTS:226if (qName.compareTo("testcase") == 0) {227state = TESTCASE;228int id_n = attributes.getIndex("id");229230if (id_n == -1) {231throw new RuntimeException("id attribute expected");232}233234int vmid_n = attributes.getIndex("VmIdentifierInput");235if (vmid_n == -1) {236throw new RuntimeException(237"VmIdentifier attribute expected");238}239240String hostid_input = null;241int hostid_n = attributes.getIndex("HostIdentifierInput");242if (hostid_n != -1) {243hostid_input = attributes.getValue(hostid_n);244}245246String vmid_input = attributes.getValue(vmid_n);247String id = attributes.getValue(id_n);248249test = new TestCase(id, vmid_input, hostid_input);250} else {251System.err.println("unexpected input: state = " + state252+ " input = " + qName);253}254break;255256case TESTCASE:257if (test == null) {258throw new RuntimeException("TestCase null");259}260value = null;261if (qName.compareTo("description") == 0) {262state = DESCRIPTION;263264} else if (qName.compareTo("VmIdentifier") == 0) {265state = VMIDENTIFIER;266267} else if (qName.compareTo("HostIdentifier") == 0) {268state = HOSTIDENTIFIER;269270} else if (qName.compareTo("Resolved") == 0) {271state = RESOLVED;272273} else {274System.err.println("unexpected input: state = " + state275+ " input = " + qName);276}277break;278279case DESCRIPTION:280case VMIDENTIFIER:281case HOSTIDENTIFIER:282case RESOLVED:283if (test == null) {284throw new RuntimeException("TestCase null");285}286break;287288default:289System.err.println("Unexpected state: " + state);290break;291}292}293294public void startPrefixMapping(String prefix, String uri) {295if (debug) {296System.out.println("startPrefixMapping():"297+ " prefix = " + prefix298+ " uri = " + uri);299}300}301}302303class VmIdentifierException extends Exception {304String result;305TestCase test;306307VmIdentifierException(TestCase test, String result) {308this.test = test;309this.result = result;310}311312public String getMessage() {313return "Test " + test.id + " " + "Failed: "314+ "Expected = " + test.expectedVmIdentifier + " "315+ "Actual = " + result;316}317}318319class HostIdentifierException extends Exception {320String result;321TestCase test;322323HostIdentifierException(TestCase test, String result) {324this.test = test;325this.result = result;326}327328public String getMessage() {329return "Test " + test.id + " " + "Failed: "330+ "Expected = " + test.expectedHostIdentifier + " "331+ "Actual = " + result;332}333}334335class ResolvedVmIdentifierException extends Exception {336String result;337TestCase test;338339ResolvedVmIdentifierException(TestCase test, String result) {340this.test = test;341this.result = result;342}343public String getMessage() {344return "Test " + test.id + " " + "Failed: "345+ "Expected = " + test.resolvedVmIdentifier + " "346+ "Actual = " + result;347}348}349350class TestCase {351private static final boolean debug = false;352353String id;354String vmid;355String hostid;356String expectedVmIdentifier;357String expectedHostIdentifier;358String resolvedVmIdentifier;359String description;360361public TestCase(String id, String vmid, String hostid) {362this.id = id;363this.vmid = vmid;364this.hostid = hostid;365}366367public void run() throws Exception {368if (expectedVmIdentifier == null || expectedHostIdentifier == null369|| resolvedVmIdentifier == null) {370throw new IllegalArgumentException(371"expected values not initialized");372}373374VmIdentifier test_vmid = null;375HostIdentifier test_hostid = null;376VmIdentifier resolved_vmid = null;377378if (debug) {379System.out.println("creating VmIdentifier");380}381382test_vmid = new VmIdentifier(vmid);383384if (debug) {385System.out.println("creating HostIdentifier");386}387388if (hostid != null) {389test_hostid = new HostIdentifier(hostid);390} else {391test_hostid = new HostIdentifier(test_vmid);392}393394if (debug) {395System.out.println("resolving VmIdentifier");396}397398resolved_vmid = test_hostid.resolve(test_vmid);399400String test_vmid_str = test_vmid.toString();401String test_hostid_str = test_hostid.toString();402String resolved_vmid_str = resolved_vmid.toString();403404if (debug) {405System.out.println("comparing VmIdentifier result");406}407408if (test_vmid_str.compareTo(expectedVmIdentifier) != 0) {409throw new VmIdentifierException(this, test_vmid_str);410}411412if (debug) {413System.out.println("comparing HostIdentifier result");414}415416if (test_hostid_str.compareTo(expectedHostIdentifier) != 0) {417throw new HostIdentifierException(this, test_hostid_str);418}419420if (debug) {421System.out.println("comparing VmIdentifier result");422}423424if (resolved_vmid_str.compareTo(resolvedVmIdentifier) != 0) {425throw new ResolvedVmIdentifierException(this, resolved_vmid_str);426}427}428429public void setDescription(String description) {430this.description = description;431}432433public void setExpectedVmIdentifier(String expectedVmIdentifier) {434if (debug) {435System.out.println("setting vmidentifier string to " + vmid);436}437this.expectedVmIdentifier = expectedVmIdentifier;438}439440public void setExpectedHostIdentifier(String expectedHostIdentifier) {441this.expectedHostIdentifier = expectedHostIdentifier;442}443444public void setResolvedVmIdentifier(String resolvedVmIdentifier) {445this.resolvedVmIdentifier = resolvedVmIdentifier;446}447}448449450