Skip to content
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

Avoid adding extra comma separator if File input is empty or null #284

Merged
merged 5 commits into from
Apr 11, 2018
Merged
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
46 changes: 46 additions & 0 deletions sdk/src/androidTest/java/com/bugsnag/android/JsonStreamTest.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package com.bugsnag.android;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;

Expand All @@ -21,11 +26,20 @@ public class JsonStreamTest {

private StringWriter writer;
private JsonStream stream;
private File file;

/**
* Deletes a file in the cache directory if it already exists from previous test cases
*
* @throws Exception if setup failed
*/
@Before
public void setUp() throws Exception {
writer = new StringWriter();
stream = new JsonStream(writer);
File cacheDir = InstrumentationRegistry.getContext().getCacheDir();
file = new File(cacheDir, "whoops");
file.delete();
}

@Test
Expand Down Expand Up @@ -61,4 +75,36 @@ public void testSaneValues() throws JSONException, IOException {
assertEquals(123, json.getInt("int"));
assertEquals(123L, json.getLong("long"));
}

@Test
public void testEmptyFileValue() throws Throwable {
file.createNewFile();
stream.beginArray();
stream.value(file);
stream.value(file);
stream.endArray();
assertEquals("[]", writer.toString());
}

@Test
public void testNullFileValue() throws Throwable {
File file = null;
stream.beginArray();
stream.value(file);
stream.value(file);
stream.endArray();
assertEquals("[]", writer.toString());
}

@Test
public void testDeletedFile() throws Throwable {
file.createNewFile();
file.delete();
stream.beginArray();
stream.value(file);
stream.value(file);
stream.endArray();
assertEquals("[]", writer.toString());
}

}
4 changes: 4 additions & 0 deletions sdk/src/main/java/com/bugsnag/android/JsonStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public void value(@Nullable Streamable streamable) throws IOException {
* Writes a File (its content) into the stream
*/
public void value(@NonNull File file) throws IOException {
if (file == null || file.length() <= 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need the null check if @NonNull is used above?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NonNull doesn't enforce anything in Java unfortunately. The IDE would highlight any null parameters with a warning, but developers could choose to ignore this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, fair enough.

return;
}

super.flush();
beforeValue(false); // add comma if in array

Expand Down