PageRenderTime 31ms CodeModel.GetById 19ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/scalate-jsp-converter/src/test/scala/org/fusesource/scalate/converter/ConvertJspTest.scala

http://github.com/scalate/scalate
Scala | 195 lines | 146 code | 29 blank | 20 comment | 3 complexity | d9d39294ad768472c3428ac3c2b35d6a MD5 | raw file
  1/**
  2 * Copyright (C) 2009-2011 the original author or authors.
  3 * See the notice.md file distributed with this work for additional
  4 * information regarding copyright ownership.
  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 */
 18package org.fusesource.scalate.converter
 19
 20import _root_.org.junit.runner.RunWith
 21import _root_.org.scalatest.junit.JUnitRunner
 22import _root_.org.scalatest.FunSuite
 23
 24import _root_.org.fusesource.scalate._
 25import org.fusesource.scalate.util.Log
 26
 27/**
 28 * @version $Revision : 1.1 $
 29 */
 30@RunWith(classOf[JUnitRunner])
 31class ConvertJspTest extends FunSuite with Log {
 32
 33  assertJustText("<foo/>")
 34  assertJustText("text <foo/> text")
 35  assertJustText("<curl value='/foo'/>")
 36
 37  assertConvert(
 38    """<c:url value='/foo'/>""",
 39    """${uri("/foo")}""")
 40
 41  assertConvert(
 42    """blah <c:url value='/foo'/> blah""",
 43    """blah ${uri("/foo")} blah""")
 44
 45  assertConvert(
 46    """blah <c:url value='/foo/${x}/bar/${y}'/> blah""",
 47    """blah ${uri("/foo/" + x + "/bar/" + y)} blah""")
 48
 49  assertConvert(
 50    """<a href="<c:url value='/foo'/>">body</a>""",
 51    """<a href="${uri("/foo")}">body</a>""")
 52
 53  assertConvert(
 54    """something <c:out value="${foo}"/> or other""",
 55    """something ${foo} or other""")
 56
 57  assertConvert(
 58    """something <c:out value="${foo}" escapeXml="true"/> or other""",
 59    """something ${escape(foo)} or other""")
 60
 61  assertConvert(
 62    """something <c:out value="${foo}" escapeXml="false"/> or other""",
 63    """something ${unescape(foo)} or other""")
 64
 65  assertConvert(
 66    """something <c:out value="${foo}" escapeXml="x"/> or other""",
 67    """something ${value(foo, x)} or other""")
 68
 69  assertConvert(
 70    """foo <c:if test='${foo}'> a <c:if test='${bar}'> b </c:if> c </c:if> whatnot""",
 71    """foo #if(foo) a #if(bar) b #end c #end whatnot""")
 72
 73  assertConvert(
 74    """foo <c:set var="x" value='${foo}'/> whatnot""",
 75    """foo #{ var x = foo }# whatnot""")
 76
 77  assertConvert(
 78    """foo <c:if test="${it.language eq 'Cheese'}"> bar </c:if> whatnot""",
 79    """foo #if(it.getLanguage == "Cheese") bar #end whatnot""")
 80
 81  assertConvert(
 82    """foo <c:if test='${foo}'> bar </c:if> whatnot""",
 83    """foo #if(foo) bar #end whatnot""")
 84
 85  assertConvert(
 86    """
 87foo
 88<c:if test="${x.y == 5}">
 89  bar
 90</c:if>
 91whatnot""",
 92    """
 93foo
 94#if(x.getY == 5)
 95  bar
 96#end
 97whatnot""")
 98
 99  assertConvert(
100    """
101foo
102<c:forEach var="foo" items="${something.whatnot}">
103 blah ${foo.bar}
104</c:forEach>
105whatnot""",
106    """
107foo
108#for(foo <- something.getWhatnot)
109 blah ${foo.getBar}
110#end
111whatnot""")
112
113  assertConvert(
114    """
115foo
116<c:forEach var="i" begin="1" end="10">
117 blah ${i}
118</c:forEach>
119whatnot""",
120    """
121foo
122#for(i <- 1.to(10))
123 blah ${i}
124#end
125whatnot""")
126
127  assertConvert(
128    """
129foo
130<c:forEach var="i" begin="1" end="10" step="3">
131 blah ${i}
132</c:forEach>
133whatnot""",
134    """
135foo
136#for(i <- 1.to(10, 3))
137 blah ${i}
138#end
139whatnot""")
140
141  assertConvert(
142    """
143foo
144<c:choose>
145<c:when test="${x == 5}">
146five
147</c:when>
148<c:when test="${x == 6}">
149six
150</c:when>
151<c:otherwise>
152default
153</c:otherwise>
154</c:choose>
155whatnot""",
156    """
157foo
158
159#if(x == 5)
160five
161
162#elseif(x == 6)
163six
164
165#else
166default
167
168#end
169whatnot""")
170
171  def assertJustText(jsp: String): String = {
172    val result = convert(jsp)
173    assertResult(jsp, "converting JSP: " + jsp) { result }
174    result
175
176  }
177
178  def assertConvert(jsp: String, ssp: String): String = {
179    val result = convert(jsp)
180    assertResult(ssp, "converting JSP: " + jsp) { result }
181    result
182  }
183
184  def convert(jsp: String): String = {
185    log.info("Converting JSP: " + jsp)
186
187    val converter = new JspConverter
188    val result = converter.convert(jsp)
189
190    log.info(" => " + result)
191
192    result
193  }
194
195}