Skip to content

test: Various test fixes #1226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ jacoco.exec
node_modules/
npm-debug.log
*.hprof
/.vscode
10 changes: 10 additions & 0 deletions parse/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
}

Expand Down
55 changes: 52 additions & 3 deletions parse/src/test/java/com/parse/ParseCountingUriHttpBodyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,52 @@
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;
import java.io.IOException;
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');
Expand All @@ -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;

Expand All @@ -75,16 +115,25 @@ 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));
}

@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);
}
}
24 changes: 24 additions & 0 deletions parse/src/test/java/com/parse/ParseFileControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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");
}

Expand All @@ -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
Expand Down Expand Up @@ -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<ParseFile.State> task = controller.saveAsync(state, uri, null, null, null);
Expand Down
45 changes: 45 additions & 0 deletions parse/src/test/java/com/parse/ParseUriHttpBodyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,61 @@
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};
String contentType = "application/json";
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());
Expand All @@ -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
Expand Down
Loading