Path: blob/master/test/jdk/sun/jvmstat/monitor/VmIdentifier/VmIdentifierCreateResolve.java
41152 views
/*1* Copyright (c) 2004, 2017, 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*28* @modules java.xml29* jdk.internal.jvmstat/sun.jvmstat.monitor30*/3132import java.io.*;33import java.net.*;34import javax.xml.parsers.*;35import org.xml.sax.*;36import org.xml.sax.helpers.DefaultHandler;37import sun.jvmstat.monitor.*;3839public class VmIdentifierCreateResolve {4041public static void main(String args[]) throws Exception {42File testcases =43new File(System.getProperty("test.src", "."), "testcases");4445SAXParserFactory spf = SAXParserFactory.newInstance();46SAXParser sp = spf.newSAXParser();47DefaultHandler dh = new VmIdentifierTestHandler();48sp.parse(testcases, dh);49}50}5152class VmIdentifierTestHandler extends DefaultHandler {53private static final boolean debug = false;54private static final int START = 0;55private static final int VMIDENTIFIER_TESTS = 1;56private static final int TESTCASE = 2;57private static final int DESCRIPTION = 3;58private static final int VMIDENTIFIER = 4;59private static final int HOSTIDENTIFIER = 5;60private static final int RESOLVED = 6;6162private TestCase test;63private String value = null;64private int state;65private Attributes attributes;6667public VmIdentifierTestHandler() {68super();69}7071public void characters(char[] ch, int start, int length) {72String s = new String(ch, start, length);73if (debug) {74System.out.println("characters: start = " + start +75" length = " + length +76" chars = " + s);77}7879if (value == null) {80value = s.trim();81} else {82value = value + s.trim();83if (debug) {84System.out.println("characters: appended characters to "85+ "previous value: new value = " + value);86}87}88}8990public void endDocument() {91if (debug) {92System.out.println("endDocument()");93}94}9596public void endElement(String namespaceURI, String localName,97String qName)98throws SAXException {99if (debug) {100System.out.println("endElement(): namespaceURI = " + namespaceURI101+ " localName = " + localName102+ " qName = " + qName103+ " state = " + state);104}105106switch (state) {107case START:108throw new RuntimeException("Unexpected state: " + state);109110case VMIDENTIFIER_TESTS:111state = START;112break;113114case TESTCASE:115if (test == null) {116throw new RuntimeException("Unexpected thread state");117}118try {119System.out.println("running test case " + test.id);120test.run(); // run the test121}122catch (Exception e) {123throw new SAXException(e);124}125state = VMIDENTIFIER_TESTS;126test = null;127value = null;128break;129130case DESCRIPTION:131test.setDescription(value);132state = TESTCASE;133value = null;134break;135136case VMIDENTIFIER:137test.setExpectedVmIdentifier(value);138state = TESTCASE;139value = null;140break;141142case HOSTIDENTIFIER:143test.setExpectedHostIdentifier(value);144state = TESTCASE;145value = null;146break;147148case RESOLVED:149test.setResolvedVmIdentifier(value);150state = TESTCASE;151value = null;152break;153154default:155throw new RuntimeException("Unexpected state: " + state);156}157}158159public void endPrefixMapping(String prefix) {160if (debug) {161System.out.println("endPrefixMapping(): prefix = " + prefix);162}163}164165public void ignorableWhitespace(char[] ch, int start, int length) {166if (debug) {167System.out.println("ignoreableWhitespace():"168+ " ch = " + new String(ch, start, length)169+ " start = " + start170+ " length = " + length);171}172}173174public void processingInstruction(String target, String data) {175if (debug) {176System.out.println("processingInstruction():"177+ " target = " + target178+ " data = " + data);179}180}181182public void setDocumentLocator(Locator locator) {183if (debug) {184System.out.println("setDocumentLocator(): locator = " + locator);185}186}187188public void skippedEntity(String name) {189if (debug) {190System.out.println("skippedEntity(): name = " + name);191}192}193194public void startDocument() {195if (debug) {196System.out.println("startDocument():");197}198}199200public void startElement(String namespaceURI, String localName,201String qName, Attributes attributes) {202if (debug) {203System.out.println("startElement():"204+ " namespaceURI = " + namespaceURI205+ " localName = " + localName206+ " qName = " + qName207+ " state = " + state);208209System.out.println(" Attributes(" + attributes.getLength() + ")");210for (int i = 0; i < attributes.getLength(); i++) {211System.out.println(" name = " + attributes.getQName(i)212+ " value = " + attributes.getValue(i));213}214}215216this.attributes = attributes;217218switch (state) {219case START:220if (qName.compareTo("VmIdentifierTests") == 0) {221state = VMIDENTIFIER_TESTS;222} else {223System.err.println("unexpected input: state = " + state224+ " input = " + qName);225}226break;227228case VMIDENTIFIER_TESTS:229if (qName.compareTo("testcase") == 0) {230state = TESTCASE;231int id_n = attributes.getIndex("id");232233if (id_n == -1) {234throw new RuntimeException("id attribute expected");235}236237int vmid_n = attributes.getIndex("VmIdentifierInput");238if (vmid_n == -1) {239throw new RuntimeException(240"VmIdentifier attribute expected");241}242243String hostid_input = null;244int hostid_n = attributes.getIndex("HostIdentifierInput");245if (hostid_n != -1) {246hostid_input = attributes.getValue(hostid_n);247}248249String vmid_input = attributes.getValue(vmid_n);250String id = attributes.getValue(id_n);251252test = new TestCase(id, vmid_input, hostid_input);253} else {254System.err.println("unexpected input: state = " + state255+ " input = " + qName);256}257break;258259case TESTCASE:260if (test == null) {261throw new RuntimeException("TestCase null");262}263value = null;264if (qName.compareTo("description") == 0) {265state = DESCRIPTION;266267} else if (qName.compareTo("VmIdentifier") == 0) {268state = VMIDENTIFIER;269270} else if (qName.compareTo("HostIdentifier") == 0) {271state = HOSTIDENTIFIER;272273} else if (qName.compareTo("Resolved") == 0) {274state = RESOLVED;275276} else {277System.err.println("unexpected input: state = " + state278+ " input = " + qName);279}280break;281282case DESCRIPTION:283case VMIDENTIFIER:284case HOSTIDENTIFIER:285case RESOLVED:286if (test == null) {287throw new RuntimeException("TestCase null");288}289break;290291default:292System.err.println("Unexpected state: " + state);293break;294}295}296297public void startPrefixMapping(String prefix, String uri) {298if (debug) {299System.out.println("startPrefixMapping():"300+ " prefix = " + prefix301+ " uri = " + uri);302}303}304}305306class VmIdentifierException extends Exception {307String result;308TestCase test;309310VmIdentifierException(TestCase test, String result) {311this.test = test;312this.result = result;313}314315public String getMessage() {316return "Test " + test.id + " " + "Failed: "317+ "Expected = " + test.expectedVmIdentifier + " "318+ "Actual = " + result;319}320}321322class HostIdentifierException extends Exception {323String result;324TestCase test;325326HostIdentifierException(TestCase test, String result) {327this.test = test;328this.result = result;329}330331public String getMessage() {332return "Test " + test.id + " " + "Failed: "333+ "Expected = " + test.expectedHostIdentifier + " "334+ "Actual = " + result;335}336}337338class ResolvedVmIdentifierException extends Exception {339String result;340TestCase test;341342ResolvedVmIdentifierException(TestCase test, String result) {343this.test = test;344this.result = result;345}346public String getMessage() {347return "Test " + test.id + " " + "Failed: "348+ "Expected = " + test.resolvedVmIdentifier + " "349+ "Actual = " + result;350}351}352353class TestCase {354private static final boolean debug = false;355356String id;357String vmid;358String hostid;359String expectedVmIdentifier;360String expectedHostIdentifier;361String resolvedVmIdentifier;362String description;363364public TestCase(String id, String vmid, String hostid) {365this.id = id;366this.vmid = vmid;367this.hostid = hostid;368}369370public void run() throws Exception {371if (expectedVmIdentifier == null || expectedHostIdentifier == null372|| resolvedVmIdentifier == null) {373throw new IllegalArgumentException(374"expected values not initialized");375}376377VmIdentifier test_vmid = null;378HostIdentifier test_hostid = null;379VmIdentifier resolved_vmid = null;380381if (debug) {382System.out.println("creating VmIdentifier");383}384385test_vmid = new VmIdentifier(vmid);386387if (debug) {388System.out.println("creating HostIdentifier");389}390391if (hostid != null) {392test_hostid = new HostIdentifier(hostid);393} else {394test_hostid = new HostIdentifier(test_vmid);395}396397if (debug) {398System.out.println("resolving VmIdentifier");399}400401resolved_vmid = test_hostid.resolve(test_vmid);402403String test_vmid_str = test_vmid.toString();404String test_hostid_str = test_hostid.toString();405String resolved_vmid_str = resolved_vmid.toString();406407if (debug) {408System.out.println("comparing VmIdentifier result");409}410411if (test_vmid_str.compareTo(expectedVmIdentifier) != 0) {412throw new VmIdentifierException(this, test_vmid_str);413}414415if (debug) {416System.out.println("comparing HostIdentifier result");417}418419if (test_hostid_str.compareTo(expectedHostIdentifier) != 0) {420throw new HostIdentifierException(this, test_hostid_str);421}422423if (debug) {424System.out.println("comparing VmIdentifier result");425}426427if (resolved_vmid_str.compareTo(resolvedVmIdentifier) != 0) {428throw new ResolvedVmIdentifierException(this, resolved_vmid_str);429}430}431432public void setDescription(String description) {433this.description = description;434}435436public void setExpectedVmIdentifier(String expectedVmIdentifier) {437if (debug) {438System.out.println("setting vmidentifier string to " + vmid);439}440this.expectedVmIdentifier = expectedVmIdentifier;441}442443public void setExpectedHostIdentifier(String expectedHostIdentifier) {444this.expectedHostIdentifier = expectedHostIdentifier;445}446447public void setResolvedVmIdentifier(String resolvedVmIdentifier) {448this.resolvedVmIdentifier = resolvedVmIdentifier;449}450}451452453