Browse Source

Merge branch 'JNI-132 - implement java.object.equals() for Blob' into Release_2.1

JNI-149
heck 4 years ago
parent
commit
f1424178a1
  1. 27
      src/java/foundation/pEp/jniadapter/Blob.java
  2. 24
      src/java/foundation/pEp/jniadapter/Utils.java
  3. 34
      test/java/foundation/pEp/jniadapter/test/jni132/Makefile
  4. 93
      test/java/foundation/pEp/jniadapter/test/jni132/TestAlice.java

27
src/java/foundation/pEp/jniadapter/Blob.java

@ -1,7 +1,8 @@
package foundation.pEp.jniadapter;
import foundation.pEp.jniadapter.interfaces.*;
import foundation.pEp.jniadapter.interfaces.BlobInterface;
import java.io.Serializable;
import java.util.Arrays;
public class Blob implements BlobInterface, Serializable {
public byte[] data;
@ -36,5 +37,29 @@ public class Blob implements BlobInterface, Serializable {
return ret;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null) return false;
if (this.getClass() != o.getClass()) return false;
boolean ret = false;
Blob blob = (Blob) o;
if (Arrays.equals(data, blob.data)) {
if (this.mime_type.equals(((Blob) o).mime_type)) {
if (Utils.URIEqual(this.filename, ((Blob) o).filename)) {
ret = true;
}
}
}
return ret;
}
@Override
public int hashCode() {
int result = mime_type == null ? 0 : mime_type.hashCode();
result = 31 * result + Utils.URIHash(filename);
result = 31 * result + Arrays.hashCode(data);
return result;
}
}

24
src/java/foundation/pEp/jniadapter/Utils.java

@ -116,12 +116,24 @@ public class Utils {
}
public static boolean URIEqual(String left, String right) {
Pattern pattern = Pattern.compile("^.*?://");
Matcher leftMatcher = pattern.matcher(left.trim());
String leftRelevant = leftMatcher.replaceAll("");
Matcher rightMatcher = pattern.matcher(right.trim());
String rightRelevant = rightMatcher.replaceAll("");
if(left == right) return true;
if(left == null) return false;
if(right == null) return false;
return URITrim(left).equals(URITrim(right));
}
// Returns the hash of a string that represents a URI
// Returns 0 if uri i null
public static int URIHash(String uri) {
if(uri == null) {
return 0;
}
return URITrim(uri).hashCode();
}
return rightRelevant.equals(leftRelevant);
private static String URITrim(String uri) {
Pattern pattern = Pattern.compile("^.*?://");
Matcher leftMatcher = pattern.matcher(uri.trim());
return leftMatcher.replaceAll("");
}
}

34
test/java/foundation/pEp/jniadapter/test/jni132/Makefile

@ -0,0 +1,34 @@
include ../../../../../../../Makefile.conf
include ../Makefile.conf
TEST_UNIT_NAME=jni132
JAVA_CLASSES+= \
TestAlice.class
.PHONY: pitytest compile alice test clean
all: alice compile
pitytest:
$(MAKE) -C $(PITYTEST_DIR)
alice: compile clean-pep-home-alice
cd $(JAVA_CWD);pwd;HOME=$(JAVA_PEP_HOME_DIR_ALICE) $(JAVA) $(JAVA_PKG_BASENAME).$(TEST_UNIT_NAME).TestAlice
compile: $(JAVA_CLASSES) pitytest
%.class: %.java
cd $(JAVA_CWD);javac -cp $(CLASSPATH) $(JAVA_PKG_BASEPATH)/$(TEST_UNIT_NAME)/$<
clean:
rm -f $(JAVA_CLASSES)
rm -f *.class
rm -f *.log
rm -Rf .gnupg
rm -Rf .lldb
clean-pep-home: clean-pep-home-alice
clean-pep-home-alice:
rm -rf $(PEP_HOME_DIR_ALICE)/.pEp

93
test/java/foundation/pEp/jniadapter/test/jni132/TestAlice.java

@ -0,0 +1,93 @@
package foundation.pEp.jniadapter.test.jni132;
import foundation.pEp.jniadapter.Blob;
import foundation.pEp.jniadapter.test.utils.AdapterTestUtils;
import foundation.pEp.jniadapter.test.utils.CTXBase;
import foundation.pEp.pitytest.TestSuite;
import foundation.pEp.pitytest.TestUnit;
import foundation.pEp.pitytest.utils.TestUtils;
import java.util.Arrays;
/*
JNI-132 - implement java.object.equals() for Blob
tests equality definition of Blob
equality definition:
- bytewise equality of data
- URI equality of filename
- bytewise equality of mime_type
*/
class TestAlice {
public static void main(String[] args) throws Exception {
TestSuite.getDefault().setVerbose(true);
TestSuite.getDefault().setTestColor(TestUtils.TermColor.GREEN);
CTXBase jni132Ctx = new CTXBase();
// SAME
new TestUnit<CTXBase>("Blob.equals() equality", new CTXBase(), ctx -> {
Blob one = AdapterTestUtils.makeNewTestBlob("testBlob data", "testblobfilename", null);
Blob two = AdapterTestUtils.makeNewTestBlob("testBlob data", "testblobfilename", null);
assert one.equals(two) : "\n" + one.toString() + "\n" + "does not equal:\n" + two.toString();
});
new TestUnit<CTXBase>("Blob.equals() equality", new CTXBase(), ctx -> {
Blob one = AdapterTestUtils.makeNewTestBlob("testBlob data", "file://testblobfilename", null);
Blob two = AdapterTestUtils.makeNewTestBlob("testBlob data", "testblobfilename", null);
assert one.equals(two) : "\n" + one.toString() + "\n" + "does not equal:\n" + two.toString();
});
new TestUnit<CTXBase>("Blob.equals() equality", new CTXBase(), ctx -> {
Blob one = AdapterTestUtils.makeNewTestBlob(1000000, "testfilename", "anything goes");
Blob two = new Blob();
two.mime_type = new String(one.mime_type);
two.filename = new String(one.filename);
two.data = Arrays.copyOf(one.data, one.data.length);
assert one.equals(two) : "\n" + one.toString() + "\n" + "does not equal:\n" + two.toString();
});
new TestUnit<CTXBase>("Blob.equals() equality", new CTXBase(), ctx -> {
Blob one = AdapterTestUtils.makeNewTestBlob(1000000, "file://testfilename", "anything goes");
Blob two = new Blob();
two.mime_type = new String(one.mime_type);
two.filename = new String(one.filename);
two.data = Arrays.copyOf(one.data, one.data.length);
assert one.equals(two) : "\n" + one.toString() + "\n" + "does not equal:\n" + two.toString();
});
// NOT SAME
new TestUnit<CTXBase>("Blob.equals() - diff mime_type", new CTXBase(), ctx -> {
Blob one = ctx.attachment1KB;
Blob two = new Blob();
two.mime_type = "diff";
two.filename = ctx.attachment1KB.filename;
two.data = ctx.attachment1KB.data;
assert !one.equals(two) : "\n" + one.toString() + "\n" + "equals:\n" + two.toString();
});
new TestUnit<CTXBase>("Blob.equals() diff filename", new CTXBase(), ctx -> {
Blob one = ctx.attachment1KB;
Blob two = new Blob();
two.mime_type = ctx.attachment1KB.mime_type;
two.filename = "diff";
two.data = ctx.attachment1KB.data;
assert !one.equals(two) : "\n" + one.toString() + "\n" + "equals:\n" + two.toString();
});
new TestUnit<CTXBase>("Blob.equals() diff data", new CTXBase(), ctx -> {
Blob one = ctx.attachment1KB;
Blob two = new Blob();
two.mime_type = ctx.attachment1KB.mime_type;
two.filename = ctx.attachment1KB.filename;
two.data = ctx.attachment1MB.data;
assert !one.equals(two) : "\n" + one.toString() + "\n" + "equals:\n" + two.toString();
});
TestSuite.getDefault().run();
}
}
Loading…
Cancel
Save