-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle the
NullPointerException
when looking up a resource value of…
… a `DynamicRefTable` when the data type is invalid. Given that the function has the return type as error code, when the data type is invalid, it would make sense to return the corresponding bad code instead of throw an exception. PiperOrigin-RevId: 631829984
- Loading branch information
Googler
authored and
Copybara-Service
committed
May 8, 2024
1 parent
3cdc3f1
commit fb268ae
Showing
5 changed files
with
65 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
resources/src/test/java/org/robolectric/res/android/DataTypeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.robolectric.res.android; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static org.junit.Assert.assertThrows; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
public final class DataTypeTest { | ||
|
||
@Test | ||
public void fromCode_shouldThrowExceptionForInvalidCode() { | ||
assertThrows(IllegalArgumentException.class, () -> DataType.fromCode(99)); | ||
} | ||
|
||
@Test | ||
public void fromCode_shouldReturnCorrectDataTypeForValidCode() { | ||
assertThat(DataType.fromCode(0)).isEqualTo(DataType.NULL); | ||
assertThat(DataType.fromCode(3)).isEqualTo(DataType.STRING); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
resources/src/test/java/org/robolectric/res/android/DynamicRefTableTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.robolectric.res.android; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static org.robolectric.res.android.Errors.BAD_TYPE; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
import org.robolectric.res.android.ResourceTypes.Res_value; | ||
|
||
@RunWith(JUnit4.class) | ||
public final class DynamicRefTableTest { | ||
|
||
private static final Ref<Res_value> RES_VALUE_OF_BAD_TYPE = | ||
new Ref<>(new Res_value(/* dataType= */ (byte) 99, /* data= */ 0)); | ||
|
||
@Test | ||
public void lookupResourceValue_returnsBadTypeIfTypeOutOfEnumRange() { | ||
DynamicRefTable pseudoRefTable = | ||
new DynamicRefTable(/* packageId= */ (byte) 0, /* appAsLib= */ true); | ||
assertThat(pseudoRefTable.lookupResourceValue(RES_VALUE_OF_BAD_TYPE)).isEqualTo(BAD_TYPE); | ||
} | ||
} |