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 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. Copyright (c) 2007 Health Market Science, Inc.
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with this library; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  14. USA
  15. You can contact Health Market Science at info@healthmarketscience.com
  16. or at the following address:
  17. Health Market Science
  18. 2700 Horizon Drive
  19. Suite 200
  20. King of Prussia, PA 19406
  21. */
  22. package com.healthmarketscience.jackcess;
  23. import java.io.BufferedWriter;
  24. import java.io.StringWriter;
  25. import java.text.DateFormat;
  26. import java.text.SimpleDateFormat;
  27. import junit.framework.TestCase;
  28. import org.apache.commons.lang.SystemUtils;
  29. import static com.healthmarketscience.jackcess.Database.*;
  30. import static com.healthmarketscience.jackcess.DatabaseTest.*;
  31. /**
  32. *
  33. * @author James Ahlborn
  34. */
  35. public class ExportTest extends TestCase
  36. {
  37. private static final String NL = SystemUtils.LINE_SEPARATOR;
  38. public ExportTest(String name) {
  39. super(name);
  40. }
  41. public void testExportToFile() throws Exception
  42. {
  43. DateFormat df = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
  44. for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) {
  45. Database db = create(fileFormat);
  46. Table t = new TableBuilder("test")
  47. .addColumn(new ColumnBuilder("col1", DataType.TEXT).toColumn())
  48. .addColumn(new ColumnBuilder("col2", DataType.LONG).toColumn())
  49. .addColumn(new ColumnBuilder("col3", DataType.DOUBLE).toColumn())
  50. .addColumn(new ColumnBuilder("col4", DataType.OLE).toColumn())
  51. .addColumn(new ColumnBuilder("col5", DataType.BOOLEAN).toColumn())
  52. .addColumn(new ColumnBuilder("col6", DataType.SHORT_DATE_TIME).toColumn())
  53. .toTable(db);
  54. t.addRow("some text||some more", 13, 13.25, createString(30).getBytes(), true, df.parse("19801231 00:00:00"));
  55. t.addRow("crazy'data\"here", -345, -0.000345, createString(7).getBytes(),
  56. true, null);
  57. StringWriter out = new StringWriter();
  58. ExportUtil.exportWriter(db, "test", new BufferedWriter(out));
  59. String expected =
  60. "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" + NL +
  61. "79 7A 61 62 63 64\",true,Wed Dec 31 00:00:00 EST 1980" + NL +
  62. "\"crazy'data\"\"here\",-345,-3.45E-4,61 62 63 64 65 66 67,true," + NL;
  63. assertEquals(expected, out.toString());
  64. out = new StringWriter();
  65. ExportUtil.exportWriter(db, "test", new BufferedWriter(out),
  66. true, "||", '\'', SimpleExportFilter.INSTANCE);
  67. expected =
  68. "col1||col2||col3||col4||col5||col6" + NL +
  69. "'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" + NL +
  70. "79 7A 61 62 63 64'||true||Wed Dec 31 00:00:00 EST 1980" + NL +
  71. "'crazy''data\"here'||-345||-3.45E-4||61 62 63 64 65 66 67||true||" + NL;
  72. assertEquals(expected, out.toString());
  73. }
  74. }
  75. }