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