|
| 1 | +package org.apache.flinkx.api |
| 2 | + |
| 3 | +import org.apache.flink.api.common.typeinfo.TypeInformation |
| 4 | +import org.apache.flinkx.api.serializers._ |
| 5 | +import org.apache.flinkx.api.typeinfo.ProductTypeInformation |
| 6 | +import org.scalatest.flatspec.AnyFlatSpec |
| 7 | +import org.scalatest.matchers.should |
| 8 | + |
| 9 | +// This import is not available in Scala 3 |
| 10 | +import scala.reflect.runtime.universe.TypeTag |
| 11 | + |
| 12 | +class GenericCaseClassScala2Test extends AnyFlatSpec with should.Matchers { |
| 13 | + |
| 14 | + import GenericCaseClassScala2Test._ |
| 15 | + |
| 16 | + "Both TypeInformation of Animal Basket" should "have their respective TypeInformation of Animal" in { |
| 17 | + typeInformationOfAnimalBasketShouldHaveATypeInformationOfAnimal[Cat](classOf[Cat]) |
| 18 | + // This second call is failing without the fix: TypeInformation of DogBasket hold a wrong TypeInformation of Cat |
| 19 | + typeInformationOfAnimalBasketShouldHaveATypeInformationOfAnimal[Dog](classOf[Dog]) |
| 20 | + } |
| 21 | + |
| 22 | + def typeInformationOfAnimalBasketShouldHaveATypeInformationOfAnimal[A <: Animal: TypeTag: TypeInformation]( |
| 23 | + aClass: Class[A] |
| 24 | + ): Unit = { |
| 25 | + // cacheKey=org.apache.flinkx.api.GenericCaseClassTest.Cat[] => OK |
| 26 | + val catInfo: TypeInformation[Cat] = implicitly[TypeInformation[Cat]] |
| 27 | + // cacheKey=org.apache.flinkx.api.GenericCaseClassTest.Dog[] => OK |
| 28 | + val dogInfo: TypeInformation[Dog] = implicitly[TypeInformation[Dog]] |
| 29 | + // cacheKey=org.apache.flinkx.api.GenericCaseClassTest.Cat[] or Dog[] => OK |
| 30 | + val aInfo: TypeInformation[A] = implicitly[TypeInformation[A]] |
| 31 | + // cacheKey=org.apache.flinkx.api.GenericCaseClassTest.Basket[org.apache.flinkx.api.GenericCaseClassTest.Cat[]] => OK |
| 32 | + val catBasketInfo: TypeInformation[Basket[Cat]] = implicitly[TypeInformation[Basket[Cat]]] |
| 33 | + // cacheKey=org.apache.flinkx.api.GenericCaseClassTest.Basket[org.apache.flinkx.api.GenericCaseClassTest.Dog[]] => OK |
| 34 | + val dogBasketInfo: TypeInformation[Basket[Dog]] = implicitly[TypeInformation[Basket[Dog]]] |
| 35 | + // without the fix: cacheKey=org.apache.flinkx.api.GenericCaseClassTest.Basket[org.apache.flinkx.api.GenericCaseClassTest.typeInformationOfAnimalBasketShouldHaveATypeInformationOfAnimal.A[]] => issue |
| 36 | + // with the fix: cacheKey=org.apache.flinkx.api.GenericCaseClassTest.Basket[org.apache.flinkx.api.GenericCaseClassTest.Cat] or Dog => OK |
| 37 | + val aBasketInfo: TypeInformation[Basket[A]] = implicitly[TypeInformation[Basket[A]]] |
| 38 | + |
| 39 | + if (classOf[Cat].isAssignableFrom(aClass)) { |
| 40 | + aInfo should be theSameInstanceAs catInfo |
| 41 | + aBasketInfo should be theSameInstanceAs catBasketInfo // Fails without the fix => cache miss |
| 42 | + } |
| 43 | + if (classOf[Dog].isAssignableFrom(aClass)) { |
| 44 | + aInfo should be theSameInstanceAs dogInfo |
| 45 | + aBasketInfo should be theSameInstanceAs dogBasketInfo // Fails without the fix => cache miss |
| 46 | + } |
| 47 | + catBasketInfo.asInstanceOf[ProductTypeInformation[A]].getFieldTypes()(0) should be theSameInstanceAs catInfo |
| 48 | + dogBasketInfo.asInstanceOf[ProductTypeInformation[A]].getFieldTypes()(0) should be theSameInstanceAs dogInfo |
| 49 | + |
| 50 | + // This check is failing on second call without the fix: TypeInformation of DogBasket hold a wrong TypeInformation of Cat |
| 51 | + aBasketInfo.asInstanceOf[ProductTypeInformation[A]].getFieldTypes()(0) should be theSameInstanceAs aInfo |
| 52 | + } |
| 53 | + |
| 54 | +} |
| 55 | + |
| 56 | +object GenericCaseClassScala2Test { |
| 57 | + |
| 58 | + sealed trait Animal extends Product { |
| 59 | + def name: String |
| 60 | + } |
| 61 | + |
| 62 | + case class Cat(name: String) extends Animal |
| 63 | + case class Dog(name: String) extends Animal |
| 64 | + |
| 65 | + case class Basket[A <: Animal](animal: A) |
| 66 | + |
| 67 | +} |
0 commit comments