001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.chain.generic; 018 019 020 import junit.framework.Test; 021 import junit.framework.TestCase; 022 import junit.framework.TestSuite; 023 import org.apache.commons.chain.Context; 024 import org.apache.commons.chain.Catalog; 025 import org.apache.commons.chain.impl.CatalogBase; 026 import org.apache.commons.chain.impl.ContextBase; 027 import org.apache.commons.chain.impl.ChainBase; 028 import org.apache.commons.chain.impl.CatalogFactoryBase; 029 import org.apache.commons.chain.impl.DelegatingCommand; 030 import org.apache.commons.chain.impl.NonDelegatingCommand; 031 032 /** 033 * <p>Test case for the <code>LookupCommand</code> class.</p> 034 * 035 * @author Sean Schofield 036 * @version $Revision: 480477 $ 037 */ 038 039 public class LookupCommandTestCase extends TestCase { 040 041 042 // ---------------------------------------------------- Instance Variables 043 044 /** 045 * The instance of {@link Catalog} to use when looking up commands 046 */ 047 protected Catalog catalog; 048 049 /** 050 * The {@link LookupCommand} instance under test. 051 */ 052 protected LookupCommand command; 053 054 /** 055 * The {@link Context} instance on which to execute the chain. 056 */ 057 protected Context context = null; 058 059 060 // ---------------------------------------------------------- Constructors 061 062 /** 063 * Construct a new instance of this test case. 064 * 065 * @param name Name of the test case 066 */ 067 public LookupCommandTestCase(String name) { 068 super(name); 069 } 070 071 072 // -------------------------------------------------- Overall Test Methods 073 074 075 /** 076 * Set up instance variables required by this test case. 077 */ 078 public void setUp() { 079 catalog = new CatalogBase(); 080 CatalogFactoryBase.getInstance().setCatalog(catalog); 081 command = new LookupCommand(); 082 context = new ContextBase(); 083 } 084 085 086 /** 087 * Return the tests included in this test suite. 088 */ 089 public static Test suite() { 090 return (new TestSuite(LookupCommandTestCase.class)); 091 } 092 093 /** 094 * Tear down instance variables required by this test case. 095 */ 096 public void tearDown() { 097 catalog = null; 098 CatalogFactoryBase.getInstance().clear(); 099 command = null; 100 context = null; 101 } 102 103 104 // ------------------------------------------------ Individual Test Methods 105 106 107 // Test ability to lookup and execute single non-delegating command 108 public void testExecuteMethodLookup_1a() { 109 110 // use default catalog 111 catalog.addCommand("foo", new NonDelegatingCommand("1a")); 112 command.setName("foo"); 113 114 try { 115 assertTrue("Command should return true", 116 command.execute(context)); 117 } catch (Exception e) { 118 fail("Threw exception: " + e); 119 } 120 checkExecuteLog("1a"); 121 } 122 123 // Test ability to lookup and execute a chain 124 public void testExecuteMethodLookup_1b() { 125 126 ChainBase chain = new ChainBase(); 127 chain.addCommand(new DelegatingCommand("1b1")); 128 chain.addCommand(new DelegatingCommand("1b2")); 129 chain.addCommand(new NonDelegatingCommand("1b3")); 130 131 // use default catalog 132 catalog.addCommand("foo", chain); 133 command.setName("foo"); 134 135 try { 136 assertTrue("Command should return true", 137 command.execute(context)); 138 } catch (Exception e) { 139 fail("Threw exception: " + e); 140 } 141 checkExecuteLog("1b1/1b2/1b3"); 142 } 143 144 // Test ability to lookup and execute single non-delegating command 145 // using the context 146 public void testExecuteMethodLookup_2a() { 147 148 // use default catalog 149 catalog.addCommand("foo", new NonDelegatingCommand("2a")); 150 command.setNameKey("nameKey"); 151 context.put("nameKey", "foo"); 152 153 try { 154 assertTrue("Command should return true", 155 command.execute(context)); 156 } catch (Exception e) { 157 fail("Threw exception: " + e); 158 } 159 checkExecuteLog("2a"); 160 } 161 162 // Test ability to lookup and execute a chain using the context 163 public void testExecuteMethodLookup_2b() { 164 165 ChainBase chain = new ChainBase(); 166 chain.addCommand(new DelegatingCommand("2b1")); 167 chain.addCommand(new DelegatingCommand("2b2")); 168 chain.addCommand(new NonDelegatingCommand("2b3")); 169 170 // use default catalog 171 catalog.addCommand("foo", chain); 172 command.setNameKey("nameKey"); 173 context.put("nameKey", "foo"); 174 175 try { 176 assertTrue("Command should return true", 177 command.execute(context)); 178 } catch (Exception e) { 179 fail("Threw exception: " + e); 180 } 181 checkExecuteLog("2b1/2b2/2b3"); 182 } 183 184 // Test ability to lookup and execute single non-delegating command, ignoring its result 185 public void testExecuteMethodLookup_3a() { 186 187 // use default catalog 188 catalog.addCommand("foo", new NonDelegatingCommand("3a")); 189 command.setIgnoreExecuteResult(true); 190 command.setName("foo"); 191 192 try { 193 assertFalse("Command should return false", 194 command.execute(context)); 195 } catch (Exception e) { 196 fail("Threw exception: " + e); 197 } 198 checkExecuteLog("3a"); 199 } 200 201 202 // -------------------------------------------------------- Support Methods 203 204 205 // Verify the contents of the execution log 206 protected void checkExecuteLog(String expected) { 207 StringBuffer log = (StringBuffer) context.get("log"); 208 assertNotNull("Context failed to return log", log); 209 assertEquals("Context returned correct log", 210 expected, log.toString()); 211 } 212 213 214 }