From ed4d815fba70fd539df0f5db8a93a377e5eb8c77 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Mon, 26 Jan 2015 20:26:13 +0100 Subject: [PATCH] Make Message.setType(Type) not throw if type is null MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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). """ --- .../java/org/jivesoftware/smack/packet/Message.java | 13 +++++-------- .../org/jivesoftware/smack/packet/MessageTest.java | 6 ------ 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/packet/Message.java b/smack-core/src/main/java/org/jivesoftware/smack/packet/Message.java index 9d76a771c..1f785cc8d 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/packet/Message.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/packet/Message.java @@ -55,7 +55,7 @@ public final class Message extends Packet { public static final String ELEMENT = "message"; public static final String BODY = "body"; - private Type type = Type.normal; + private Type type; private String thread = null; private final Set subjects = new HashSet(); @@ -105,6 +105,9 @@ public final class Message extends Packet { * @return the type of the message. */ public Type getType() { + if (type == null) { + return Type.normal; + } return type; } @@ -112,12 +115,8 @@ public final class Message extends Packet { * Sets 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) { - if (type == null) { - throw new IllegalArgumentException("Type cannot be null."); - } this.type = type; } @@ -404,9 +403,7 @@ public final class Message extends Packet { XmlStringBuilder buf = new XmlStringBuilder(); buf.halfOpenElement(ELEMENT); addCommonAttributes(buf); - if (type != Type.normal) { - buf.attribute("type", type); - } + buf.optAttribute("type", type); buf.rightAngleBracket(); // Add the subject in the default language diff --git a/smack-core/src/test/java/org/jivesoftware/smack/packet/MessageTest.java b/smack-core/src/test/java/org/jivesoftware/smack/packet/MessageTest.java index c6c99d5af..1a0c02c3a 100644 --- a/smack-core/src/test/java/org/jivesoftware/smack/packet/MessageTest.java +++ b/smack-core/src/test/java/org/jivesoftware/smack/packet/MessageTest.java @@ -69,12 +69,6 @@ public class MessageTest { assertXMLEqual(control, messageTypeSet.toXML().toString()); } - @Test(expected=IllegalArgumentException.class) - public void setMessageTypeNullTest() { - Message message = getNewMessage(); - message.setType(null); - } - @Test(expected=NullPointerException.class) public void setNullMessageBodyTest() { Message message = getNewMessage();