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