KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > standalone > jmx > JmxNotificationTest


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.scenario.standalone.jmx;
26
27 import java.io.StringReader JavaDoc;
28 import java.io.StringWriter JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Hashtable JavaDoc;
31
32 import org.dom4j.Document;
33 import org.dom4j.Node;
34 import org.dom4j.io.OutputFormat;
35 import org.dom4j.io.SAXReader;
36 import org.dom4j.io.XMLWriter;
37 import org.objectweb.cjdbc.common.jmx.notifications.JmxNotification;
38 import org.objectweb.cjdbc.scenario.templates.NoTemplate;
39
40 /**
41  * This class defines a JmxNotificationTest class
42  *
43  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
44  */

45 public class JmxNotificationTest extends NoTemplate
46 {
47   
48   /**
49    * Test xml serialization of jmx notifications
50    * @throws Exception if fails
51    */

52   public void testCreateJmxNotification() throws Exception JavaDoc
53   {
54     System.out
55         .println("###Create notification with parameters using the above method...");
56     Hashtable JavaDoc data = new Hashtable JavaDoc();
57     ArrayList JavaDoc backends = new ArrayList JavaDoc();
58     backends.add("localhost");
59     backends.add("localhost1");
60     data.put("backends", backends);
61     JmxNotification not = new JmxNotification("priority",
62         "sequence", "type", "description", "time", "controller", "mbeanclass",
63         "mbeanname", "serverIP", "serverPort",data);
64     Document document = not.toXmlDocument();
65
66     System.out.println("###Pretty print the document to System.out");
67     OutputFormat format = OutputFormat.createPrettyPrint();
68     XMLWriter writer = new XMLWriter(System.out, format);
69     writer.write(document);
70
71     System.out.println("###Compact format to string writer");
72     StringWriter JavaDoc swriter = new StringWriter JavaDoc();
73     format = OutputFormat.createCompactFormat();
74     writer = new XMLWriter(swriter, format);
75     writer.write(document);
76
77     System.out.println("###Re-create the document from the string");
78     StringReader JavaDoc sreader = new StringReader JavaDoc(swriter.toString());
79     SAXReader reader = new SAXReader();
80     Document document2 = reader.read(sreader);
81     
82     JmxNotification notif0 = JmxNotification.createNotificationFromXml(document2);
83     JmxNotification notif1 = JmxNotification.createNotificationFromXml(document);
84     assertEquals(notif0.toString(),notif1.toString());
85     
86
87     System.out.println("###Re-pretty print the document");
88     writer = new XMLWriter(System.out, OutputFormat.createPrettyPrint());
89     writer.write(document2);
90
91     // Get some attribute
92
Node node = document.selectSingleNode("//jmxevent/info/description");
93     System.out.println("Description of the node:" + node.getText());
94     assertEquals("Value not expected",node.getText(),"description");
95
96     // Serialize and de-serialize the notification
97
JmxNotification notification = JmxNotification
98         .createNotificationFromXml(document2);
99     Document document3 = notification.toXmlDocument();
100     JmxNotification notif2 = JmxNotification
101         .createNotificationFromXml(document3);
102     assertEquals("Xml documents are different", notification.toString(), notif2
103         .toString());
104   }
105 }
Popular Tags