GRAILS-1019: Allowing expressions to be used with the 'disabled' attribute for g...
[grails.git] / test / groovy / org / codehaus / groovy / grails / web / mapping / UrlMappingEvaluatorTests.groovy
blobdcc55d75c911f870f0385583fe87c832e15f5555
1 package org.codehaus.groovy.grails.web.mapping
3 import org.springframework.core.io.*
5 class UrlMappingEvaluatorTests extends GroovyTestCase {
7 def mappingScript = '''
8 mappings {
9 "/$id/$year?/$month?/$day?" {
10 controller = "blog"
11 action = "show"
12 constraints {
13 year(matches:/\\d{4}/)
14 month(matches:/\\d{2}/)
18 "/product/$name" {
19 controller = "product"
20 action = "show"
22 "/book/$author/$title/$test" {
23 controller = "book"
26 "/author/$lastName/$firstName" (controller:'author', action:'show') {
27 constraints {
28 lastName(maxSize:5)
29 firstName(maxSize:5)
33 "/music/$band/$album" (controller:'music', action:'show')
35 '''
37 void testEvaluateMappings() {
38 def res = new ByteArrayResource(mappingScript.bytes)
40 def evaluator = new DefaultUrlMappingEvaluator()
41 def mappings = evaluator.evaluateMappings(res)
43 assert mappings
44 assertEquals 5, mappings.size()
46 def m1 = mappings[0]
47 assertEquals "/(*)/(*)?/(*)?/(*)?", m1.urlData.urlPattern
48 assertEquals 4, m1.urlData.tokens.size()
51 def info = m1.match("/myentry/2007/04/28")
53 assertEquals "myentry", info.id
54 assertEquals "2007", info.parameters.year
55 assertEquals "04", info.parameters.month
56 assertEquals "28", info.parameters.day
57 assertEquals "blog", info.controllerName
58 assertEquals "show", info.actionName
61 assert m1.match("/myentry/2007/04/28")
62 assert m1.match("/myentry/2007/04")
63 assert m1.match("/myentry/2007")
64 assert m1.match("/myentry")
67 def m2 = mappings[1]
69 info = m2.match("/product/MacBook")
70 assert info
71 assertEquals "MacBook", info.parameters.name
73 assert !m2.match("/product")
74 assert !m2.match("/foo/bar")
75 assert !m2.match("/product/MacBook/foo")
77 def m4 = mappings[3]
78 info = m4.match("/author/Brown/Jeff")
79 assert info
80 assertEquals "Brown", info.parameters.lastName
81 assertEquals "Jeff", info.parameters.firstName
82 assertEquals "show", info.actionName
83 assertEquals "author", info.controllerName
85 // first name too long
86 assert !m4.match("/author/Lang/Johnny")
88 // both names too long
89 assert !m4.match("/author/Winter/Johnny")
91 // last name too long
92 assert !m4.match("/author/Winter/Edgar")
94 def m5 = mappings[4]
95 info = m5.match("/music/Rush/Hemispheres")
96 assert info
97 assertEquals "Rush", info.parameters.band
98 assertEquals "Hemispheres", info.parameters.album
99 assertEquals "show", info.actionName
100 assertEquals "music", info.controllerName