1 52 package org.jivesoftware.smack.test; 53 54 import java.io.InputStream ; 55 import java.net.URL ; 56 import java.util.Enumeration ; 57 58 import javax.net.SocketFactory; 59 60 import org.jivesoftware.smack.XMPPConnection; 61 import org.jivesoftware.smack.XMPPException; 62 import org.xmlpull.v1.*; 63 import org.xmlpull.mxp1.MXParser; 64 65 import junit.framework.TestCase; 66 67 83 public abstract class SmackTestCase extends TestCase { 84 85 private String host = "localhost"; 86 private String serviceName = "localhost"; 87 private int port = 5222; 88 89 private String chatDomain = "chat.localhost"; 90 private String mucDomain = "conference.localhost"; 91 92 private XMPPConnection[] connections = null; 93 94 98 public SmackTestCase(String arg0) { 99 super(arg0); 100 } 101 102 109 protected abstract int getMaxConnections(); 110 111 120 protected SocketFactory getSocketFactory() { 121 return null; 122 } 123 124 136 protected XMPPConnection getConnection(int index) { 137 if (index > getMaxConnections()) { 138 throw new IllegalArgumentException ("Index out of bounds"); 139 } 140 return connections[index]; 141 } 142 143 150 protected String getUsername(int index) { 151 if (index > getMaxConnections()) { 152 throw new IllegalArgumentException ("Index out of bounds"); 153 } 154 return "user" + index; 155 } 156 157 164 protected String getBareJID(int index) { 165 return getUsername(index) + "@" + getConnection(index).getServiceName(); 166 } 167 168 175 protected String getFullJID(int index) { 176 return getBareJID(index) + "/Smack"; 177 } 178 179 protected String getHost() { 180 return host; 181 } 182 183 protected int getPort() { 184 return port; 185 } 186 187 protected String getServiceName() { 188 return serviceName; 189 } 190 191 196 protected String getChatDomain() { 197 return chatDomain; 198 } 199 200 205 protected String getMUCDomain() { 206 return mucDomain; 207 } 208 209 protected void setUp() throws Exception { 210 super.setUp(); 211 init(); 212 if (getMaxConnections() < 1) { 213 return; 214 } 215 connections = new XMPPConnection[getMaxConnections()]; 216 try { 217 for (int i = 0; i < getMaxConnections(); i++) { 219 if (getSocketFactory() == null) { 220 connections[i] = new XMPPConnection(host, port); 221 } 222 else { 223 connections[i] = new XMPPConnection(host, port, host, getSocketFactory()); 224 } 225 } 226 host = connections[0].getHost(); 230 serviceName = connections[0].getServiceName(); 231 if (!getConnection(0).getAccountManager().supportsAccountCreation()) 233 fail("Server does not support account creation"); 234 235 for (int i = 0; i < getMaxConnections(); i++) { 236 try { 238 getConnection(i).getAccountManager().createAccount("user" + i, "user" + i); 239 } catch (XMPPException e) { 240 if (e.getXMPPError().getCode() != 409) { 242 throw e; 243 } 244 } 245 getConnection(i).login("user" + i, "user" + i); 247 } 248 Thread.sleep(150); 250 } 251 catch (Exception e) { 252 e.printStackTrace(); 253 fail(e.getMessage()); 254 } 255 } 256 257 protected void tearDown() throws Exception { 258 super.tearDown(); 259 260 for (int i = 0; i < getMaxConnections(); i++) { 261 getConnection(i).getAccountManager().deleteAccount(); 263 getConnection(i).close(); 265 266 } 267 } 268 269 276 private void init() { 277 try { 278 boolean found = false; 279 Enumeration resources = 281 ClassLoader.getSystemClassLoader().getResources(getConfigurationFilename()); 282 while (resources.hasMoreElements()) { 283 found = parseURL((URL ) resources.nextElement()); 284 } 285 if (!found) { 288 resources = ClassLoader.getSystemClassLoader().getResources("config/test-case.xml"); 289 while (resources.hasMoreElements()) { 290 found = parseURL((URL ) resources.nextElement()); 291 } 292 } 293 if (!found) { 294 System.err.println("File config/test-case.xml not found. Using default config."); 295 } 296 } 297 catch (Exception e) { 298 } 299 } 300 301 309 private boolean parseURL(URL url) { 310 boolean parsedOK = false; 311 InputStream systemStream = null; 312 try { 313 systemStream = url.openStream(); 314 XmlPullParser parser = new MXParser(); 315 parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); 316 parser.setInput(systemStream, "UTF-8"); 317 int eventType = parser.getEventType(); 318 do { 319 if (eventType == XmlPullParser.START_TAG) { 320 if (parser.getName().equals("host")) { 321 host = parser.nextText(); 322 } 323 else if (parser.getName().equals("port")) { 324 port = parseIntProperty(parser, port); 325 } 326 else if (parser.getName().equals("serviceName")) { 327 serviceName = parser.nextText(); 328 } 329 else if (parser.getName().equals("chat")) { 330 chatDomain = parser.nextText(); 331 } 332 else if (parser.getName().equals("muc")) { 333 mucDomain = parser.nextText(); 334 } 335 } 336 eventType = parser.next(); 337 } 338 while (eventType != XmlPullParser.END_DOCUMENT); 339 parsedOK = true; 340 } 341 catch (Exception e) { 342 e.printStackTrace(); 343 } 344 finally { 345 try { 346 systemStream.close(); 347 } 348 catch (Exception e) { 349 } 350 } 351 return parsedOK; 352 } 353 354 private static int parseIntProperty(XmlPullParser parser, int defaultValue) throws Exception { 355 try { 356 return Integer.parseInt(parser.nextText()); 357 } 358 catch (NumberFormatException nfe) { 359 nfe.printStackTrace(); 360 return defaultValue; 361 } 362 } 363 364 372 private String getConfigurationFilename() { 373 String fullClassName = this.getClass().getName(); 374 int firstChar = fullClassName.lastIndexOf('.') + 1; 375 return "config/" + fullClassName.substring(firstChar) + ".xml"; 376 } 377 378 } 379 | Popular Tags |