VmIdentifierCreateResolve.java revision 0:37a05a11f281
1/*
2 * Copyright 2004 Sun Microsystems, Inc.  All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @bug 4990825
27 * @summary test that VmIdentifier objects get created as expected
28 */
29
30import java.io.*;
31import java.net.*;
32import javax.xml.parsers.*;
33import org.xml.sax.*;
34import org.xml.sax.helpers.DefaultHandler;
35import sun.jvmstat.monitor.*;
36
37public class VmIdentifierCreateResolve {
38
39    public static void main(String args[]) throws Exception {
40        File testcases =
41                new File(System.getProperty("test.src", "."), "testcases");
42
43        SAXParserFactory spf = SAXParserFactory.newInstance();
44        SAXParser sp = spf.newSAXParser();
45        DefaultHandler dh = new VmIdentifierTestHandler();
46        sp.parse(testcases, dh);
47    }
48}
49
50class VmIdentifierTestHandler extends DefaultHandler {
51    private static final boolean debug = false;
52    private static final int START                     = 0;
53    private static final int VMIDENTIFIER_TESTS        = 1;
54    private static final int TESTCASE                  = 2;
55    private static final int DESCRIPTION               = 3;
56    private static final int VMIDENTIFIER              = 4;
57    private static final int HOSTIDENTIFIER            = 5;
58    private static final int RESOLVED                  = 6;
59
60    private TestCase test;
61    private String value = null;
62    private int state;
63    private Attributes attributes;
64
65    public VmIdentifierTestHandler() {
66        super();
67    }
68
69    public void characters(char[] ch, int start, int length) {
70        String s = new String(ch, start, length);
71        if (debug) {
72            System.out.println("characters: start = " + start +
73                               " length = " + length +
74                               " chars = " + s);
75        }
76
77        if (value == null) {
78            value = s.trim();
79        } else {
80            value = value + s.trim();
81            if (debug) {
82                System.out.println("characters: appended characters to "
83                                   + "previous value: new value = " + value);
84            }
85        }
86    }
87
88    public void endDocument() {
89        if (debug) {
90            System.out.println("endDocument()");
91        }
92    }
93
94    public void endElement(String namespaceURI, String localName,
95                           String qName)
96                throws SAXException {
97        if (debug) {
98            System.out.println("endElement(): namespaceURI = " + namespaceURI
99                               + " localName = " + localName
100                               + " qName = " + qName
101                               + " state = " + state);
102        }
103
104        switch (state) {
105        case START:
106            throw new RuntimeException("Unexpected state: " + state);
107
108        case VMIDENTIFIER_TESTS:
109            state = START;
110            break;
111
112        case TESTCASE:
113            if (test == null) {
114                throw new RuntimeException("Unexpected thread state");
115            }
116            try {
117              System.out.println("running test case " + test.id);
118              test.run();            // run the test
119            }
120            catch (Exception e) {
121              throw new SAXException(e);
122            }
123            state = VMIDENTIFIER_TESTS;
124            test = null;
125            value = null;
126            break;
127
128        case DESCRIPTION:
129            test.setDescription(value);
130            state = TESTCASE;
131            value = null;
132            break;
133
134        case VMIDENTIFIER:
135            test.setExpectedVmIdentifier(value);
136            state = TESTCASE;
137            value = null;
138            break;
139
140        case HOSTIDENTIFIER:
141            test.setExpectedHostIdentifier(value);
142            state = TESTCASE;
143            value = null;
144            break;
145
146        case RESOLVED:
147            test.setResolvedVmIdentifier(value);
148            state = TESTCASE;
149            value = null;
150            break;
151
152        default:
153            throw new RuntimeException("Unexpected state: " + state);
154        }
155    }
156
157    public void endPrefixMapping(String prefix) {
158        if (debug) {
159            System.out.println("endPrefixMapping(): prefix = " + prefix);
160        }
161    }
162
163    public void ignorableWhitespace(char[] ch, int start, int length) {
164        if (debug) {
165            System.out.println("ignoreableWhitespace():"
166                               + " ch = " + new String(ch, start, length)
167                               + " start = " + start
168                               + " length = " + length);
169        }
170    }
171
172    public void processingInstruction(String target, String data) {
173        if (debug) {
174            System.out.println("processingInstruction():"
175                               + " target = " + target
176                               + " data = " + data);
177        }
178    }
179
180    public void setDocumentLocator(Locator locator) {
181        if (debug) {
182            System.out.println("setDocumentLocator(): locator = " + locator);
183        }
184    }
185
186    public void skippedEntity(String name) {
187        if (debug) {
188            System.out.println("skippedEntity(): name = " + name);
189        }
190    }
191
192    public void startDocument() {
193        if (debug) {
194            System.out.println("startDocument():");
195        }
196    }
197
198    public void startElement(String namespaceURI, String localName,
199                             String qName, Attributes attributes) {
200        if (debug) {
201            System.out.println("startElement():"
202                               + " namespaceURI = " + namespaceURI
203                               + " localName = " + localName
204                               + " qName = " + qName
205                               + " state = " + state);
206
207            System.out.println("   Attributes(" + attributes.getLength() + ")");
208            for (int i = 0; i < attributes.getLength(); i++) {
209                System.out.println("     name = " + attributes.getQName(i)
210                                   + " value = " + attributes.getValue(i));
211            }
212        }
213
214        this.attributes = attributes;
215
216        switch (state) {
217        case START:
218            if (qName.compareTo("VmIdentifierTests") == 0) {
219                state = VMIDENTIFIER_TESTS;
220            } else {
221                System.err.println("unexpected input: state = " + state
222                                   + " input = " + qName);
223            }
224            break;
225
226        case VMIDENTIFIER_TESTS:
227            if (qName.compareTo("testcase") == 0) {
228                state = TESTCASE;
229                int id_n = attributes.getIndex("id");
230
231                if (id_n == -1) {
232                    throw new RuntimeException("id attribute expected");
233                }
234
235                int vmid_n = attributes.getIndex("VmIdentifierInput");
236                if (vmid_n == -1) {
237                    throw new RuntimeException(
238                            "VmIdentifier attribute expected");
239                }
240
241                String hostid_input = null;
242                int hostid_n = attributes.getIndex("HostIdentifierInput");
243                if (hostid_n != -1) {
244                    hostid_input = attributes.getValue(hostid_n);
245                }
246
247                String vmid_input = attributes.getValue(vmid_n);
248                String id = attributes.getValue(id_n);
249
250                test = new TestCase(id, vmid_input, hostid_input);
251            } else {
252                System.err.println("unexpected input: state = " + state
253                                   + " input = " + qName);
254            }
255            break;
256
257        case TESTCASE:
258            if (test == null) {
259                throw new RuntimeException("TestCase null");
260            }
261            value = null;
262            if (qName.compareTo("description") == 0) {
263                state = DESCRIPTION;
264
265            } else if (qName.compareTo("VmIdentifier") == 0) {
266                state = VMIDENTIFIER;
267
268            } else if (qName.compareTo("HostIdentifier") == 0) {
269                state = HOSTIDENTIFIER;
270
271            } else if (qName.compareTo("Resolved") == 0) {
272                state = RESOLVED;
273
274            } else {
275                System.err.println("unexpected input: state = " + state
276                                   + " input = " + qName);
277            }
278            break;
279
280        case DESCRIPTION:
281        case VMIDENTIFIER:
282        case HOSTIDENTIFIER:
283        case RESOLVED:
284            if (test == null) {
285                throw new RuntimeException("TestCase null");
286            }
287            break;
288
289        default:
290            System.err.println("Unexpected state: " + state);
291            break;
292        }
293    }
294
295    public void startPrefixMapping(String prefix, String uri) {
296        if (debug) {
297            System.out.println("startPrefixMapping():"
298                               + " prefix = " + prefix
299                               + " uri = " + uri);
300        }
301    }
302}
303
304class VmIdentifierException extends Exception {
305    String result;
306    TestCase test;
307
308    VmIdentifierException(TestCase test, String result) {
309        this.test = test;
310        this.result = result;
311    }
312
313    public String getMessage() {
314        return "Test " + test.id + " " + "Failed: "
315               + "Expected = " + test.expectedVmIdentifier + " "
316               + "Actual = " + result;
317    }
318}
319
320class HostIdentifierException extends Exception {
321    String result;
322    TestCase test;
323
324    HostIdentifierException(TestCase test, String result) {
325        this.test = test;
326        this.result = result;
327    }
328
329    public String getMessage() {
330        return "Test " + test.id + " " + "Failed: "
331               + "Expected = " + test.expectedHostIdentifier + " "
332               + "Actual = " + result;
333    }
334}
335
336class ResolvedVmIdentifierException extends Exception {
337    String result;
338    TestCase test;
339
340    ResolvedVmIdentifierException(TestCase test, String result) {
341        this.test = test;
342        this.result = result;
343    }
344    public String getMessage() {
345        return "Test " + test.id + " " + "Failed: "
346               + "Expected = " + test.resolvedVmIdentifier + " "
347               + "Actual = " + result;
348    }
349}
350
351class TestCase {
352    private static final boolean debug = false;
353
354    String id;
355    String vmid;
356    String hostid;
357    String expectedVmIdentifier;
358    String expectedHostIdentifier;
359    String resolvedVmIdentifier;
360    String description;
361
362    public TestCase(String id, String vmid, String hostid) {
363        this.id = id;
364        this.vmid = vmid;
365        this.hostid = hostid;
366    }
367
368    public void run() throws Exception {
369        if (expectedVmIdentifier == null || expectedHostIdentifier == null
370                || resolvedVmIdentifier == null) {
371            throw new IllegalArgumentException(
372                    "expected values not initialized");
373        }
374
375        VmIdentifier test_vmid = null;
376        HostIdentifier test_hostid = null;
377        VmIdentifier resolved_vmid =  null;
378
379        if (debug) {
380            System.out.println("creating VmIdentifier");
381        }
382
383        test_vmid = new VmIdentifier(vmid);
384
385        if (debug) {
386            System.out.println("creating HostIdentifier");
387        }
388
389        if (hostid != null) {
390            test_hostid = new HostIdentifier(hostid);
391        } else {
392            test_hostid = new HostIdentifier(test_vmid);
393        }
394
395        if (debug) {
396            System.out.println("resolving VmIdentifier");
397        }
398
399        resolved_vmid =  test_hostid.resolve(test_vmid);
400
401        String test_vmid_str = test_vmid.toString();
402        String test_hostid_str = test_hostid.toString();
403        String resolved_vmid_str = resolved_vmid.toString();
404
405        if (debug) {
406            System.out.println("comparing VmIdentifier result");
407        }
408
409        if (test_vmid_str.compareTo(expectedVmIdentifier) != 0) {
410            throw new VmIdentifierException(this, test_vmid_str);
411        }
412
413        if (debug) {
414            System.out.println("comparing HostIdentifier result");
415        }
416
417        if (test_hostid_str.compareTo(expectedHostIdentifier) != 0) {
418            throw new HostIdentifierException(this, test_hostid_str);
419        }
420
421        if (debug) {
422            System.out.println("comparing VmIdentifier result");
423        }
424
425        if (resolved_vmid_str.compareTo(resolvedVmIdentifier) != 0) {
426            throw new ResolvedVmIdentifierException(this, resolved_vmid_str);
427        }
428    }
429
430    public void setDescription(String description) {
431        this.description = description;
432    }
433
434    public void setExpectedVmIdentifier(String expectedVmIdentifier) {
435        if (debug) {
436            System.out.println("setting vmidentifier string to " + vmid);
437        }
438        this.expectedVmIdentifier = expectedVmIdentifier;
439    }
440
441    public void setExpectedHostIdentifier(String expectedHostIdentifier) {
442        this.expectedHostIdentifier = expectedHostIdentifier;
443    }
444
445    public void setResolvedVmIdentifier(String resolvedVmIdentifier) {
446        this.resolvedVmIdentifier = resolvedVmIdentifier;
447    }
448}
449