Skip to content

Commit

Permalink
Fix Google-internal common.collect tests under an Android emulator.
Browse files Browse the repository at this point in the history
The primary change comes as a followup to cl/543468006: Now that the Google-internal `ImmutableTable` declares [methods with Java 8+ types in their signatures](#6567), we get a `NoClassDefFoundError` when performing reflection on the class under old Android emulators. We perform reflection only when (a) using `NullPointerTester` or (b) using serialization. So we (a) stop running `NullPointerTester` under an Android emulator (though we continue to run the same `guava-android` test under a JVM) and (b) specify a `serialVersionUID` so that serialization doesn't need to compute one. (Specifying `serialVersionUID` was our MO for a while. Perhaps we just got careless, and perhaps we should be more diligent. Alternatively, perhaps we should change the `serialVersionUID` every release or so so as to encourage people [not to persist serialized data](https://github.com/google/guava#important-warnings). I at least went to a tiny bit of effort to keep `guava-jre` and `guava-android` on different `serialVersionUID` values, though maybe that's more likely to cause needless trouble someday than to prevent it?)

The other change is to avoid running another GC test under an Android emulator. We'd originally done this for one copy of `AbstractIteratorTester` in cl/444929745, but for whatever reason, we didn't need it for the other copy until (I assume) cl/546307327 and its switch to a different emulator version.

RELNOTES=n/a
PiperOrigin-RevId: 550872250
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Jul 25, 2023
1 parent 75cdb3f commit 230b0cd
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public Integer computeNext() {


@GwtIncompatible // weak references
@AndroidIncompatible // depends on details of GC
public void testFreesNextReference() {
Iterator<Object> itr =
new AbstractIterator<Object>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -479,4 +479,33 @@ public void testOverflowCondition() {
}
assertTrue(builder.build() instanceof SparseImmutableTable);
}

@GwtIncompatible // NullPointerTester
@Override
public void testNullPointerInstance() {
if (isAndroid()) {
/*
* NPT fails under the old versions of Android we test under because it performs reflection on
* ImmutableTable, which declares static methods that refer to Collector, which is unavailable
* under such versions.
*
* We use a runtime check here instead of @AndroidIncompatible: @AndroidIncompatible operates
* by stripping annotated methods entirely, and if we strip this method, then JUnit would just
* run the supermethod as usual.
*
* TODO: b/292578973: Use @AndroidIncompatible if we change our system to keep the methods in
* place but to have the test runner skip them. However, note that if we choose to *both*
* strip the methods *and* have the test runner not run them (for some unusual cases in which
* we don't run the stripping test for technical reasons), then we'd be back to the problem
* described above, since the supermethod is *not* annotated @AndroidIncompatible (since it
* works fine with the other Table implementations).
*/
return;
}
super.testNullPointerInstance();
}

private static boolean isAndroid() {
return System.getProperty("java.runtime.name", "").contains("Android");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -463,4 +463,6 @@ final Object writeReplace() {
private void readObject(ObjectInputStream stream) throws InvalidObjectException {
throw new InvalidObjectException("Use SerializedForm");
}

private static final long serialVersionUID = 0xdecaf;
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public Integer computeNext() {


@GwtIncompatible // weak references
@AndroidIncompatible // depends on details of GC
public void testFreesNextReference() {
Iterator<Object> itr =
new AbstractIterator<Object>() {
Expand Down
29 changes: 29 additions & 0 deletions guava-tests/test/com/google/common/collect/ImmutableTableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -485,4 +485,33 @@ public void testOverflowCondition() {
}
assertTrue(builder.build() instanceof SparseImmutableTable);
}

@GwtIncompatible // NullPointerTester
@Override
public void testNullPointerInstance() {
if (isAndroid()) {
/*
* NPT fails under the old versions of Android we test under because it performs reflection on
* ImmutableTable, which declares static methods that refer to Collector, which is unavailable
* under such versions.
*
* We use a runtime check here instead of @AndroidIncompatible: @AndroidIncompatible operates
* by stripping annotated methods entirely, and if we strip this method, then JUnit would just
* run the supermethod as usual.
*
* TODO: b/292578973: Use @AndroidIncompatible if we change our system to keep the methods in
* place but to have the test runner skip them. However, note that if we choose to *both*
* strip the methods *and* have the test runner not run them (for some unusual cases in which
* we don't run the stripping test for technical reasons), then we'd be back to the problem
* described above, since the supermethod is *not* annotated @AndroidIncompatible (since it
* works fine with the other Table implementations).
*/
return;
}
super.testNullPointerInstance();
}

private static boolean isAndroid() {
return System.getProperty("java.runtime.name", "").contains("Android");
}
}
2 changes: 2 additions & 0 deletions guava/src/com/google/common/collect/ImmutableTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -512,4 +512,6 @@ final Object writeReplace() {
private void readObject(ObjectInputStream stream) throws InvalidObjectException {
throw new InvalidObjectException("Use SerializedForm");
}

private static final long serialVersionUID = 0xcafebabe;
}

0 comments on commit 230b0cd

Please sign in to comment.