1 package org.xmlBlaster.test.jmx;
2
3 import java.io.IOException;
4 import junit.framework.*;
5
6 import java.util.logging.Logger;
7 import java.util.logging.Level;
8 import org.xmlBlaster.util.Global;
9
10 import org.xmlBlaster.util.admin.extern.SerializeHelper;
11 import org.xmlBlaster.util.admin.extern.MethodInvocation;
12 import org.xmlBlaster.client.I_XmlBlasterAccess;
13 import org.xmlBlaster.client.qos.ConnectQos;
14 import org.xmlBlaster.util.qos.address.Address;
15 import org.xmlBlaster.client.qos.PublishReturnQos;
16 import org.xmlBlaster.client.qos.*;
17
18 import org.xmlBlaster.client.key.*;
19
20
21 import org.xmlBlaster.client.I_Callback;
22
23 import org.xmlBlaster.util.MsgUnit;
24
25 import org.xmlBlaster.util.XmlBlasterException;
26
27 import java.util.Properties;
28
29 //import org.xmlBlaster.test.Util;
30
31 public class TestConnector extends TestCase implements I_Callback {
32 private final int serverPort = 3424;
33 private final static String ME = "TestConnector";
34 private Global glob = null;
35 private static Logger log = Logger.getLogger(TestConnector.class.getName());
36 private I_XmlBlasterAccess returnCon;
37 private I_XmlBlasterAccess invokeCon;
38
39 private MsgUnit msg = null;
40 private static String port = "3424";
41
42 SerializeHelper sh = null;
43 MethodInvocation mi = null;
44
45 public TestConnector(String testName)
46 {
47 super(testName);
48 }
49
50
51 protected void setUp()
52 {
53 if (this.glob == null) this.glob = Global.instance();
54
55 log.info("setUp of TestConnector...");
56 //connect to embedded xmlBlaster
57
58 Properties prop = new Properties();
59 prop.setProperty("bootstrapPort",port);
60 prop.setProperty("bootstrapHostname","localhost");
61
62 glob.init(prop);
63
64 invokeCon = glob.getXmlBlasterAccess();
65 returnCon = glob.getXmlBlasterAccess();
66
67 // log.info("Connecting to embedded xmlBlaster on port "+ port +" Adresse " + addr.getAddress());
68 try {
69 ConnectQos qos = new ConnectQos(glob, "InternalConnector", "connector");
70 returnCon.connect(qos, this);
71 }
72 catch (XmlBlasterException ex) {
73 assertTrue("Error when connecting to xmlBlaster " + ex.toString(), false);
74 log.severe("Error when connecting to xmlBlaster " + ex.toString());
75 }
76 SubscribeKey subKey = new SubscribeKey(this.glob, "xmlBlasterMBeans_Return");
77
78 SubscribeQos sQos = new SubscribeQos(this.glob);
79 sQos.setWantLocal(false);
80 try {
81 returnCon.subscribe(subKey, sQos);
82 }
83 catch (XmlBlasterException ex) {
84 assertTrue("Error when subscribing to xmlBlaster " + ex.toString(), false);
85 log.severe("Error when subscribing to xmlBlaster " + ex.toString());
86 }
87 sh = new SerializeHelper(glob);
88
89 if (invokeCon.isConnected() && returnCon.isConnected()) {log.info("connection establisheld");}
90 else log.warning("Couldnt connect to server on port " + port);
91 }
92
93
94 /**
95 * Method is used by TestRunner to load these tests
96 */
97 public static Test suite() {
98 TestSuite suite= new TestSuite();
99 suite.addTest(new TestConnector("testConnector"));
100 return suite;
101 }
102
103 /**
104 * TEST:
105 * <p />
106 */
107 public void testConnector() {
108 try {
109 log.info("Creating TestMessage");
110 mi = new MethodInvocation();
111 mi.setMethodName("getDefaultDomain");
112 log.info("new MethodInvocation build " + mi.getMethodName());
113 PublishReturnQos rqos = invokeCon.publish(new MsgUnit("<key oid='xmlBlasterMBeans_Invoke'/>",sh.serializeObject(mi),"<qos/>"));
114 log.info("Publish test Message to jmx-topic..");
115 }
116 catch (XmlBlasterException ex) {
117 ex.printStackTrace();
118 }
119 catch (IOException ex) {
120 log.severe("Error when creating methodInvocation " + ex.toString());
121 ex.printStackTrace();
122 }
123
124 }
125
126
127 /**
128 * This is the callback method invoked from xmlBlaster
129 * delivering us a new asynchronous message.
130 * @see org.xmlBlaster.client.I_Callback#update(String, UpdateKey, byte[], UpdateQos)
131 */
132 public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos)
133 {
134 log.info("Receiving update of a message, checking ...");
135 MethodInvocation mi = null;
136 try {
137 mi = (MethodInvocation) sh.deserializeObject(content);
138 }
139 catch (IOException ex) {
140 log.severe("Error when deserializing object");
141 }
142 Object obj = mi.getReturnValue();
143 log.info("Received Object: " + obj);
144 if (obj.toString().length()>0) log.info("Success... Received Domainname: " + obj);
145 else {
146 log.severe("Error when receiving returning object...");
147 assertTrue("Error when receiving returning object...", false);
148 }
149 return "";
150 }
151
152 }
syntax highlighted by Code2HTML, v. 0.9.1