1 24 25 package org.objectweb.cjdbc.scenario.standalone.jmx; 26 27 import java.io.StringReader ; 28 import java.io.StringWriter ; 29 import java.util.ArrayList ; 30 import java.util.Hashtable ; 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 45 public class JmxNotificationTest extends NoTemplate 46 { 47 48 52 public void testCreateJmxNotification() throws Exception 53 { 54 System.out 55 .println("###Create notification with parameters using the above method..."); 56 Hashtable data = new Hashtable (); 57 ArrayList backends = new ArrayList (); 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 swriter = new StringWriter (); 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 sreader = new StringReader (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 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 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 |