Java call mongodump

Note: the version just supports macOS and linux.
100% testing coverage, please feel free to use.

Usage

new MongoDump.Builder()
                .runtime(mockRuntime)
                .uri("mongodb://127.0.0.1:27017/gt_ut")
                .archive("dbname.archive")
                .commandPath("/usr/local/bin/mongodump")
                .build()
                .execute();

Source Code

MongoDump.java
package learningops.mongo;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * @author learningops
 * @date 22/03/2018
 */
public class MongoDump {
    private static final Logger LOG = LoggerFactory.getLogger(MongoDump.class);

    private String uri;
    private String archive;
    private String commandPath;
    private Runtime runtime;

    public static class Builder {

        private String uri;
        private String archive;
        private String commandPath;
        private Runtime runtime;


        public Builder archive(String archive) {
            this.archive = archive;
            return this;
        }

        public Builder runtime(Runtime runtime) {
            this.runtime = runtime;
            return this;
        }

        public Builder commandPath(String commandPath) {
            this.commandPath = commandPath;
            return this;
        }

        public Builder uri(String uri) {
            this.uri = uri;
            return this;
        }

        public MongoDump build() {
            MongoDump result = new MongoDump();
            result.uri = checkNotNull(uri, "uri was null.");
            result.commandPath = checkNotNull(commandPath, "commandPath was null.");
            result.archive = checkNotNull(archive, "archive was null.");
            Runtime rt = runtime;
            if (rt == null) {
                rt = Runtime.getRuntime();
            }
            result.runtime = rt;
            return result;
        }
    }

    public String execute() {
        try {
            String command = String.format("%s --archive=%s --uri=%s", commandPath, archive, uri);
            LOG.debug("command: {}", command);
            Process runtimeProcess = runtime.exec(new String[]{"/bin/sh", "-c", command});
            int exitValue = runtimeProcess.waitFor();
            if (exitValue != 0) {
                InputStream error = runtimeProcess.getErrorStream();
                String errorMessage = IOUtils.toString(error, "UTF-8");
                throw new MongoDumpException(errorMessage);
            }
            InputStream message = runtimeProcess.getInputStream();
            return IOUtils.toString(message, "UTF-8");
        } catch (MongoDumpException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
MongoDumpException.java
package learningops.mongo;

/**
 * @author learningops
 * @date 22/03/2018
 */
public class MongoDumpException extends RuntimeException {
    public MongoDumpException(String message) {
        super(message);
    }
}

Unit Testing

package learningops.mongo;

import org.apache.commons.io.IOUtils;
import org.testng.annotations.Test;

import java.io.InputStream;

import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;

/**
 * @author learningops
 * @date 22/03/2018
 */
public class MongoDumpTest {

    @Test
    public void test() throws Exception {
        Runtime mockRuntime = mock(Runtime.class);
        Process mockProcess = mock(Process.class);
        when(mockProcess.getInputStream()).thenReturn(IOUtils.toInputStream("success message", "UTF-8"));
        when(mockRuntime.exec(new String[]{anyString()})).thenReturn(mockProcess);
        when(mockProcess.waitFor()).thenReturn(0);
        String result = new MongoDump.Builder()
                .runtime(mockRuntime)
                .uri("mongodb://127.0.0.1:27017/gt_ut")
                .archive("dbname.archive")
                .commandPath("/usr/local/bin/mongodump")
                .build()
                .execute();
        assertEquals(result, "success message");
    }

    @Test(expectedExceptions = {MongoDumpException.class}, expectedExceptionsMessageRegExp = "error message")
    public void unknownTermination() throws Exception {
        Runtime mockRuntime = mock(Runtime.class);
        Process mockProcess = mock(Process.class);
        when(mockRuntime.exec(new String[]{anyString()})).thenReturn(mockProcess);
        when(mockProcess.waitFor()).thenReturn(1);
        InputStream errorStream = IOUtils.toInputStream("error message", "UTF-8");
        when(mockProcess.getErrorStream()).thenReturn(errorStream);
        new MongoDump.Builder()
                .runtime(mockRuntime)
                .uri("mongodb://127.0.0.1:27017/dbname")
                .archive("dbname.archive")
                .commandPath("/usr/local/bin/mongodump")
                .build()
                .execute();
    }

    @Test(expectedExceptions = {RuntimeException.class})
    public void unknownException() throws Exception {
        Runtime mockRuntime = mock(Runtime.class);
        Process mockProcess = mock(Process.class);
        when(mockRuntime.exec(new String[]{anyString()})).thenReturn(mockProcess);
        InputStream errorStream = IOUtils.toInputStream("error message", "UTF-8");
        when(mockProcess.getErrorStream()).thenReturn(errorStream);
        new MongoDump.Builder()
                .runtime(mockRuntime)
                .uri("mongodb://127.0.0.1:27017/dbname")
                .archive("dbname.archive")
                .commandPath("/usr/local/bin/mongodump")
                .build()
                .execute();
    }

    @Test
    public void buildWithDefaultRuntime() {
        new MongoDump.Builder()
                .uri("mongodb://127.0.0.1:27017/dbname")
                .archive("dbname.archive")
                .commandPath("/usr/local/bin/mongodump")
                .build();
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值