Skip to content

Commit 101519a

Browse files
authored
Merge pull request #250 from Microsoft/develop
0.3.1 additional commits
2 parents f7d4e23 + 3a86174 commit 101519a

File tree

8 files changed

+271
-51
lines changed

8 files changed

+271
-51
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[![Build Status](https://www.bitrise.io/app/78891228f9c6e6dc.svg?token=KQ6kVAci490XBjulCcQuGQ&branch=develop)](https://www.bitrise.io/app/78891228f9c6e6dc)
22
[![codecov](https://codecov.io/gh/Microsoft/MobileCenter-SDK-Android/branch/develop/graph/badge.svg?token=YwMZRPnYK3)](https://codecov.io/gh/Microsoft/MobileCenter-SDK-Android)
33
[![GitHub Release](https://img.shields.io/github/release/Microsoft/MobileCenter-SDK-Android.svg)](https://github.com/Microsoft/MobileCenter-SDK-Android/releases/latest)
4-
[![Bintray](https://img.shields.io/bintray/v/mobilecenter/mobilecenter/mobile-center.svg)](https://bintray.com/mobilecenter/mobilecenter)
54
[![license](https://img.shields.io/badge/license-MIT%20License-yellow.svg)](https://github.com/Microsoft/MobileCenter-SDK-Android/blob/develop/license.txt)
65

76
# Mobile Center SDK for Android

codecov.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
codecov:
2+
bot: xabldint

sdk/mobile-center-crashes/src/androidTest/java/com/microsoft/azure/mobile/crashes/WrapperSdkExceptionManagerAndroidTest.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,20 @@ class ErrorData {
7777
}
7878
}
7979

80-
// even after deleting errorA, it should exist in memory - so, we can still load it
80+
/* Even after deleting errorA, it should exist in memory - so, we can still load it. */
8181
WrapperSdkExceptionManager.deleteWrapperExceptionData(errorA.id);
8282
byte[] loadedDataA = WrapperSdkExceptionManager.loadWrapperExceptionData(errorA.id);
8383
assertNotNull(loadedDataA);
84-
8584
for (int i = 0; i < errorA.data.length; ++i) {
8685
assertEquals(errorA.data[i], loadedDataA[i]);
8786
}
88-
}
8987

88+
/* Try to load data bypassing the cache. */
89+
WrapperSdkExceptionManager.sWrapperExceptionDataContainer.clear();
90+
byte[] loadedDataC = WrapperSdkExceptionManager.loadWrapperExceptionData(errorC.id);
91+
assertNotNull(loadedDataC);
92+
for (int i = 0; i < errorC.data.length; ++i) {
93+
assertEquals(errorC.data[i], loadedDataC[i]);
94+
}
95+
}
9096
}

sdk/mobile-center-crashes/src/main/java/com/microsoft/azure/mobile/crashes/WrapperSdkExceptionManager.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@
2121
public class WrapperSdkExceptionManager {
2222

2323
/**
24-
* File extension for data files created by this class.
24+
* Contains wrapper SDK data that has been loaded into memory
2525
*/
26-
private static final String DATA_FILE_EXTENSION = ".dat";
26+
@VisibleForTesting
27+
static final Map<String, byte[]> sWrapperExceptionDataContainer = new HashMap<>();
2728

2829
/**
29-
* Contains wrapper SDK data that has been loaded into memory
30+
* File extension for data files created by this class.
3031
*/
31-
private static final Map<String, byte[]> sWrapperExceptionDataContainer = new HashMap<>();
32+
private static final String DATA_FILE_EXTENSION = ".dat";
3233

3334
@VisibleForTesting
3435
WrapperSdkExceptionManager() {
@@ -37,7 +38,7 @@ public class WrapperSdkExceptionManager {
3738
/**
3839
* Store the in-memory wrapper exception data to disk. This should only be used by a wrapper SDK.
3940
*
40-
* @param data The data to be saved
41+
* @param data The data to be saved
4142
* @param errorId The associated error UUID
4243
*/
4344
public static void saveWrapperExceptionData(byte[] data, UUID errorId) {
@@ -50,36 +51,35 @@ public static void saveWrapperExceptionData(byte[] data, UUID errorId) {
5051
File dataFile = getFile(errorId);
5152
try {
5253
StorageHelper.InternalStorage.writeObject(dataFile, data);
53-
}
54-
catch (IOException e) {
54+
} catch (IOException e) {
5555
MobileCenterLog.error(Crashes.LOG_TAG, "Failed to save wrapper exception data to file", e);
5656
}
5757
}
5858

5959
/**
6060
* Delete wrapper exception data from disk and store it in memory
6161
*
62-
* @param errorId The associated error UUID
62+
* @param errorId The associated error UUID
6363
*/
6464
public static void deleteWrapperExceptionData(UUID errorId) {
6565
if (errorId == null) {
6666
MobileCenterLog.error(Crashes.LOG_TAG, "Failed to delete wrapper exception data: null errorId");
6767
return;
6868
}
69-
7069
File dataFile = getFile(errorId);
71-
byte[] loadResult = loadWrapperExceptionData(errorId);
72-
if (loadResult == null) {
73-
MobileCenterLog.error(Crashes.LOG_TAG, "Failed to delete wrapper exception data: data not found");
74-
return;
70+
if (dataFile.exists()) {
71+
byte[] loadResult = loadWrapperExceptionData(errorId);
72+
if (loadResult == null) {
73+
MobileCenterLog.error(Crashes.LOG_TAG, "Failed to delete wrapper exception data: data not found");
74+
}
75+
StorageHelper.InternalStorage.delete(dataFile);
7576
}
76-
StorageHelper.InternalStorage.delete(dataFile);
7777
}
7878

7979
/**
8080
* Load wrapper exception data into memory
8181
*
82-
* @param errorId The associated error UUID
82+
* @param errorId The associated error UUID
8383
* @return The data loaded into memory
8484
*/
8585
public static byte[] loadWrapperExceptionData(UUID errorId) {
@@ -101,8 +101,7 @@ public static byte[] loadWrapperExceptionData(UUID errorId) {
101101
sWrapperExceptionDataContainer.put(errorId.toString(), dataBytes);
102102
}
103103
return dataBytes;
104-
}
105-
catch (ClassNotFoundException | IOException e) {
104+
} catch (ClassNotFoundException | IOException e) {
106105
MobileCenterLog.error(Crashes.LOG_TAG, "Cannot access wrapper exception data file " + dataFile.getName(), e);
107106
}
108107
return null;
@@ -111,7 +110,7 @@ public static byte[] loadWrapperExceptionData(UUID errorId) {
111110
/**
112111
* Get a file object for wrapper exception data
113112
*
114-
* @param errorId The associated error UUID
113+
* @param errorId The associated error UUID
115114
* @return The corresponding file object
116115
*/
117116
private static File getFile(@NonNull UUID errorId) {

sdk/mobile-center-crashes/src/main/java/com/microsoft/azure/mobile/crashes/ingestion/models/Exception.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class Exception implements Model {
4848
/**
4949
* Name of the wrapper SDK that emitted this exception.
5050
* Consists of the name of the SDK and the wrapper platform,
51-
* e.g. "mobilecentersdk.xamarin", "hockeysdk.cordova".
51+
* e.g. "mobilecenter.xamarin", "hockeysdk.cordova".
5252
*/
5353
private String wrapperSdkName;
5454

sdk/mobile-center-crashes/src/test/java/com/microsoft/azure/mobile/crashes/CrashesTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ public String getMessage() {
489489
when(ErrorLogHelper.getStoredThrowableFile(any(UUID.class))).thenReturn(new File("."));
490490
when(ErrorLogHelper.getErrorReportFromErrorLog(any(ManagedErrorLog.class), any(Throwable.class))).thenCallRealMethod();
491491

492-
when(StorageHelper.InternalStorage.readObject(any(File.class))).thenReturn(exception).thenReturn(new byte[]{}).thenReturn(exception);
492+
when(StorageHelper.InternalStorage.readObject(any(File.class))).thenReturn(exception);
493493

494494
Crashes.setListener(new AbstractCrashesListener() {
495495
@Override
@@ -801,7 +801,7 @@ public void crashInLastSessionError() throws JSONException, IOException, ClassNo
801801
assertNull(Crashes.getLastSessionCrashReport());
802802

803803
/*
804-
* Deserializing fails twice: processing the log from last time as part of the bulk processing.
804+
* De-serializing fails twice: processing the log from last time as part of the bulk processing.
805805
* And loading that same file for exposing it in getLastErrorReport.
806806
*/
807807
verifyStatic(times(2));

sdk/mobile-center-crashes/src/test/java/com/microsoft/azure/mobile/crashes/WrapperSdkExceptionManagerTest.java

Lines changed: 68 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
import com.microsoft.azure.mobile.utils.storage.StorageHelper;
66

77
import org.junit.Before;
8+
import org.junit.Rule;
89
import org.junit.Test;
9-
import org.junit.runner.RunWith;
10-
import org.mockito.Mockito;
11-
import org.powermock.api.mockito.PowerMockito;
1210
import org.powermock.core.classloader.annotations.PrepareForTest;
13-
import org.powermock.modules.junit4.PowerMockRunner;
11+
import org.powermock.modules.junit4.rule.PowerMockRule;
1412

1513
import java.io.File;
1614
import java.io.IOException;
@@ -21,16 +19,24 @@
2119
import static org.mockito.Matchers.any;
2220
import static org.mockito.Matchers.anyString;
2321
import static org.mockito.Matchers.eq;
22+
import static org.mockito.Mockito.never;
23+
import static org.mockito.Mockito.when;
24+
import static org.powermock.api.mockito.PowerMockito.doThrow;
25+
import static org.powermock.api.mockito.PowerMockito.mock;
2426
import static org.powermock.api.mockito.PowerMockito.mockStatic;
2527
import static org.powermock.api.mockito.PowerMockito.verifyStatic;
28+
import static org.powermock.api.mockito.PowerMockito.whenNew;
2629

27-
@RunWith(PowerMockRunner.class)
28-
@PrepareForTest({MobileCenterLog.class, StorageHelper.InternalStorage.class, Crashes.class})
30+
@PrepareForTest({WrapperSdkExceptionManager.class, MobileCenterLog.class, StorageHelper.InternalStorage.class, Crashes.class})
2931
public class WrapperSdkExceptionManagerTest {
3032

33+
@Rule
34+
public PowerMockRule rule = new PowerMockRule();
35+
3136
@Before
3237
public void setUp() {
3338
mockStatic(StorageHelper.InternalStorage.class);
39+
mockStatic(MobileCenterLog.class);
3440
}
3541

3642
@Test
@@ -40,42 +46,85 @@ public void constructWrapperSdkExceptionManager() {
4046

4147
@Test
4248
public void loadWrapperExceptionData() throws Exception {
43-
PowerMockito.doThrow(new IOException()).when(StorageHelper.InternalStorage.class);
49+
doThrow(new IOException()).when(StorageHelper.InternalStorage.class);
4450
StorageHelper.InternalStorage.readObject(any(File.class));
4551
assertNull(WrapperSdkExceptionManager.loadWrapperExceptionData(UUID.randomUUID()));
46-
PowerMockito.doThrow(new ClassNotFoundException()).when(StorageHelper.InternalStorage.class);
52+
doThrow(new ClassNotFoundException()).when(StorageHelper.InternalStorage.class);
4753
StorageHelper.InternalStorage.readObject(any(File.class));
4854
assertNull(WrapperSdkExceptionManager.loadWrapperExceptionData(UUID.randomUUID()));
4955
assertNull(WrapperSdkExceptionManager.loadWrapperExceptionData(null));
5056
}
5157

5258
@Test
53-
public void deleteWrapperExceptionDataWithNullId() throws Exception {
59+
public void deleteWrapperExceptionDataWithNullId() {
60+
61+
/* Delete null does nothing. */
5462
WrapperSdkExceptionManager.deleteWrapperExceptionData(null);
55-
verifyStatic(Mockito.never());
63+
verifyStatic(never());
64+
StorageHelper.InternalStorage.delete(any(File.class));
65+
verifyStatic();
66+
MobileCenterLog.error(eq(Crashes.LOG_TAG), anyString());
67+
}
68+
69+
@Test
70+
public void deleteWrapperExceptionDataWithMissingId() {
71+
72+
/* Delete with file not found does nothing. */
73+
WrapperSdkExceptionManager.deleteWrapperExceptionData(UUID.randomUUID());
74+
verifyStatic(never());
5675
StorageHelper.InternalStorage.delete(any(File.class));
76+
verifyStatic(never());
77+
MobileCenterLog.error(eq(Crashes.LOG_TAG), anyString());
78+
}
5779

80+
@Test
81+
public void deleteWrapperExceptionDataWithLoadingError() throws Exception {
82+
83+
/* Delete with file that cannot be loaded because invalid content should just log an error. */
84+
File file = mock(File.class);
85+
whenNew(File.class).withAnyArguments().thenReturn(file);
86+
when(file.exists()).thenReturn(true);
5887
WrapperSdkExceptionManager.deleteWrapperExceptionData(UUID.randomUUID());
59-
verifyStatic(Mockito.never());
88+
verifyStatic();
6089
StorageHelper.InternalStorage.delete(any(File.class));
90+
verifyStatic();
91+
MobileCenterLog.error(eq(Crashes.LOG_TAG), anyString());
6192
}
6293

6394
@Test
64-
public void saveWrapperExceptionData() throws IOException {
65-
// cannot save with a null uuid
95+
public void saveWrapperExceptionDataNull() throws IOException {
96+
97+
/* Cannot save with a null uuid */
6698
WrapperSdkExceptionManager.saveWrapperExceptionData(null, null);
67-
verifyStatic(Mockito.never());
99+
verifyStatic(never());
68100
StorageHelper.InternalStorage.writeObject(any(File.class), anyString());
101+
verifyStatic();
102+
MobileCenterLog.error(eq(Crashes.LOG_TAG), anyString());
103+
}
69104

70-
// ok to save null if there is a uuid
71-
WrapperSdkExceptionManager.saveWrapperExceptionData(null, UUID.randomUUID()); //save null data
105+
@Test
106+
public void saveWrapperExceptionDataMissingId() throws IOException {
107+
108+
/* Ok to save null if there is a uuid */
109+
WrapperSdkExceptionManager.saveWrapperExceptionData(null, UUID.randomUUID());
72110
verifyStatic();
73111
StorageHelper.InternalStorage.writeObject(any(File.class), any(Serializable.class));
74-
75-
PowerMockito.doThrow(new IOException()).when(StorageHelper.InternalStorage.class);
76-
StorageHelper.InternalStorage.writeObject(any(File.class), anyString());
112+
verifyStatic(never());
113+
MobileCenterLog.error(eq(Crashes.LOG_TAG), anyString(), any(IOException.class));
114+
verifyStatic(never());
115+
MobileCenterLog.error(eq(Crashes.LOG_TAG), anyString());
116+
}
117+
118+
@Test
119+
public void saveWrapperExceptionDataFailing() throws IOException {
120+
121+
/* Save null and test failure. */
122+
doThrow(new IOException()).when(StorageHelper.InternalStorage.class);
123+
StorageHelper.InternalStorage.writeObject(any(File.class), any(Serializable.class));
77124
WrapperSdkExceptionManager.saveWrapperExceptionData(null, UUIDUtils.randomUUID());
78125
verifyStatic();
126+
StorageHelper.InternalStorage.writeObject(any(File.class), anyString());
127+
verifyStatic();
79128
MobileCenterLog.error(eq(Crashes.LOG_TAG), anyString(), any(IOException.class));
80129
}
81130
}

0 commit comments

Comments
 (0)