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.assertNotNull; 021import static org.junit.jupiter.api.Assertions.assertNull; 022import static org.junit.jupiter.api.Assertions.assertSame; 023import static org.junit.jupiter.api.Assertions.assertTrue; 024import static org.junit.jupiter.api.Assertions.fail; 025 026import java.util.Iterator; 027 028import org.apache.commons.chain.Catalog; 029import org.apache.commons.chain.CatalogFactory; 030import org.apache.commons.chain.Command; 031import org.apache.commons.chain.Context; 032import org.junit.jupiter.api.AfterEach; 033import org.junit.jupiter.api.BeforeEach; 034import org.junit.jupiter.api.Test; 035 036/** 037 * Test case for the {@code CatalogFactoryBase} class. 038 * 039 * @author Craig R. McClanahan 040 * @version $Revision$ $Date$ 041 */ 042public class CatalogFactoryBaseTestCase { 043 044 // ---------------------------------------------------- Instance Variables 045 046 /** 047 * The {@link CatalogFactory} instance under test. 048 */ 049 protected CatalogFactory<Context> factory = null; 050 051 // ---------------------------------------------------------- Constructors 052 053 /** 054 * The Default-Constructor for this class. 055 */ 056 public CatalogFactoryBaseTestCase() { 057 } 058 059 // -------------------------------------------------- Overall Test Methods 060 061 /** 062 * Set up instance variables required by this test case. 063 */ 064 @BeforeEach 065 public void init() { 066 CatalogFactory.clear(); 067 factory = CatalogFactory.getInstance(); 068 } 069 070 /** 071 * Tear down instance variables required by this test case. 072 */ 073 @AfterEach 074 public void tearDown() { 075 factory = null; 076 } 077 078 // ------------------------------------------------ Individual Test Methods 079 080 /** 081 * Test a pristine instance of {@link CatalogFactory}. 082 */ 083 @Test 084 public void testPristine() { 085 assertNotNull(factory); 086 assertNull(factory.getCatalog()); 087 assertNull(factory.getCatalog("foo")); 088 assertEquals(0, getCatalogCount()); 089 } 090 091 /** 092 * Test the default {@link Catalog} instance. 093 */ 094 @Test 095 public void testDefaultCatalog() { 096 Catalog<Context> catalog = new CatalogBase<>(); 097 factory.setCatalog(catalog); 098 assertTrue(catalog == factory.getCatalog()); 099 assertEquals(0, getCatalogCount()); 100 } 101 102 /** 103 * Test adding a specifically named {@link Catalog} instance. 104 */ 105 @Test 106 public void testSpecificCatalog() { 107 Catalog<Context> catalog = new CatalogBase<>(); 108 factory.setCatalog(catalog); 109 catalog = new CatalogBase<>(); 110 factory.addCatalog("foo", catalog); 111 assertTrue(catalog == factory.getCatalog("foo")); 112 assertEquals(1, getCatalogCount()); 113 factory.addCatalog("foo", new CatalogBase<>()); 114 assertEquals(1, getCatalogCount()); 115 assertTrue(!(catalog == factory.getCatalog("foo"))); 116 CatalogFactory.clear(); 117 factory = CatalogFactory.getInstance(); 118 assertEquals(0, getCatalogCount()); 119 } 120 121 /** 122 * Test {@code getCatalog()} method. 123 */ 124 @Test 125 public void testCatalogIdentifier() { 126 Catalog<Context> defaultCatalog = new CatalogBase<>(); 127 Command<Context> defaultFoo = new NonDelegatingCommand(); 128 defaultCatalog.addCommand("foo", defaultFoo); 129 Command<Context> fallback = new NonDelegatingCommand(); 130 defaultCatalog.addCommand("noSuchCatalog:fallback", fallback); 131 132 factory.setCatalog(defaultCatalog); 133 134 Catalog<Context> specificCatalog = new CatalogBase<>(); 135 Command<Context> specificFoo = new NonDelegatingCommand(); 136 specificCatalog.addCommand("foo", specificFoo); 137 factory.addCatalog("specific", specificCatalog); 138 139 Command<Context> command = factory.getCommand("foo"); 140 assertSame(defaultFoo, command); 141 142 command = factory.getCommand("specific:foo"); 143 assertSame(specificFoo, command); 144 145 command = factory.getCommand("void"); 146 assertNull(command); 147 148 command = factory.getCommand("foo:void"); 149 assertNull(command); 150 151 command = factory.getCommand("specific:void"); 152 assertNull(command); 153 154 command = factory.getCommand("noSuchCatalog:fallback"); 155 assertNull(command); 156 157 try { 158 command = factory.getCommand("multiple:delimiters:reserved"); 159 fail("A command ID with more than one delimiter should throw an IllegalArgumentException"); 160 } 161 catch (IllegalArgumentException ex) { 162 // expected behavior 163 } 164 } 165 166 // ------------------------------------------------------- Support Methods 167 168 /** 169 * Return the number of {@link Catalog}s defined in our 170 * {@link CatalogFactory}. 171 * 172 * @return the number of {@link Catalog}s 173 */ 174 private int getCatalogCount() { 175 Iterator<String> names = factory.getNames(); 176 assertNotNull(names); 177 int n = 0; 178 while (names.hasNext()) { 179 names.next(); 180 n++; 181 } 182 return n; 183 } 184}