1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-06-17 08:54:49 +02:00
Smack/smack-core/src/test/java/org/jivesoftware/smack/packet/IQResponseTest.java
Florian Schmaus 9e797c1b17 Enable PacketExtensions for IQs
This is actually only part one, i.e. with this commit if the user adds a
PacketExtension to an IQ it will be included in IQ.toXml(). Which was
previously only the case if the IQ subclass explicitly included packet
extensions.

The second part of the change is to change the IQ provider, so that
packet extensions are automatically parsed.

Cases where PacketExtensions are used for Message and IQ are slightly
changed. The IQ sublcass now only has a field with this
PacketExtension (see for example
bytestreams.ibb.packet.DataPacketExtension).

Also changed hoxt API: Removed unnecessary indirection and made the
API more Smack idiomatic.
2014-11-10 11:43:18 +01:00

125 lines
4.1 KiB
Java

/**
*
* Copyright 2010 Jive Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.packet;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import org.junit.Test;
/**
* Tests that verifies the correct behavior of creating result and error IQ packets.
*
* @see <a href="http://xmpp.org/rfcs/rfc3920.html#stanzas-semantics-iq">IQ Semantics</a>
* @author Guenther Niess
*/
public class IQResponseTest {
private static final String ELEMENT = "child";
private static final String NAMESPACE = "http://igniterealtime.org/protocol/test";
/**
* Test creating a simple and empty IQ response.
*/
@Test
public void testGeneratingSimpleResponse() {
final IQ request = new TestIQ(ELEMENT, NAMESPACE);
request.setFrom("sender@test/Smack");
request.setTo("receiver@test/Smack");
final IQ result = IQ.createResultIQ(request);
assertEquals(IQ.Type.result, result.getType());
assertNotNull(result.getPacketID());
assertEquals(request.getPacketID(), result.getPacketID());
assertEquals(request.getFrom(), result.getTo());
assertEquals(request.getTo(), result.getFrom());
assertEquals("", result.getChildElementXML().toString());
}
/**
* Test creating a error response based on an IQ request.
*/
@Test
public void testGeneratingValidErrorResponse() {
final XMPPError error = new XMPPError(XMPPError.Condition.bad_request);
final IQ request = new TestIQ(ELEMENT, NAMESPACE);
request.setType(IQ.Type.set);
request.setFrom("sender@test/Smack");
request.setTo("receiver@test/Smack");
final IQ result = IQ.createErrorResponse(request, error);
assertEquals(IQ.Type.error, result.getType());
assertNotNull(result.getPacketID());
assertEquals(request.getPacketID(), result.getPacketID());
assertEquals(request.getFrom(), result.getTo());
assertEquals(error, result.getError());
// TODO this test was never valid
// assertEquals(CHILD_ELEMENT, result.getChildElementXML());
}
/**
* According to <a href="http://xmpp.org/rfcs/rfc3920.html#stanzas-semantics-iq"
* >RFC3920: IQ Semantics</a> we shouldn't respond to an IQ of type result.
*/
@Test
public void testGeneratingResponseBasedOnResult() {
final IQ request = new TestIQ(ELEMENT, NAMESPACE);
request.setType(IQ.Type.result);
request.setFrom("sender@test/Smack");
request.setTo("receiver@test/Smack");
try {
IQ.createResultIQ(request);
}
catch (IllegalArgumentException e) {
return;
}
fail("It shouldn't be possible to generate a response for a result IQ!");
}
/**
* According to <a href="http://xmpp.org/rfcs/rfc3920.html#stanzas-semantics-iq"
* >RFC3920: IQ Semantics</a> we shouldn't respond to an IQ of type error.
*/
@Test
public void testGeneratingErrorBasedOnError() {
final XMPPError error = new XMPPError(XMPPError.Condition.bad_request);
final IQ request = new TestIQ(ELEMENT, NAMESPACE);
request.setType(IQ.Type.error);
request.setFrom("sender@test/Smack");
request.setTo("receiver@test/Smack");
request.setError(error);
try {
IQ.createErrorResponse(request, error);
}
catch (IllegalArgumentException e) {
return;
}
fail("It shouldn't be possible to generate a response for a error IQ!");
}
}