1. 程式人生 > 其它 >把xmind的轉換為csv測試用例

把xmind的轉換為csv測試用例

import org.xmind.core.*;
import org.xmind.core.io.ByteArrayStorage;

import java.io.*;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class XmindToCSV {
private Map<String,String> cases=new LinkedHashMap<String, String>( );
/** CSV檔案列分隔符 */
private static final String CSV_COLUMN_SEPARATOR = ",";
/** CSV檔案行分隔符 */
private static final String CSV_ROW_SEPARATOR = "\r\n";

/**
* @param dataList 資料集合資料
*/
public ByteArrayOutputStream doExport(Map<String,String> dataList) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
//表頭部資料
String[] colNames={"用例標題","步驟","預期","用例型別","適用階段"};
try {
StringBuffer buf = new StringBuffer();
// 完成資料csv檔案的封裝
// 輸出列頭
for (int i = 0; i < colNames.length; i++) {
buf.append(colNames[i]).append(CSV_COLUMN_SEPARATOR);
}
buf.append(CSV_ROW_SEPARATOR);

if (null != dataList) { // 輸出資料
for(Map.Entry entry:dataList.entrySet ()){
//用例標題
String key=entry.getKey ().toString ();
buf.append ( "\"" ).append(key).append ( "\"" ).append(CSV_COLUMN_SEPARATOR);
//步驟
buf.append ( "\"" ).append(key).append ( "\"" ).append(CSV_COLUMN_SEPARATOR);
//預期
String value=entry.getValue ().toString ();
buf.append("\"").append (value).append ( "\"" ).append(CSV_COLUMN_SEPARATOR);
//用例型別
buf.append("功能測試" ).append(CSV_COLUMN_SEPARATOR);
//適用階段
buf.append("系統測試階段" ).append(CSV_COLUMN_SEPARATOR);
buf.append(CSV_ROW_SEPARATOR);
}
}
// 寫出響應
os.write(buf.toString().getBytes("GBK"));
os.flush();
os.close();
return os;
} catch (Exception e) {
e.printStackTrace();
}
return os;
}

public void xmindToCsv(String filepath){
IWorkbookBuilder builder = Core.getWorkbookBuilder();// 初始化builder
IWorkbook workbook = null;
try {
workbook = builder.loadFromFile(new File(filepath),new ByteArrayStorage(),null);// 開啟XMind檔案
} catch (Exception e) {

}
ISheet defSheet = workbook.getPrimarySheet();// 獲取主Sheet
ITopic rootTopic = defSheet.getRootTopic(); // 獲取根Topic

cases=getNodes(rootTopic);
System.out.println ( "用例個數:"+cases.size () );
// for(Map.Entry<String,String> entry:cases.entrySet ()){
// String key=entry.getKey ();
//// System.out.println("name="+entry.getKey ());
// String value=entry.getValue ();
// System.out.println("name="+key+","+"value="+value);
// }

}

//遞迴遍歷節點
public Map<String,String> getNodes(ITopic node) {
//遞迴遍歷當前節點所有的子節點
List<ITopic> listElement = node.getAllChildren ();
//遍歷所有一級子節點
int num=1;
for (ITopic e : listElement) {
List <ITopic> ee = e.getAllChildren ();
//找到最後一個節點
if (ee.size () == 0) {
List <ITopic> path=e.getPath ().toTopicList ();
StringBuffer sb=new StringBuffer();
for(ITopic t:path) {
sb.append ( t.getTitleText () );
sb.append ( "-" );
}

// String key=(sb.substring (sb.indexOf ( "-" )+1, sb.lastIndexOf ( "-" ) ));

String key=(sb.substring (0, sb.lastIndexOf ( "-" ) ));
// key=key.substring ( 0,key.lastIndexOf ( "-" )+1 )+num;
String value=e.getTitleText ();
cases.put (key,value );
}
num++;
//遞迴遍歷
this.getNodes ( e );
}
return cases;
}

public Map <String, String> getCases() {
return cases;
}

public void setCases(Map <String, String> cases) {
this.cases = cases;
}

public static void main(String[]args) throws IOException {
XmindToCSV test=new XmindToCSV();
String outfile="轉換為csv的檔案路徑";
test.xmindToCsv ( "xmind檔案路徑");
Map<String,String> cases=test.getCases ();//獲取xmind用例


ByteArrayOutputStream bos =test.doExport(cases);//匯出禪道規範用例
FileOutputStream fos=null;
try {
fos=new FileOutputStream(outfile);
fos.write(bos.toByteArray());
} catch (FileNotFoundException e) {
e.printStackTrace ();
} catch (IOException e) {
e.printStackTrace ();
}finally {
bos.close ();
fos.close ();
}
}
}