PageRenderTime 26ms CodeModel.GetById 12ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 1ms

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