1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.extensions.webscripts;
20  
21  import java.util.Map;
22  import java.util.Set;
23  import java.util.TreeSet;
24  
25  import junit.framework.TestCase;
26  
27  import org.springframework.extensions.webscripts.JaxRSUriIndex;
28  import org.springframework.extensions.webscripts.UriTemplate;
29  import org.springframework.extensions.webscripts.WebScriptException;
30  import org.springframework.extensions.webscripts.JaxRSUriIndex.IndexEntry;
31  
32  
33  /**
34   * Test Jax-RS Uri Template
35   * 
36   * @author davidc
37   */
38  public class JaxRSUriIndexTest extends TestCase
39  {
40  
41      public void testInvalidTemplate()
42      {
43          try
44          {
45              new UriTemplate(null);
46              fail("Failed to catch null template");
47          }
48          catch(WebScriptException e) {};
49          try
50          {
51              new UriTemplate("");
52              fail("Failed to catch empty template");
53          }
54          catch(WebScriptException e) {};
55          try
56          {
57              new UriTemplate("//");
58              fail("Failed to catch double /");
59          }
60          catch(WebScriptException e) {};
61          try
62          {
63              new UriTemplate("/a//");
64              fail("Failed to catch double /");
65          }
66          catch(WebScriptException e) {};
67          try
68          {
69              new UriTemplate("/a//b");
70              fail("Failed to catch double /");
71          }
72          catch(WebScriptException e) {};
73          try
74          {
75              new UriTemplate("/{1a}");
76              fail("Failed to catch var name beginning with number");
77          }
78          catch(WebScriptException e) {};
79          try
80          {
81              new UriTemplate("/;a");
82              fail("Failed to catch semi-colon without prefix");
83          }
84          catch(WebScriptException e) {};
85          try
86          {
87              new UriTemplate("/{1;a}");
88              fail("Failed to catch semi-colon in template var name");
89          }
90          catch(WebScriptException e) {};
91      }
92  
93      
94      public void testValidTemplate()
95      {
96          try
97          {
98              new UriTemplate("/");
99          }
100         catch(WebScriptException e)
101         {
102             fail("Root path is valid");
103         };
104         try
105         {
106             new UriTemplate("/a;");
107         }
108         catch(WebScriptException e)
109         {
110             fail("Semi-colon in path is valid");
111         };
112         try
113         {
114             new UriTemplate("/a;aaaa");
115         }
116         catch(WebScriptException e)
117         {
118             fail("Semi-colon in path is valid");
119         };
120         try
121         {
122             new UriTemplate("/a;a;aaaa");
123         }
124         catch(WebScriptException e)
125         {
126             fail("Semi-colon in path is valid");
127         };
128         try
129         {
130             new UriTemplate("/{a_b}");
131         }
132         catch(WebScriptException e)
133         {
134             fail("Underscore in token name is valid");
135         };
136         try
137         {
138             new UriTemplate("/ads/test-2/{storeid}/{path}");   
139         }
140         catch(WebScriptException e)
141         {
142             fail("Hypthen in path is valid");
143         };
144         try
145         {
146             new UriTemplate("/ads/test_2/{storeid}/{path}");   
147         }
148         catch(WebScriptException e)
149         {
150             fail("Hypthen in path is valid");
151         };
152     }
153 
154     
155     public void testParseTemplate()
156     {
157         UriTemplate i1 = new UriTemplate("/");
158         assertEquals("/", i1.getTemplate());
159         assertEquals("/", i1.getRegex().pattern());
160         assertEquals(1, i1.getStaticCharCount());
161         assertEquals(0, i1.getVariableNames().length);
162 
163         UriTemplate i2 = new UriTemplate("/a/{a1}/b{b1}b/{c_c}");
164         assertEquals("/a/{a1}/b{b1}b/{c_c}", i2.getTemplate());
165         assertEquals("/a/(.*?)/b(.*?)b/(.*?)", i2.getRegex().pattern());
166         assertEquals(7, i2.getStaticCharCount());
167         assertEquals(3, i2.getVariableNames().length);
168         assertEquals("a1", i2.getVariableNames()[0]);
169         assertEquals("b1", i2.getVariableNames()[1]);
170         assertEquals("c_c", i2.getVariableNames()[2]);
171     }
172  
173 
174     public void testTemplateMatch()
175     {
176         UriTemplate i1 = new UriTemplate("/a/{a1}/b/b{b1}b");
177         assertNull(i1.match("/"));
178         assertNull(i1.match("/a"));
179         assertNull(i1.match("/a/1/b"));
180         assertNull(i1.match("/a/1/b/2"));
181         assertNull(i1.match("/a/1/b/b2"));
182         assertNull(i1.match("/a/1/b/b2b/"));
183         
184         Map<String, String> values1 = i1.match("/a/1/b/b2b");
185         assertNotNull(values1);
186         assertEquals(2, values1.size());
187         assertEquals("1", values1.get("a1"));
188         assertEquals("2", values1.get("b1"));
189 
190         UriTemplate i2 = new UriTemplate("/a/{a1}/b/{b1}");
191         Map<String, String> values2 = i2.match("/a/1/b/2/3");
192         assertNotNull(values2);
193         assertEquals(2, values2.size());
194         assertEquals("1", values2.get("a1"));
195         assertEquals("2/3", values2.get("b1"));
196 
197         UriTemplate i3 = new UriTemplate("/a/{a1}/b/{a1}");
198         Map<String, String> values3 = i3.match("/a/1/b/2");
199         assertNull(values3);
200         
201         UriTemplate i4 = new UriTemplate("/a/b{b}/{c}");
202         Map<String, String> values4 = i4.match("/a/b/c");
203         assertEquals(2, values4.size());
204         assertEquals("", values4.get("b"));
205         assertEquals("c", values4.get("c"));
206 
207         UriTemplate i5 = new UriTemplate("/a/b{b}/{c}");
208         Map<String, String> values5 = i5.match("/a/bb/c");
209         assertEquals(2, values5.size());
210         assertEquals("b", values5.get("b"));
211         assertEquals("c", values5.get("c"));        
212     }
213     
214     
215     public void testTemplateDotMatch()
216     {
217         UriTemplate i1 = new UriTemplate("/a/b/{b}/c");
218         Map<String, String> values1 = i1.match("/a/b/x/y/z.ext/c");
219         assertEquals(1, values1.size());
220         assertEquals("x/y/z.ext", values1.get("b"));
221     }
222     
223     
224     public void testIndexSort()
225     {
226         IndexEntry i1 = new IndexEntry("GET", new UriTemplate("/"), false, null);
227         IndexEntry i2 = new IndexEntry("POST", new UriTemplate("/a/{a}"), false, null);
228         IndexEntry i3 = new IndexEntry("get", new UriTemplate("/a/{a}/b"), false, null);
229         IndexEntry i4 = new IndexEntry("get", new UriTemplate("/a"), false, null);
230         IndexEntry i5 = new IndexEntry("get", new UriTemplate("/c/d"), false, null);
231         IndexEntry i6 = new IndexEntry("get", new UriTemplate("/c/d/{e}"), true, null);
232         IndexEntry i7 = new IndexEntry("get", new UriTemplate("/a/b"), false, null);
233         IndexEntry i8 = new IndexEntry("get", new UriTemplate("/c/d/{e}/{e}"), false, null);
234         IndexEntry i9 = new IndexEntry("get", new UriTemplate("/e"), false, null);
235         IndexEntry i10 = new IndexEntry("GET", new UriTemplate("/a/{a}"), false, null);
236 
237         Set<IndexEntry> index = new TreeSet<IndexEntry>(JaxRSUriIndex.COMPARATOR);
238         index.add(i1);
239         index.add(i2);
240         index.add(i3);
241         index.add(i4);
242         index.add(i5);
243         index.add(i6);
244         index.add(i7);
245         index.add(i8);
246         index.add(i9);
247         index.add(i10);
248 
249         IndexEntry[] sorted = new IndexEntry[index.size()];
250         index.toArray(sorted);
251         assertEquals(i1, sorted[9]);
252         assertEquals(i4, sorted[8]);
253         assertEquals(i9, sorted[7]);
254         assertEquals(i10, sorted[6]);
255         assertEquals(i2, sorted[5]);
256         assertEquals(i7, sorted[4]);
257         assertEquals(i5, sorted[3]);
258         assertEquals(i3, sorted[2]);
259         assertEquals(i6, sorted[1]);
260         assertEquals(i8, sorted[0]);
261     }
262     
263 }