Skip to content
This repository has been archived by the owner on Mar 10, 2022. It is now read-only.

Commit

Permalink
Merge dd234d0 into e9ab0a4
Browse files Browse the repository at this point in the history
  • Loading branch information
jayahariv committed Feb 7, 2019
2 parents e9ab0a4 + dd234d0 commit 73e90dc
Showing 1 changed file with 79 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
Expand All @@ -33,6 +35,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class BlobTest extends BaseTest {
final static String kBlobTestBlob1 = "i'm blob";
Expand All @@ -41,6 +44,9 @@ public class BlobTest extends BaseTest {
@Rule
public ExpectedException thrown = ExpectedException.none();

@Rule
public TemporaryFolder folder= new TemporaryFolder();

@Test
public void testEquals() throws CouchbaseLiteException {

Expand Down Expand Up @@ -195,42 +201,95 @@ public void testGetNonCachedContent6MBFile() throws IOException, CouchbaseLiteEx

@Test
public void testBlobFromFileURL() throws Exception {
String contentType = "application/json";

String contentType = "image/png";
Blob blob = null;
URL url = null;
File path = folder.newFile("attachment.png");
InputStream is = getAsset("attachment.png");

try {
byte[] bytes = IOUtils.toByteArray(is);
FileOutputStream fos = new FileOutputStream(path);
fos.write(bytes);
fos.close();

blob = new Blob(contentType, path.toURI().toURL());
} catch(Exception e) {
fail(" failed at " + e);
} finally {
is.close();
}

byte[] bytes = IOUtils.toByteArray(path);
byte[] content = blob.getContent();
assertTrue(Arrays.equals(content, bytes));

thrown.expect(IllegalArgumentException.class);
blob = new Blob(contentType, url);
blob = new Blob(null, url);

String assetName = "iTunesMusicLibrary.json";
final File path = new File(
context.getFilesDir(),
"/assets/" + assetName
);
thrown.expect(IllegalArgumentException.class);
blob = new Blob(contentType, (URL) null);

thrown.expect(IllegalArgumentException.class);
blob = new Blob(null, path.toURI().toURL());
blob = new Blob(contentType, new URL("http://java.sun.com"));
}

@Test
public void testBlobReadFunctions() throws Exception {
byte[] bytes;
InputStream is = getAsset(assetName);

InputStream is = getAsset("iTunesMusicLibrary.json");
try {
bytes = IOUtils.toByteArray(is);
} finally {
is.close();
}

blob = new Blob(contentType, path.toURI().toURL());
assertEquals(blob.getContent(), bytes);
assertEquals(blob.getContent().hashCode(), bytes.hashCode());
assertEquals(blob.getContentStream().read(), bytes[0]);
Blob blob = new Blob("application/json", bytes);
try {
String blobToString = "Blob[application/json; 6560 KB]";
assertEquals(blob.toString(), blobToString);
assertEquals(blob.getContentStream().read(), bytes[0]);

blob = new Blob("application/json", bytes);
byte[] bytesReadFromBlob = new byte[bytes.length];
blob.getContentStream().read(bytesReadFromBlob, 0, bytes.length);
assertTrue(Arrays.equals(bytesReadFromBlob, bytes));

blob = new Blob("application/json", bytes);
InputStream iStream = blob.getContentStream();
iStream.skip(2);
assertEquals(iStream.read(), bytes[2]);
} catch(Exception e) {
fail("failed when reading the blobs " + e);
}
}

byte[] bytesReadFromBlob = new byte[bytes.length];
blob.getContentStream().read(bytesReadFromBlob, 0, bytes.length);
assertEquals(bytesReadFromBlob, bytes);
@Test
public void testBlobConstructorsWithEmptyArgs() throws Exception {
byte[] bytes;
String contentType = "image/png";

InputStream is = getAsset("attachment.png");
try {
bytes = IOUtils.toByteArray(is);
} finally {
is.close();
}

InputStream iStream = blob.getContentStream();
iStream.skip(2);
assertEquals(iStream.read(), bytes[2]);
thrown.expect(IllegalArgumentException.class);
Blob blob = new Blob(null, bytes);

thrown.expect(IllegalArgumentException.class);
blob = new Blob(contentType, (byte[]) null);

thrown.expect(IllegalArgumentException.class);
blob = new Blob(null, is);

thrown.expect(IllegalArgumentException.class);
blob = new Blob(contentType, (InputStream) null);

thrown.expect(IllegalArgumentException.class);
blob = new Blob(contentType, (InputStream) null);
}
}

0 comments on commit 73e90dc

Please sign in to comment.