Skip to content

Commit

Permalink
Merge pull request #577 from adobe/staging
Browse files Browse the repository at this point in the history
  • Loading branch information
praveek authored Oct 30, 2023
2 parents 9dd5589 + 637276e commit f7c1e4a
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ package com.adobe.marketing.mobile.internal

internal object CoreConstants {
const val LOG_TAG = "MobileCore"
const val VERSION = "2.5.0"
const val VERSION = "2.5.1"

object EventDataKeys {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public final class StreamUtils {
private StreamUtils() {}

/**
* Reads the contents of {@code InputStream} as String
* Reads the contents of the {@code InputStream} as a String. The input stream provided will be
* closed after contents are read.
*
* @param inputStream {@link InputStream} to read
* @return {@link String} representation of the input stream
Expand All @@ -36,11 +37,9 @@ public static String readAsString(final InputStream inputStream) {
return null;
}

final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
final byte[] data = new byte[STREAM_READ_BUFFER_SIZE];
int bytesRead;

try {
try (final ByteArrayOutputStream buffer = new ByteArrayOutputStream()) {
final byte[] data = new byte[STREAM_READ_BUFFER_SIZE];
int bytesRead;
while ((bytesRead = inputStream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, bytesRead);
}
Expand All @@ -53,6 +52,15 @@ public static String readAsString(final InputStream inputStream) {
TAG,
"Unable to convert InputStream to String," + ex.getLocalizedMessage());
return null;
} finally {
try {
inputStream.close();
} catch (final IOException ex) {
Log.trace(
CoreConstants.LOG_TAG,
TAG,
"Unable to close InputStream," + ex.getLocalizedMessage());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);

// make sure we have a valid message before trying to proceed
if (message == null) {
Log.debug(
ServiceConstants.LOG_TAG,
TAG,
"%s (Message Fragment), failed to create the fragment.",
UNEXPECTED_NULL_VALUE);
return;
}

final MessageSettings messageSettings = message.getMessageSettings();
if (messageSettings == null) {
Log.debug(
Expand Down Expand Up @@ -205,6 +215,16 @@ public void onDetach() {

removeListeners();

// make sure we have a valid message before trying to proceed
if (message == null) {
Log.debug(
ServiceConstants.LOG_TAG,
TAG,
"%s (Message Fragment), failed to detach the fragment.",
UNEXPECTED_NULL_VALUE);
return;
}

// clean webview parent in case the detach is occurring due to an orientation change
final WebView webView = message.getWebView();
if (webView != null && webView.getParent() != null) {
Expand Down Expand Up @@ -297,6 +317,15 @@ public boolean onTouch(final View view, final MotionEvent motionEvent) {
* android.view.View.OnTouchListener}
*/
private void addListeners() {
if (message == null) {
Log.debug(
ServiceConstants.LOG_TAG,
TAG,
"%s (AEPMessage), unable to add listeners.",
UNEXPECTED_NULL_VALUE);
return;
}

final View contentView = getActivity().findViewById(android.R.id.content);

// if we have an orientation change, wait for it to complete then proceed with webview
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import kotlin.test.assertTrue
@RunWith(MockitoJUnitRunner.Silent::class)
class MobileCoreTests {

private var EXTENSION_VERSION = "2.5.0"
private var EXTENSION_VERSION = "2.5.1"

@Mock
private lateinit var mockedEventHub: EventHub
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ import kotlin.test.assertTrue
@RunWith(MockitoJUnitRunner.Silent::class)
class ConfigurationExtensionTests {

private var EXTENSION_VERSION = "2.5.0"
private var EXTENSION_VERSION = "2.5.1"

@Mock
private lateinit var mockServiceProvider: ServiceProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@

package com.adobe.marketing.mobile.util;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.adobe.marketing.mobile.TestHelper;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.junit.Test;
import org.mockito.Mockito;

public final class StreamUtilsTests {

Expand All @@ -39,4 +47,13 @@ public void testStreamToString_when_validInputStream() throws Exception {
InputStream stream = new ByteArrayInputStream("myTestExample".getBytes("UTF-8"));
assertEquals("myTestExample", StreamUtils.readAsString(stream));
}

@Test
public void testStreamUtilsClosesStream_when_invalidInputStream() throws Exception {
InputStream stream = Mockito.mock(InputStream.class);
when(stream.read(any(), anyInt(), anyInt()))
.thenThrow(new IOException("Mocked IO Exception"));
assertNull(StreamUtils.readAsString(stream));
verify(stream).close();
}
}
2 changes: 1 addition & 1 deletion code/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ android.useAndroidX=true
#
#Maven artifacts
#Core extension
coreExtensionVersion=2.5.0
coreExtensionVersion=2.5.1
coreExtensionName=core
coreExtensionAARName=core-phone-release.aar
coreMavenRepoName=AdobeMobileCoreSdk
Expand Down

0 comments on commit f7c1e4a

Please sign in to comment.