1 package org.xmlBlaster.test.classtest;
2
3 import java.util.logging.Logger;
4 import org.xmlBlaster.util.Global;
5 import org.xmlBlaster.util.XmlBlasterException;
6 import org.xmlBlaster.util.protocol.socket.SocketUrl;
7 import org.xmlBlaster.util.qos.address.Address;
8
9 import junit.framework.*;
10
11 /**
12 * Invoke: java -Djava.compiler= junit.textui.TestRunner org.xmlBlaster.test.classtest.SocketUrlTest
13 *
14 * @see org.xmlBlaster.protocol.socket.SocketUrl
15 */
16 public class SocketUrlTest extends TestCase {
17 protected Global glob;
18 private static Logger log = Logger.getLogger(SocketUrlTest.class.getName());
19
20 public SocketUrlTest(String name) {
21 super(name);
22 }
23
24 protected void setUp() {
25 glob = new Global();
26
27 }
28
29 protected void tearDown() {
30 }
31
32 public void testBasic() {
33 try {
34 {
35 String hostname = null;
36 int port = -1;
37 SocketUrl s = new SocketUrl(glob, hostname, port);
38 assertEquals("", glob.getLocalIP(), s.getHostname());
39 assertEquals("", port, s.getPort());
40 assertEquals("", "socket://"+glob.getLocalIP()+":"+port, s.getUrl());
41 log.info("SUCCESS testBasic(): hostname=" + hostname + " port=" + port + " resultUrl=" + s.getUrl());
42 }
43
44 {
45 String hostname = "127.1.5.4";
46 int port = 9999;
47 SocketUrl s = new SocketUrl(glob, hostname, port);
48 assertEquals("", hostname, s.getHostname());
49 assertEquals("", port, s.getPort());
50 assertEquals("", "socket://"+hostname+":"+port, s.getUrl());
51 log.info("SUCCESS testBasic(): hostname=" + hostname + " port=" + port + " resultUrl=" + s.getUrl());
52 }
53
54 {
55 SocketUrl s = new SocketUrl(glob, "192.1.1.5:911");
56 SocketUrl other = new SocketUrl(glob, "192.1.1.5", 911);
57 assertTrue("", s.equals(other));
58 log.info("SUCCESS testBasic(): equals=true");
59 }
60
61 {
62 SocketUrl s = new SocketUrl(glob, "192.1.1.5");
63 SocketUrl other = new SocketUrl(glob, "192.1.1.5", SocketUrl.DEFAULT_SERVER_PORT);
64 assertTrue("", s.equals(other));
65 log.info("SUCCESS testBasic(): equals=true");
66 }
67
68 {
69 SocketUrl s = new SocketUrl(glob, "192.1.1.5:900");
70 SocketUrl other = new SocketUrl(glob, "192.1.1.5", 911);
71 assertTrue("", !s.equals(other));
72 log.info("SUCCESS testBasic(): equals=false");
73 }
74
75 {
76 SocketUrl s = new SocketUrl(glob, "192.1.1.5:911");
77 SocketUrl other = new SocketUrl(glob, "192.1.77.5", 911);
78 assertTrue("", !s.equals(other));
79 log.info("SUCCESS testBasic(): equals=false");
80 }
81 }
82 catch (XmlBlasterException e) {
83 log.severe("ERROR: " + e.toString());
84 fail(e.toString());
85 }
86 log.info("SUCCESS testBasic()");
87 }
88
89 public void testAddress() {
90 try {
91 {
92 String hostname = "168.2.2.2";
93 int port = 8888;
94 String type = "socket";
95 String[] args = {
96 "-plugin/"+type+"/hostname",
97 hostname,
98 "-plugin/"+type+"/port",
99 ""+port
100 };
101 glob.init(args);
102 Address address = new Address(glob, type);
103 SocketUrl s = new SocketUrl(glob, address);
104 assertEquals("", hostname, s.getHostname());
105 assertEquals("", port, s.getPort());
106 assertEquals("", "socket://"+hostname+":"+port, s.getUrl());
107 log.info("SUCCESS testAddress(): resultUrl=" + s.getUrl());
108 }
109
110 {
111 String hostname = "168.99.55.2";
112 int port = 6666;
113 Address address = new Address(glob);
114 address.setPluginProperty("hostname", hostname);
115 address.setPluginProperty("port", ""+port);
116 SocketUrl s = new SocketUrl(glob, address);
117 assertEquals("", hostname, s.getHostname());
118 assertEquals("", port, s.getPort());
119 assertEquals("", "socket://"+hostname+":"+port, s.getUrl());
120 log.info("SUCCESS testAddress(): resultUrl=" + s.getUrl());
121 }
122 }
123 catch (XmlBlasterException e) {
124 log.severe("ERROR: " + e.toString());
125 fail(e.toString());
126 }
127 log.info("SUCCESS testAddress()");
128 }
129
130 public void testParseUrl() {
131 try {
132 try {
133 String url = null;
134 new SocketUrl(glob, url);
135 fail("Null url is not allowed");
136 } catch (XmlBlasterException e) {
137 log.info("SUCCESS testParseUrl(): expected exception: " + e.toString());
138 }
139
140 {
141 String url = "";
142 SocketUrl s = new SocketUrl(glob, url);
143 assertEquals("", glob.getLocalIP(), s.getHostname());
144 assertEquals("", SocketUrl.DEFAULT_SERVER_PORT, s.getPort());
145 assertEquals("", "socket://"+glob.getLocalIP()+":"+SocketUrl.DEFAULT_SERVER_PORT, s.getUrl());
146 log.info("SUCCESS testParseUrl(): url=" + url + " resultUrl=" + s.getUrl());
147 }
148
149 {
150 String url = "127.1.1.1";
151 SocketUrl s = new SocketUrl(glob, url);
152 assertEquals("", url, s.getHostname());
153 assertEquals("", SocketUrl.DEFAULT_SERVER_PORT, s.getPort());
154 assertEquals("", "socket://"+url+":"+SocketUrl.DEFAULT_SERVER_PORT, s.getUrl());
155 log.info("SUCCESS testParseUrl(): url=" + url + " resultUrl=" + s.getUrl());
156 }
157
158 {
159 String url = "127.1.1.1:8080";
160 SocketUrl s = new SocketUrl(glob, url);
161 assertEquals("", "127.1.1.1", s.getHostname());
162 assertEquals("", 8080, s.getPort());
163 assertEquals("", "socket://"+url, s.getUrl());
164 log.info("SUCCESS testParseUrl(): url=" + url + " resultUrl=" + s.getUrl());
165 }
166
167 {
168 String url = "socket:127.1.1.1:8080";
169 SocketUrl s = new SocketUrl(glob, url);
170 assertEquals("", "127.1.1.1", s.getHostname());
171 assertEquals("", 8080, s.getPort());
172 assertEquals("", "socket://127.1.1.1:8080", s.getUrl());
173 log.info("SUCCESS testParseUrl(): url=" + url + " resultUrl=" + s.getUrl());
174 }
175
176 {
177 String url = "socket://127.1.1.1:8080";
178 SocketUrl s = new SocketUrl(glob, url);
179 assertEquals("", "127.1.1.1", s.getHostname());
180 assertEquals("", 8080, s.getPort());
181 assertEquals("", url, s.getUrl());
182 log.info("SUCCESS testParseUrl(): url=" + url + " resultUrl=" + s.getUrl());
183 }
184 }
185 catch (XmlBlasterException e) {
186 log.severe("ERROR: " + e.toString());
187 fail(e.toString());
188 }
189 log.info("SUCCESS testParseUrl()");
190 }
191
192 /**
193 * Invoke: java org.xmlBlaster.test.classtest.SocketUrlTest
194 * @deprecated Use the TestRunner from the testsuite to run it:<p />
195 * <pre> java -Djava.compiler= junit.textui.TestRunner org.xmlBlaster.test.classtest.SocketUrlTest</pre>
196 */
197 public static void main(String args[])
198 {
199 Global glob = new Global();
200 if (glob.init(args) != 0) {
201 System.err.println("******* SocketUrlTest: Init failed");
202 }
203 SocketUrlTest testSub = new SocketUrlTest("SocketUrlTest");
204 testSub.setUp();
205 testSub.testBasic();
206 testSub.testAddress();
207 testSub.testParseUrl();
208 testSub.tearDown();
209 }
210 }
syntax highlighted by Code2HTML, v. 0.9.1