Fix JavaClassTest checks to be more robust against future JDK versions
Previously, we were verifying parsing by checking for specifically named
fields and functions, but some of those fields / methods have been
removed starting in JDK11.
Since we don't care about exact fields / methods, but just more that the
parsing code works, we instead check the properties of the fields /
methods for verification, which should be a lot more future proof.
Fixes: 161611073
Test: Only test code updated
Change-Id: I0d330e0247edd051206ad186afeaa929c7d3496e
diff --git a/compiler/src/test/java/android/databinding/tool/reflection/JavaClassTest.kt b/compiler/src/test/java/android/databinding/tool/reflection/JavaClassTest.kt
index bdb16cd..5ec19c3 100644
--- a/compiler/src/test/java/android/databinding/tool/reflection/JavaClassTest.kt
+++ b/compiler/src/test/java/android/databinding/tool/reflection/JavaClassTest.kt
@@ -29,24 +29,45 @@
fun getAllMethods() {
val modelClass = ModelAnalyzer.getInstance().findClass("java.lang.String", null)!!
val methods = modelClass.allMethods
- // public methods
- assertTrue(methods.any { it.name == "charAt" })
- assertTrue(methods.any { it.name == "startsWith" })
- // private methods
- assertTrue(methods.any { it.name == "lastIndexOfSupplementary" })
- // static methods
- assertTrue(methods.any { it.name == "checkBounds" })
- // methods from super class
- assertTrue(methods.any { it.name == "wait" })
+
+ // NOTE: We don't care about any particular method that we're testing against here; we're just
+ // sampling to make sure that methods are being exposed with expected metadata.
+ methods
+ .filter { it.isPublic }
+ .let { publicMethods ->
+ // instance methods
+ assertTrue(publicMethods.any { it.name == "charAt" })
+ assertTrue(publicMethods.any { it.name == "startsWith" })
+ // methods from base class
+ assertTrue(publicMethods.any { it.name == "getClass" })
+ assertTrue(publicMethods.any { it.name == "wait" })
+ // static methods
+ assertTrue(publicMethods.any { it.name == "format" })
+ assertTrue(publicMethods.any { it.name == "valueOf" })
+ }
+
+ // private methods exist
+ assertTrue(methods.any { !it.isPublic && !it.isProtected })
}
@Test
fun getAllFields() {
- val modelClass = ModelAnalyzer.getInstance().findClass("java.math.BigDecimal", null)!!
+ val modelClass = ModelAnalyzer.getInstance().findClass("java.awt.Rectangle", null)!!
val fields = modelClass.allFields
- // private fields
- assertTrue(fields.any { it.name == "intVal" })
- // static fields
- assertTrue(fields.any { it.name == "ONE" })
+
+ // NOTE: We don't care about any particular field that we're testing against here; we're just
+ // sampling to make sure that fields are being exposed with expected metadata.
+ fields
+ .filter { it.isPublic }
+ .let { publicFields ->
+ // instance
+ assertTrue(publicFields.any { it.name == "width"})
+ assertTrue(publicFields.any { it.name == "height"})
+ // static
+ assertTrue(publicFields.any { it.name == "OUT_LEFT" })
+ }
+
+ // private fields exist
+ assertTrue(fields.any { !it.isPublic })
}
}
\ No newline at end of file