1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-22 20:12:07 +01:00

Make Message.setType(Type) not throw if type is null

RFC 6121 § 5.2.2:
"""
If an application receives a message with no 'type' attribute or the
application does not understand the value of the 'type' attribute
provided, it MUST consider the message to be of type "normal" (i.e.,
"normal" is the default).
"""
This commit is contained in:
Florian Schmaus 2015-01-26 20:26:13 +01:00
parent 3299515b36
commit ed4d815fba
2 changed files with 5 additions and 14 deletions

View file

@ -55,7 +55,7 @@ public final class Message extends Packet {
public static final String ELEMENT = "message"; public static final String ELEMENT = "message";
public static final String BODY = "body"; public static final String BODY = "body";
private Type type = Type.normal; private Type type;
private String thread = null; private String thread = null;
private final Set<Subject> subjects = new HashSet<Subject>(); private final Set<Subject> subjects = new HashSet<Subject>();
@ -105,6 +105,9 @@ public final class Message extends Packet {
* @return the type of the message. * @return the type of the message.
*/ */
public Type getType() { public Type getType() {
if (type == null) {
return Type.normal;
}
return type; return type;
} }
@ -112,12 +115,8 @@ public final class Message extends Packet {
* Sets the type of the message. * Sets the type of the message.
* *
* @param type the type of the message. * @param type the type of the message.
* @throws IllegalArgumentException if null is passed in as the type
*/ */
public void setType(Type type) { public void setType(Type type) {
if (type == null) {
throw new IllegalArgumentException("Type cannot be null.");
}
this.type = type; this.type = type;
} }
@ -404,9 +403,7 @@ public final class Message extends Packet {
XmlStringBuilder buf = new XmlStringBuilder(); XmlStringBuilder buf = new XmlStringBuilder();
buf.halfOpenElement(ELEMENT); buf.halfOpenElement(ELEMENT);
addCommonAttributes(buf); addCommonAttributes(buf);
if (type != Type.normal) { buf.optAttribute("type", type);
buf.attribute("type", type);
}
buf.rightAngleBracket(); buf.rightAngleBracket();
// Add the subject in the default language // Add the subject in the default language

View file

@ -69,12 +69,6 @@ public class MessageTest {
assertXMLEqual(control, messageTypeSet.toXML().toString()); assertXMLEqual(control, messageTypeSet.toXML().toString());
} }
@Test(expected=IllegalArgumentException.class)
public void setMessageTypeNullTest() {
Message message = getNewMessage();
message.setType(null);
}
@Test(expected=NullPointerException.class) @Test(expected=NullPointerException.class)
public void setNullMessageBodyTest() { public void setNullMessageBodyTest() {
Message message = getNewMessage(); Message message = getNewMessage();