2015-06-04 21 views
0

我必須將平面文件發送到外部系統。下面的樣本文件內容給出:在Java中測試平面文件內容和格式

START 20150602 
HEADER 100.00USD 
PRODUCT TEST1     50.00USD 
PRODUCT TEST2     50.00USD 
FOOTER 002 

此文件應遵循以下原則:

First line - starts with START, then space, then today's date in YYYYMMDD 
Header line - starts with HEADER, then space, then total amount of first, second, third etc products with 2 decimal points, then currency 
First product - starts with PRODUCT, then space, then product name left aligned with total 25 characters, then amount with 2 decimal points, then currency 
More products similar to first product, the number of products is dynamic 
Footer - starts with FOOTER, then space, then total number of products with 3 digits 

這種格式已經在Excel中的外部系統供應商給出。

你們中的任何一個人都可以在這種情況下提出更好的測試策略嗎?有沒有測試文件內容和格式的工具?

+2

所以,你的任務就是測試,如果文件遵循的準則?這與單元測試無關。也沒有代碼 - 究竟是什麼問題? – f1sh

+0

是的,我將不得不測試生成的文件是否遵循準則。我已經實施了這個邏輯。現在檢查如何爲此添加測試用例。我正在尋找一些工具,以便我可以在其他地方重複使用。我還有一些其他的文件格式。 – Ani

回答

1

在這種情況下,我更願意通過基於基本Java特性的簡單方法開始探索我的選擇。這裏,每行可以用正則表達式來描述:

START (\d{8}) 
HEADER (\d+\.\d{2})(\w{3}) 
PRODUCT (.{25}) (\d+\.\d{2})(\w{3}) 
FOOTER (\d{3}) 

如果您不需要提取和檢查值,則可能不需要括號。 OTOH,添加一個字符串列表,爲這些字段提供名稱,以便它們可以輸入到Map中。

您可以爲每行添加最小和最大重複次數,這將允許您編寫一些簡單的邏輯來遍歷文件的行。

所有的上面是一對夫婦行的定義,這些可能是工廠方法調用:

LineDef.create("HEADER (\\d+\\.\\d{2})(\\w{3})", 1, 1, 
       "totalAmount", "totalCurrency"); 
LineDef.create("PRODUCT (.{25}) (\\d+\\.\\d{2})(\\w{3})", 1, 999, 
       "name", "amount", "currency"); 
相關問題