Skip to content

Commit

Permalink
Merge pull request #32 from smartdevicelink/release/1.2.0
Browse files Browse the repository at this point in the history
Release 1.2.0
  • Loading branch information
jacobkeeler authored May 21, 2019
2 parents a0edb48 + 3e96c72 commit 5e79ef2
Show file tree
Hide file tree
Showing 36 changed files with 6,384 additions and 1,130 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
*.so
*.la
*.lo
*.log
*.trs
sample
Makefile
.deps/
Expand All @@ -10,3 +12,5 @@ Makefile
/config.*
/libtool
/stamp-h1
/test/bson_object_test
/test/bson_util_test
2 changes: 1 addition & 1 deletion BiSON.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Pod::Spec.new do |s|
# ――― Spec Metadata ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #

s.name = "BiSON"
s.version = "1.1.1"
s.version = "1.2.0"
s.summary = "A portable BSON C library"


Expand Down
13 changes: 6 additions & 7 deletions BsonJavaPort/bson_java_port/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ apply plugin: 'com.android.library'


android {
compileSdkVersion 21
buildToolsVersion "27.0.3"
compileSdkVersion 28
defaultConfig {
minSdkVersion 8
targetSdkVersion 19
versionCode 1
versionName "1.1.1"
targetSdkVersion 26
versionCode 2
versionName "1.2.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
ndk{
abiFilters "armeabi-v7a", "arm64-v8a", "x86", "x86_64"
Expand Down Expand Up @@ -43,8 +42,8 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
package com.livio.bsonjavaport;

import android.test.AndroidTestCase;

import com.livio.BSON.BsonEncoder;

import junit.framework.TestCase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Set;

/**
* Instrumentation test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class BsonEncoderTests extends AndroidTestCase{
public class BsonEncoderTests extends TestCase {

private HashMap<String, Object> testMapA;
private HashMap<String, Object> testMapB;
private HashMap<String, Object> testMapC;
private byte[] testMapAbytes, testMapBbytes;

public void setUp() throws Exception{
Expand Down Expand Up @@ -52,6 +54,41 @@ public void setUp() throws Exception{
"00" +
"00"
);

testMapC = new HashMap<>();

HashMap<String, Object> map2 = new HashMap<>();
HashMap<String, Object> map3 = new HashMap<>();
ArrayList<Object> list2 = new ArrayList<>();
ArrayList<Object> list3 = new ArrayList<>();

testMapC.put("correct", true);
testMapC.put("one", 64);
testMapC.put("two", 2.5);
testMapC.put("chars", "aaaaaaaaaaaa");

map2.put("a", 604);
map2.put("b", "AnotherString");
map2.put("c", 2.45);

testMapC.put("MapTest", map2);

list2.add(23);
list2.add(5.4);
list2.add("A string");

map3.put("i", 64);
map3.put("Test", 4);

list3.add(235);
list3.add(5.54);
list3.add("AString");

map3.put("secondarray", list3);

list2.add(map3);

testMapC.put("ArrayTest", list2);
}

public void testEncoding(){
Expand All @@ -60,7 +97,7 @@ public void testEncoding(){
try {
assertEquals(observedMapAbytes[i], testMapAbytes[i]);
}catch (Exception e){
assert(false);
fail();
}
}

Expand All @@ -69,7 +106,7 @@ public void testEncoding(){
try {
assertEquals(observedMapBbytes[i], testMapBbytes[i]);
}catch (Exception e){
assert(false);
fail();
}
}
}
Expand All @@ -78,8 +115,22 @@ public void testDecoding(){
HashMap<String, Object> decodedMapA = BsonEncoder.decodeFromBytes(testMapAbytes);
HashMap<String, Object> decodedMapB = BsonEncoder.decodeFromBytes(testMapBbytes);

assert(compareHashMaps(testMapA, decodedMapA));
assert(compareHashMaps(testMapB, decodedMapB));
assertTrue(compareHashMaps(testMapA, decodedMapA));
assertTrue(compareHashMaps(testMapB, decodedMapB));
}

public void testDecodingRandomData() {
// Checking for proper handling of invalid data
byte[] randomBytes = new byte[200];
new Random().nextBytes(randomBytes);
BsonEncoder.decodeFromBytes(randomBytes);
}

public void testEncodeDecodeConsistency() {
// Test nested objects and arrays
byte[] bytes = BsonEncoder.encodeToBytes(testMapC);
HashMap<String, Object> outMap = BsonEncoder.decodeFromBytes(bytes);
assertEquals(testMapC, outMap);
}

private boolean compareHashMaps(HashMap<String,Object> testMap, HashMap<String,Object> obsvMap){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,105 +8,130 @@ public class BsonEncoder {
static {
System.loadLibrary("bson-c-lib");
}
@SuppressWarnings("unchecked")
public static byte[] encodeToBytes(HashMap<String, Object> map) throws ClassCastException {
long bsonRef = buildBsonObject(map);
byte[] bytes = bson_object_to_bytes(bsonRef);

public static byte[] encodeToBytes(HashMap<String, Object> map) throws ClassCastException {
long bsonRef = initializeBsonObject();
long arrayRef = -1;

for (String key : map.keySet()) {
Object value = map.get(key);
if (value instanceof List){
arrayRef = buildBsonArray((List<Object>) value);
bson_object_put_array(bsonRef, key, arrayRef);
} else if (value instanceof Integer) {
bson_object_put_int32(bsonRef, key, (Integer) value);
} else if (value instanceof Long) {
bson_object_put_int64(bsonRef, key, (Long) value);
} else if (value instanceof String) {
bson_object_put_string(bsonRef, key, (String) value);
} else if (value instanceof Boolean) {
bson_object_put_bool(bsonRef, key, (Boolean) value);
} else if (value instanceof Double) {
bson_object_put_double(bsonRef, key, (Double) value);
}
}

byte[] bytes = bson_object_to_bytes(bsonRef);

deinitializeBsonObject(bsonRef);

return bytes;
}
deinitializeBsonObject(bsonRef);

public static HashMap<String, Object> decodeFromBytes(byte[] bytes){
return bytes;
}

HashMap<String, Object> map = new HashMap<String, Object>();
public static HashMap<String, Object> decodeFromBytes(byte[] bytes){

long bsonRef = bson_object_from_bytes(bytes);
map = bson_object_get_hashmap(bsonRef);
return map;
}
HashMap<String, Object> map = new HashMap<String, Object>();

private static long buildBsonArray(List<Object> elements) {

long bsonRef = initializeBsonArray();

for(Object e : elements){
if (e instanceof Integer) {
bson_array_add_int32(bsonRef, (Integer) e);
} else if (e instanceof Long) {
bson_array_add_int64(bsonRef, (Long) e);
} else if (e instanceof String) {
bson_array_add_string(bsonRef, (String) e);
} else if (e instanceof Boolean) {
bson_array_add_bool(bsonRef, (Boolean) e);
} else if (e instanceof Double) {
bson_array_add_double(bsonRef, (Double) e);
}
}

return bsonRef;
}
long bsonRef = bson_object_from_bytes(bytes);
if (bsonRef == -1) {
return map;
}
map = bson_object_get_hashmap(bsonRef);
deinitializeBsonObject(bsonRef);

return map;
}

private static long buildBsonObject(HashMap<String, Object> elements) throws ClassCastException {
long bsonRef = initializeBsonObject();

for (String key : elements.keySet()) {
Object value = elements.get(key);
if (value instanceof HashMap){
long subObjRef = buildBsonObject((HashMap<String, Object>) value);
bson_object_put_object(bsonRef, key, subObjRef);
} else if (value instanceof List){
long arrayRef = buildBsonArray((List<Object>) value);
bson_object_put_array(bsonRef, key, arrayRef);
} else if (value instanceof Integer) {
bson_object_put_int32(bsonRef, key, (Integer) value);
} else if (value instanceof Long) {
bson_object_put_int64(bsonRef, key, (Long) value);
} else if (value instanceof String) {
bson_object_put_string(bsonRef, key, (String) value);
} else if (value instanceof Boolean) {
bson_object_put_bool(bsonRef, key, (Boolean) value);
} else if (value instanceof Double) {
bson_object_put_double(bsonRef, key, (Double) value);
}
}

return bsonRef;
}

private static long buildBsonArray(List<Object> elements) throws ClassCastException {
long bsonRef = initializeBsonArray(elements.size());

for(Object value : elements){
if (value instanceof HashMap){
long subObjRef = buildBsonObject((HashMap<String, Object>) value);
bson_array_add_object(bsonRef, subObjRef);
} else if (value instanceof List){
long arrayRef = buildBsonArray((List<Object>) value);
bson_array_add_array(bsonRef, arrayRef);
} else if (value instanceof Integer) {
bson_array_add_int32(bsonRef, (Integer) value);
} else if (value instanceof Long) {
bson_array_add_int64(bsonRef, (Long) value);
} else if (value instanceof String) {
bson_array_add_string(bsonRef, (String) value);
} else if (value instanceof Boolean) {
bson_array_add_bool(bsonRef, (Boolean) value);
} else if (value instanceof Double) {
bson_array_add_double(bsonRef, (Double) value);
}
}

return bsonRef;
}

// BSON Object Methods

private static native long initializeBsonObject();

private static native void deinitializeBsonObject(long bsonRef);

private static native boolean bson_object_put_object(long bsonRef, String key, long value);

// BSON Object Methods
private static native boolean bson_object_put_array(long bsonRef, String key, long value);

private static native long initializeBsonObject();
private static native boolean bson_object_put_int32(long bsonRef, String key, int value);

private static native void deinitializeBsonObject(long bsonRef);
private static native boolean bson_object_put_int64(long bsonRef, String key, long value);

private static native boolean bson_object_put_array(long bsonRef, String key, long arrayRef);
private static native boolean bson_object_put_string(long bsonRef, String key, String value);

private static native boolean bson_object_put_int32(long bsonRef, String key, int value);
private static native boolean bson_object_put_bool(long bsonRef, String key, boolean value);

private static native boolean bson_object_put_int64(long bsonRef, String key, long value);
private static native boolean bson_object_put_double(long bsonRef, String key, double value);

private static native boolean bson_object_put_string(long bsonRef, String key, String value);
private static native byte[] bson_object_to_bytes(long bsonRef);

private static native boolean bson_object_put_bool(long bsonRef, String key, boolean value);
private static native long bson_object_from_bytes(byte[] data);

private static native boolean bson_object_put_double(long bsonRef, String key, double value);
private static native HashMap<String, Object> bson_object_get_hashmap(long bsonRef);

private static native byte[] bson_object_to_bytes(long bsonRef);
//private static native List<String, Object> bson_object_get_list(long bsonRef);

private static native long bson_object_from_bytes(byte[] data);
// BSON Array methods

private static native HashMap<String, Object> bson_object_get_hashmap(long bsonRef);
private static native long initializeBsonArray(long size);

// BSON Array methods
private static native void deinitializeBsonArray(long bsonRef);

private static native long initializeBsonArray();
private static native boolean bson_array_add_object(long bsonRef, long value);

private static native void deinitializeBsonArray(long bsonRef);
private static native boolean bson_array_add_array(long bsonRef, long value);

private static native boolean bson_array_add_int32(long bsonRef, int value);
private static native boolean bson_array_add_int32(long bsonRef, int value);

private static native boolean bson_array_add_int64(long bsonRef, long value);
private static native boolean bson_array_add_int64(long bsonRef, long value);

private static native boolean bson_array_add_string(long bsonRef, String value);
private static native boolean bson_array_add_string(long bsonRef, String value);

private static native boolean bson_array_add_bool(long bsonRef, boolean value);
private static native boolean bson_array_add_bool(long bsonRef, boolean value);

private static native boolean bson_array_add_double(long bsonRef, double value);
private static native boolean bson_array_add_double(long bsonRef, double value);
}


Loading

0 comments on commit 5e79ef2

Please sign in to comment.