|
8 | 8 | */
|
9 | 9 | package com.parse;
|
10 | 10 |
|
| 11 | +import static org.hamcrest.core.Is.is; |
11 | 12 | import static org.junit.Assert.assertEquals;
|
12 | 13 | import static org.junit.Assert.assertFalse;
|
| 14 | +import static org.junit.Assert.assertNotNull; |
13 | 15 | import static org.junit.Assert.assertNull;
|
14 | 16 | import static org.junit.Assert.assertSame;
|
| 17 | +import static org.junit.Assert.assertThat; |
15 | 18 | import static org.junit.Assert.assertTrue;
|
16 | 19 | import static org.mockito.ArgumentMatchers.nullable;
|
17 | 20 | import static org.mockito.Matchers.any;
|
|
46 | 49 | import org.robolectric.RuntimeEnvironment;
|
47 | 50 |
|
48 | 51 | @RunWith(RobolectricTestRunner.class)
|
49 |
| -public class ParseObjectTest { |
| 52 | +public class ParseObjectTest extends ResetPluginsParseTest { |
50 | 53 |
|
51 | 54 | @Rule public final ExpectedException thrown = ExpectedException.none();
|
52 | 55 |
|
@@ -80,16 +83,17 @@ private static TaskCompletionSource<Void> mockObjectControllerForDelete() {
|
80 | 83 | }
|
81 | 84 |
|
82 | 85 | @Before
|
83 |
| - public void setUp() { |
| 86 | + public void setUp() throws Exception { |
| 87 | + super.setUp(); |
84 | 88 | ParseFieldOperations.registerDefaultDecoders(); // to test JSON / Parcel decoding
|
85 | 89 | }
|
86 | 90 |
|
87 | 91 | // region testRevert
|
88 | 92 |
|
89 | 93 | @After
|
90 |
| - public void tearDown() { |
91 |
| - ParseCorePlugins.getInstance().reset(); |
92 |
| - ParsePlugins.reset(); |
| 94 | + public void tearDown() throws Exception { |
| 95 | + super.tearDown(); |
| 96 | + Parse.destroy(); |
93 | 97 | }
|
94 | 98 |
|
95 | 99 | @Test
|
@@ -159,6 +163,118 @@ public void testFromJsonWithLdsStackOverflow() throws JSONException {
|
159 | 163 |
|
160 | 164 | // endregion
|
161 | 165 |
|
| 166 | + @Test |
| 167 | + public void testSaveCustomObjectIdMissing() { |
| 168 | + // Mocked to let save work |
| 169 | + mockCurrentUserController(); |
| 170 | + |
| 171 | + Parse.Configuration configuration = |
| 172 | + new Parse.Configuration.Builder(RuntimeEnvironment.application) |
| 173 | + .applicationId(BuildConfig.LIBRARY_PACKAGE_NAME) |
| 174 | + .server("https://api.parse.com/1") |
| 175 | + .enableLocalDataStore() |
| 176 | + .allowCustomObjectId() |
| 177 | + .build(); |
| 178 | + ParsePlugins plugins = mock(ParsePlugins.class); |
| 179 | + when(plugins.configuration()).thenReturn(configuration); |
| 180 | + when(plugins.applicationContext()).thenReturn(RuntimeEnvironment.application); |
| 181 | + Parse.initialize(configuration, plugins); |
| 182 | + |
| 183 | + ParseObject object = new ParseObject("TestObject"); |
| 184 | + try { |
| 185 | + object.save(); |
| 186 | + } catch (ParseException e) { |
| 187 | + assertEquals(e.getCode(), 104); |
| 188 | + assertThat(e.getMessage(), is("ObjectId must not be null")); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + @Test |
| 193 | + public void testSaveCustomObjectIdNotMissing() { |
| 194 | + // Mocked to let save work |
| 195 | + mockCurrentUserController(); |
| 196 | + |
| 197 | + Parse.Configuration configuration = |
| 198 | + new Parse.Configuration.Builder(RuntimeEnvironment.application) |
| 199 | + .applicationId(BuildConfig.LIBRARY_PACKAGE_NAME) |
| 200 | + .server("https://api.parse.com/1") |
| 201 | + .enableLocalDataStore() |
| 202 | + .allowCustomObjectId() |
| 203 | + .build(); |
| 204 | + ParsePlugins plugins = mock(ParsePlugins.class); |
| 205 | + when(plugins.configuration()).thenReturn(configuration); |
| 206 | + when(plugins.applicationContext()).thenReturn(RuntimeEnvironment.application); |
| 207 | + Parse.initialize(configuration, plugins); |
| 208 | + |
| 209 | + ParseObject object = new ParseObject("TestObject"); |
| 210 | + object.setObjectId("ABCDEF123456"); |
| 211 | + |
| 212 | + ParseException exception = null; |
| 213 | + try { |
| 214 | + object.save(); |
| 215 | + } catch (ParseException e) { |
| 216 | + exception = e; |
| 217 | + } |
| 218 | + assertNull(exception); |
| 219 | + } |
| 220 | + |
| 221 | + @Test |
| 222 | + public void testSaveEventuallyCustomObjectIdMissing() { |
| 223 | + // Mocked to let save work |
| 224 | + mockCurrentUserController(); |
| 225 | + |
| 226 | + Parse.Configuration configuration = |
| 227 | + new Parse.Configuration.Builder(RuntimeEnvironment.application) |
| 228 | + .applicationId(BuildConfig.LIBRARY_PACKAGE_NAME) |
| 229 | + .server("https://api.parse.com/1") |
| 230 | + .enableLocalDataStore() |
| 231 | + .allowCustomObjectId() |
| 232 | + .build(); |
| 233 | + ParsePlugins plugins = ParseTestUtils.mockParsePlugins(configuration); |
| 234 | + Parse.initialize(configuration, plugins); |
| 235 | + |
| 236 | + ParseObject object = new ParseObject("TestObject"); |
| 237 | + object.saveEventually( |
| 238 | + new SaveCallback() { |
| 239 | + @Override |
| 240 | + public void done(ParseException e) { |
| 241 | + assertNotNull(e); |
| 242 | + assertEquals(e.getCode(), 104); |
| 243 | + assertThat(e.getMessage(), is("ObjectId must not be null")); |
| 244 | + } |
| 245 | + }); |
| 246 | + |
| 247 | + Parse.setLocalDatastore(null); |
| 248 | + } |
| 249 | + |
| 250 | + @Test |
| 251 | + public void testSaveEventuallyCustomObjectIdNotMissing() throws ParseException { |
| 252 | + // Mocked to let save work |
| 253 | + mockCurrentUserController(); |
| 254 | + |
| 255 | + Parse.Configuration configuration = |
| 256 | + new Parse.Configuration.Builder(RuntimeEnvironment.application) |
| 257 | + .applicationId(BuildConfig.LIBRARY_PACKAGE_NAME) |
| 258 | + .server("https://api.parse.com/1") |
| 259 | + .enableLocalDataStore() |
| 260 | + .allowCustomObjectId() |
| 261 | + .build(); |
| 262 | + ParsePlugins plugins = ParseTestUtils.mockParsePlugins(configuration); |
| 263 | + Parse.initialize(configuration, plugins); |
| 264 | + |
| 265 | + ParseObject object = new ParseObject("TestObject"); |
| 266 | + object.setObjectId("ABCDEF123456"); |
| 267 | + object.saveEventually( |
| 268 | + new SaveCallback() { |
| 269 | + @Override |
| 270 | + public void done(ParseException e) { |
| 271 | + assertNull(e); |
| 272 | + } |
| 273 | + }); |
| 274 | + |
| 275 | + Parse.setLocalDatastore(null); |
| 276 | + } |
| 277 | + |
162 | 278 | // region testGetter
|
163 | 279 |
|
164 | 280 | @Test
|
|
0 commit comments