KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > smack > IQTest


1 /**
2  * $RCSfile$
3  * $Revision: 2434 $
4  * $Date: 2004-12-23 16:05:27 -0300 (Thu, 23 Dec 2004) $
5  *
6  * Copyright 2003-2004 Jive Software.
7  *
8  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */

20
21 package org.jivesoftware.smack;
22
23 import org.jivesoftware.smack.test.SmackTestCase;
24 import org.jivesoftware.smack.packet.IQ;
25 import org.jivesoftware.smack.filter.PacketFilter;
26 import org.jivesoftware.smack.filter.AndFilter;
27 import org.jivesoftware.smack.filter.PacketIDFilter;
28 import org.jivesoftware.smack.filter.PacketTypeFilter;
29
30 /**
31  * Ensure that the server is handling IQ packets correctly.
32  *
33  * @author Gaston Dombiak
34  */

35 public class IQTest extends SmackTestCase {
36
37     public IQTest(String JavaDoc arg0) {
38         super(arg0);
39     }
40
41     /**
42      * Check that the server responds a 503 error code when the client sends an IQ packet with an
43      * invalid namespace.
44      */

45     public void testInvalidNamespace() {
46         IQ iq = new IQ() {
47             public String JavaDoc getChildElementXML() {
48                 StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
49                 buf.append("<query xmlns=\"jabber:iq:anything\">");
50                 buf.append("</query>");
51                 return buf.toString();
52             }
53         };
54
55         PacketFilter filter = new AndFilter(new PacketIDFilter(iq.getPacketID()),
56                 new PacketTypeFilter(IQ.class));
57         PacketCollector collector = getConnection(0).createPacketCollector(filter);
58         // Send the iq packet with an invalid namespace
59
getConnection(0).sendPacket(iq);
60
61         IQ result = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
62         // Stop queuing results
63
collector.cancel();
64         if (result == null) {
65             fail("No response from server");
66         }
67         else if (result.getType() != IQ.Type.ERROR) {
68             fail("The server didn't reply with an error packet");
69         }
70         else {
71             assertEquals("Server answered an incorrect error code", 503, result.getError().getCode());
72         }
73     }
74
75     protected int getMaxConnections() {
76         return 1;
77     }
78 }
79
Popular Tags