You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExportTest.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. Copyright (c) 2007 Health Market Science, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.healthmarketscience.jackcess.util;
  14. import java.io.BufferedWriter;
  15. import java.io.StringWriter;
  16. import java.text.DateFormat;
  17. import java.text.SimpleDateFormat;
  18. import java.util.Date;
  19. import static com.healthmarketscience.jackcess.Database.*;
  20. import com.healthmarketscience.jackcess.ColumnBuilder;
  21. import com.healthmarketscience.jackcess.DataType;
  22. import com.healthmarketscience.jackcess.Database;
  23. import com.healthmarketscience.jackcess.DateTimeType;
  24. import com.healthmarketscience.jackcess.Table;
  25. import com.healthmarketscience.jackcess.TableBuilder;
  26. import com.healthmarketscience.jackcess.impl.JetFormatTest;
  27. import static com.healthmarketscience.jackcess.TestUtil.*;
  28. import static org.junit.jupiter.api.Assertions.*;
  29. import org.junit.jupiter.api.Test;
  30. /**
  31. *
  32. * @author James Ahlborn
  33. */
  34. public class ExportTest
  35. {
  36. private static final String NL = System.lineSeparator();
  37. @Test
  38. public void testExportToFile() throws Exception
  39. {
  40. DateFormat df = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
  41. df.setTimeZone(TEST_TZ);
  42. for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) {
  43. Database db = create(fileFormat);
  44. db.setDateTimeType(DateTimeType.DATE);
  45. db.setTimeZone(TEST_TZ);
  46. Table t = new TableBuilder("test")
  47. .addColumn(new ColumnBuilder("col1", DataType.TEXT))
  48. .addColumn(new ColumnBuilder("col2", DataType.LONG))
  49. .addColumn(new ColumnBuilder("col3", DataType.DOUBLE))
  50. .addColumn(new ColumnBuilder("col4", DataType.OLE))
  51. .addColumn(new ColumnBuilder("col5", DataType.BOOLEAN))
  52. .addColumn(new ColumnBuilder("col6", DataType.SHORT_DATE_TIME))
  53. .toTable(db);
  54. Date testDate = df.parse("19801231 00:00:00");
  55. t.addRow("some text||some more", 13, 13.25, createString(30).getBytes(),
  56. true, testDate);
  57. t.addRow("crazy'data\"here", -345, -0.000345, createString(7).getBytes(),
  58. true, null);
  59. t.addRow("C:\\temp\\some_file.txt", 25, 0.0, null, false, null);
  60. StringWriter out = new StringWriter();
  61. new ExportUtil.Builder(db, "test")
  62. .exportWriter(new BufferedWriter(out));
  63. String expected =
  64. "some text||some more,13,13.25,\"61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F 70 71 72 73 74 75 76 77 78\n79 7A 61 62 63 64\",true," + testDate + NL +
  65. "\"crazy'data\"\"here\",-345,-3.45E-4,61 62 63 64 65 66 67,true," + NL +
  66. "C:\\temp\\some_file.txt,25,0.0,,false," + NL;
  67. assertEquals(expected, out.toString());
  68. out = new StringWriter();
  69. new ExportUtil.Builder(db, "test")
  70. .setHeader(true)
  71. .setDelimiter("||")
  72. .setQuote('\'')
  73. .exportWriter(new BufferedWriter(out));
  74. expected =
  75. "col1||col2||col3||col4||col5||col6" + NL +
  76. "'some text||some more'||13||13.25||'61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F 70 71 72 73 74 75 76 77 78\n79 7A 61 62 63 64'||true||" + testDate + NL +
  77. "'crazy''data\"here'||-345||-3.45E-4||61 62 63 64 65 66 67||true||" + NL +
  78. "C:\\temp\\some_file.txt||25||0.0||||false||" + NL;
  79. assertEquals(expected, out.toString());
  80. ExportFilter oddFilter = new SimpleExportFilter() {
  81. private int _num;
  82. @Override
  83. public Object[] filterRow(Object[] row) {
  84. if((_num++ % 2) == 1) {
  85. return null;
  86. }
  87. return row;
  88. }
  89. };
  90. out = new StringWriter();
  91. new ExportUtil.Builder(db, "test")
  92. .setFilter(oddFilter)
  93. .exportWriter(new BufferedWriter(out));
  94. expected =
  95. "some text||some more,13,13.25,\"61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F 70 71 72 73 74 75 76 77 78\n79 7A 61 62 63 64\",true," + testDate + NL +
  96. "C:\\temp\\some_file.txt,25,0.0,,false," + NL;
  97. assertEquals(expected, out.toString());
  98. }
  99. }
  100. }