|
| 1 | +/* |
| 2 | + * Copyright (c) 2015-present, Parse, LLC. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + */ |
| 9 | +package com.parse; |
| 10 | + |
| 11 | +import org.junit.Rule; |
| 12 | +import org.junit.Test; |
| 13 | +import org.junit.rules.TemporaryFolder; |
| 14 | + |
| 15 | +import java.io.ByteArrayOutputStream; |
| 16 | +import java.io.File; |
| 17 | +import java.io.FileWriter; |
| 18 | +import java.io.IOException; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.concurrent.Semaphore; |
| 21 | +import java.util.concurrent.TimeUnit; |
| 22 | + |
| 23 | +import static org.junit.Assert.assertArrayEquals; |
| 24 | +import static org.junit.Assert.assertTrue; |
| 25 | +import static org.junit.Assert.fail; |
| 26 | + |
| 27 | +public class ParseCountingFileHttpBodyTest { |
| 28 | + |
| 29 | + @Rule |
| 30 | + public TemporaryFolder temporaryFolder = new TemporaryFolder(); |
| 31 | + |
| 32 | + @Test |
| 33 | + public void testWriteTo() throws Exception { |
| 34 | + final Semaphore didReportIntermediateProgress = new Semaphore(0); |
| 35 | + final Semaphore finish = new Semaphore(0); |
| 36 | + |
| 37 | + ParseCountingFileHttpBody body = new ParseCountingFileHttpBody( |
| 38 | + makeTestFile(temporaryFolder.getRoot()), new ProgressCallback() { |
| 39 | + Integer maxProgressSoFar = 0; |
| 40 | + @Override |
| 41 | + public void done(Integer percentDone) { |
| 42 | + if (percentDone > maxProgressSoFar) { |
| 43 | + maxProgressSoFar = percentDone; |
| 44 | + assertTrue(percentDone >= 0 && percentDone <= 100); |
| 45 | + |
| 46 | + if (percentDone < 100 && percentDone > 0) { |
| 47 | + didReportIntermediateProgress.release(); |
| 48 | + } else if (percentDone == 100) { |
| 49 | + finish.release(); |
| 50 | + } else if (percentDone == 0) { |
| 51 | + // do nothing |
| 52 | + } else { |
| 53 | + fail("percentDone should be within 0 - 100"); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + // Check content |
| 60 | + ByteArrayOutputStream output = new ByteArrayOutputStream(); |
| 61 | + body.writeTo(output); |
| 62 | + assertArrayEquals(getData().getBytes(), output.toByteArray()); |
| 63 | + // Check progress callback |
| 64 | + assertTrue(didReportIntermediateProgress.tryAcquire(5, TimeUnit.SECONDS)); |
| 65 | + assertTrue(finish.tryAcquire(5, TimeUnit.SECONDS)); |
| 66 | + } |
| 67 | + |
| 68 | + @Test(expected = IllegalArgumentException.class) |
| 69 | + public void testWriteToWithNullOutput() throws Exception { |
| 70 | + ParseCountingFileHttpBody body = new ParseCountingFileHttpBody( |
| 71 | + makeTestFile(temporaryFolder.getRoot()), null); |
| 72 | + body.writeTo(null); |
| 73 | + } |
| 74 | + |
| 75 | + private static String getData() { |
| 76 | + char[] chars = new char[64 << 14]; // 1MB |
| 77 | + Arrays.fill(chars, '1'); |
| 78 | + return new String(chars); |
| 79 | + } |
| 80 | + |
| 81 | + private static File makeTestFile(File root) throws IOException { |
| 82 | + File file = new File(root, "test"); |
| 83 | + FileWriter writer = new FileWriter(file); |
| 84 | + writer.write(getData()); |
| 85 | + writer.close(); |
| 86 | + return file; |
| 87 | + } |
| 88 | +} |
0 commit comments