Repository: square/leakcanary
File: shark/shark/src/test/java/shark/OpenJdkInstanceRefReadersTest.kt (Kotlin)

Code before:
      assertThat(owningClassName).isEqualTo(ReferenceQueue::class.qualifiedName)
      assertThat(referenceDisplayName).isEqualTo("head")
    }
    with(refPath[2]) {
      assertThat(owningClassName).isEqualTo("java.util.WeakHashMap\$Entry")
      assertThat(referenceDisplayName).isEqualTo("value")
    }
  }

  @Test fun `WeakHashMap no expander with string key`() {
    val map = WeakHashMap<Any, Any>()
    leakRoot = map
    map["StringKey"] = Retained()

    System.gc()

    val refPath = findLeak { null }

    assertThat(refPath).hasSize(3)

    with(refPath[0]) {
      assertThat(owningClassName).isEqualTo(WeakHashMap::class.qualifiedName)
      assertThat(referenceDisplayName).isEqualTo("table")
    }
    with(refPath[1]) {
      assertThat(owningClassName).isEqualTo("java.util.WeakHashMap\$Entry[]")
      assertThat(referenceDisplayName).isEqualTo("[0]")
    }
    with(refPath[2]) {
      assertThat(owningClassName).isEqualTo("java.util.WeakHashMap\$Entry")
      assertThat(referenceDisplayName).isEqualTo("value")
    }
  }

  @Test fun `WeakHashMap no expander with singleton key`() {
    val map = WeakHashMap<Any, Any>()
    leakRoot = map
    map[leakRoot] = Retained()

    System.gc()

    val refPath = findLeak { null }

    assertThat(refPath).hasSize(3)

    with(refPath[0]) {
      assertThat(owningClassName).isEqualTo(WeakHashMap::class.qualifiedName)
      assertThat(referenceDisplayName).isEqualTo("table")
    }
    with(refPath[1]) {
      assertThat(owningClassName).isEqualTo("java.util.WeakHashMap\$Entry[]")

Review comment (bug):
This is failing in CI

```
shark.OpenJdkInstanceRefReadersTest > WeakHashMap expanded FAILED
    org.junit.ComparisonFailure: expected:<"java.[util.WeakHashMap]"> but was:<"java.[lang.ref.Reference]">
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
        at shark.OpenJdkInstanceRefReadersTest.WeakHashMap expanded(OpenJdkInstanceRefReadersTest.kt:432)
```

Code after:
      assertThat(referenceDisplayName).isEqualTo("table")
    }
    with(refPath[1]) {
      assertThat(owningClassName).isEqualTo("java.util.WeakHashMap\$Entry[]")
      assertThat(referenceDisplayName).isEqualTo("[0]")
    }
    with(refPath[2]) {
      assertThat(owningClassName).isEqualTo("java.util.WeakHashMap\$Entry")
      assertThat(referenceDisplayName).isEqualTo("value")
    }
  }

  @Test fun `WeakHashMap expanded`() {
    val map = WeakHashMap<Any, Any>()
    leakRoot = map
    val retainedKey = SomeKey()
    map[retainedKey] = Retained()

    System.gc()

    val refPath = findLeak(OpenJdkInstanceRefReaders.WEAK_HASH_MAP)

    assertThat(refPath).hasSize(1)

    with(refPath[0]) {
      assertThat(owningClassName).isEqualTo(WeakHashMap::class.qualifiedName)
      assertThat(referenceDisplayName).matches("\\[instance @\\d* of shark\\.OpenJdkInstanceRefReadersTest\\\$SomeKey]")
    }
  }

  @Test fun `WeakHashMap expanded with string key`() {
    val map = WeakHashMap<Any, Any>()
    leakRoot = map
    val retainedKey = "StringKey"
    map[retainedKey] = Retained()

    System.gc()

    val refPath = findLeak(OpenJdkInstanceRefReaders.WEAK_HASH_MAP)

    assertThat(refPath).hasSize(1)

    with(refPath.first()) {
      assertThat(referenceDisplayName).isEqualTo("[\"StringKey\"]")
    }
  }

  @Test fun `WeakHashMap expanded with null key`() {
    val map = WeakHashMap<Any, Any>()
    leakRoot = map
    map[null] = Retained()