1 18 19 20 package org.apache.struts.config; 21 22 23 import junit.framework.Test; 24 import junit.framework.TestSuite; 25 26 import org.apache.struts.action.ActionMapping; 27 import org.apache.struts.action.ActionForward; 28 import org.apache.struts.config.ForwardConfig; 29 import org.apache.struts.config.ActionConfig; 30 import org.apache.struts.mock.TestMockBase; 31 32 33 38 39 public class TestActionConfigMatcher extends TestMockBase { 40 41 42 44 45 public TestActionConfigMatcher(String name) { 46 super(name); 47 } 48 49 50 public static void main(String args[]) { 51 junit.awtui.TestRunner.main 52 (new String [] { TestActionConfigMatcher.class.getName() } ); 53 } 54 55 56 public static Test suite() { 57 return (new TestSuite(TestActionConfigMatcher.class)); 58 } 59 60 61 63 64 65 67 68 public void setUp() { 69 70 super.setUp(); 71 72 } 73 74 75 public void tearDown() { 76 77 super.tearDown(); 78 79 } 80 81 82 84 85 87 88 public void testNoMatch() { 89 ActionConfig[] configs = new ActionConfig[1]; 90 ActionConfig mapping = buildActionConfig("/foo"); 91 configs[0] = mapping; 92 ActionConfigMatcher matcher = new ActionConfigMatcher(configs); 93 94 assertNull("ActionConfig shouldn't be matched", matcher.match("/test")); 95 } 96 97 public void testNoWildcardMatch() { 98 ActionConfig[] configs = new ActionConfig[1]; 99 ActionConfig mapping = buildActionConfig("/fooBar"); 100 configs[0] = mapping; 101 ActionConfigMatcher matcher = new ActionConfigMatcher(configs); 102 103 assertNull("ActionConfig shouldn't be matched", matcher.match("/fooBar")); 104 } 105 106 public void testShouldMatch() { 107 ActionConfig[] configs = new ActionConfig[1]; 108 ActionConfig mapping = buildActionConfig("/foo*"); 109 configs[0] = mapping; 110 ActionConfigMatcher matcher = new ActionConfigMatcher(configs); 111 112 ActionConfig matched = matcher.match("/fooBar"); 113 assertNotNull("ActionConfig should be matched", matched); 114 assertTrue("ActionConfig should have two action forward", matched.findForwardConfigs().length == 2); 115 assertTrue("ActionConfig should have two exception forward", matched.findExceptionConfigs().length == 2); 116 } 117 118 public void testCheckSubstitutionsMatch() { 119 ActionConfig[] configs = new ActionConfig[1]; 120 ActionConfig mapping = buildActionConfig("/foo*"); 121 configs[0] = mapping; 122 ActionConfigMatcher matcher = new ActionConfigMatcher(configs); 123 ActionConfig m = matcher.match("/fooBar"); 124 125 assertTrue("Name hasn't been replaced", "name,Bar".equals(m.getName())); 126 assertTrue("Path hasn't been replaced", "/fooBar".equals(m.getPath())); 127 assertTrue("Prefix isn't correct", "foo".equals(m.getPrefix())); 128 assertTrue("Scope isn't correct", "request".equals(m.getScope())); 129 assertTrue("Suffix isn't correct", "bar".equals(m.getSuffix())); 130 assertTrue("Unknown isn't correct", !m.getUnknown()); 131 assertTrue("Validate isn't correct", m.getValidate()); 132 133 assertTrue("Type hasn't been replaced", "foo.bar.BarAction".equals(m.getType())); 134 assertTrue("Roles hasn't been replaced", "public,Bar".equals(m.getRoles())); 135 assertTrue("Parameter hasn't been replaced", "param,Bar".equals(m.getParameter())); 136 assertTrue("Attribute hasn't been replaced", "attrib,Bar".equals(m.getAttribute())); 137 assertTrue("Forward hasn't been replaced", "fwd,Bar".equals(m.getForward())); 138 assertTrue("Include hasn't been replaced", "include,Bar".equals(m.getInclude())); 139 assertTrue("Input hasn't been replaced", "input,Bar".equals(m.getInput())); 140 141 ForwardConfig[] fConfigs = m.findForwardConfigs(); 142 boolean found = false; 143 for (int x=0; x<fConfigs.length; x++) { 144 ForwardConfig cfg = fConfigs[x]; 145 if ("name".equals(cfg.getName())) { 146 found = true; 147 assertTrue("ContextRelative isn't correct", cfg.getContextRelative()); 148 assertTrue("Path hasn't been replaced", "path,Bar".equals(cfg.getPath())); 149 } 150 } 151 assertTrue("The forward config 'name' cannot be found", found); 152 } 153 154 public void testCheckMultipleSubstitutions() { 155 ActionMapping[] mapping = new ActionMapping[1]; 156 mapping[0] = new ActionMapping(); 157 mapping[0].setPath("/foo*"); 158 mapping[0].setName("name,{1}-{1}"); 159 160 ActionConfigMatcher matcher = new ActionConfigMatcher(mapping); 161 ActionConfig m = matcher.match("/fooBar"); 162 163 assertTrue("Name hasn't been replaced correctly: "+m.getName(), "name,Bar-Bar".equals(m.getName())); 164 } 165 166 private ActionConfig buildActionConfig(String path) { 167 ActionMapping mapping = new ActionMapping(); 168 169 mapping.setName("name,{1}"); 170 mapping.setPath(path); 171 mapping.setPrefix("foo"); 172 mapping.setScope("request"); 173 mapping.setSuffix("bar"); 174 mapping.setUnknown(false); 175 mapping.setValidate(true); 176 177 mapping.setType("foo.bar.{1}Action"); 178 mapping.setRoles("public,{1}"); 179 mapping.setParameter("param,{1}"); 180 mapping.setAttribute("attrib,{1}"); 181 mapping.setForward("fwd,{1}"); 182 mapping.setInclude("include,{1}"); 183 mapping.setInput("input,{1}"); 184 185 ForwardConfig cfg = new ActionForward(); 186 cfg.setContextRelative(true); 187 cfg.setName("name"); 188 cfg.setPath("path,{1}"); 189 mapping.addForwardConfig(cfg); 190 191 cfg = new ActionForward(); 192 cfg.setContextRelative(true); 193 cfg.setName("name2"); 194 cfg.setPath("path2"); 195 mapping.addForwardConfig(cfg); 196 197 ExceptionConfig excfg = new ExceptionConfig(); 198 excfg.setKey("foo"); 199 excfg.setType("foo.Bar"); 200 excfg.setPath("path"); 201 mapping.addExceptionConfig(excfg); 202 203 excfg = new ExceptionConfig(); 204 excfg.setKey("foo2"); 205 excfg.setType("foo.Bar2"); 206 excfg.setPath("path2"); 207 mapping.addExceptionConfig(excfg); 208 209 210 mapping.freeze(); 211 212 return mapping; 213 } 214 215 216 } 217 | Popular Tags |