PageRenderTime 5ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/scalate-core/src/test/scala/org/fusesource/scalate/support/TemplateConversionsTest.scala

http://github.com/scalate/scalate
Scala | 67 lines | 37 code | 12 blank | 18 comment | 1 complexity | 09535962e59dc7bd4023366434c36cec 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.support
  19. import org.fusesource.scalate.FunSuiteSupport
  20. import java.{ util => ju }
  21. // the following imports are included by default in TemplateEngine
  22. import scala.collection.JavaConverters._
  23. import TemplateConversions._
  24. case class Address(city: String, country: String)
  25. case class Person(name: String, address: Address)
  26. class TemplateConversionsTest extends FunSuiteSupport {
  27. test("iterate over maps using Map.Entry like object") {
  28. val map = new ju.HashMap[String, String]
  29. map.put("a", "1")
  30. map.put("b", "2")
  31. for (e <- map.asScala) {
  32. val key = e.getKey
  33. val value = e.getValue
  34. log.info(" " + key + " = " + value)
  35. }
  36. }
  37. test("null pointer handling") {
  38. val a: String = null
  39. val answer = a ?: "default"
  40. log.info("got answer: " + answer)
  41. assertResult("default") { answer }
  42. }
  43. test("orElse method") {
  44. val a: String = null
  45. val answer = orElse(a, "default")
  46. log.info("got answer: " + answer)
  47. assertResult("default") { answer }
  48. }
  49. test("orElse with null pointer exception") {
  50. val person = Person("James", null)
  51. val answer = orElse(person.address.city, "default")
  52. log.info("got answer: " + answer)
  53. assertResult("default") { answer }
  54. }
  55. }