You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2002/07/20 20:33:36 UTC
cvs commit: jakarta-commons-sandbox/pattern/src/test/org/apache/commons/pattern/command TestCommandUtils.java CommandTestSuite.java
scolebourne 2002/07/20 11:33:36
Added: pattern/src/test/org/apache/commons/pattern/command
TestCommandUtils.java CommandTestSuite.java
Log:
Add CommandUtils tests
Revision Changes Path
1.1 jakarta-commons-sandbox/pattern/src/test/org/apache/commons/pattern/command/TestCommandUtils.java
Index: TestCommandUtils.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* 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 acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* 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 THE APACHE SOFTWARE FOUNDATION 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.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.pattern.command;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.commons.pattern.predicate.Predicate;
import org.apache.commons.pattern.predicate.PredicateUtils;
/**
* Tests the org.apache.commons.pattern.command.CommandUtils class.
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id: TestCommandUtils.java,v 1.1 2002/07/20 18:33:36 scolebourne Exp $
*/
public class TestCommandUtils extends junit.framework.TestCase {
private static final Object cObject = new Object();
private static final Object cString = "Hello";
private static final Object cInteger = new Integer(6);
/**
* Construct
*/
public TestCommandUtils(String name) {
super(name);
}
/**
* Return class aa a test suite.
*/
public static Test suite() {
return new TestSuite(TestCommandUtils.class);
}
/**
* Set up instance variables required by this test case.
*/
public void setUp() {
}
/**
* Tear down instance variables required by this test case.
*/
public void tearDown() {
}
static class MockCommand implements Command {
int count = 0;
/**
* @see org.apache.commons.pattern.command.Command#execute(Object)
*/
public void execute(Object object) {
count++;
}
}
// exceptionCommand
//------------------------------------------------------------------
public void testExceptionCommand() {
assertNotNull(CommandUtils.exceptionCommand());
assertSame(CommandUtils.exceptionCommand(), CommandUtils.exceptionCommand());
try {
CommandUtils.exceptionCommand().execute(null);
} catch (CommandException ex) {
try {
CommandUtils.exceptionCommand().execute(cString);
} catch (CommandException ex2) {
return;
}
}
fail();
}
// nopCommand
//------------------------------------------------------------------
public void testNopCommand() {
StringBuffer buf = new StringBuffer("Hello");
CommandUtils.nopCommand().execute(null);
assertEquals("Hello", buf.toString());
CommandUtils.nopCommand().execute("Hello");
assertEquals("Hello", buf.toString());
}
// invokeCommand
//------------------------------------------------------------------
public void testInvokeCommand() {
StringBuffer buf = new StringBuffer("Hello");
CommandUtils.invokeCommand("reverse").execute(buf);
assertEquals("olleH", buf.toString());
buf = new StringBuffer("Hello");
CommandUtils.invokeCommand("setLength", new Class[] {Integer.TYPE}, new Object[] {new Integer(2)}).execute(buf);
assertEquals("He", buf.toString());
}
// forCommand
//------------------------------------------------------------------
public void testForCommand() {
MockCommand cmd = new MockCommand();
CommandUtils.forCommand(5, cmd).execute(null);
assertEquals(5, cmd.count);
try {
CommandUtils.forCommand(-1, new MockCommand());
} catch (IllegalArgumentException ex) {
return;
}
fail();
}
// whileCommand
//------------------------------------------------------------------
public void testWhileCommand() {
MockCommand cmd = new MockCommand();
CommandUtils.whileCommand(PredicateUtils.falsePredicate(), cmd).execute(null);
assertEquals(0, cmd.count);
try {
CommandUtils.whileCommand(null, null);
} catch (IllegalArgumentException ex) {
return;
}
fail();
}
// doWhileCommand
//------------------------------------------------------------------
public void testDoWhileCommand() {
MockCommand cmd = new MockCommand();
CommandUtils.doWhileCommand(cmd, PredicateUtils.falsePredicate()).execute(null);
assertEquals(1, cmd.count);
try {
CommandUtils.doWhileCommand(null, null);
} catch (IllegalArgumentException ex) {
return;
}
fail();
}
// chainedCommand
//------------------------------------------------------------------
public void testChainedCommand() {
MockCommand a = new MockCommand();
MockCommand b = new MockCommand();
CommandUtils.chainedCommand(a, b).execute(null);
assertEquals(1, a.count);
assertEquals(1, b.count);
a = new MockCommand();
b = new MockCommand();
CommandUtils.chainedCommand(new Command[] {a, b, a}).execute(null);
assertEquals(2, a.count);
assertEquals(1, b.count);
a = new MockCommand();
b = new MockCommand();
Collection coll = new ArrayList();
coll.add(b);
coll.add(a);
coll.add(b);
CommandUtils.chainedCommand(coll).execute(null);
assertEquals(1, a.count);
assertEquals(2, b.count);
}
public void testChainedCommandEx1a() {
try {
CommandUtils.chainedCommand(null, null);
} catch (IllegalArgumentException ex) {
return;
}
fail();
}
public void testChainedCommandEx1b() {
try {
CommandUtils.chainedCommand((Command[]) null);
} catch (IllegalArgumentException ex) {
return;
}
fail();
}
public void testChainedCommandEx1c() {
try {
CommandUtils.chainedCommand((Collection) null);
} catch (IllegalArgumentException ex) {
return;
}
fail();
}
public void testChainedCommandEx2() {
try {
CommandUtils.chainedCommand(new Command[0]);
} catch (IllegalArgumentException ex) {
return;
}
fail();
}
public void testChainedCommandEx3() {
try {
CommandUtils.chainedCommand(new Command[] {null, null});
} catch (IllegalArgumentException ex) {
return;
}
fail();
}
public void testChainedCommandEx4() {
try {
CommandUtils.chainedCommand(Collections.EMPTY_LIST);
} catch (IllegalArgumentException ex) {
return;
}
fail();
}
public void testChainedCommandEx5() {
try {
Collection coll = new ArrayList();
coll.add(null);
coll.add(null);
CommandUtils.chainedCommand(coll);
} catch (IllegalArgumentException ex) {
return;
}
fail();
}
// switchCommand
//------------------------------------------------------------------
public void testSwitchCommand() {
MockCommand a = new MockCommand();
MockCommand b = new MockCommand();
CommandUtils.switchCommand(PredicateUtils.truePredicate(), a, b).execute(null);
assertEquals(1, a.count);
assertEquals(0, b.count);
a = new MockCommand();
b = new MockCommand();
CommandUtils.switchCommand(PredicateUtils.falsePredicate(), a, b).execute(null);
assertEquals(0, a.count);
assertEquals(1, b.count);
a = new MockCommand();
b = new MockCommand();
CommandUtils.switchCommand(
new Predicate[] {PredicateUtils.equalPredicate("HELLO"), PredicateUtils.equalPredicate("THERE")},
new Command[] {a, b}).execute("WELL");
assertEquals(0, a.count);
assertEquals(0, b.count);
a = new MockCommand();
b = new MockCommand();
CommandUtils.switchCommand(
new Predicate[] {PredicateUtils.equalPredicate("HELLO"), PredicateUtils.equalPredicate("THERE")},
new Command[] {a, b}).execute("HELLO");
assertEquals(1, a.count);
assertEquals(0, b.count);
a = new MockCommand();
b = new MockCommand();
MockCommand c = new MockCommand();
CommandUtils.switchCommand(
new Predicate[] {PredicateUtils.equalPredicate("HELLO"), PredicateUtils.equalPredicate("THERE")},
new Command[] {a, b}, c).execute("WELL");
assertEquals(0, a.count);
assertEquals(0, b.count);
assertEquals(1, c.count);
a = new MockCommand();
b = new MockCommand();
Map map = new HashMap();
map.put(PredicateUtils.equalPredicate("HELLO"), a);
map.put(PredicateUtils.equalPredicate("THERE"), b);
CommandUtils.switchCommand(map).execute(null);
assertEquals(0, a.count);
assertEquals(0, b.count);
a = new MockCommand();
b = new MockCommand();
map = new HashMap();
map.put(PredicateUtils.equalPredicate("HELLO"), a);
map.put(PredicateUtils.equalPredicate("THERE"), b);
CommandUtils.switchCommand(map).execute("THERE");
assertEquals(0, a.count);
assertEquals(1, b.count);
a = new MockCommand();
b = new MockCommand();
c = new MockCommand();
map = new HashMap();
map.put(PredicateUtils.equalPredicate("HELLO"), a);
map.put(PredicateUtils.equalPredicate("THERE"), b);
map.put(null, c);
CommandUtils.switchCommand(map).execute("WELL");
assertEquals(0, a.count);
assertEquals(0, b.count);
assertEquals(1, c.count);
}
public void testSwitchCommandEx1a() {
try {
CommandUtils.switchCommand(null, null);
} catch (IllegalArgumentException ex) {
return;
}
fail();
}
public void testSwitchCommandEx1b() {
try {
CommandUtils.switchCommand((Predicate[]) null, (Command[]) null);
} catch (IllegalArgumentException ex) {
return;
}
fail();
}
public void testSwitchCommandEx1c() {
try {
CommandUtils.switchCommand((Map) null);
} catch (IllegalArgumentException ex) {
return;
}
fail();
}
public void testSwitchCommandEx2() {
try {
CommandUtils.switchCommand(new Predicate[0], new Command[0]);
} catch (IllegalArgumentException ex) {
return;
}
fail();
}
public void testSwitchCommandEx3() {
try {
CommandUtils.switchCommand(new Predicate[2], new Command[2]);
} catch (IllegalArgumentException ex) {
return;
}
fail();
}
public void testSwitchCommandEx4() {
try {
CommandUtils.switchCommand(Collections.EMPTY_MAP);
} catch (IllegalArgumentException ex) {
return;
}
fail();
}
public void testSwitchCommandEx5() {
try {
Map map = new HashMap();
map.put(null, null);
map.put(null, null);
CommandUtils.switchCommand(map);
} catch (IllegalArgumentException ex) {
return;
}
fail();
}
public void testSwitchCommandEx6() {
try {
CommandUtils.switchCommand(new Predicate[2], new Command[1]);
} catch (IllegalArgumentException ex) {
return;
}
fail();
}
// switchMapCommand
//------------------------------------------------------------------
public void testSwitchMapCommand() {
MockCommand a = new MockCommand();
MockCommand b = new MockCommand();
Map map = new HashMap();
map.put("HELLO", a);
map.put("THERE", b);
CommandUtils.switchMapCommand(map).execute(null);
assertEquals(0, a.count);
assertEquals(0, b.count);
a = new MockCommand();
b = new MockCommand();
map = new HashMap();
map.put("HELLO", a);
map.put("THERE", b);
CommandUtils.switchMapCommand(map).execute("THERE");
assertEquals(0, a.count);
assertEquals(1, b.count);
a = new MockCommand();
b = new MockCommand();
MockCommand c = new MockCommand();
map = new HashMap();
map.put("HELLO", a);
map.put("THERE", b);
map.put(null, c);
CommandUtils.switchMapCommand(map).execute("WELL");
assertEquals(0, a.count);
assertEquals(0, b.count);
assertEquals(1, c.count);
}
public void testSwitchMapCommandEx1() {
try {
CommandUtils.switchMapCommand(null);
} catch (IllegalArgumentException ex) {
return;
}
fail();
}
public void testSwitchMapCommandEx2() {
try {
CommandUtils.switchMapCommand(Collections.EMPTY_MAP);
} catch (IllegalArgumentException ex) {
return;
}
fail();
}
}
1.1 jakarta-commons-sandbox/pattern/src/test/org/apache/commons/pattern/command/CommandTestSuite.java
Index: CommandTestSuite.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* 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 acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* 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 THE APACHE SOFTWARE FOUNDATION 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.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.pattern.command;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
/**
* Test suite for the Command pattern package.
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id: CommandTestSuite.java,v 1.1 2002/07/20 18:33:36 scolebourne Exp $
*/
public class CommandTestSuite extends TestCase {
/**
* Construct a new instance.
*/
public CommandTestSuite(String name) {
super(name);
}
/**
* Command-line interface.
*/
public static void main(String[] args) {
TestRunner.run(suite());
}
/**
* Get the suite of tests
*/
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest(TestCommandUtils.suite());
return suite;
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>