|
| 1 | +package com.alipay.rdf.file.writer; |
| 2 | + |
| 3 | +import com.alipay.rdf.file.interfaces.FileFactory; |
| 4 | +import com.alipay.rdf.file.interfaces.FileReader; |
| 5 | +import com.alipay.rdf.file.interfaces.FileWriter; |
| 6 | +import com.alipay.rdf.file.model.FileConfig; |
| 7 | +import com.alipay.rdf.file.model.StorageConfig; |
| 8 | +import com.alipay.rdf.file.util.DateUtil; |
| 9 | +import com.alipay.rdf.file.util.TemporaryFolderUtil; |
| 10 | +import org.junit.After; |
| 11 | +import org.junit.Assert; |
| 12 | +import org.junit.Before; |
| 13 | +import org.junit.Test; |
| 14 | + |
| 15 | +import java.io.File; |
| 16 | +import java.io.IOException; |
| 17 | +import java.math.BigDecimal; |
| 18 | +import java.util.Date; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +/** |
| 23 | + * sp 分隔符测试 写测试 |
| 24 | + */ |
| 25 | +public class SpSeperatorFileWriterTest { |
| 26 | + TemporaryFolderUtil tf = new TemporaryFolderUtil(); |
| 27 | + |
| 28 | + @Before |
| 29 | + public void setUp() throws IOException { |
| 30 | + tf.create(); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testWriter() throws Exception { |
| 35 | + String filePath = tf.getRoot().getAbsolutePath(); |
| 36 | + System.out.println(filePath); |
| 37 | + |
| 38 | + FileConfig config = new FileConfig(new File(filePath, "test.txt").getAbsolutePath(), |
| 39 | + "/writer/template/sp_split.json", new StorageConfig("nas")); |
| 40 | + config.setLineBreak("\r"); |
| 41 | + config.setFileEncoding("UTF-8"); |
| 42 | + writeAndValide(config); |
| 43 | + } |
| 44 | + |
| 45 | + private void writeAndValide(FileConfig config) throws Exception { |
| 46 | + FileWriter fileWriter = FileFactory.createWriter(config); |
| 47 | + |
| 48 | + Map<String, Object> head = new HashMap<String, Object>(); |
| 49 | + head.put("totalCount", 2); |
| 50 | + head.put("totalAmount", new BigDecimal("23.22")); |
| 51 | + fileWriter.writeHead(head); |
| 52 | + |
| 53 | + Map<String, Object> body = new HashMap<String, Object>(); |
| 54 | + |
| 55 | + Date testDate = DateUtil.parse("2017-01-03 12:22:33", "yyyy-MM-dd HH:mm:ss"); |
| 56 | + |
| 57 | + body.put("seq", "seq12345"); |
| 58 | + body.put("instSeq", "303"); |
| 59 | + body.put("gmtApply", testDate); |
| 60 | + body.put("date", testDate); |
| 61 | + body.put("dateTime", testDate); |
| 62 | + body.put("applyNumber", 12); |
| 63 | + body.put("amount", new BigDecimal("1.22")); |
| 64 | + body.put("age", new Integer(33)); |
| 65 | + body.put("longN", new Long(33)); |
| 66 | + body.put("bol", true); |
| 67 | + body.put("memo", "memo1"); |
| 68 | + fileWriter.writeRow(body); |
| 69 | + |
| 70 | + testDate = DateUtil.parse("2016-02-03 12:22:33", "yyyy-MM-dd HH:mm:ss"); |
| 71 | + |
| 72 | + body.put("seq", "seq234567"); |
| 73 | + body.put("instSeq", "505"); |
| 74 | + body.put("gmtApply", testDate); |
| 75 | + body.put("date", testDate); |
| 76 | + body.put("dateTime", testDate); |
| 77 | + body.put("applyNumber", 12); |
| 78 | + body.put("amount", new BigDecimal("1.09")); |
| 79 | + body.put("age", 66); |
| 80 | + body.put("longN", 125); |
| 81 | + body.put("bol", false); |
| 82 | + body.put("memo", "memo2"); |
| 83 | + fileWriter.writeRow(body); |
| 84 | + |
| 85 | + fileWriter.close(); |
| 86 | + |
| 87 | + FileReader fileReader = FileFactory.createReader(config); |
| 88 | + HashMap readHead = fileReader.readHead(HashMap.class); |
| 89 | + |
| 90 | + Assert.assertEquals("{totalAmount=23.22, totalCount=2}", readHead.toString()); |
| 91 | + System.out.println(readHead); |
| 92 | + HashMap row1 = fileReader.readRow(HashMap.class); |
| 93 | + HashMap row2 = fileReader.readRow(HashMap.class); |
| 94 | + Assert.assertEquals("{date=Tue Jan 03 00:00:00 CST 2017, dateTime=Tue Jan 03 12:22:33 CST 2017, amount=1.22, instSeq=303, longN=33, memo=memo1, gmtApply=Tue Jan 03 12:22:33 CST 2017, seq=seq12345, age=33, bol=true, applyNumber=12}", row1.toString()); |
| 95 | + Assert.assertEquals("{date=Wed Feb 03 00:00:00 CST 2016, dateTime=Wed Feb 03 12:22:33 CST 2016, amount=1.09, instSeq=505, longN=125, memo=memo2, gmtApply=Wed Feb 03 12:22:33 CST 2016, seq=seq234567, age=66, bol=false, applyNumber=12}", row2.toString()); |
| 96 | + System.out.println(row1); |
| 97 | + System.out.println(row2); |
| 98 | + } |
| 99 | + |
| 100 | + @After |
| 101 | + public void after() { |
| 102 | + tf.delete(); |
| 103 | + } |
| 104 | +} |
0 commit comments