Skip to content

Commit

Permalink
fbandroid/libraries/fbjni/test/DocTests.java
Browse files Browse the repository at this point in the history
Reviewed By: jocelynluizzi13

Differential Revision: D67084875

fbshipit-source-id: a593264b3a2d651cbb184ee4fd7c9a3e0a475333
  • Loading branch information
generatedunixname89002005287564 authored and facebook-github-bot committed Dec 11, 2024
1 parent f5c4ced commit 273f269
Showing 1 changed file with 43 additions and 6 deletions.
49 changes: 43 additions & 6 deletions test/DocTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;

import com.facebook.infer.annotation.Nullsafe;
import com.facebook.soloader.nativeloader.NativeLoader;
import java.nio.ByteBuffer;
import java.util.Arrays;
Expand All @@ -29,6 +30,7 @@
import org.junit.BeforeClass;
import org.junit.Test;

@Nullsafe(Nullsafe.Mode.LOCAL)
public class DocTests extends BaseFBJniTests {
@BeforeClass
public static void setup() {
Expand All @@ -42,9 +44,13 @@ public String toString() {

// SECTION basic_methods
native void nativeVoidMethod();

static native void staticNativeVoidMethod();

void voidMethod() {}

static void staticVoidMethod() {}

// END

@Test
Expand All @@ -55,19 +61,29 @@ public void testVoids() {

// SECTION primitives
static native long addSomeNumbers(byte b, short s, int i);
static long doubler(int i) { return i + i; }

static long doubler(int i) {
return i + i;
}

// END

@Test
public void testNumbers() {
// NULLSAFE_FIXME[Not Vetted Third-Party]
assertThat(addSomeNumbers((byte) 1, (short) 2, 3)).isEqualTo(14);
}

// SECTION strings
// Java methods used by the C++ code below.
static native String fancyCat(String s1, String s2);

static native String getCString();
static String doubler(String s) { return s + s; }

static String doubler(String s) {
return s + s;
}

// END

@Test
Expand All @@ -78,10 +94,12 @@ public void testStrings() {

// SECTION primitive_arrays
static native int[] primitiveArrays(int[] arr);

// END

@Test
public void testPrimitiveArrays() {
// NULLSAFE_FIXME[Not Vetted Third-Party]
assertThat(primitiveArrays(new int[] {1, 2})).contains(1, 2, 3);
}

Expand All @@ -103,7 +121,9 @@ public void testCastReferences() {
castReferences(new MyBaseClass());
failBecauseExceptionWasNotThrown(ClassCastException.class);
} catch (ClassCastException e) {
// NULLSAFE_FIXME[Not Vetted Third-Party]
assertThat(e).hasMessageContaining("MyBaseClass");
// NULLSAFE_FIXME[Not Vetted Third-Party]
assertThat(e).hasMessageContaining("MyDerivedClass");
}
}
Expand All @@ -124,6 +144,7 @@ public void testCallGetAndSetFields() {
synchronized (DataHolder.class) {
DataHolder dh1 = new DataHolder(1, "1");
DataHolder dh2 = new DataHolder(3, "3");
// NULLSAFE_FIXME[Field Not Nullable]
DataHolder.someInstance = null;
callGetAndSetFields(dh1);
assertThat(dh1.i).isEqualTo(2);
Expand All @@ -133,6 +154,7 @@ public void testCallGetAndSetFields() {
assertThat(dh2.i).isEqualTo(4);
assertThat(dh2.s).isEqualTo("31");
assertThat(DataHolder.someInstance).isSameAs(dh1);
// NULLSAFE_FIXME[Field Not Nullable]
DataHolder.someInstance = null;
}
}
Expand All @@ -153,24 +175,27 @@ public void testCatchAndThrow() {
catchAndThrow();
failBecauseExceptionWasNotThrown(RuntimeException.class);
} catch (RuntimeException e) {
// NULLSAFE_FIXME[Not Vetted Third-Party]
assertThat(e)
.hasMessageStartingWith("Caught 'java.lang.NoSuchMethodError:")
.hasMessageContaining("doesNotExist")
;
.hasMessageContaining("doesNotExist");
}
}

// SECTION boxed
static native Double scaleUp(Integer number);

// END

@Test
public void testScaleUp() {
// NULLSAFE_FIXME[Not Vetted Third-Party]
assertThat(scaleUp(5)).isEqualTo(7.5);
}

// SECTION iterables
static native String concatMatches(List<Integer> values, Map<String, Integer> names);

// END

@Test
Expand All @@ -185,22 +210,29 @@ public void testConcatMatches() {

// SECTION collections
static native Map<String, List<Integer>> buildCollections();

// END

@Test
public void testBuildCollections() {
Map<String, List<Integer>> ret = buildCollections();
// NULLSAFE_FIXME[Not Vetted Third-Party]
assertThat(ret.keySet()).isEqualTo(new HashSet<>(Arrays.asList("primes")));
// NULLSAFE_FIXME[Parameter Not Nullable]
assertThat(ret.get("primes")).isEqualTo(Arrays.asList(2, 3));
}

// SECTION byte_buffer
static native ByteBuffer transformBuffer(ByteBuffer data);

static void receiveBuffer(ByteBuffer buffer) {
assertThat(buffer.capacity()).isEqualTo(2);
assertThat(buffer.get(0)).isEqualTo((byte)2);
assertThat(buffer.get(1)).isEqualTo((byte)3);
// NULLSAFE_FIXME[Not Vetted Third-Party]
assertThat(buffer.get(0)).isEqualTo((byte) 2);
// NULLSAFE_FIXME[Not Vetted Third-Party]
assertThat(buffer.get(1)).isEqualTo((byte) 3);
}

@Test
public void testByteBuffers() {
ByteBuffer data = ByteBuffer.allocateDirect(2);
Expand All @@ -213,23 +245,28 @@ public void testByteBuffers() {

// SECTION inheritance
class MyBaseClass {}

class MyDerivedClass extends MyBaseClass {}

// END

// SECTION nested_class
class Outer {
class Nested {}
}

// END

// SECTION constructor
class DataHolder {
int i;
String s;

DataHolder(int i, String s) {
this.i = i;
this.s = s;
}

static DataHolder someInstance;
}
// END

0 comments on commit 273f269

Please sign in to comment.