diff --git a/.gitignore b/.gitignore index 8a0096f1a..aef945267 100644 --- a/.gitignore +++ b/.gitignore @@ -46,3 +46,4 @@ jacoco.exec node_modules/ npm-debug.log *.hprof +/.vscode diff --git a/parse/build.gradle b/parse/build.gradle index 9253396d9..31b06fdde 100644 --- a/parse/build.gradle +++ b/parse/build.gradle @@ -15,6 +15,16 @@ android { testOptions { unitTests { includeAndroidResources = true + all { + jvmArgs '-Dnet.bytebuddy.experimental=true' + jvmArgs '--add-opens=java.base/java.lang=ALL-UNNAMED' + jvmArgs '--add-opens=java.base/java.lang.reflect=ALL-UNNAMED' + jvmArgs '--add-opens=java.base/java.io=ALL-UNNAMED' + jvmArgs '--add-opens=java.base/java.util=ALL-UNNAMED' + jvmArgs '--add-opens=java.base/java.text=ALL-UNNAMED' + jvmArgs '--add-opens=java.base/java.util.concurrent=ALL-UNNAMED' + jvmArgs '--add-opens=java.base/java.net=ALL-UNNAMED' + } } } diff --git a/parse/src/test/java/com/parse/ParseCountingUriHttpBodyTest.java b/parse/src/test/java/com/parse/ParseCountingUriHttpBodyTest.java index 777851e53..99319ed85 100644 --- a/parse/src/test/java/com/parse/ParseCountingUriHttpBodyTest.java +++ b/parse/src/test/java/com/parse/ParseCountingUriHttpBodyTest.java @@ -13,6 +13,7 @@ import static org.junit.Assert.fail; import android.net.Uri; +import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileWriter; @@ -20,14 +21,44 @@ import java.util.Arrays; import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; +import org.junit.After; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.Shadows; +import org.robolectric.shadows.ShadowContentResolver; +@RunWith(RobolectricTestRunner.class) public class ParseCountingUriHttpBodyTest { @Rule public final TemporaryFolder temporaryFolder = new TemporaryFolder(); + @Before + public void setUp() { + ParseCorePlugins.getInstance().reset(); + ParsePlugins.reset(); + + Parse.Configuration configuration = + new Parse.Configuration.Builder(RuntimeEnvironment.application) + .applicationId("test") + .server("https://api.parse.com/1") + .build(); + + ParsePlugins plugins = ParseTestUtils.mockParsePlugins(configuration); + Parse.initialize(configuration, plugins); + } + + @After + public void tearDown() { + ParseCorePlugins.getInstance().reset(); + ParsePlugins.reset(); + Parse.destroy(); + } + private static String getData() { char[] chars = new char[64 << 14]; // 1MB Arrays.fill(chars, '1'); @@ -47,9 +78,18 @@ public void testWriteTo() throws Exception { final Semaphore didReportIntermediateProgress = new Semaphore(0); final Semaphore finish = new Semaphore(0); + Uri testUri = makeTestUri(temporaryFolder.getRoot()); + + // Register the Uri with Robolectric's ShadowContentResolver + String testData = getData(); + ShadowContentResolver shadowContentResolver = + Shadows.shadowOf(RuntimeEnvironment.application.getContentResolver()); + shadowContentResolver.registerInputStream(testUri, + new ByteArrayInputStream(testData.getBytes())); + ParseCountingUriHttpBody body = new ParseCountingUriHttpBody( - makeTestUri(temporaryFolder.getRoot()), + testUri, new ProgressCallback() { Integer maxProgressSoFar = 0; @@ -75,7 +115,7 @@ public void done(Integer percentDone) { // Check content ByteArrayOutputStream output = new ByteArrayOutputStream(); body.writeTo(output); - assertArrayEquals(getData().getBytes(), output.toByteArray()); + assertArrayEquals(testData.getBytes(), output.toByteArray()); // Check progress callback assertTrue(didReportIntermediateProgress.tryAcquire(5, TimeUnit.SECONDS)); assertTrue(finish.tryAcquire(5, TimeUnit.SECONDS)); @@ -83,8 +123,17 @@ public void done(Integer percentDone) { @Test(expected = IllegalArgumentException.class) public void testWriteToWithNullOutput() throws Exception { + Uri testUri = makeTestUri(temporaryFolder.getRoot()); + + // Register the Uri with Robolectric's ShadowContentResolver + String testData = getData(); + ShadowContentResolver shadowContentResolver = + Shadows.shadowOf(RuntimeEnvironment.application.getContentResolver()); + shadowContentResolver.registerInputStream(testUri, + new ByteArrayInputStream(testData.getBytes())); + ParseCountingUriHttpBody body = - new ParseCountingUriHttpBody(makeTestUri(temporaryFolder.getRoot()), null); + new ParseCountingUriHttpBody(testUri, null); body.writeTo(null); } } diff --git a/parse/src/test/java/com/parse/ParseFileControllerTest.java b/parse/src/test/java/com/parse/ParseFileControllerTest.java index 7bc9c8298..a86c3a61d 100644 --- a/parse/src/test/java/com/parse/ParseFileControllerTest.java +++ b/parse/src/test/java/com/parse/ParseFileControllerTest.java @@ -39,6 +39,9 @@ import org.junit.rules.TemporaryFolder; import org.junit.runner.RunWith; import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.Shadows; +import org.robolectric.shadows.ShadowContentResolver; // For org.json @RunWith(RobolectricTestRunner.class) @@ -48,6 +51,17 @@ public class ParseFileControllerTest { @Before public void setUp() throws MalformedURLException { + ParseCorePlugins.getInstance().reset(); + ParsePlugins.reset(); + + Parse.Configuration configuration = + new Parse.Configuration.Builder(RuntimeEnvironment.application) + .applicationId("test") + .server("https://api.parse.com/1") + .build(); + + ParsePlugins plugins = ParseTestUtils.mockParsePlugins(configuration); + Parse.initialize(configuration, plugins); ParseRESTCommand.server = new URL("https://api.parse.com/1"); } @@ -56,6 +70,9 @@ public void tearDown() { // TODO(grantland): Remove once we no longer rely on retry logic. ParseRequest.setDefaultInitialRetryDelay(ParseRequest.DEFAULT_INITIAL_RETRY_DELAY); ParseRESTCommand.server = null; + ParseCorePlugins.getInstance().reset(); + ParsePlugins.reset(); + Parse.destroy(); } @Test @@ -221,6 +238,13 @@ public void testSaveAsyncSuccessWithUri() throws Exception { File file = new File(root, "test"); ParseFileUtils.writeStringToFile(file, "content", "UTF-8"); Uri uri = Uri.fromFile(file); + + // Register the Uri with Robolectric's ShadowContentResolver + ShadowContentResolver shadowContentResolver = + Shadows.shadowOf(RuntimeEnvironment.application.getContentResolver()); + shadowContentResolver.registerInputStream(uri, + new ByteArrayInputStream("content".getBytes())); + ParseFile.State state = new ParseFile.State.Builder().name("file_name").mimeType("mime_type").build(); Task task = controller.saveAsync(state, uri, null, null, null); diff --git a/parse/src/test/java/com/parse/ParseUriHttpBodyTest.java b/parse/src/test/java/com/parse/ParseUriHttpBodyTest.java index 8e472195d..aab0ba15f 100644 --- a/parse/src/test/java/com/parse/ParseUriHttpBodyTest.java +++ b/parse/src/test/java/com/parse/ParseUriHttpBodyTest.java @@ -12,16 +12,47 @@ import static org.junit.Assert.assertEquals; import android.net.Uri; +import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; +import org.junit.After; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.Shadows; +import org.robolectric.shadows.ShadowContentResolver; +@RunWith(RobolectricTestRunner.class) public class ParseUriHttpBodyTest { @Rule public final TemporaryFolder temporaryFolder = new TemporaryFolder(); + @Before + public void setUp() { + ParseCorePlugins.getInstance().reset(); + ParsePlugins.reset(); + + Parse.Configuration configuration = + new Parse.Configuration.Builder(RuntimeEnvironment.application) + .applicationId("test") + .server("https://api.parse.com/1") + .build(); + + ParsePlugins plugins = ParseTestUtils.mockParsePlugins(configuration); + Parse.initialize(configuration, plugins); + } + + @After + public void tearDown() { + ParseCorePlugins.getInstance().reset(); + ParsePlugins.reset(); + Parse.destroy(); + } + @Test public void testInitializeWithUri() throws IOException { byte[] content = {1, 1, 1, 1, 1}; @@ -29,6 +60,13 @@ public void testInitializeWithUri() throws IOException { File file = temporaryFolder.newFile("name"); ParseFileUtils.writeByteArrayToFile(file, content); Uri uri = Uri.fromFile(file); + + // Register the Uri with Robolectric's ShadowContentResolver + ShadowContentResolver shadowContentResolver = + Shadows.shadowOf(RuntimeEnvironment.application.getContentResolver()); + shadowContentResolver.registerInputStream(uri, + new ByteArrayInputStream(content)); + ParseUriHttpBody body = new ParseUriHttpBody(uri, contentType); assertArrayEquals(content, ParseIOUtils.toByteArray(body.getContent())); assertEquals(contentType, body.getContentType()); @@ -42,6 +80,13 @@ public void testWriteTo() throws IOException { File file = temporaryFolder.newFile("name"); ParseFileUtils.writeStringToFile(file, content, "UTF-8"); Uri uri = Uri.fromFile(file); + + // Register the Uri with Robolectric's ShadowContentResolver + ShadowContentResolver shadowContentResolver = + Shadows.shadowOf(RuntimeEnvironment.application.getContentResolver()); + shadowContentResolver.registerInputStream(uri, + new ByteArrayInputStream(content.getBytes())); + ParseUriHttpBody body = new ParseUriHttpBody(uri, contentType); // Check content