PageRenderTime 43ms CodeModel.GetById 31ms app.highlight 10ms RepoModel.GetById 0ms app.codeStats 0ms

/core/src/test/java/com/ocpsoft/pretty/faces/url/URLPatternParserTest.java

https://github.com/fcaputo/prettyfaces
Java | 295 lines | 247 code | 30 blank | 18 comment | 0 complexity | 63ac52c5d900caaab72992050c39047e MD5 | raw file
  1/*
  2 * Copyright 2010 Lincoln Baxter, III
  3 * 
  4 * Licensed under the Apache License, Version 2.0 (the "License");
  5 * you may not use this file except in compliance with the License.
  6 * You may obtain a copy of the License at
  7 * 
  8 *     http://www.apache.org/licenses/LICENSE-2.0
  9 * 
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS,
 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 * See the License for the specific language governing permissions and
 14 * limitations under the License.
 15 */
 16package com.ocpsoft.pretty.faces.url;
 17
 18import static org.junit.Assert.assertEquals;
 19import static org.junit.Assert.assertFalse;
 20import static org.junit.Assert.assertNotSame;
 21import static org.junit.Assert.assertTrue;
 22
 23import java.util.ArrayList;
 24import java.util.HashMap;
 25import java.util.List;
 26import java.util.Map;
 27
 28import org.junit.Test;
 29
 30import com.ocpsoft.pretty.PrettyException;
 31import com.ocpsoft.pretty.faces.config.mapping.PathParameter;
 32
 33/**
 34 * @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
 35 */
 36public class URLPatternParserTest
 37{
 38   @Test
 39   public void testSimplestMatch() throws Exception
 40   {
 41      URLPatternParser parser = new URLPatternParser("/");
 42      assertTrue(parser.matches(new URL("/")));
 43      List<PathParameter> parameters = parser.parse(new URL("/"));
 44      assertEquals(0, parameters.size());
 45   }
 46
 47   @Test
 48   public void testGetNamedParameterList()
 49   {
 50      URLPatternParser namedParamParser = new URLPatternParser("/foo/#{named}/");
 51      List<PathParameter> params = namedParamParser.parse(new URL("/foo/love/"));
 52      assertEquals(1, params.size());
 53
 54      PathParameter p = params.get(0);
 55      assertEquals(0, p.getPosition());
 56      assertEquals("love", p.getValue());
 57      assertEquals("named", p.getName());
 58      assertEquals("#{named}", p.getExpression().getELExpression());
 59   }
 60
 61   @Test
 62   public void testGetInjectedParameterList()
 63   {
 64      URLPatternParser namedParamParser = new URLPatternParser("/foo/#{:injected}/");
 65      List<PathParameter> params = namedParamParser.parse(new URL("/foo/love/"));
 66      assertEquals(1, params.size());
 67
 68      PathParameter p = params.get(0);
 69      assertEquals(0, p.getPosition());
 70      assertEquals("love", p.getValue());
 71      assertEquals("#{injected}", p.getExpression().getELExpression());
 72   }
 73
 74   @Test
 75   public void testGetNamedValuedParameterList()
 76   {
 77      URLPatternParser namedValuedParamParser = new URLPatternParser("/foo/#{named:bean.value}/");
 78      List<PathParameter> params = namedValuedParamParser.parse(new URL("/foo/love/"));
 79      assertEquals(1, params.size());
 80
 81      PathParameter p = params.get(0);
 82      assertEquals(0, p.getPosition());
 83      assertEquals("love", p.getValue());
 84      assertEquals("named", p.getName());
 85      assertEquals("#{bean.value}", p.getExpression().getELExpression());
 86   }
 87
 88   @Test
 89   public void testMatches()
 90   {
 91      URLPatternParser parser = new URLPatternParser(
 92               "/project/#{paramsBean.project}/#{paramsBean.iteration}/#{paramsBean.story}");
 93      assertTrue(parser.matches(new URL("/project/starfish1/starfish2/story1")));
 94      assertFalse(parser.matches(new URL("project/starfish1/starfish2/story1")));
 95      assertFalse(parser.matches(new URL("/project/starfish1/starfish2/story1/")));
 96      assertFalse(parser.matches(new URL("project/starfish1/starfish2/story1/test")));
 97      assertFalse(parser.matches(new URL("project/starfish2/story1")));
 98      assertFalse(parser.matches(new URL("project/starfish1/starfish2")));
 99      assertFalse(parser.matches(new URL("project/starfish1/starfish2/")));
100   }
101
102   @Test
103   public void testMatchesPrefixedSegmentExpressionInjected()
104   {
105      URLPatternParser parser = new URLPatternParser("/project/#{paramsBean.project}/story-#{paramsBean.story}/");
106      assertTrue(parser.matches(new URL("/project/starfish/story-1/")));
107      assertFalse(parser.matches(new URL("/project/starfish/story-1/-")));
108      assertFalse(parser.matches(new URL("/project/starfish/story-/")));
109      assertFalse(parser.matches(new URL("project/starfish/story-23/")));
110   }
111
112   @Test
113   public void testMatchesPrefixedSegmentExpressionNamed()
114   {
115      URLPatternParser parser = new URLPatternParser("/project/#{paramsBean.project}/story-#{story}/");
116      assertTrue(parser.matches(new URL("/project/starfish/story-1/")));
117   }
118
119   @Test
120   public void testMatchesMultipleExpressionNamedSegment()
121   {
122      URLPatternParser parser = new URLPatternParser("/project/#{paramsBean.project}/story-#{story}-#{comment}");
123      assertTrue(parser.matches(new URL("/project/starfish/story-1-23")));
124      assertFalse(parser.matches(new URL("/project/starfish/story-1-23/")));
125   }
126
127   @Test
128   public void testMatchesPrefixedSegmentExpressionNamedInjected()
129   {
130      URLPatternParser parser = new URLPatternParser("/project/#{paramsBean.project}/story-#{story:paramsBean.story}/");
131      assertTrue(parser.matches(new URL("/project/starfish/story-1/")));
132   }
133
134   @Test
135   public void testGetMappedParameters()
136   {
137      URLPatternParser parser = new URLPatternParser(
138               "/project/#{paramsBean.project}/#{paramsBean.iteration}/#{paramsBean.story}");
139      List<PathParameter> params = parser.parse(new URL("/project/starfish1/sprint1/story1"));
140      assertEquals(3, params.size());
141      assertEquals("starfish1", params.get(0).getValue());
142      assertEquals("sprint1", params.get(1).getValue());
143      assertEquals("story1", params.get(2).getValue());
144   }
145
146   @Test
147   public void testGetMappedParametersDuplicatesAreRepresented()
148   {
149      URLPatternParser duplicateParamsParser = new URLPatternParser(
150               "/project/#{paramsBean.project}/#{paramsBean.project}/#{paramsBean.story}");
151      List<PathParameter> params = duplicateParamsParser.parse(new URL("/project/starfish1/sprint1/story1"));
152      assertEquals(3, params.size());
153      assertEquals("starfish1", params.get(0).getValue());
154      assertEquals("sprint1", params.get(1).getValue());
155      assertEquals("story1", params.get(2).getValue());
156   }
157
158   @Test
159   public void testGetMappedParameterList()
160   {
161      URLPatternParser parser = new URLPatternParser(
162               "/project/#{paramsBean.project}/#{paramsBean.iteration}/#{paramsBean.story}");
163      List<PathParameter> params = parser.parse(new URL("/project/starfish1/sprint1/story1"));
164      assertEquals(3, params.size());
165
166      PathParameter p = params.get(0);
167      assertEquals(0, p.getPosition());
168      assertEquals("starfish1", p.getValue());
169      assertEquals("#{paramsBean.project}", p.getExpression().getELExpression());
170
171      PathParameter p1 = params.get(1);
172      assertEquals(1, p1.getPosition());
173      assertEquals("sprint1", p1.getValue());
174      assertEquals("#{paramsBean.iteration}", p1.getExpression().getELExpression());
175
176      PathParameter p2 = params.get(2);
177      assertEquals(2, p2.getPosition());
178      assertEquals("story1", p2.getValue());
179      assertEquals("#{paramsBean.story}", p2.getExpression().getELExpression());
180   }
181
182   @Test
183   public void testGetMappedUrl()
184   {
185      URLPatternParser parser = new URLPatternParser(
186               "/project/#{paramsBean.project}/#{paramsBean.iteration}/#{paramsBean.story}");
187      String mappedUrl = parser.getMappedURL("p1", 22, 55).toURL();
188      assertEquals("/project/p1/22/55", mappedUrl);
189   }
190
191   @Test
192   public void testGetParameterCount()
193   {
194      URLPatternParser parser = new URLPatternParser(
195               "/project/#{paramsBean.project}/#{paramsBean.iteration}/#{paramsBean.story}");
196      assertEquals(3, parser.getParameterCount());
197   }
198
199   @Test
200   public void testGetMappedUrlWithList()
201   {
202      URLPatternParser parser = new URLPatternParser(
203               "/project/#{paramsBean.project}/#{paramsBean.iteration}/#{paramsBean.story}");
204      List<Object> params = new ArrayList<Object>();
205      params.add("p1");
206      params.add(22);
207      params.add(55);
208      String mappedUrl = parser.getMappedURL(params).toURL();
209      assertEquals("/project/p1/22/55", mappedUrl);
210      params.remove(2);
211      try
212      {
213         mappedUrl = parser.getMappedURL(params, 55).toURL();
214         assertEquals("/project/p1/22/55", mappedUrl);
215         assertTrue("Parameter count is wrong.", false);
216      }
217      catch (PrettyException pe)
218      {
219         assertTrue(true);
220      }
221      assertNotSame("/project/p1/22/55", parser.getMappedURL(params, 22, 55).toURL());
222   }
223
224   @Test
225   public void testGetMappedUrlWithArray()
226   {
227      URLPatternParser parser = new URLPatternParser(
228               "/project/#{paramsBean.project}/#{paramsBean.iteration}/#{paramsBean.story}");
229      Object[] params = new Object[3];
230      params[0] = "p1";
231      params[1] = 22;
232      params[2] = 55;
233      String mappedUrl = parser.getMappedURL(params).toURL();
234      assertEquals("/project/p1/22/55", mappedUrl);
235      params[2] = null;
236      params[1] = null;
237      try
238      {
239         mappedUrl = parser.getMappedURL(params, 55).toURL();
240         assertEquals("/project/p1/22/55", mappedUrl);
241         assertTrue("An exception should have been thrown by now", false);
242      }
243      catch (PrettyException pe)
244      {
245         assertTrue(true);
246      }
247      assertNotSame("/project/p1/22/55", parser.getMappedURL(params, 22, 55).toURL());
248   }
249
250   @Test
251   public void testGetMappedUrlWithMap()
252   {
253      URLPatternParser parser = new URLPatternParser(
254               "/project/#{paramsBean.project}/#{paramsBean.iteration}/#{paramsBean.story}");
255      Map<String, String> params = new HashMap<String, String>();
256      params.put("1", "p1");
257      params.put("2", "22");
258      params.put("3", "55");
259      try
260      {
261         String mappedUrl = parser.getMappedURL(params).toURL();
262         assertEquals("An exception should have been thrown: Map parameters are not supported at this time.", mappedUrl);
263      }
264      catch (PrettyException pe)
265      {
266         assertTrue(true);
267      }
268      assertNotSame("/project/p1/22/55", parser.getMappedURL(params, 22, 55).toURL());
269   }
270
271   @Test
272   public void testGetMappedUrlWithListWithoutParams()
273   {
274      URLPatternParser noParamsParser = new URLPatternParser("/no/param");
275      List<?> params = new ArrayList<Object>();
276      String mappedUrl = noParamsParser.getMappedURL(params).toURL();
277      assertEquals("Empty list failed", "/no/param", mappedUrl);
278   }
279
280   @Test
281   public void testGetMappedUrlNullListWithoutParams()
282   {
283      URLPatternParser noParamsParser = new URLPatternParser("/no/param");
284      List<?> params = null;
285      String mappedUrl = noParamsParser.getMappedURL(params).toURL();
286      assertEquals("Null param failed", "/no/param", mappedUrl);
287   }
288
289   @Test
290   public void testGetMappedUrlWithRegexes()
291   {
292      URLPatternParser regexParser = new URLPatternParser("/(foo|bar|baz|cat|dog).jsf");
293      List<PathParameter> params = regexParser.parse(new URL("/foo.jsf"));
294   }
295}