@Test
public void testExportImport() throws Exception {

    ArrayList<ServerName> serverNameList;
    List<HRegionInfo> regionInfoList;
    ServerName rs1, rs2;
    List<String> assignmentList;
    String expFileName = "export_test.exp";
    splitTable("a".getBytes());
    splitTable("b".getBytes());
    serverNameList = getServerNameList();
    rs1 = serverNameList.get(0);
    rs2 = serverNameList.get(1);
    // move all regions to rs1
    regionInfoList = getRegionInfoList(tableName);
    for (HRegionInfo hRegionInfo : regionInfoList)
        move(hRegionInfo, rs1);
    assertEquals(regionInfoList.size(), getRegionInfoList(rs1, tableName).size());
    boolean balancerRunning = false;
    try {
        String[] argsParam;
        Args args;
        Assign command;
        // export
        balancerRunning = admin.setBalancerRunning(false, true);
        argsParam = new String[]{"zookeeper", "export", expFileName};
        args = new ManagerArgs(argsParam);
        command = new Assign(admin, args);
        command.run();
        assignmentList = readExportFile(expFileName);
        assertEquals(AssignAction.getProcessedCount(), assignmentList.size());
        // modify exported file
        try (PrintWriter writer = new PrintWriter(expFileName, Constant.CHARSET.name())) {
            int i = 0, j = 0;
            for (String assignment : assignmentList) {
                if (i == 0) {
                    // add an invalid region
                    writer.println(assignment.replaceAll("/.*/", "/nonono/"));
                }
                // add a not existing table
                if (j == 0 && assignment.endsWith(tableName)) {
                    writer.println(assignment.replaceAll("/" + tableName, "/" + tableName + "_INVALID"));
                    j++;
                }
                if (i == 2) {
                    // modify region server timestamp
                    int index1 = assignment.indexOf(",");
                    int index2 = assignment.indexOf(",", index1 + 1);
                    int index3 = assignment.indexOf(AssignAction.DELIMITER, index2 + 1);
                    String rsTimestamp = assignment.substring(index2 + 1, index3);
                    writer.println(assignment.replaceAll(rsTimestamp, String.valueOf(System.currentTimeMillis())));
                } else {
                    writer.println(assignment);
                }
                i++;
            }
        }
        assignmentList = readExportFile(expFileName);
        assertEquals(AssignAction.getProcessedCount() + 2, assignmentList.size());
        // remember region count of rs1
        int regionCountRS1 = getRegionInfoList(rs1, tableName).size();
        // move all regions to rs2
        regionInfoList = getRegionInfoList(tableName);
        for (HRegionInfo hRegionInfo : regionInfoList)
            move(hRegionInfo, rs2);
        assertEquals(0, getRegionInfoList(rs1, tableName).size());
        assertEquals(regionInfoList.size(), getRegionInfoList(rs2, tableName).size());
        // import
        balancerRunning = admin.setBalancerRunning(false, true);
        argsParam = new String[]{"zookeeper", "import", expFileName, "--force-proceed"};
        args = new ManagerArgs(argsParam);
        command = new Assign(admin, args);
        command.run();
        // verify
        assertEquals(regionCountRS1, getRegionInfoList(rs1, tableName).size());
        assertEquals(regionInfoList.size(), getRegionInfoList(tableName).size());
    } finally {
        if (balancerRunning)
            admin.setBalancerRunning(true, true);
        Files.delete(Paths.get(expFileName));
    }
}