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 */ 017package org.apache.commons.chain.generic; 018 019import static org.junit.jupiter.api.Assertions.assertEquals; 020import static org.junit.jupiter.api.Assertions.assertFalse; 021import static org.junit.jupiter.api.Assertions.assertNotNull; 022import static org.junit.jupiter.api.Assertions.assertTrue; 023import static org.junit.jupiter.api.Assertions.fail; 024 025import org.apache.commons.chain.Catalog; 026import org.apache.commons.chain.CatalogFactory; 027import org.apache.commons.chain.Context; 028import org.apache.commons.chain.impl.CatalogBase; 029import org.apache.commons.chain.impl.ChainBase; 030import org.apache.commons.chain.impl.ContextBase; 031import org.apache.commons.chain.impl.DelegatingCommand; 032import org.apache.commons.chain.impl.NonDelegatingCommand; 033import org.junit.jupiter.api.AfterEach; 034import org.junit.jupiter.api.BeforeEach; 035import org.junit.jupiter.api.Test; 036 037/** 038 * Test case for the {@code LookupCommand} class. 039 * 040 * @author Sean Schofield 041 * @version $Revision$ 042 */ 043public class LookupCommandTestCase { 044 045 // ---------------------------------------------------------- Constructors 046 047 /** 048 * The Default-Constructor for this class. 049 */ 050 public LookupCommandTestCase() { 051 } 052 053 // ---------------------------------------------------- Instance Variables 054 055 /** 056 * The instance of {@link Catalog} to use when looking up commands 057 */ 058 protected Catalog<Context> catalog; 059 060 /** 061 * The {@link LookupCommand} instance under test. 062 */ 063 protected LookupCommand<Context> command; 064 065 /** 066 * The {@link Context} instance on which to execute the chain. 067 */ 068 protected Context context = null; 069 070 // -------------------------------------------------- Overall Test Methods 071 072 /** 073 * Set up instance variables required by this test case. 074 */ 075 @BeforeEach 076 public void init() { 077 catalog = new CatalogBase<>(); 078 CatalogFactory.getInstance().setCatalog(catalog); 079 command = new LookupCommand<>(); 080 context = new ContextBase(); 081 } 082 083 /** 084 * Tear down instance variables required by this test case. 085 */ 086 @AfterEach 087 public void tearDown() { 088 catalog = null; 089 CatalogFactory.clear(); 090 command = null; 091 context = null; 092 } 093 094 // ------------------------------------------------ Individual Test Methods 095 096 /** 097 * Test ability to lookup and execute single non-delegating command 098 */ 099 @Test 100 public void testExecuteMethodLookup_1a() { 101 // use default catalog 102 catalog.addCommand("foo", new NonDelegatingCommand("1a")); 103 command.setName("foo"); 104 105 try { 106 assertTrue(command.execute(context), 107 "Command should return true"); 108 } catch (Exception e) { 109 fail("Threw exception: " + e); 110 } 111 checkExecuteLog("1a"); 112 } 113 114 /** 115 * Test ability to lookup and execute a chain 116 */ 117 @Test 118 public void testExecuteMethodLookup_1b() { 119 ChainBase<Context> chain = new ChainBase<>(); 120 chain.addCommand(new DelegatingCommand("1b1")); 121 chain.addCommand(new DelegatingCommand("1b2")); 122 chain.addCommand(new NonDelegatingCommand("1b3")); 123 124 // use default catalog 125 catalog.addCommand("foo", chain); 126 command.setName("foo"); 127 128 try { 129 assertTrue(command.execute(context), 130 "Command should return true"); 131 } catch (Exception e) { 132 fail("Threw exception: " + e); 133 } 134 checkExecuteLog("1b1/1b2/1b3"); 135 } 136 137 /** 138 * Test ability to lookup and execute single non-delegating command 139 * using the context 140 */ 141 @Test 142 public void testExecuteMethodLookup_2a() { 143 // use default catalog 144 catalog.addCommand("foo", new NonDelegatingCommand("2a")); 145 command.setNameKey("nameKey"); 146 context.put("nameKey", "foo"); 147 148 try { 149 assertTrue(command.execute(context), 150 "Command should return true"); 151 } catch (Exception e) { 152 fail("Threw exception: " + e); 153 } 154 checkExecuteLog("2a"); 155 } 156 157 /** 158 * Test ability to lookup and execute a chain using the context 159 */ 160 @Test 161 public void testExecuteMethodLookup_2b() { 162 ChainBase<Context> chain = new ChainBase<>(); 163 chain.addCommand(new DelegatingCommand("2b1")); 164 chain.addCommand(new DelegatingCommand("2b2")); 165 chain.addCommand(new NonDelegatingCommand("2b3")); 166 167 // use default catalog 168 catalog.addCommand("foo", chain); 169 command.setNameKey("nameKey"); 170 context.put("nameKey", "foo"); 171 172 try { 173 assertTrue(command.execute(context), 174 "Command should return true"); 175 } catch (Exception e) { 176 fail("Threw exception: " + e); 177 } 178 checkExecuteLog("2b1/2b2/2b3"); 179 } 180 181 /** 182 * Test ability to lookup and execute single non-delegating command, ignoring its result 183 */ 184 @Test 185 public void testExecuteMethodLookup_3a() { 186 // use default catalog 187 catalog.addCommand("foo", new NonDelegatingCommand("3a")); 188 command.setIgnoreExecuteResult(true); 189 command.setName("foo"); 190 191 try { 192 assertFalse(command.execute(context), 193 "Command should return false"); 194 } catch (Exception e) { 195 fail("Threw exception: " + e); 196 } 197 checkExecuteLog("3a"); 198 } 199 200 // -------------------------------------------------------- Support Methods 201 202 /** 203 * Verify the contents of the execution log 204 * 205 * @param expected the expected value 206 */ 207 protected void checkExecuteLog(String expected) { 208 StringBuffer log = (StringBuffer) context.get("log"); 209 assertNotNull(log, "Context failed to return log"); 210 assertEquals(expected, log.toString(), 211 "Context returned correct log"); 212 } 213}