Skip to content

Commit c9e86e8

Browse files
authored
Fix 2634 : Adding the null guard when getTransferNetworkConnectionType is null (#2797)
* Adding the null guard when getTransferNetworkConnectionType is null * Add tests * removing unused imports
1 parent ff01397 commit c9e86e8

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferRecord.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,16 +357,15 @@ public String toString() {
357357
* @param connManager the android connectivity manager
358358
* @return true if the preferred network is available, false otherwise.
359359
*/
360-
private boolean checkPreferredNetworkAvailability(final TransferStatusUpdater updater,
360+
protected boolean checkPreferredNetworkAvailability(final TransferStatusUpdater updater,
361361
final ConnectivityManager connManager) {
362362

363363
if (connManager == null) {
364364
// Unable to get the details of the network, we will start the transfer.
365365
return true;
366366
}
367-
368-
if (transferUtilityOptions != null &&
369-
!transferUtilityOptions.getTransferNetworkConnectionType().isConnected(connManager)) {
367+
368+
if (transferUtilityOptions != null && transferUtilityOptions.getTransferNetworkConnectionType() != null && !transferUtilityOptions.getTransferNetworkConnectionType().isConnected(connManager)) {
370369
// the network that is configured in the TransferUtilityOptions is not available.
371370
// we will set the state to WAITING_FOR_NETWORK. The transfer will be started
372371
// when a future notification is received for the desired network.

aws-android-sdk-s3/src/main/java/com/amazonaws/mobileconnectors/s3/transferutility/TransferUtilityOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public class TransferUtilityOptions implements Serializable {
6767
/**
6868
* Type of connection to use for transfers.
6969
*/
70-
private TransferNetworkConnectionType transferNetworkConnectionType;
70+
protected TransferNetworkConnectionType transferNetworkConnectionType;
7171

7272
/**
7373
* Constructor that sets the options to the

aws-android-sdk-s3/src/test/java/com/amazonaws/mobileconnectors/s3/transferutility/UploadInputStreamTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
package com.amazonaws.mobileconnectors.s3.transferutility;
1717

1818
import android.content.Context;
19+
import android.net.ConnectivityManager;
1920
import android.util.Log;
21+
2022
import androidx.test.platform.app.InstrumentationRegistry;
2123

2224
import com.amazonaws.services.s3.AmazonS3Client;
@@ -82,6 +84,7 @@ public void setup() {
8284
/**
8385
* Test that {@link TransferUtility#upload(String, InputStream)} calls the File based
8486
* upload method with all of the expected defaults.
87+
*
8588
* @throws IOException if upload fails to read input file
8689
*/
8790
@Test
@@ -101,6 +104,7 @@ public void testUpload() throws IOException {
101104
/**
102105
* Test that {@link TransferUtility#upload(String, InputStream, UploadOptions)} with
103106
* a default Builder calls the File based upload method with all of the expected defaults.
107+
*
104108
* @throws IOException if upload fails to read input file
105109
*/
106110
@Test
@@ -121,6 +125,7 @@ public void testUploadWithDefaults() throws IOException {
121125
* Test that {@link TransferUtility#upload(String, InputStream, UploadOptions)} with
122126
* all parameters specified calls the File based upload method with the same parameters
123127
* provided as input.
128+
*
124129
* @throws IOException if upload fails to read input file
125130
*/
126131
@Test
@@ -146,6 +151,7 @@ public void testUploadWithAllParametersSet() throws IOException {
146151

147152
/**
148153
* Verify that the File does in fact get deleted after the upload is complete.
154+
*
149155
* @throws IOException if upload fails to read input file
150156
*/
151157
@Test
@@ -169,6 +175,7 @@ public void testDeleteTempFileAfterUpload() throws IOException {
169175

170176
/**
171177
* Cleans up input stream
178+
*
172179
* @throws IOException if input stream fails to close
173180
*/
174181
@After
@@ -197,4 +204,31 @@ public void onError(int id, Exception exception) {
197204
Log.e(TAG, "Error during upload: " + id, exception);
198205
}
199206
}
207+
208+
@Test
209+
public void testTransferRecordCheckPreferredNetworkAvailability() {
210+
Context context = InstrumentationRegistry.getInstrumentation().getContext();
211+
TransferStatusUpdater transferStatusUpdater = TransferStatusUpdater.getInstance(context);
212+
ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
213+
transferStatusUpdater.updateState(2342, TransferState.IN_PROGRESS);
214+
transferStatusUpdater.addTransfer(new TransferRecord(34234));
215+
for (TransferRecord record : transferStatusUpdater.getTransfers().values()) {
216+
//When connectionManager is null - Base case
217+
assertTrue(record.checkPreferredNetworkAvailability(transferStatusUpdater, null));
218+
219+
//When TransferUtilityOptions is null
220+
record.transferUtilityOptions = null;
221+
assertTrue(record.checkPreferredNetworkAvailability(transferStatusUpdater, connManager));
222+
223+
//When TransferNetworkConnectionType is null
224+
record.transferUtilityOptions = new TransferUtilityOptions();
225+
record.transferUtilityOptions.transferNetworkConnectionType = null;
226+
assertTrue(record.checkPreferredNetworkAvailability(transferStatusUpdater, connManager));
227+
228+
//When TransferNetworkConnectionType is not null
229+
record.transferUtilityOptions.transferNetworkConnectionType = TransferNetworkConnectionType.ANY;
230+
assertTrue(record.checkPreferredNetworkAvailability(transferStatusUpdater, connManager));
231+
}
232+
233+
}
200234
}

0 commit comments

Comments
 (0)