PageRenderTime 22ms CodeModel.GetById 11ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/scalate-core/src/test/scala/org/fusesource/scalate/introspector/IntrospectorTest.scala

http://github.com/scalate/scalate
Scala | 163 lines | 102 code | 35 blank | 26 comment | 4 complexity | 94e9f94bad2e9e40adcf9fd4ed651cb9 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.introspector
 19
 20import _root_.org.fusesource.scalate.FunSuiteSupport
 21
 22case class MyProduct(name: String, age: Int) {
 23  // should be a value which returns a function
 24  def bold(text: String) = "<b>" + text + "</b>"
 25}
 26
 27class MyBean {
 28  var _name: String = _
 29
 30  def getName() = _name
 31
 32  def setName(name: String): Unit = _name = name
 33
 34  var _age: Int = _
 35
 36  def getAge() = _age
 37
 38  def setAge(age: Int): Unit = _age = age
 39
 40  // not a bean property but visible anyway
 41  def foo = "bar"
 42
 43  // should be a value which returns a function
 44  def bold(text: String) = "<b>" + text + "</b>"
 45
 46  override def toString = "MyBean(" + getName + ", " + getAge + ")"
 47}
 48
 49class IntrospectorTest extends FunSuiteSupport {
 50
 51  lazy val isScala_2_13 = scala.util.Properties.versionNumberString.startsWith("2.13")
 52
 53  test("product introspector") {
 54    val introspector = Introspector(classOf[MyProduct])
 55    assertResult("myProduct") { introspector.typeStyleName }
 56
 57    val properties = introspector.properties.sortBy(_.name)
 58    if (isScala_2_13) {
 59      // Since Scala 2.13.0-M5, productElementNames has been appended
 60      // 0 = {MethodProperty@2349} "MethodProperty(age: int)"
 61      // 1 = {MethodProperty@2350} "MethodProperty(name: java.lang.String)"
 62      // 2 = {MethodProperty@2351} "MethodProperty(productElementNames: scala.collection.Iterator)"
 63      assertProperties(properties, 3)
 64      assertProperty(properties(0), "age", "age", classOf[Int])
 65      assertProperty(properties(1), "name", "name", classOf[String])
 66      assertProperty(properties(2), "productElementNames", "productElementNames", classOf[Iterator[_]])
 67    } else {
 68      assertProperties(properties, 2)
 69      assertProperty(properties(0), "age", "age", classOf[Int])
 70      assertProperty(properties(1), "name", "name", classOf[String])
 71    }
 72  }
 73
 74  test("bean introspector") {
 75    val introspector = Introspector(classOf[MyBean])
 76    assertResult("myBean") { introspector.typeStyleName }
 77
 78    val properties = introspector.properties.sortBy(_.name)
 79    assertProperties(properties, 2)
 80
 81    assertProperty(properties(0), "age", "age", classOf[Int])
 82    assertProperty(properties(1), "name", "name", classOf[String])
 83  }
 84
 85  test("product get") {
 86    val v = MyProduct("James", 40)
 87    val introspector = Introspector(classOf[MyProduct])
 88    dump(introspector)
 89
 90    assertResult(Some("James")) { introspector.get("name", v) }
 91    assertResult(Some(40)) { introspector.get("age", v) }
 92
 93    assertStringFunctor(introspector, v, "bold", "product", "<b>product</b>")
 94  }
 95
 96  test("bean get") {
 97    val v = new MyBean
 98    v.setName("Hiram")
 99    v.setAge(30)
100
101    val introspector = Introspector(classOf[MyBean])
102    dump(introspector)
103
104    assertResult(Some("Hiram")) { introspector.get("name", v) }
105    assertResult(Some(30)) { introspector.get("age", v) }
106
107    // autodiscover methods too
108    assertResult(Some("bar")) { introspector.get("foo", v) }
109
110    assertStringFunctor(introspector, v, "bold", "bean", "<b>bean</b>")
111  }
112
113  test("bean set") {
114    val v = new MyBean
115
116    val introspector = Introspector(classOf[MyBean])
117    val name = introspector.property("name").get
118    val age = introspector.property("age").get
119
120    name.set(v, "James")
121    assertResult("James") { name(v) }
122
123    age.set(v, 30)
124    assertResult(30) { age(v) }
125
126    debug("created bean: %s", v)
127    // TODO....
128  }
129
130  def dump[T](introspector: Introspector[T]): Unit = {
131    debug("Introspector for %s", introspector.elementType.getName)
132    val expressions = introspector.expressions
133    for (k <- expressions.keysIterator.toSeq.sortWith(_ < _)) {
134      debug("Expression: %s = %s", k, expressions(k))
135    }
136  }
137
138  def assertStringFunctor[T](introspector: Introspector[T], instance: T, name: String, arg: String, expected: Any): Unit = {
139    introspector.get(name, instance) match {
140      case Some(f: Function1[_, _]) =>
141        val _f = f.asInstanceOf[Function1[String, _]]
142        debug("calling function %s named %s on %s = %s", _f, name, instance, _f(arg))
143        assertResult(expected) { _f(arg) }
144      case Some(v) =>
145        fail("Expected function for expression " + name + " but got " + v)
146      case _ =>
147        fail("Expected function for expression " + name)
148    }
149  }
150
151  def assertProperty(property: Property[_], name: String, label: String, propertyType: Class[_]) = {
152    assertResult(name) { property.name }
153    assertResult(label) { property.label }
154    assertResult(propertyType) { property.propertyType }
155  }
156
157  def assertProperties(properties: collection.Seq[Property[_]], expectedSize: Int) = {
158    for (property <- properties) {
159      debug("Property: %s", property)
160    }
161    assertResult(expectedSize) { properties.size }
162  }
163}