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.impl; 018 019import static org.junit.jupiter.api.Assertions.assertEquals; 020import static org.junit.jupiter.api.Assertions.assertInstanceOf; 021import static org.junit.jupiter.api.Assertions.assertNotNull; 022import static org.junit.jupiter.api.Assertions.assertNull; 023 024import java.util.Iterator; 025 026import org.apache.commons.chain.Catalog; 027import org.apache.commons.chain.Command; 028import org.apache.commons.chain.Context; 029import org.junit.jupiter.api.AfterEach; 030import org.junit.jupiter.api.BeforeEach; 031import org.junit.jupiter.api.Test; 032 033/** 034 * Test case for the {@code CatalogBase} class. 035 * 036 * @author Craig R. McClanahan 037 * @version $Revision$ $Date$ 038 */ 039public class CatalogBaseTestCase { 040 041 // ---------------------------------------------------- Instance Variables 042 043 /** 044 * The {@link Catalog} instance under test. 045 */ 046 protected CatalogBase<Context> catalog = null; 047 048 // ---------------------------------------------------------- Constructors 049 050 /** 051 * The Default-Constructor for this class. 052 */ 053 public CatalogBaseTestCase() { 054 } 055 056 // -------------------------------------------------- Overall Test Methods 057 058 /** 059 * Set up instance variables required by this test case. 060 */ 061 @BeforeEach 062 public void init() { 063 catalog = new CatalogBase<>(); 064 } 065 066 /** 067 * Tear down instance variables required by this test case. 068 */ 069 @AfterEach 070 public void tearDown() { 071 catalog = null; 072 } 073 074 // ------------------------------------------------ Individual Test Methods 075 076 /** 077 * Test adding commands 078 */ 079 @Test 080 public void testAddCommand() { 081 addCommands(); 082 checkCommandCount(8); 083 } 084 085 /** 086 * Test getting commands 087 */ 088 @Test 089 public void testGetCommand() { 090 addCommands(); 091 Command<Context> command = null; 092 093 command = catalog.getCommand("AddingCommand"); 094 assertNotNull(command); 095 assertInstanceOf(AddingCommand.class, command); 096 097 command = catalog.getCommand("DelegatingCommand"); 098 assertNotNull(command); 099 assertInstanceOf(DelegatingCommand.class, command); 100 101 command = catalog.getCommand("DelegatingFilter"); 102 assertNotNull(command); 103 assertInstanceOf(DelegatingFilter.class, command); 104 105 command = catalog.getCommand("ExceptionCommand"); 106 assertNotNull(command); 107 assertInstanceOf(ExceptionCommand.class, command); 108 109 command = catalog.getCommand("ExceptionFilter"); 110 assertNotNull(command); 111 assertInstanceOf(ExceptionFilter.class, command); 112 113 command = catalog.getCommand("NonDelegatingCommand"); 114 assertNotNull(command); 115 assertInstanceOf(NonDelegatingCommand.class, command); 116 117 command = catalog.getCommand("NonDelegatingFilter"); 118 assertNotNull(command); 119 assertInstanceOf(NonDelegatingFilter.class, command); 120 121 command = catalog.getCommand("ChainBase"); 122 assertNotNull(command); 123 assertInstanceOf(ChainBase.class, command); 124 } 125 126 // The getNames() method is implicitly tested by checkCommandCount() 127 128 /** 129 * Test pristine instance 130 */ 131 @Test 132 public void testPristine() { 133 checkCommandCount(0); 134 assertNull(catalog.getCommand("AddingCommand")); 135 assertNull(catalog.getCommand("DelegatingCommand")); 136 assertNull(catalog.getCommand("DelegatingFilter")); 137 assertNull(catalog.getCommand("ExceptionCommand")); 138 assertNull(catalog.getCommand("ExceptionFilter")); 139 assertNull(catalog.getCommand("NonDelegatingCommand")); 140 assertNull(catalog.getCommand("NonDelegatingFilter")); 141 assertNull(catalog.getCommand("ChainBase")); 142 } 143 144 // -------------------------------------------------------- Support Methods 145 146 /** 147 * Add an interesting set of commands to the catalog 148 */ 149 protected void addCommands() { 150 catalog.addCommand("AddingCommand", new AddingCommand("", null)); 151 catalog.addCommand("DelegatingCommand", new DelegatingCommand("")); 152 catalog.addCommand("DelegatingFilter", new DelegatingFilter("", "")); 153 catalog.addCommand("ExceptionCommand", new ExceptionCommand("")); 154 catalog.addCommand("ExceptionFilter", new ExceptionFilter("", "")); 155 catalog.addCommand("NonDelegatingCommand", new NonDelegatingCommand("")); 156 catalog.addCommand("NonDelegatingFilter", new NonDelegatingFilter("", "")); 157 catalog.addCommand("ChainBase", new ChainBase<>()); 158 } 159 160 /** 161 * Verify the number of configured commands 162 * 163 * @param expected the expected value 164 */ 165 protected void checkCommandCount(int expected) { 166 int n = 0; 167 Iterator<String> names = catalog.getNames(); 168 while (names.hasNext()) { 169 String name = names.next(); 170 n++; 171 assertNotNull(catalog.getCommand(name), name + " exists"); 172 } 173 assertEquals(expected, n, "Correct command count"); 174 } 175}