Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/jvmstat/monitor/HostIdentifier/HostIdentifierCreate.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 HostIdentifier 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 HostIdentifierCreate {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 HostIdentifierTestHandler();45sp.parse(testcases, dh);46}47}4849class HostIdentifierTestHandler extends DefaultHandler {50private static final boolean debug = false;51private static final int START = 0;52private static final int HOSTIDENTIFIER_TESTS = 1;53private static final int TESTCASE = 2;54private static final int DESCRIPTION = 3;55private static final int HOSTIDENTIFIER = 4;5657private TestCase test;58private String value = null;59private int state;60private Attributes attributes;6162public HostIdentifierTestHandler() {63super();64}6566public void characters(char[] ch, int start, int length) {67String s = new String(ch, start, length);68if (debug) {69System.out.println("characters: start = " + start +70" length = " + length +71" chars = " + s);72}7374if (value == null) {75value = s.trim();76} else {77value = value + s.trim();78if (debug) {79System.out.println("characters: appended characters to "80+ "previous value: new value = " + value);81}82}83}8485public void endDocument() {86if (debug) {87System.out.println("endDocument()");88}89}9091public void endElement(String namespaceURI, String localName,92String qName)93throws SAXException {94if (debug) {95System.out.println("endElement(): namespaceURI = " + namespaceURI96+ " localName = " + localName97+ " qName = " + qName98+ " state = " + state);99}100101switch (state) {102case START:103throw new RuntimeException("Unexpected state: " + state);104105case HOSTIDENTIFIER_TESTS:106state = START;107break;108109case TESTCASE:110if (test == null) {111throw new RuntimeException("Unexpected thread state");112}113try {114System.out.println("running test case " + test.id);115test.run(); // run the test116}117catch (Exception e) {118throw new SAXException("Testcase id = " + test.id, e);119}120state = HOSTIDENTIFIER_TESTS;121test = null;122value = null;123break;124125case DESCRIPTION:126test.setDescription(value);127state = TESTCASE;128value = null;129break;130131case HOSTIDENTIFIER:132test.setExpectedHostIdentifier(value);133state = TESTCASE;134value = null;135break;136137default:138throw new RuntimeException("Unexpected state: " + state);139}140}141142public void endPrefixMapping(String prefix) {143if (debug) {144System.out.println("endPrefixMapping(): prefix = " + prefix);145}146}147148public void ignorableWhitespace(char[] ch, int start, int length) {149if (debug) {150System.out.println("ignoreableWhitespace():"151+ " ch = " + new String(ch, start, length)152+ " start = " + start153+ " length = " + length);154}155}156157public void processingInstruction(String target, String data) {158if (debug) {159System.out.println("processingInstruction():"160+ " target = " + target161+ " data = " + data);162}163}164165public void setDocumentLocator(Locator locator) {166if (debug) {167System.out.println("setDocumentLocator(): locator = " + locator);168}169}170171public void skippedEntity(String name) {172if (debug) {173System.out.println("skippedEntity(): name = " + name);174}175}176177public void startDocument() {178if (debug) {179System.out.println("startDocument():");180}181}182183public void startElement(String namespaceURI, String localName,184String qName, Attributes attributes) {185if (debug) {186System.out.println("startElement():"187+ " namespaceURI = " + namespaceURI188+ " localName = " + localName189+ " qName = " + qName190+ " state = " + state);191192System.out.println(" Attributes(" + attributes.getLength() + ")");193for (int i = 0; i < attributes.getLength(); i++) {194System.out.println(" name = " + attributes.getQName(i)195+ " value = " + attributes.getValue(i));196}197}198199this.attributes = attributes;200201switch (state) {202case START:203if (qName.compareTo("HostIdentifierTests") == 0) {204state = HOSTIDENTIFIER_TESTS;205} else {206System.err.println("unexpected input: state = " + state207+ " input = " + qName);208}209break;210211case HOSTIDENTIFIER_TESTS:212if (qName.compareTo("testcase") == 0) {213state = TESTCASE;214int id_n = attributes.getIndex("id");215216if (id_n == -1) {217throw new RuntimeException("id attribute expected");218}219220String hostid_input = null;221int hostid_n = attributes.getIndex("HostIdentifierInput");222if (hostid_n != -1) {223hostid_input = attributes.getValue(hostid_n);224if (hostid_input.length() == 0) {225hostid_input = null;226}227}228229String id = attributes.getValue(id_n);230231test = new TestCase(id, hostid_input);232} else {233System.err.println("unexpected input: state = " + state234+ " input = " + qName);235}236break;237238case TESTCASE:239if (test == null) {240throw new RuntimeException("TestCase null");241}242value = null;243if (qName.compareTo("description") == 0) {244state = DESCRIPTION;245246} else if (qName.compareTo("HostIdentifier") == 0) {247state = HOSTIDENTIFIER;248249} else {250System.err.println("unexpected input: state = " + state251+ " input = " + qName);252}253break;254255case DESCRIPTION:256case HOSTIDENTIFIER:257if (test == null) {258throw new RuntimeException("TestCase null");259}260break;261262default:263System.err.println("Unexpected state: " + state);264break;265}266}267268public void startPrefixMapping(String prefix, String uri) {269if (debug) {270System.out.println("startPrefixMapping():"271+ " prefix = " + prefix272+ " uri = " + uri);273}274}275}276277class HostIdentifierException extends Exception {278String result;279TestCase test;280281HostIdentifierException(TestCase test, String result) {282this.test = test;283this.result = result;284}285286public String getMessage() {287return "Test " + test.id + " " + "Failed: "288+ "Expected = " + test.expectedHostIdentifier + " "289+ "Actual = " + result;290}291}292293class TestCase {294private static final boolean debug = false;295296String id;297String hostid;298String expectedHostIdentifier;299String description;300301public TestCase(String id, String hostid) {302this.id = id;303this.hostid = hostid;304}305306public void run() throws Exception {307if (expectedHostIdentifier == null) {308throw new IllegalArgumentException(309"HostIdentifier not initialized");310}311312HostIdentifier test_hostid = new HostIdentifier(hostid);313314String test_hostid_str = test_hostid.toString();315316if (debug) {317System.out.println("comparing HostIdentifier result");318}319320if (test_hostid_str.compareTo(expectedHostIdentifier) != 0) {321throw new HostIdentifierException(this, test_hostid_str);322}323}324325public void setDescription(String description) {326this.description = description;327}328329public void setExpectedHostIdentifier(String expectedHostIdentifier) {330this.expectedHostIdentifier = expectedHostIdentifier;331}332}333334335