|
| 1 | +package com.fasterxml.jackson |
| 2 | +package module.scala |
| 3 | +package introspect |
| 4 | + |
| 5 | +import com.fasterxml.jackson.core.JsonGenerator |
| 6 | +import com.fasterxml.jackson.databind |
| 7 | +import com.fasterxml.jackson.databind.module.SimpleModule |
| 8 | +import com.fasterxml.jackson.databind.ser.ContextualSerializer |
| 9 | +import com.fasterxml.jackson.databind.{SerializerProvider, JsonSerializer, BeanDescription, ObjectMapper} |
| 10 | +import experimental.ScalaObjectMapper |
| 11 | + |
| 12 | +import org.junit.runner.RunWith |
| 13 | +import org.scalatest.{fixture, Matchers} |
| 14 | +import org.scalatest.junit.JUnitRunner |
| 15 | +import org.scalatest.LoneElement._ |
| 16 | + |
| 17 | +import beans.BeanProperty |
| 18 | +import collection.JavaConverters._ |
| 19 | + |
| 20 | +object ScalaAnnotationIntrospectorTest |
| 21 | +{ |
| 22 | + class Token |
| 23 | + class BasicPropertyClass(val param: Int) |
| 24 | + class BeanPropertyClass(@BeanProperty val param: Int) |
| 25 | + class AnnotatedBasicPropertyClass(@JsonScalaTestAnnotation val param: Token) |
| 26 | + class AnnotatedBeanPropertyClass(@JsonScalaTestAnnotation @BeanProperty val param: Token) |
| 27 | +} |
| 28 | + |
| 29 | +@RunWith(classOf[JUnitRunner]) |
| 30 | +class ScalaAnnotationIntrospectorTest extends fixture.FlatSpec with Matchers { |
| 31 | + import ScalaAnnotationIntrospectorTest._ |
| 32 | + |
| 33 | + type FixtureParam = ObjectMapper with ScalaObjectMapper |
| 34 | + |
| 35 | + override def withFixture(test: OneArgTest) = { |
| 36 | + val mapper = new ObjectMapper with ScalaObjectMapper |
| 37 | + mapper.registerModule(DefaultScalaModule) |
| 38 | + withFixture(test.toNoArgTest(mapper)) |
| 39 | + } |
| 40 | + |
| 41 | + behavior of "ScalaAnnotationIntrospector" |
| 42 | + |
| 43 | + it should "detect a val property" in { mapper => |
| 44 | + val bean = new BasicPropertyClass(1) |
| 45 | + val allProps = getProps(mapper, bean) |
| 46 | + allProps.loneElement should have ('name ("param")) |
| 47 | + |
| 48 | + val prop = allProps.asScala.head |
| 49 | + prop should have ( |
| 50 | + 'hasField (true), |
| 51 | + 'hasGetter (true), |
| 52 | + 'hasConstructorParameter (true) |
| 53 | + ) |
| 54 | + |
| 55 | + val accessor = prop.getAccessor |
| 56 | + accessor shouldNot be (null) |
| 57 | + } |
| 58 | + |
| 59 | + it should "detect annotations on a val property" in { mapper => |
| 60 | + mapper.registerModule(new SimpleModule() { |
| 61 | + addSerializer(new JsonSerializer[Token] with ContextualSerializer { |
| 62 | + override val handledType = classOf[Token] |
| 63 | + override def serialize(value: Token, gen: JsonGenerator, serializers: SerializerProvider): Unit = |
| 64 | + gen.writeString("") |
| 65 | + override def createContextual(prov: SerializerProvider, property: databind.BeanProperty): JsonSerializer[_] = { |
| 66 | + property.getAnnotation(classOf[JsonScalaTestAnnotation]) shouldNot be (null) |
| 67 | + this |
| 68 | + } |
| 69 | + }) |
| 70 | + }) |
| 71 | + |
| 72 | + val bean = new AnnotatedBasicPropertyClass(new Token) |
| 73 | + mapper.writeValueAsString(bean) |
| 74 | + } |
| 75 | + |
| 76 | + it should "detect a bean property" in { mapper => |
| 77 | + val bean = new BeanPropertyClass(1) |
| 78 | + val allProps = getProps(mapper, bean) |
| 79 | + allProps.loneElement should have ('name ("param")) |
| 80 | + |
| 81 | + val prop = allProps.asScala.head |
| 82 | + prop should have ( |
| 83 | + 'hasField (true), |
| 84 | + 'hasGetter (true), |
| 85 | + 'hasConstructorParameter (true) |
| 86 | + ) |
| 87 | + |
| 88 | + val accessor = prop.getAccessor |
| 89 | + accessor shouldNot be (null) |
| 90 | + } |
| 91 | + |
| 92 | + it should "detect annotations on a bean property" in { mapper => |
| 93 | + mapper.registerModule(new SimpleModule() { |
| 94 | + addSerializer(new JsonSerializer[Token] with ContextualSerializer { |
| 95 | + override val handledType = classOf[Token] |
| 96 | + override def serialize(value: Token, gen: JsonGenerator, serializers: SerializerProvider): Unit = |
| 97 | + gen.writeString("") |
| 98 | + override def createContextual(prov: SerializerProvider, property: databind.BeanProperty): JsonSerializer[_] = { |
| 99 | + property.getAnnotation(classOf[JsonScalaTestAnnotation]) shouldNot be (null) |
| 100 | + this |
| 101 | + } |
| 102 | + }) |
| 103 | + }) |
| 104 | + |
| 105 | + val bean = new AnnotatedBeanPropertyClass(new Token) |
| 106 | + val allProps = getProps(mapper, bean) |
| 107 | + allProps.loneElement should have ('name ("param")) |
| 108 | + |
| 109 | + val prop = allProps.asScala.head |
| 110 | + prop should have ( |
| 111 | + 'hasField (true), |
| 112 | + 'hasGetter (true), |
| 113 | + 'hasConstructorParameter (true) |
| 114 | + ) |
| 115 | + val param = prop.getConstructorParameter |
| 116 | + param.getAnnotation(classOf[JsonScalaTestAnnotation]) shouldNot be (null) |
| 117 | + |
| 118 | + mapper.writeValueAsString(bean) |
| 119 | + } |
| 120 | + |
| 121 | + private def getProps(mapper: ObjectMapper, bean: AnyRef) = { |
| 122 | + val config = mapper.getSerializationConfig |
| 123 | + val beanDescription: BeanDescription = config.introspect(mapper.constructType(bean.getClass)) |
| 124 | + beanDescription.findProperties() |
| 125 | + } |
| 126 | +} |
0 commit comments