1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-25 13:32:07 +01:00

Add Message.toString() providing a short description

This commit is contained in:
Florian Schmaus 2016-05-25 22:52:29 +02:00
parent 98ec77caee
commit 86e11e69e8
8 changed files with 126 additions and 4 deletions

View file

@ -112,6 +112,18 @@ public abstract class IQ extends Stanza {
return childElementNamespace;
}
@Override
public final String toString() {
StringBuilder sb = new StringBuilder();
sb.append("IQ Stanza (");
sb.append(getChildElementName()).append(' ').append(getChildElementNamespace());
sb.append(") [");
logCommonAttributes(sb);
sb.append("type=").append(type).append(',');
sb.append(']');
return sb.toString();
}
@Override
public final XmlStringBuilder toXML() {
XmlStringBuilder buf = new XmlStringBuilder();

View file

@ -418,6 +418,18 @@ public final class Message extends Stanza implements TypedCloneable<Message> {
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Message Stanza [");
logCommonAttributes(sb);
if (type != null) {
sb.append("type=").append(type).append(',');
}
sb.append(']');
return sb.toString();
}
@Override
public XmlStringBuilder toXML() {
XmlStringBuilder buf = new XmlStringBuilder();

View file

@ -21,6 +21,7 @@ import java.util.Locale;
import org.jivesoftware.smack.packet.id.StanzaIdUtil;
import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.TypedCloneable;
import org.jivesoftware.smack.util.XmlStringBuilder;
@ -222,6 +223,25 @@ public final class Presence extends Stanza implements TypedCloneable<Presence> {
this.mode = mode;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Presence Stanza [");
logCommonAttributes(sb);
sb.append("type=").append(type).append(',');
if (mode != null) {
sb.append("mode=").append(mode).append(',');
}
if (!StringUtils.isNullOrEmpty(status)) {
sb.append("status=").append(status).append(',');
}
if (priority != Integer.MIN_VALUE) {
sb.append("prio=").append(priority).append(',');
}
sb.append(']');
return sb.toString();
}
@Override
public XmlStringBuilder toXML() {
XmlStringBuilder buf = new XmlStringBuilder();

View file

@ -458,11 +458,11 @@ public abstract class Stanza implements TopLevelStreamElement {
return removeExtension(extension.getElementName(), extension.getNamespace());
}
/**
* Returns a short String describing the Stanza. This method is suited for log purposes.
*/
@Override
// NOTE When Smack is using Java 8, then this method should be moved in Element as "Default Method".
public String toString() {
return toXML().toString();
}
public abstract String toString();
/**
* Returns the extension sub-packets (including properties data) as an XML
@ -501,6 +501,18 @@ public abstract class Stanza implements TopLevelStreamElement {
xml.xmllangAttribute(getLanguage());
}
protected void logCommonAttributes(StringBuilder sb) {
if (getTo() != null) {
sb.append("to=").append(to).append(',');
}
if (getFrom() != null) {
sb.append("from=").append(from).append(',');
}
if (hasStanzaIdSet()) {
sb.append("id=").append(id).append(',');
}
}
/**
* Append an XMPPError is this stanza(/packet) has one set.
*

View file

@ -206,5 +206,10 @@ public class PacketCollectorTest
{
return "<packetId>" + getStanzaId() + "</packetId>";
}
@Override
public String toString() {
return toXML();
}
}
}

View file

@ -0,0 +1,51 @@
/**
*
* Copyright © 2016 Florian Schmaus
*
* 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 org.jivesoftware.smack.packet.Presence.Mode;
import org.junit.Test;
import org.jxmpp.jid.JidTestUtil;
import org.jxmpp.stringprep.XmppStringprepException;
public class ToStringTest {
@Test
public void messageTest() {
Message message = new Message(JidTestUtil.BARE_JID_1, Message.Type.headline);
message.setStanzaId("message-id");
String string = message.toString();
assertEquals("Message Stanza [to=one@exampleone.org,id=message-id,type=headline,]", string);
}
@Test
public void presenceTest() {
Presence presence = new Presence(Presence.Type.subscribe, null, 0, Mode.away);
presence.setStanzaId("presence-id");
String string = presence.toString();
assertEquals("Presence Stanza [id=presence-id,type=subscribe,mode=away,prio=0,]", string);
}
@Test
public void iqTest() throws XmppStringprepException {
Bind bindIq = Bind.newResult(JidTestUtil.DUMMY_AT_EXAMPLE_ORG_SLASH_DUMMYRESOURCE);
bindIq.setStanzaId("bind-id");
String string = bindIq.toString();
assertEquals("IQ Stanza (bind urn:ietf:params:xml:ns:xmpp-bind) [id=bind-id,type=get,]", string);
}
}

View file

@ -996,6 +996,11 @@ public class EnhancedDebugger implements SmackDebugger {
return text;
}
@Override
public String toString() {
return toXML();
}
}
/**

View file

@ -311,6 +311,11 @@ public class MultipleRecipientManager {
return text;
}
@Override
public String toString() {
return toXML().toString();
}
}
}