From 49e1c837b2ba19708d51d0e71ebb9bc947113683 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Thu, 26 Feb 2015 08:45:28 +0100 Subject: [PATCH] Adjust AbstractListFilter.toString() to produce a similar styled output like the other toString() methods and add unit-test for the method. --- .../smack/filter/AbstractListFilter.java | 11 +++-- .../smack/filters/FilterToStringTest.java | 42 +++++++++++++++++++ 2 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 smack-core/src/test/java/org/jivesoftware/smack/filters/FilterToStringTest.java diff --git a/smack-core/src/main/java/org/jivesoftware/smack/filter/AbstractListFilter.java b/smack-core/src/main/java/org/jivesoftware/smack/filter/AbstractListFilter.java index d32fb6eef..897ae24b6 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/filter/AbstractListFilter.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/filter/AbstractListFilter.java @@ -19,6 +19,7 @@ package org.jivesoftware.smack.filter; import java.util.ArrayList; import java.util.Arrays; +import java.util.Iterator; import java.util.List; import org.jivesoftware.smack.util.Objects; @@ -68,9 +69,13 @@ public abstract class AbstractListFilter implements PacketFilter { public final String toString() { StringBuilder sb = new StringBuilder(); sb.append(getClass().getSimpleName()); - sb.append(" ("); - for (PacketFilter filter : filters) { - sb.append(' ' + filter.toString() + ','); + sb.append(": ("); + for (Iterator it = filters.iterator(); it.hasNext();) { + PacketFilter filter = it.next(); + sb.append(filter.toString()); + if (it.hasNext()) { + sb.append(", "); + } } sb.append(")"); return sb.toString(); diff --git a/smack-core/src/test/java/org/jivesoftware/smack/filters/FilterToStringTest.java b/smack-core/src/test/java/org/jivesoftware/smack/filters/FilterToStringTest.java new file mode 100644 index 000000000..8d5ed2e8d --- /dev/null +++ b/smack-core/src/test/java/org/jivesoftware/smack/filters/FilterToStringTest.java @@ -0,0 +1,42 @@ +/** + * + * Copyright 2015 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.filters; + +import static org.junit.Assert.assertEquals; + +import org.jivesoftware.smack.filter.AndFilter; +import org.jivesoftware.smack.filter.MessageWithBodiesFilter; +import org.jivesoftware.smack.filter.StanzaIdFilter; +import org.jivesoftware.smack.filter.ThreadFilter; +import org.junit.Test; + +/** + * + */ +public class FilterToStringTest { + + @Test + public void abstractListFilterToStringTest() { + AndFilter andFilter = new AndFilter(); + andFilter.addFilter(new StanzaIdFilter("foo")); + andFilter.addFilter(new ThreadFilter("42")); + andFilter.addFilter(MessageWithBodiesFilter.INSTANCE); + + final String res =andFilter.toString(); + assertEquals("AndFilter: (StanzaIdFilter: id=foo, ThreadFilter: thread=42, MessageWithBodiesFilter)", res); + } +}