ComponentNameUtils { @NonNull public static String packageNameFrom(@Nullable final String flattenComponentName) { val parts = nullToEmpty(flattenComponentName).split(COMPONENT_NAME_SEPARATOR); return (parts.length == 2) ? parts[0] : ""; } private  ComponentNameUtils(); @NonNull static String packageNameFrom(@Nullable final String flattenComponentName); @NonNull static ComponentName fromFlattenString(@Nullable final String flattenComponentName); static final ComponentName DUMMY_COMPONENT; }
@Test public void shouldReturnEmptyPackageNameForInvalidFlattenComponentString() { val resultForNull = ComponentNameUtils.packageNameFrom(null); assertThat(resultForNull, is("")); val resultForEmpty = ComponentNameUtils.packageNameFrom(""); assertThat(resultForEmpty, is("")); val resultForInvalidFormat = ComponentNameUtils.packageNameFrom("com.abc.MyActivity"); assertThat(resultForInvalidFormat, is("")); }
@Test public void shouldExtractPackageNameFromComponentNameFlattenString() { val packageName = ComponentNameUtils.packageNameFrom("com.abc/MyActivity"); assertThat(packageName, is("com.abc")); }