Adjust AbstractListFilter.toString()

to produce a similar styled output like the other toString() methods and
add unit-test for the method.
This commit is contained in:
Florian Schmaus 2015-02-26 08:45:28 +01:00
parent 4b7a396b9b
commit 49e1c837b2
2 changed files with 50 additions and 3 deletions

View File

@ -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<PacketFilter> it = filters.iterator(); it.hasNext();) {
PacketFilter filter = it.next();
sb.append(filter.toString());
if (it.hasNext()) {
sb.append(", ");
}
}
sb.append(")");
return sb.toString();

View File

@ -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);
}
}