Skip to content

Commit

Permalink
test(binding/java): add create_dir test (#3184)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-XD authored Sep 26, 2023
1 parent 31b5620 commit 802e1de
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 0 deletions.
20 changes: 20 additions & 0 deletions bindings/java/src/blocking_operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,23 @@ fn intern_delete(env: &mut JNIEnv, op: &mut BlockingOperator, path: JString) ->
let path = env.get_string(&path)?;
Ok(op.delete(path.to_str()?)?)
}

/// # Safety
///
/// This function should not be called before the Operator are ready.
#[no_mangle]
pub unsafe extern "system" fn Java_org_apache_opendal_BlockingOperator_createDir(
mut env: JNIEnv,
_: JClass,
op: *mut BlockingOperator,
path: JString,
) {
intern_create_dir(&mut env, &mut *op, path).unwrap_or_else(|e| {
e.throw(&mut env);
})
}

fn intern_create_dir(env: &mut JNIEnv, op: &mut BlockingOperator, path: JString) -> Result<()> {
let path = env.get_string(&path)?;
Ok(op.create_dir(path.to_str()?)?)
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public Metadata stat(String path) {
return new Metadata(stat(nativeHandle, path));
}

public void createDir(String path) {
createDir(nativeHandle, path);
}

@Override
protected native void disposeInternal(long handle);

Expand All @@ -80,4 +84,6 @@ public Metadata stat(String path) {
private static native void delete(long nativeHandle, String path);

private static native long stat(long nativeHandle, String path);

private static native long createDir(long nativeHandle, String path);
}
7 changes: 7 additions & 0 deletions bindings/java/src/main/java/org/apache/opendal/Operator.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ public CompletableFuture<Void> delete(String path) {
return AsyncRegistry.take(requestId);
}

public CompletableFuture<Void> createDir(String path) {
final long requestId = createDir(nativeHandle, path);
return AsyncRegistry.take(requestId);
}

@Override
protected native void disposeInternal(long handle);

Expand All @@ -201,4 +206,6 @@ public CompletableFuture<Void> delete(String path) {
private static native OperatorInfo makeOperatorInfo(long nativeHandle);

private static native long makeBlockingOp(long nativeHandle);

private static native long createDir(long nativeHandle, String path);
}
34 changes: 34 additions & 0 deletions bindings/java/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,40 @@ fn intern_make_operator_info(env: &mut JNIEnv, op: *mut Operator) -> Result<jobj
Ok(make_operator_info(env, op.info())?.into_raw())
}

/// # Safety
///
/// This function should not be called before the Operator are ready.
#[no_mangle]
pub unsafe extern "system" fn Java_org_apache_opendal_Operator_createDir(
mut env: JNIEnv,
_: JClass,
op: *mut Operator,
path: JString,
) -> jlong {
intern_create_dir(&mut env, op, path).unwrap_or_else(|e| {
e.throw(&mut env);
0
})
}

fn intern_create_dir(env: &mut JNIEnv, op: *mut Operator, path: JString) -> Result<jlong> {
let op = unsafe { &mut *op };
let id = request_id(env)?;

let path = env.get_string(&path)?.to_str()?.to_string();

unsafe { get_global_runtime() }.spawn(async move {
let result = do_create_dir(op, path).await;
complete_future(id, result.map(|_| JValueOwned::Void))
});

Ok(id)
}

async fn do_create_dir(op: &mut Operator, path: String) -> Result<()> {
Ok(op.create_dir(&path).await?)
}

/// # Safety
///
/// This function should not be called before the Operator are ready.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,45 @@ public void testAppendCreateAppend() {
}
}

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Nested
class AsyncCreateDirTest {
@BeforeAll
public void precondition() {
final Capability capability = operator.info.fullCapability;
assumeTrue(capability.createDir);
}

/**
* Create dir with dir path should succeed.
*/
@Test
public void testCreateDir() {
final String path = String.format("%s/", UUID.randomUUID().toString());
operator.createDir(path).join();

try (final Metadata meta = operator.stat(path).join()) {
assertThat(meta.isFile()).isFalse();
}
operator.delete(path).join();
}

/**
* Create dir on existing dir should succeed.
*/
@Test
public void testCreateDirExisting() {
final String path = String.format("%s/", UUID.randomUUID().toString());
operator.createDir(path).join();
operator.createDir(path).join();

try (final Metadata meta = operator.stat(path).join()) {
assertThat(meta.isFile()).isFalse();
}
operator.delete(path).join();
}
}

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Nested
class BlockingWriteTest {
Expand Down Expand Up @@ -212,6 +251,45 @@ public void testBlockingStatFile() {
}
}

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Nested
class BlockingCreateDirTest {
@BeforeAll
public void precondition() {
final Capability capability = blockingOperator.info.fullCapability;
assumeTrue(capability.createDir);
}

/**
* Create dir with dir path should succeed.
*/
@Test
public void testBlockingCreateDir() {
final String path = String.format("%s/", UUID.randomUUID().toString());
blockingOperator.createDir(path);

try (final Metadata meta = blockingOperator.stat(path)) {
assertThat(meta.isFile()).isFalse();
}
blockingOperator.delete(path);
}

/**
* Create dir on existing dir should succeed.
*/
@Test
public void testBlockingDirExisting() {
final String path = String.format("%s/", UUID.randomUUID().toString());
blockingOperator.createDir(path);
blockingOperator.createDir(path);

try (final Metadata meta = blockingOperator.stat(path)) {
assertThat(meta.isFile()).isFalse();
}
blockingOperator.delete(path);
}
}

/**
* Generates a byte array of random content.
*/
Expand Down

0 comments on commit 802e1de

Please sign in to comment.