From 6871224e65e9396cf51c302eb50b8e68deb65aa6 Mon Sep 17 00:00:00 2001 From: Bill Lynch Date: Sat, 2 Aug 2003 17:33:50 +0000 Subject: [PATCH] Initial checkin of test cases. git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@2002 b35dd754-fafc-0310-a699-88a17e54d16e --- .../smack/filter/AndFilterTest.java | 92 ++++++++ .../smack/filter/FromContainsFilterTest.java | 104 +++++++++ .../smack/filter/NotFilterTest.java | 86 +++++++ .../smack/filter/OrFilterTest.java | 92 ++++++++ .../smack/filter/PacketIDFilterTest.java | 98 ++++++++ .../smack/filter/PacketTypeFilterTest.java | 91 ++++++++ .../smack/filter/ToContainsFilterTest.java | 104 +++++++++ .../jivesoftware/smack/packet/MockPacket.java | 69 ++++++ .../smack/packet/MockPacketFilter.java | 73 ++++++ .../smack/util/StringUtilsTest.java | 209 ++++++++++++++++++ 10 files changed, 1018 insertions(+) create mode 100644 test/org/jivesoftware/smack/filter/AndFilterTest.java create mode 100644 test/org/jivesoftware/smack/filter/FromContainsFilterTest.java create mode 100644 test/org/jivesoftware/smack/filter/NotFilterTest.java create mode 100644 test/org/jivesoftware/smack/filter/OrFilterTest.java create mode 100644 test/org/jivesoftware/smack/filter/PacketIDFilterTest.java create mode 100644 test/org/jivesoftware/smack/filter/PacketTypeFilterTest.java create mode 100644 test/org/jivesoftware/smack/filter/ToContainsFilterTest.java create mode 100644 test/org/jivesoftware/smack/packet/MockPacket.java create mode 100644 test/org/jivesoftware/smack/packet/MockPacketFilter.java create mode 100644 test/org/jivesoftware/smack/util/StringUtilsTest.java diff --git a/test/org/jivesoftware/smack/filter/AndFilterTest.java b/test/org/jivesoftware/smack/filter/AndFilterTest.java new file mode 100644 index 000000000..1dfb54024 --- /dev/null +++ b/test/org/jivesoftware/smack/filter/AndFilterTest.java @@ -0,0 +1,92 @@ +/** + * $RCSfile$ + * $Revision$ + * $Date$ + * + * Copyright (C) 2002-2003 Jive Software. All rights reserved. + * ==================================================================== + * The Jive Software License (based on Apache Software License, Version 1.1) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by + * Jive Software (http://www.jivesoftware.com)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Smack" and "Jive Software" must not be used to + * endorse or promote products derived from this software without + * prior written permission. For written permission, please + * contact webmaster@jivesoftware.com. + * + * 5. Products derived from this software may not be called "Smack", + * nor may "Smack" appear in their name, without prior written + * permission of Jive Software. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + */ + +package org.jivesoftware.smack.filter; + +import junit.framework.TestCase; +import org.jivesoftware.smack.packet.*; + +/** + * A test case for the AndFilter class. + */ +public class AndFilterTest extends TestCase { + + public void testNullArgs() { + PacketFilter filter = null; + try { + AndFilter and = new AndFilter(filter, filter); + fail("Should have thrown IllegalArgumentException"); + } + catch (IllegalArgumentException e) { + assertTrue(true); + } + } + + public void testAccept() { + MockPacketFilter trueFilter = new MockPacketFilter(true); + MockPacketFilter falseFilter = new MockPacketFilter(false); + + MockPacket packet = new MockPacket(); + + AndFilter andFilter = new AndFilter(trueFilter, trueFilter); + assertTrue(andFilter.accept(packet)); + + andFilter = new AndFilter(trueFilter, falseFilter); + assertFalse(andFilter.accept(packet)); + + andFilter = new AndFilter(falseFilter, trueFilter); + assertFalse(andFilter.accept(packet)); + + andFilter = new AndFilter(falseFilter, falseFilter); + assertFalse(andFilter.accept(packet)); + } +} diff --git a/test/org/jivesoftware/smack/filter/FromContainsFilterTest.java b/test/org/jivesoftware/smack/filter/FromContainsFilterTest.java new file mode 100644 index 000000000..c06df49aa --- /dev/null +++ b/test/org/jivesoftware/smack/filter/FromContainsFilterTest.java @@ -0,0 +1,104 @@ +/** + * $RCSfile$ + * $Revision$ + * $Date$ + * + * Copyright (C) 2002-2003 Jive Software. All rights reserved. + * ==================================================================== + * The Jive Software License (based on Apache Software License, Version 1.1) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by + * Jive Software (http://www.jivesoftware.com)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Smack" and "Jive Software" must not be used to + * endorse or promote products derived from this software without + * prior written permission. For written permission, please + * contact webmaster@jivesoftware.com. + * + * 5. Products derived from this software may not be called "Smack", + * nor may "Smack" appear in their name, without prior written + * permission of Jive Software. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + */ + +package org.jivesoftware.smack.filter; + +import junit.framework.TestCase; +import org.jivesoftware.smack.packet.*; + +/** + * A test case for the FromContainsFilter class. + */ +public class FromContainsFilterTest extends TestCase { + + public void testNullArgs() { + try { + new FromContainsFilter(null); + fail("Parameter can not be null"); + } + catch (IllegalArgumentException e) { + assertTrue(true); + } + } + + public void testAccept() { + MockFromPacket packet = new MockFromPacket("foo@bar.com"); + + FromContainsFilter fromContainsFilter = new FromContainsFilter(""); + assertTrue(fromContainsFilter.accept(packet)); + + fromContainsFilter = new FromContainsFilter("foo"); + assertTrue(fromContainsFilter.accept(packet)); + + fromContainsFilter = new FromContainsFilter("foo@bar.com"); + assertTrue(fromContainsFilter.accept(packet)); + + fromContainsFilter = new FromContainsFilter("bar@foo.com"); + assertFalse(fromContainsFilter.accept(packet)); + + fromContainsFilter = new FromContainsFilter("blah-stuff,net"); + assertFalse(fromContainsFilter.accept(packet)); + } + + /** + * Wraps the MockPacket class to always give an expected From field. + */ + private class MockFromPacket extends MockPacket { + private String from; + public MockFromPacket(String from) { + this.from = from; + } + public String getFrom() { + return from; + } + } +} diff --git a/test/org/jivesoftware/smack/filter/NotFilterTest.java b/test/org/jivesoftware/smack/filter/NotFilterTest.java new file mode 100644 index 000000000..da8204f6e --- /dev/null +++ b/test/org/jivesoftware/smack/filter/NotFilterTest.java @@ -0,0 +1,86 @@ +/** + * $RCSfile$ + * $Revision$ + * $Date$ + * + * Copyright (C) 2002-2003 Jive Software. All rights reserved. + * ==================================================================== + * The Jive Software License (based on Apache Software License, Version 1.1) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by + * Jive Software (http://www.jivesoftware.com)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Smack" and "Jive Software" must not be used to + * endorse or promote products derived from this software without + * prior written permission. For written permission, please + * contact webmaster@jivesoftware.com. + * + * 5. Products derived from this software may not be called "Smack", + * nor may "Smack" appear in their name, without prior written + * permission of Jive Software. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + */ + +package org.jivesoftware.smack.filter; + +import junit.framework.TestCase; +import org.jivesoftware.smack.packet.*; + +/** + * A test case for the NotFilter class. + */ +public class NotFilterTest extends TestCase { + + public void testNullArgs() { + PacketFilter filter = null; + try { + NotFilter or = new NotFilter(filter); + fail("Should have thrown IllegalArgumentException"); + } + catch (IllegalArgumentException e) { + assertTrue(true); + } + } + + public void testAccept() { + MockPacketFilter trueFilter = new MockPacketFilter(true); + MockPacketFilter falseFilter = new MockPacketFilter(false); + + MockPacket packet = new MockPacket(); + + NotFilter NotFilter = new NotFilter(trueFilter); + assertFalse(NotFilter.accept(packet)); + + NotFilter = new NotFilter(falseFilter); + assertTrue(NotFilter.accept(packet)); + } +} diff --git a/test/org/jivesoftware/smack/filter/OrFilterTest.java b/test/org/jivesoftware/smack/filter/OrFilterTest.java new file mode 100644 index 000000000..79d4ad697 --- /dev/null +++ b/test/org/jivesoftware/smack/filter/OrFilterTest.java @@ -0,0 +1,92 @@ +/** + * $RCSfile$ + * $Revision$ + * $Date$ + * + * Copyright (C) 2002-2003 Jive Software. All rights reserved. + * ==================================================================== + * The Jive Software License (based on Apache Software License, Version 1.1) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by + * Jive Software (http://www.jivesoftware.com)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Smack" and "Jive Software" must not be used to + * endorse or promote products derived from this software without + * prior written permission. For written permission, please + * contact webmaster@jivesoftware.com. + * + * 5. Products derived from this software may not be called "Smack", + * nor may "Smack" appear in their name, without prior written + * permission of Jive Software. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + */ + +package org.jivesoftware.smack.filter; + +import junit.framework.TestCase; +import org.jivesoftware.smack.packet.*; + +/** + * A test case for the OrFilter class. + */ +public class OrFilterTest extends TestCase { + + public void testNullArgs() { + PacketFilter filter = null; + try { + OrFilter or = new OrFilter(filter, filter); + fail("Should have thrown IllegalArgumentException"); + } + catch (IllegalArgumentException e) { + assertTrue(true); + } + } + + public void testAccept() { + MockPacketFilter trueFilter = new MockPacketFilter(true); + MockPacketFilter falseFilter = new MockPacketFilter(false); + + MockPacket packet = new MockPacket(); + + OrFilter OrFilter = new OrFilter(trueFilter, trueFilter); + assertTrue(OrFilter.accept(packet)); + + OrFilter = new OrFilter(trueFilter, falseFilter); + assertTrue(OrFilter.accept(packet)); + + OrFilter = new OrFilter(falseFilter, trueFilter); + assertTrue(OrFilter.accept(packet)); + + OrFilter = new OrFilter(falseFilter, falseFilter); + assertFalse(OrFilter.accept(packet)); + } +} diff --git a/test/org/jivesoftware/smack/filter/PacketIDFilterTest.java b/test/org/jivesoftware/smack/filter/PacketIDFilterTest.java new file mode 100644 index 000000000..8561c7a7b --- /dev/null +++ b/test/org/jivesoftware/smack/filter/PacketIDFilterTest.java @@ -0,0 +1,98 @@ +/** + * $RCSfile$ + * $Revision$ + * $Date$ + * + * Copyright (C) 2002-2003 Jive Software. All rights reserved. + * ==================================================================== + * The Jive Software License (based on Apache Software License, Version 1.1) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by + * Jive Software (http://www.jivesoftware.com)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Smack" and "Jive Software" must not be used to + * endorse or promote products derived from this software without + * prior written permission. For written permission, please + * contact webmaster@jivesoftware.com. + * + * 5. Products derived from this software may not be called "Smack", + * nor may "Smack" appear in their name, without prior written + * permission of Jive Software. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + */ + +package org.jivesoftware.smack.filter; + +import junit.framework.TestCase; +import org.jivesoftware.smack.packet.*; + +/** + * A test case for the PacketIDFilter class. + */ +public class PacketIDFilterTest extends TestCase { + + public void testNullArgs() { + try { + new PacketIDFilter(null); + fail("Parameter can not be null"); + } + catch (IllegalArgumentException e) { + assertTrue(true); + } + } + + public void testAccept() { + MockIDPacket packet = new MockIDPacket("foo"); + + PacketIDFilter packetIDFilter = new PacketIDFilter(""); + assertFalse(packetIDFilter.accept(packet)); + + packetIDFilter = new PacketIDFilter("foo"); + assertTrue(packetIDFilter.accept(packet)); + + packetIDFilter = new PacketIDFilter("fOO"); + assertFalse(packetIDFilter.accept(packet)); + } + + /** + * Wraps the MockPacket class to always give an expected packet ID field. + */ + private class MockIDPacket extends MockPacket { + private String id; + public MockIDPacket(String id) { + this.id = id; + } + public String getPacketID() { + return id; + } + } +} diff --git a/test/org/jivesoftware/smack/filter/PacketTypeFilterTest.java b/test/org/jivesoftware/smack/filter/PacketTypeFilterTest.java new file mode 100644 index 000000000..00229c195 --- /dev/null +++ b/test/org/jivesoftware/smack/filter/PacketTypeFilterTest.java @@ -0,0 +1,91 @@ +/** + * $RCSfile$ + * $Revision$ + * $Date$ + * + * Copyright (C) 2002-2003 Jive Software. All rights reserved. + * ==================================================================== + * The Jive Software License (based on Apache Software License, Version 1.1) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by + * Jive Software (http://www.jivesoftware.com)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Smack" and "Jive Software" must not be used to + * endorse or promote products derived from this software without + * prior written permission. For written permission, please + * contact webmaster@jivesoftware.com. + * + * 5. Products derived from this software may not be called "Smack", + * nor may "Smack" appear in their name, without prior written + * permission of Jive Software. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + */ + +package org.jivesoftware.smack.filter; + +import junit.framework.TestCase; +import org.jivesoftware.smack.packet.*; + +/** + * + */ +public class PacketTypeFilterTest extends TestCase { + + private class Dummy {} + + public void testConstructor() { + try { + new PacketTypeFilter(Dummy.class); + fail("Parameter must be a subclass of Packet."); + } + catch (IllegalArgumentException e) { + } + try { + new PacketTypeFilter(MockPacket.class); + } + catch (IllegalArgumentException e) { + fail(); + } + try { + new PacketTypeFilter(IQ.class); + } + catch (IllegalArgumentException e) { + fail(); + } + } + + public void testAccept() { + MockPacket packet = new MockPacket(); + PacketTypeFilter filter = new PacketTypeFilter(MockPacket.class); + assertTrue(filter.accept(packet)); + } +} diff --git a/test/org/jivesoftware/smack/filter/ToContainsFilterTest.java b/test/org/jivesoftware/smack/filter/ToContainsFilterTest.java new file mode 100644 index 000000000..20986af1a --- /dev/null +++ b/test/org/jivesoftware/smack/filter/ToContainsFilterTest.java @@ -0,0 +1,104 @@ +/** + * $RCSfile$ + * $Revision$ + * $Date$ + * + * Copyright (C) 2002-2003 Jive Software. All rights reserved. + * ==================================================================== + * The Jive Software License (based on Apache Software License, Version 1.1) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by + * Jive Software (http://www.jivesoftware.com)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Smack" and "Jive Software" must not be used to + * endorse or promote products derived from this software without + * prior written permission. For written permission, please + * contact webmaster@jivesoftware.com. + * + * 5. Products derived from this software may not be called "Smack", + * nor may "Smack" appear in their name, without prior written + * permission of Jive Software. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + */ + +package org.jivesoftware.smack.filter; + +import junit.framework.TestCase; +import org.jivesoftware.smack.packet.*; + +/** + * A test case for the ToContainsFilter class. + */ +public class ToContainsFilterTest extends TestCase { + + public void testNullArgs() { + try { + new ToContainsFilter(null); + fail("Parameter can not be null"); + } + catch (IllegalArgumentException e) { + assertTrue(true); + } + } + + public void testAccept() { + MockToPacket packet = new MockToPacket("foo@bar.com"); + + ToContainsFilter toContainsFilter = new ToContainsFilter(""); + assertTrue(toContainsFilter.accept(packet)); + + toContainsFilter = new ToContainsFilter("foo"); + assertTrue(toContainsFilter.accept(packet)); + + toContainsFilter = new ToContainsFilter("foo@bar.com"); + assertTrue(toContainsFilter.accept(packet)); + + toContainsFilter = new ToContainsFilter("bar@foo.com"); + assertFalse(toContainsFilter.accept(packet)); + + toContainsFilter = new ToContainsFilter("blah-stuff,net"); + assertFalse(toContainsFilter.accept(packet)); + } + + /** + * Wraps the MockPacket class to always give an expected To field. + */ + private class MockToPacket extends MockPacket { + private String to; + public MockToPacket(String to) { + this.to = to; + } + public String getTo() { + return to; + } + } +} diff --git a/test/org/jivesoftware/smack/packet/MockPacket.java b/test/org/jivesoftware/smack/packet/MockPacket.java new file mode 100644 index 000000000..3fba4c016 --- /dev/null +++ b/test/org/jivesoftware/smack/packet/MockPacket.java @@ -0,0 +1,69 @@ +/** + * $RCSfile$ + * $Revision$ + * $Date$ + * + * Copyright (C) 2002-2003 Jive Software. All rights reserved. + * ==================================================================== + * The Jive Software License (based on Apache Software License, Version 1.1) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by + * Jive Software (http://www.jivesoftware.com)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Smack" and "Jive Software" must not be used to + * endorse or promote products derived from this software without + * prior written permission. For written permission, please + * contact webmaster@jivesoftware.com. + * + * 5. Products derived from this software may not be called "Smack", + * nor may "Smack" appear in their name, without prior written + * permission of Jive Software. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + */ + +package org.jivesoftware.smack.packet; + +import java.util.*; + +/** + * A mock implementation of the Packet abstract class. Implements toXML() by returning null. + */ +public class MockPacket extends Packet { + + /** + * Returns null always. + * @return null + */ + public String toXML() { + return null; + } +} diff --git a/test/org/jivesoftware/smack/packet/MockPacketFilter.java b/test/org/jivesoftware/smack/packet/MockPacketFilter.java new file mode 100644 index 000000000..f2f845d18 --- /dev/null +++ b/test/org/jivesoftware/smack/packet/MockPacketFilter.java @@ -0,0 +1,73 @@ +/** + * $RCSfile$ + * $Revision$ + * $Date$ + * + * Copyright (C) 2002-2003 Jive Software. All rights reserved. + * ==================================================================== + * The Jive Software License (based on Apache Software License, Version 1.1) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by + * Jive Software (http://www.jivesoftware.com)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Smack" and "Jive Software" must not be used to + * endorse or promote products derived from this software without + * prior written permission. For written permission, please + * contact webmaster@jivesoftware.com. + * + * 5. Products derived from this software may not be called "Smack", + * nor may "Smack" appear in their name, without prior written + * permission of Jive Software. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + */ + +package org.jivesoftware.smack.packet; + +import org.jivesoftware.smack.filter.PacketFilter; + +/** + * A mock implementation of the PacketFilter class. Pass in the value you want the + * accept(..) method to return. + */ +public class MockPacketFilter implements PacketFilter { + + private boolean acceptValue; + + public MockPacketFilter(boolean acceptValue) { + this.acceptValue = acceptValue; + } + + public boolean accept(Packet packet) { + return acceptValue; + } + +} diff --git a/test/org/jivesoftware/smack/util/StringUtilsTest.java b/test/org/jivesoftware/smack/util/StringUtilsTest.java new file mode 100644 index 000000000..0cff92d4c --- /dev/null +++ b/test/org/jivesoftware/smack/util/StringUtilsTest.java @@ -0,0 +1,209 @@ +/** + * $RCSfile$ + * $Revision$ + * $Date$ + * + * Copyright (C) 2002-2003 Jive Software. All rights reserved. + * ==================================================================== + * The Jive Software License (based on Apache Software License, Version 1.1) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by + * Jive Software (http://www.jivesoftware.com)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Smack" and "Jive Software" must not be used to + * endorse or promote products derived from this software without + * prior written permission. For written permission, please + * contact webmaster@jivesoftware.com. + * + * 5. Products derived from this software may not be called "Smack", + * nor may "Smack" appear in their name, without prior written + * permission of Jive Software. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + */ + +package org.jivesoftware.smack.util; + +import junit.framework.TestCase; + +/** + * A test case for the StringUtils class. + */ +public class StringUtilsTest extends TestCase { + + public void testEscapeForXML() { + String input = null; + + assertNull(StringUtils.escapeForXML(null)); + + input = ""; + assertEquals(StringUtils.escapeForXML(input), "<b>"); + + input = "\""; + assertEquals(StringUtils.escapeForXML(input), """); + + input = "&"; + assertEquals(StringUtils.escapeForXML(input), "&"); + + input = "\n\t\r"; + assertEquals(StringUtils.escapeForXML(input), "<b>\n\t\r</b>"); + + input = " & "; + assertEquals(StringUtils.escapeForXML(input), " & "); + + input = " \" "; + assertEquals(StringUtils.escapeForXML(input), " " "); + } + + public void testHash() { + // Test null + // @TODO - should the StringUtils.hash(String) method be fixed to handle null input? + try { + StringUtils.hash(null); + fail(); + } + catch (NullPointerException npe) { + assertTrue(true); + } + + // Test empty String + String result = StringUtils.hash(""); + assertEquals("da39a3ee5e6b4b0d3255bfef95601890afd80709", result); + + // Test a known hash + String adminInHash = "d033e22ae348aeb5660fc2140aec35850c4da997"; + result = StringUtils.hash("admin"); + assertEquals(adminInHash, result); + + // Test a random String - make sure all resulting characters are valid hash characters + // and that the returned string is 32 characters long. + String random = "jive software blah and stuff this is pretty cool"; + result = StringUtils.hash(random); + assertTrue(isValidHash(result)); + + // Test junk input: + String junk = "\n\n\t\b\r!@(!)^(#)@+_-\u2031\u09291\u00A9\u00BD\u0394\u00F8"; + result = StringUtils.hash(junk); + assertTrue(isValidHash(result)); + } + + /* ----- Utility methods and vars ----- */ + + private final String HASH_CHARS = "0123456789abcdef"; + + /** + * Returns true if the input string is valid md5 hash, false otherwise. + */ + private boolean isValidHash(String result) { + boolean valid = true; + for (int i=0; i#$%^&*"; + output = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU2Nzg5CgkiPyEuQHt9W10oKTsnLC4vPD4jJCVeJio="; + assertEquals(StringUtils.encodeBase64(input), output); + } + + /*** + * This method tests 2 StringUtil methods - decodeBase64(String) and decodeBase64(byte[]). + */ + /* + public void testDecodeBase64() { + String input = ""; + String output = ""; + assertEquals(StringUtils.decodeBase64(input), output); + + input = "Zm9vIGJhciAxMjM="; + output = "foo bar 123"; + assertEquals(StringUtils.decodeBase64(input), output); + + input = "PQ=="; + output = "="; + assertEquals(StringUtils.decodeBase64(input), output); + + input = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU2Nzg5CgkiPyEuQHt9W10oKTsnLC4vPD4jJCVeJio="; + output = "abcdefghijklmnopqrstuvwxyz0123456789\n\t\"?!.@{}[]();',./<>#$%^&*"; + assertEquals(StringUtils.decodeBase64(input), output); + } + */ + + public void testRandomString() { + // Boundary test + String result = StringUtils.randomString(-1); + assertNull(result); + + // Zero length string test + result = StringUtils.randomString(0); + assertNull(result); + + // Test various lengths - make sure the same length is returned + result = StringUtils.randomString(4); + assertTrue(result.length() == 4); + result = StringUtils.randomString(16); + assertTrue(result.length() == 16); + result = StringUtils.randomString(128); + assertTrue(result.length() == 128); + } +}