Skip to content

Commit e25570a

Browse files
committed
Add test case
1 parent e60f35a commit e25570a

File tree

2 files changed

+116
-1
lines changed

2 files changed

+116
-1
lines changed

Parse/src/main/java/com/parse/ParseFileUtils.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,11 @@ public static String readFileToString(File file, String encoding) throws IOExcep
519519
return readFileToString(file, Charset.forName(encoding));
520520
}
521521

522+
public static void writeStringToFile(File file, String string)
523+
throws IOException {
524+
writeByteArrayToFile(file, string.getBytes());
525+
}
526+
522527
public static void writeStringToFile(File file, String string, Charset encoding)
523528
throws IOException {
524529
writeByteArrayToFile(file, string.getBytes(encoding));

Parse/src/test/java/com/parse/ParseFileTest.java

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,25 @@
1313
import org.junit.Rule;
1414
import org.junit.Test;
1515
import org.junit.rules.TemporaryFolder;
16+
import org.mockito.ArgumentCaptor;
1617
import org.mockito.Matchers;
1718

1819
import java.io.File;
20+
import java.io.InputStream;
1921
import java.util.Arrays;
2022
import java.util.List;
2123

2224
import bolts.Task;
2325

26+
import static org.junit.Assert.assertArrayEquals;
2427
import static org.junit.Assert.assertEquals;
2528
import static org.junit.Assert.assertFalse;
2629
import static org.junit.Assert.assertTrue;
2730
import static org.mockito.Matchers.any;
31+
import static org.mockito.Matchers.anyString;
2832
import static org.mockito.Mockito.mock;
2933
import static org.mockito.Mockito.never;
34+
import static org.mockito.Mockito.times;
3035
import static org.mockito.Mockito.verify;
3136
import static org.mockito.Mockito.when;
3237

@@ -163,7 +168,112 @@ public void testSaveAsyncCancelled() throws Exception {
163168

164169
//endregion
165170

166-
// TODO(grantland): testGetDataAsync (same as saveAsync)
171+
172+
//region testGetDataAsync
173+
174+
@Test
175+
public void testGetDataAsyncSuccess() throws Exception {
176+
String content = "content";
177+
File file = temporaryFolder.newFile("test");
178+
ParseFileUtils.writeStringToFile(file, content);
179+
ParseFileController controller = mock(ParseFileController.class);
180+
when(controller.fetchAsync(
181+
any(ParseFile.State.class),
182+
any(String.class),
183+
any(ProgressCallback.class),
184+
Matchers.<Task<Void>>any())).thenReturn(Task.forResult(file));
185+
ParseCorePlugins.getInstance().registerFileController(controller);
186+
187+
String url = "url";
188+
ParseFile.State state = new ParseFile.State.Builder()
189+
.url(url)
190+
.build();
191+
ParseFile parseFile = new ParseFile(state);
192+
193+
byte[] data = ParseTaskUtils.wait(parseFile.getDataInBackground());
194+
195+
// Verify controller get the correct data
196+
ArgumentCaptor<ParseFile.State> stateCaptor = ArgumentCaptor.forClass(ParseFile.State.class);
197+
verify(controller, times(1)).fetchAsync(
198+
stateCaptor.capture(),
199+
anyString(),
200+
any(ProgressCallback.class),
201+
Matchers.<Task<Void>>any()
202+
);
203+
assertEquals(url, stateCaptor.getValue().url());
204+
// Verify the data we get is correct
205+
assertArrayEquals(content.getBytes(), data);
206+
}
207+
208+
@Test
209+
public void testGetDataStreamAsyncSuccess() throws Exception {
210+
String content = "content";
211+
File file = temporaryFolder.newFile("test");
212+
ParseFileUtils.writeStringToFile(file, content);
213+
ParseFileController controller = mock(ParseFileController.class);
214+
when(controller.fetchAsync(
215+
any(ParseFile.State.class),
216+
any(String.class),
217+
any(ProgressCallback.class),
218+
Matchers.<Task<Void>>any())).thenReturn(Task.forResult(file));
219+
ParseCorePlugins.getInstance().registerFileController(controller);
220+
221+
String url = "url";
222+
ParseFile.State state = new ParseFile.State.Builder()
223+
.url(url)
224+
.build();
225+
ParseFile parseFile = new ParseFile(state);
226+
227+
InputStream dataStream = ParseTaskUtils.wait(parseFile.getDataStreamInBackground());
228+
229+
// Verify controller get the correct data
230+
ArgumentCaptor<ParseFile.State> stateCaptor = ArgumentCaptor.forClass(ParseFile.State.class);
231+
verify(controller, times(1)).fetchAsync(
232+
stateCaptor.capture(),
233+
anyString(),
234+
any(ProgressCallback.class),
235+
Matchers.<Task<Void>>any()
236+
);
237+
assertEquals(url, stateCaptor.getValue().url());
238+
// Verify the data we get is correct
239+
assertArrayEquals(content.getBytes(), ParseIOUtils.toByteArray(dataStream));
240+
}
241+
242+
@Test
243+
public void testGetFileAsyncSuccess() throws Exception {
244+
String content = "content";
245+
File file = temporaryFolder.newFile("test");
246+
ParseFileUtils.writeStringToFile(file, content);
247+
ParseFileController controller = mock(ParseFileController.class);
248+
when(controller.fetchAsync(
249+
any(ParseFile.State.class),
250+
any(String.class),
251+
any(ProgressCallback.class),
252+
Matchers.<Task<Void>>any())).thenReturn(Task.forResult(file));
253+
ParseCorePlugins.getInstance().registerFileController(controller);
254+
255+
String url = "url";
256+
ParseFile.State state = new ParseFile.State.Builder()
257+
.url(url)
258+
.build();
259+
ParseFile parseFile = new ParseFile(state);
260+
261+
File fetchedFile = ParseTaskUtils.wait(parseFile.getFileInBackground());
262+
263+
// Verify controller get the correct data
264+
ArgumentCaptor<ParseFile.State> stateCaptor = ArgumentCaptor.forClass(ParseFile.State.class);
265+
verify(controller, times(1)).fetchAsync(
266+
stateCaptor.capture(),
267+
anyString(),
268+
any(ProgressCallback.class),
269+
Matchers.<Task<Void>>any()
270+
);
271+
assertEquals(url, stateCaptor.getValue().url());
272+
// Verify the data we get is correct
273+
assertArrayEquals(content.getBytes(), ParseFileUtils.readFileToByteArray(fetchedFile));
274+
}
275+
276+
//endregion
167277

168278
@Test
169279
public void testTaskQueuedMethods() throws Exception {

0 commit comments

Comments
 (0)