0N/A/*
2362N/A * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4990825
0N/A * @summary test that VmIdentifier objects get created as expected
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.net.*;
0N/Aimport javax.xml.parsers.*;
0N/Aimport org.xml.sax.*;
0N/Aimport org.xml.sax.helpers.DefaultHandler;
0N/Aimport sun.jvmstat.monitor.*;
0N/A
0N/Apublic class VmIdentifierCreateResolve {
0N/A
0N/A public static void main(String args[]) throws Exception {
0N/A File testcases =
0N/A new File(System.getProperty("test.src", "."), "testcases");
0N/A
0N/A SAXParserFactory spf = SAXParserFactory.newInstance();
0N/A SAXParser sp = spf.newSAXParser();
0N/A DefaultHandler dh = new VmIdentifierTestHandler();
0N/A sp.parse(testcases, dh);
0N/A }
0N/A}
0N/A
0N/Aclass VmIdentifierTestHandler extends DefaultHandler {
0N/A private static final boolean debug = false;
0N/A private static final int START = 0;
0N/A private static final int VMIDENTIFIER_TESTS = 1;
0N/A private static final int TESTCASE = 2;
0N/A private static final int DESCRIPTION = 3;
0N/A private static final int VMIDENTIFIER = 4;
0N/A private static final int HOSTIDENTIFIER = 5;
0N/A private static final int RESOLVED = 6;
0N/A
0N/A private TestCase test;
0N/A private String value = null;
0N/A private int state;
0N/A private Attributes attributes;
0N/A
0N/A public VmIdentifierTestHandler() {
0N/A super();
0N/A }
0N/A
0N/A public void characters(char[] ch, int start, int length) {
0N/A String s = new String(ch, start, length);
0N/A if (debug) {
0N/A System.out.println("characters: start = " + start +
0N/A " length = " + length +
0N/A " chars = " + s);
0N/A }
0N/A
0N/A if (value == null) {
0N/A value = s.trim();
0N/A } else {
0N/A value = value + s.trim();
0N/A if (debug) {
0N/A System.out.println("characters: appended characters to "
0N/A + "previous value: new value = " + value);
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void endDocument() {
0N/A if (debug) {
0N/A System.out.println("endDocument()");
0N/A }
0N/A }
0N/A
0N/A public void endElement(String namespaceURI, String localName,
0N/A String qName)
0N/A throws SAXException {
0N/A if (debug) {
0N/A System.out.println("endElement(): namespaceURI = " + namespaceURI
0N/A + " localName = " + localName
0N/A + " qName = " + qName
0N/A + " state = " + state);
0N/A }
0N/A
0N/A switch (state) {
0N/A case START:
0N/A throw new RuntimeException("Unexpected state: " + state);
0N/A
0N/A case VMIDENTIFIER_TESTS:
0N/A state = START;
0N/A break;
0N/A
0N/A case TESTCASE:
0N/A if (test == null) {
0N/A throw new RuntimeException("Unexpected thread state");
0N/A }
0N/A try {
0N/A System.out.println("running test case " + test.id);
0N/A test.run(); // run the test
0N/A }
0N/A catch (Exception e) {
0N/A throw new SAXException(e);
0N/A }
0N/A state = VMIDENTIFIER_TESTS;
0N/A test = null;
0N/A value = null;
0N/A break;
0N/A
0N/A case DESCRIPTION:
0N/A test.setDescription(value);
0N/A state = TESTCASE;
0N/A value = null;
0N/A break;
0N/A
0N/A case VMIDENTIFIER:
0N/A test.setExpectedVmIdentifier(value);
0N/A state = TESTCASE;
0N/A value = null;
0N/A break;
0N/A
0N/A case HOSTIDENTIFIER:
0N/A test.setExpectedHostIdentifier(value);
0N/A state = TESTCASE;
0N/A value = null;
0N/A break;
0N/A
0N/A case RESOLVED:
0N/A test.setResolvedVmIdentifier(value);
0N/A state = TESTCASE;
0N/A value = null;
0N/A break;
0N/A
0N/A default:
0N/A throw new RuntimeException("Unexpected state: " + state);
0N/A }
0N/A }
0N/A
0N/A public void endPrefixMapping(String prefix) {
0N/A if (debug) {
0N/A System.out.println("endPrefixMapping(): prefix = " + prefix);
0N/A }
0N/A }
0N/A
0N/A public void ignorableWhitespace(char[] ch, int start, int length) {
0N/A if (debug) {
0N/A System.out.println("ignoreableWhitespace():"
0N/A + " ch = " + new String(ch, start, length)
0N/A + " start = " + start
0N/A + " length = " + length);
0N/A }
0N/A }
0N/A
0N/A public void processingInstruction(String target, String data) {
0N/A if (debug) {
0N/A System.out.println("processingInstruction():"
0N/A + " target = " + target
0N/A + " data = " + data);
0N/A }
0N/A }
0N/A
0N/A public void setDocumentLocator(Locator locator) {
0N/A if (debug) {
0N/A System.out.println("setDocumentLocator(): locator = " + locator);
0N/A }
0N/A }
0N/A
0N/A public void skippedEntity(String name) {
0N/A if (debug) {
0N/A System.out.println("skippedEntity(): name = " + name);
0N/A }
0N/A }
0N/A
0N/A public void startDocument() {
0N/A if (debug) {
0N/A System.out.println("startDocument():");
0N/A }
0N/A }
0N/A
0N/A public void startElement(String namespaceURI, String localName,
0N/A String qName, Attributes attributes) {
0N/A if (debug) {
0N/A System.out.println("startElement():"
0N/A + " namespaceURI = " + namespaceURI
0N/A + " localName = " + localName
0N/A + " qName = " + qName
0N/A + " state = " + state);
0N/A
0N/A System.out.println(" Attributes(" + attributes.getLength() + ")");
0N/A for (int i = 0; i < attributes.getLength(); i++) {
0N/A System.out.println(" name = " + attributes.getQName(i)
0N/A + " value = " + attributes.getValue(i));
0N/A }
0N/A }
0N/A
0N/A this.attributes = attributes;
0N/A
0N/A switch (state) {
0N/A case START:
0N/A if (qName.compareTo("VmIdentifierTests") == 0) {
0N/A state = VMIDENTIFIER_TESTS;
0N/A } else {
0N/A System.err.println("unexpected input: state = " + state
0N/A + " input = " + qName);
0N/A }
0N/A break;
0N/A
0N/A case VMIDENTIFIER_TESTS:
0N/A if (qName.compareTo("testcase") == 0) {
0N/A state = TESTCASE;
0N/A int id_n = attributes.getIndex("id");
0N/A
0N/A if (id_n == -1) {
0N/A throw new RuntimeException("id attribute expected");
0N/A }
0N/A
0N/A int vmid_n = attributes.getIndex("VmIdentifierInput");
0N/A if (vmid_n == -1) {
0N/A throw new RuntimeException(
0N/A "VmIdentifier attribute expected");
0N/A }
0N/A
0N/A String hostid_input = null;
0N/A int hostid_n = attributes.getIndex("HostIdentifierInput");
0N/A if (hostid_n != -1) {
0N/A hostid_input = attributes.getValue(hostid_n);
0N/A }
0N/A
0N/A String vmid_input = attributes.getValue(vmid_n);
0N/A String id = attributes.getValue(id_n);
0N/A
0N/A test = new TestCase(id, vmid_input, hostid_input);
0N/A } else {
0N/A System.err.println("unexpected input: state = " + state
0N/A + " input = " + qName);
0N/A }
0N/A break;
0N/A
0N/A case TESTCASE:
0N/A if (test == null) {
0N/A throw new RuntimeException("TestCase null");
0N/A }
0N/A value = null;
0N/A if (qName.compareTo("description") == 0) {
0N/A state = DESCRIPTION;
0N/A
0N/A } else if (qName.compareTo("VmIdentifier") == 0) {
0N/A state = VMIDENTIFIER;
0N/A
0N/A } else if (qName.compareTo("HostIdentifier") == 0) {
0N/A state = HOSTIDENTIFIER;
0N/A
0N/A } else if (qName.compareTo("Resolved") == 0) {
0N/A state = RESOLVED;
0N/A
0N/A } else {
0N/A System.err.println("unexpected input: state = " + state
0N/A + " input = " + qName);
0N/A }
0N/A break;
0N/A
0N/A case DESCRIPTION:
0N/A case VMIDENTIFIER:
0N/A case HOSTIDENTIFIER:
0N/A case RESOLVED:
0N/A if (test == null) {
0N/A throw new RuntimeException("TestCase null");
0N/A }
0N/A break;
0N/A
0N/A default:
0N/A System.err.println("Unexpected state: " + state);
0N/A break;
0N/A }
0N/A }
0N/A
0N/A public void startPrefixMapping(String prefix, String uri) {
0N/A if (debug) {
0N/A System.out.println("startPrefixMapping():"
0N/A + " prefix = " + prefix
0N/A + " uri = " + uri);
0N/A }
0N/A }
0N/A}
0N/A
0N/Aclass VmIdentifierException extends Exception {
0N/A String result;
0N/A TestCase test;
0N/A
0N/A VmIdentifierException(TestCase test, String result) {
0N/A this.test = test;
0N/A this.result = result;
0N/A }
0N/A
0N/A public String getMessage() {
0N/A return "Test " + test.id + " " + "Failed: "
0N/A + "Expected = " + test.expectedVmIdentifier + " "
0N/A + "Actual = " + result;
0N/A }
0N/A}
0N/A
0N/Aclass HostIdentifierException extends Exception {
0N/A String result;
0N/A TestCase test;
0N/A
0N/A HostIdentifierException(TestCase test, String result) {
0N/A this.test = test;
0N/A this.result = result;
0N/A }
0N/A
0N/A public String getMessage() {
0N/A return "Test " + test.id + " " + "Failed: "
0N/A + "Expected = " + test.expectedHostIdentifier + " "
0N/A + "Actual = " + result;
0N/A }
0N/A}
0N/A
0N/Aclass ResolvedVmIdentifierException extends Exception {
0N/A String result;
0N/A TestCase test;
0N/A
0N/A ResolvedVmIdentifierException(TestCase test, String result) {
0N/A this.test = test;
0N/A this.result = result;
0N/A }
0N/A public String getMessage() {
0N/A return "Test " + test.id + " " + "Failed: "
0N/A + "Expected = " + test.resolvedVmIdentifier + " "
0N/A + "Actual = " + result;
0N/A }
0N/A}
0N/A
0N/Aclass TestCase {
0N/A private static final boolean debug = false;
0N/A
0N/A String id;
0N/A String vmid;
0N/A String hostid;
0N/A String expectedVmIdentifier;
0N/A String expectedHostIdentifier;
0N/A String resolvedVmIdentifier;
0N/A String description;
0N/A
0N/A public TestCase(String id, String vmid, String hostid) {
0N/A this.id = id;
0N/A this.vmid = vmid;
0N/A this.hostid = hostid;
0N/A }
0N/A
0N/A public void run() throws Exception {
0N/A if (expectedVmIdentifier == null || expectedHostIdentifier == null
0N/A || resolvedVmIdentifier == null) {
0N/A throw new IllegalArgumentException(
0N/A "expected values not initialized");
0N/A }
0N/A
0N/A VmIdentifier test_vmid = null;
0N/A HostIdentifier test_hostid = null;
0N/A VmIdentifier resolved_vmid = null;
0N/A
0N/A if (debug) {
0N/A System.out.println("creating VmIdentifier");
0N/A }
0N/A
0N/A test_vmid = new VmIdentifier(vmid);
0N/A
0N/A if (debug) {
0N/A System.out.println("creating HostIdentifier");
0N/A }
0N/A
0N/A if (hostid != null) {
0N/A test_hostid = new HostIdentifier(hostid);
0N/A } else {
0N/A test_hostid = new HostIdentifier(test_vmid);
0N/A }
0N/A
0N/A if (debug) {
0N/A System.out.println("resolving VmIdentifier");
0N/A }
0N/A
0N/A resolved_vmid = test_hostid.resolve(test_vmid);
0N/A
0N/A String test_vmid_str = test_vmid.toString();
0N/A String test_hostid_str = test_hostid.toString();
0N/A String resolved_vmid_str = resolved_vmid.toString();
0N/A
0N/A if (debug) {
0N/A System.out.println("comparing VmIdentifier result");
0N/A }
0N/A
0N/A if (test_vmid_str.compareTo(expectedVmIdentifier) != 0) {
0N/A throw new VmIdentifierException(this, test_vmid_str);
0N/A }
0N/A
0N/A if (debug) {
0N/A System.out.println("comparing HostIdentifier result");
0N/A }
0N/A
0N/A if (test_hostid_str.compareTo(expectedHostIdentifier) != 0) {
0N/A throw new HostIdentifierException(this, test_hostid_str);
0N/A }
0N/A
0N/A if (debug) {
0N/A System.out.println("comparing VmIdentifier result");
0N/A }
0N/A
0N/A if (resolved_vmid_str.compareTo(resolvedVmIdentifier) != 0) {
0N/A throw new ResolvedVmIdentifierException(this, resolved_vmid_str);
0N/A }
0N/A }
0N/A
0N/A public void setDescription(String description) {
0N/A this.description = description;
0N/A }
0N/A
0N/A public void setExpectedVmIdentifier(String expectedVmIdentifier) {
0N/A if (debug) {
0N/A System.out.println("setting vmidentifier string to " + vmid);
0N/A }
0N/A this.expectedVmIdentifier = expectedVmIdentifier;
0N/A }
0N/A
0N/A public void setExpectedHostIdentifier(String expectedHostIdentifier) {
0N/A this.expectedHostIdentifier = expectedHostIdentifier;
0N/A }
0N/A
0N/A public void setResolvedVmIdentifier(String resolvedVmIdentifier) {
0N/A this.resolvedVmIdentifier = resolvedVmIdentifier;
0N/A }
0N/A}