Skip to content

Commit

Permalink
More testing for Document
Browse files Browse the repository at this point in the history
  • Loading branch information
Kai Mast committed Feb 22, 2015
1 parent 15d6a27 commit afbb25b
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 3 deletions.
56 changes: 56 additions & 0 deletions bindings/java/org/hyperdex/client/Document.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* Copyright (c) 2015, Cornell University
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of HyperDex nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

package org.hyperdex.client;

public class Document {
private String json;

public Document(String json) {
this.json = json;
}

@Override
public String toString() {
return json;
}

@Override
public int hashCode() {
return json.hashCode();
}

@Override
public boolean equals(Object object) {
if(object instanceof Document) {
return ((Document)object).toString().equals(this.toString());
} else {
return false;
}
}
}
9 changes: 6 additions & 3 deletions bindings/java/org_hyperdex_client_Client.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ static jclass _string;
static jclass _byte_string;
static jmethodID _byte_string_init;
static jmethodID _byte_string_get;
static jmethodID _byte_string_to_string;

static jclass _document;
static jmethodID _document_init;
Expand Down Expand Up @@ -178,6 +179,7 @@ Java_org_hyperdex_client_Client_initialize(JNIEnv* env, jclass client)
REF(_byte_string, (*env)->FindClass(env, "org/hyperdex/client/ByteString"));
_byte_string_init = (*env)->GetMethodID(env, _byte_string, "<init>", "([B)V");
_byte_string_get = (*env)->GetMethodID(env, _byte_string, "getBytes", "()[B");
_byte_string_to_string = (*env)->GetMethodID(env, _byte_string, "toString", "()Ljava/lang/String;");
/* cache class Boolean */
REF(_boolean, (*env)->FindClass(env, "java/lang/Boolean"));
_boolean_init = (*env)->GetMethodID(env, _boolean, "<init>", "(Z)V");
Expand Down Expand Up @@ -279,6 +281,7 @@ Java_org_hyperdex_client_Client_initialize(JNIEnv* env, jclass client)
CHECK_CACHE(_byte_string);
CHECK_CACHE(_byte_string_init);
CHECK_CACHE(_byte_string_get);
CHECK_CACHE(_byte_string_to_string);
CHECK_CACHE(_boolean);
CHECK_CACHE(_boolean_init);
CHECK_CACHE(_long);
Expand Down Expand Up @@ -1483,10 +1486,10 @@ hyperdex_java_client_build_attribute(JNIEnv* env,
BUILD_FLOAT(tmp, tmp_d);
return tmp;
case HYPERDATATYPE_DOCUMENT:
BUILD_STRING(tmp2, attr->value, attr->value_sz);
BUILD_STRING(tmp, attr->value, attr->value_sz);
// Build document from string
tmp = (*env)->NewObject(env, _document, _document_init, tmp2);
return tmp;
tmp2 = (*env)->CallObjectMethod(env, tmp, _byte_string_to_string);
return (*env)->NewObject(env, _document, _document_init, tmp2);
case HYPERDATATYPE_LIST_STRING:
hyperdex_ds_iterator_init(&iter, attr->datatype, attr->value, attr->value_sz);
ret = (*env)->NewObject(env, _array_list, _array_list_init);
Expand Down
26 changes: 26 additions & 0 deletions test/java/DataTypeDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,32 @@ public void insertEmptyDocument() throws HyperDexClientException {
Map<String, Object> get = c.get("kv", "k");
Map<String, Object> expected = new HashMap<String, Object>();
expected.put("v", new Document("{}"));

String str1 = expected.get("v").toString();
String str2 = get.get("v").toString();

assertEquals(str1.length(), str2.length());
for(int i = 0; i < str1.length(); ++i)
{
assertEquals(str1.charAt(i), str2.charAt(i));
}

assertEquals(expected.get("v"), get.get("v"));
assertEquals(expected, get);
}

@Test
public void insertNestedDocument() throws HyperDexClientException {
Map<String, Object> attrs = new HashMap<String, Object>();
attrs.put("v", new Document("{\"a\" : { \"b\" : \"c\"}}"));

Boolean res = c.put("kv", "k", attrs);
assertTrue(res);

Map<String, Object> get = c.get("kv", "k");
Map<String, Object> expected = new HashMap<String, Object>();
assert(get.get("v") instanceof Document);
expected.put("v", new Document("{\"a\":{\"b\":\"c\"}}")); // be careful about formatting...
assertEquals(expected, get);
}
}
Expand Down
10 changes: 10 additions & 0 deletions test/java/DataTypeString.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,14 @@ public void insertBytes() throws HyperDexClientException {
expected.put("v", new ByteString(bytes));
assertEquals(get, expected);
}

@Test
public void correctByteString() {
String original = "42 is the answer";

ByteString bstring = new ByteString(bstring.getBytes());
String result = bstring.toString();

assertEquals(original, result);
}
}

0 comments on commit afbb25b

Please sign in to comment.