Path: blob/master/test/jdk/sun/jvmstat/monitor/HostIdentifier/HostIdentifierCreate.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 HostIdentifier 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 HostIdentifierCreate {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 HostIdentifierTestHandler();48sp.parse(testcases, dh);49}50}5152class HostIdentifierTestHandler extends DefaultHandler {53private static final boolean debug = false;54private static final int START = 0;55private static final int HOSTIDENTIFIER_TESTS = 1;56private static final int TESTCASE = 2;57private static final int DESCRIPTION = 3;58private static final int HOSTIDENTIFIER = 4;5960private TestCase test;61private String value = null;62private int state;63private Attributes attributes;6465public HostIdentifierTestHandler() {66super();67}6869public void characters(char[] ch, int start, int length) {70String s = new String(ch, start, length);71if (debug) {72System.out.println("characters: start = " + start +73" length = " + length +74" chars = " + s);75}7677if (value == null) {78value = s.trim();79} else {80value = value + s.trim();81if (debug) {82System.out.println("characters: appended characters to "83+ "previous value: new value = " + value);84}85}86}8788public void endDocument() {89if (debug) {90System.out.println("endDocument()");91}92}9394public void endElement(String namespaceURI, String localName,95String qName)96throws SAXException {97if (debug) {98System.out.println("endElement(): namespaceURI = " + namespaceURI99+ " localName = " + localName100+ " qName = " + qName101+ " state = " + state);102}103104switch (state) {105case START:106throw new RuntimeException("Unexpected state: " + state);107108case HOSTIDENTIFIER_TESTS:109state = START;110break;111112case TESTCASE:113if (test == null) {114throw new RuntimeException("Unexpected thread state");115}116try {117System.out.println("running test case " + test.id);118test.run(); // run the test119}120catch (Exception e) {121throw new SAXException("Testcase id = " + test.id, e);122}123state = HOSTIDENTIFIER_TESTS;124test = null;125value = null;126break;127128case DESCRIPTION:129test.setDescription(value);130state = TESTCASE;131value = null;132break;133134case HOSTIDENTIFIER:135test.setExpectedHostIdentifier(value);136state = TESTCASE;137value = null;138break;139140default:141throw new RuntimeException("Unexpected state: " + state);142}143}144145public void endPrefixMapping(String prefix) {146if (debug) {147System.out.println("endPrefixMapping(): prefix = " + prefix);148}149}150151public void ignorableWhitespace(char[] ch, int start, int length) {152if (debug) {153System.out.println("ignoreableWhitespace():"154+ " ch = " + new String(ch, start, length)155+ " start = " + start156+ " length = " + length);157}158}159160public void processingInstruction(String target, String data) {161if (debug) {162System.out.println("processingInstruction():"163+ " target = " + target164+ " data = " + data);165}166}167168public void setDocumentLocator(Locator locator) {169if (debug) {170System.out.println("setDocumentLocator(): locator = " + locator);171}172}173174public void skippedEntity(String name) {175if (debug) {176System.out.println("skippedEntity(): name = " + name);177}178}179180public void startDocument() {181if (debug) {182System.out.println("startDocument():");183}184}185186public void startElement(String namespaceURI, String localName,187String qName, Attributes attributes) {188if (debug) {189System.out.println("startElement():"190+ " namespaceURI = " + namespaceURI191+ " localName = " + localName192+ " qName = " + qName193+ " state = " + state);194195System.out.println(" Attributes(" + attributes.getLength() + ")");196for (int i = 0; i < attributes.getLength(); i++) {197System.out.println(" name = " + attributes.getQName(i)198+ " value = " + attributes.getValue(i));199}200}201202this.attributes = attributes;203204switch (state) {205case START:206if (qName.compareTo("HostIdentifierTests") == 0) {207state = HOSTIDENTIFIER_TESTS;208} else {209System.err.println("unexpected input: state = " + state210+ " input = " + qName);211}212break;213214case HOSTIDENTIFIER_TESTS:215if (qName.compareTo("testcase") == 0) {216state = TESTCASE;217int id_n = attributes.getIndex("id");218219if (id_n == -1) {220throw new RuntimeException("id attribute expected");221}222223String hostid_input = null;224int hostid_n = attributes.getIndex("HostIdentifierInput");225if (hostid_n != -1) {226hostid_input = attributes.getValue(hostid_n);227if (hostid_input.length() == 0) {228hostid_input = null;229}230}231232String id = attributes.getValue(id_n);233234test = new TestCase(id, hostid_input);235} else {236System.err.println("unexpected input: state = " + state237+ " input = " + qName);238}239break;240241case TESTCASE:242if (test == null) {243throw new RuntimeException("TestCase null");244}245value = null;246if (qName.compareTo("description") == 0) {247state = DESCRIPTION;248249} else if (qName.compareTo("HostIdentifier") == 0) {250state = HOSTIDENTIFIER;251252} else {253System.err.println("unexpected input: state = " + state254+ " input = " + qName);255}256break;257258case DESCRIPTION:259case HOSTIDENTIFIER:260if (test == null) {261throw new RuntimeException("TestCase null");262}263break;264265default:266System.err.println("Unexpected state: " + state);267break;268}269}270271public void startPrefixMapping(String prefix, String uri) {272if (debug) {273System.out.println("startPrefixMapping():"274+ " prefix = " + prefix275+ " uri = " + uri);276}277}278}279280class HostIdentifierException extends Exception {281String result;282TestCase test;283284HostIdentifierException(TestCase test, String result) {285this.test = test;286this.result = result;287}288289public String getMessage() {290return "Test " + test.id + " " + "Failed: "291+ "Expected = " + test.expectedHostIdentifier + " "292+ "Actual = " + result;293}294}295296class TestCase {297private static final boolean debug = false;298299String id;300String hostid;301String expectedHostIdentifier;302String description;303304public TestCase(String id, String hostid) {305this.id = id;306this.hostid = hostid;307}308309public void run() throws Exception {310if (expectedHostIdentifier == null) {311throw new IllegalArgumentException(312"HostIdentifier not initialized");313}314315HostIdentifier test_hostid = new HostIdentifier(hostid);316317String test_hostid_str = test_hostid.toString();318319if (debug) {320System.out.println("comparing HostIdentifier result");321}322323if (test_hostid_str.compareTo(expectedHostIdentifier) != 0) {324throw new HostIdentifierException(this, test_hostid_str);325}326}327328public void setDescription(String description) {329this.description = description;330}331332public void setExpectedHostIdentifier(String expectedHostIdentifier) {333this.expectedHostIdentifier = expectedHostIdentifier;334}335}336337338