예제 #1
0
  /** 1つのRecordInOutインスタンスから複数のCsvReaderをopenしたとき、 それぞれのReaderでの処理が影響しないこと。 */
  @Test
  public void openMultiReader() throws Throwable {
    // ## Arrange ##
    final MapCsvLayout<String> layout = new MapCsvLayout<String>();

    // ## Act ##
    final RecordReader<Map<String, String>> csvReader1 =
        layout.build().openReader(getResourceAsReader("-6", "tsv"));

    final Map<String, String> bean1 = CollectionsUtil.newHashMap();
    final Map<String, String> bean2 = CollectionsUtil.newHashMap();

    // ## Assert ##
    csvReader1.read(bean1);
    logger.debug(bean1.toString());
    assertEquals(4, bean1.size());
    assertEquals("あ1", bean1.get("aaa"));
    assertEquals("い1", bean1.get("bbb"));
    assertEquals("う1", bean1.get("ccc"));
    assertEquals("え1", bean1.get("ddd"));

    final RecordReader<Map<String, String>> csvReader2 =
        layout.build().openReader(getResourceAsReader("-4", "tsv"));
    csvReader2.read(bean2);
    assertEquals(3, bean2.size());
    logger.debug(bean2.toString());
    assertEquals("あ1", bean2.get("aaa"));
    assertEquals("い1", bean2.get("bbb"));
    assertEquals(" ", bean2.get("ccc"));

    csvReader1.read(bean1);
    logger.debug(bean1.toString());
    assertEquals(4, bean1.size());
    assertEquals("あ2", bean1.get("aaa"));
    assertEquals("い2", bean1.get("bbb"));
    assertEquals("う2", bean1.get("ccc"));
    assertEquals("え2", bean1.get("ddd"));

    csvReader2.read(bean2);
    logger.debug(bean2.toString());
    assertEquals(3, bean2.size());
    assertEquals(null, bean2.get("aaa"));
    assertEquals("い2", bean2.get("bbb"));
    assertEquals(null, bean2.get("ccc"));

    csvReader1.close();
    csvReader2.close();
  }
예제 #2
0
  /** 空行をskipして読めること。 その際に、データ部の改行による空行を除かないこと。 */
  @Test
  public void read_skip_emptyline() throws Throwable {
    // ## Arrange ##
    final Reader r = getResourceAsReader("-11", "tsv");

    final MapCsvLayout<String> layout = new MapCsvLayout<String>();
    layout.setLineReaderHandler(new SkipEmptyLineReadEditor());

    // ## Act ##
    final RecordReader<Map<String, String>> csvReader = layout.build().openReader(r);

    // ## Assert ##
    final Map<String, String> bean = CollectionsUtil.newHashMap();
    assertEquals(true, csvReader.hasNext());
    csvReader.read(bean);
    logger.debug(bean.toString());
    assertEquals("あ1", bean.get("aaa"));
    assertEquals("い1", bean.get("bbb"));
    assertEquals("う1", bean.get("ccc"));

    assertEquals(true, csvReader.hasNext());
    csvReader.read(bean);
    logger.debug(bean.toString());
    assertEquals("あ2", bean.get("aaa"));
    assertEquals("い\r\n\r\n\r\n2", bean.get("bbb"));
    assertEquals("う2", bean.get("ccc"));

    assertEquals(false, csvReader.hasNext());
    csvReader.close();
  }
예제 #3
0
  /** CSVヘッダが無い場合。 */
  @Test
  public void read_noheader() throws Throwable {
    // ## Arrange ##
    final Reader r = getResourceAsReader("-3", "tsv");

    final MapCsvLayout<String> layout = new MapCsvLayout<String>();
    layout.setupColumns(
        new SetupBlock<CsvColumnSetup>() {
          @Override
          public void setup(final CsvColumnSetup setup) {
            /*
             * CSVの列順
             */
            setup.column("ccc");
            setup.column("aaa");
            setup.column("bbb");
          }
        });

    layout.setWithHeader(false);

    // ## Act ##
    final RecordReader<Map<String, String>> csvReader = layout.build().openReader(r);

    // ## Assert ##
    final Map<String, String> bean = CollectionsUtil.newHashMap();
    assertReadNoheader(csvReader, bean);
  }
예제 #4
0
  private void _assert(final RecordReader<Map<String, String>> csvReader) throws IOException {
    final Map<String, String> bean = CollectionsUtil.newHashMap();
    assertEquals(true, csvReader.hasNext());
    csvReader.read(bean);
    assertEquals("a1", bean.get("a"));
    assertEquals("b1", bean.get("b"));
    assertEquals("c1", bean.get("c"));

    assertEquals(false, csvReader.hasNext());
    csvReader.close();
  }
예제 #5
0
  /** 末端まで達した後のreadでは、例外が発生すること。 */
  @Test
  public void read_afterLast() throws Throwable {
    // ## Arrange ##
    final Reader r = getResourceAsReader("-1", "tsv");

    final MapCsvLayout<String> layout = new MapCsvLayout<String>();

    // ## Act ##
    final RecordReader<Map<String, String>> csvReader = layout.build().openReader(r);

    // ## Assert ##
    final Map<String, String> bean = CollectionsUtil.newHashMap();
    BeanCsvReaderTest.assertReadAfterLast(csvReader, bean);
  }
예제 #6
0
  @Test
  public void readCsv() throws Throwable {
    // ## Arrange ##
    final Reader reader =
        BeanCsvReaderTest.getResourceAsReader("-8", "csv", Charset.forName("UTF-8"));

    final MapCsvLayout<String> layout = new MapCsvLayout<String>();
    layout.setElementSeparator(CsvSetting.COMMA);

    // ## Act ##
    final RecordReader<Map<String, String>> csvReader = layout.build().openReader(reader);

    // ## Assert ##
    final Map<String, String> bean = CollectionsUtil.newHashMap();
    assertReadCsv(csvReader, bean);
  }
예제 #7
0
  /**
   * 独自レイアウトのtsvファイルを入力する。
   *
   * <p>- header部が3行 - footer部が2行 - データ部は2列目から という想定。
   */
  @Test
  public void read_customLayout() throws Throwable {
    // ## Arrange ##
    final Reader r = getResourceAsReader("-7", "tsv");

    final MapCsvLayout<String> layout = new MapCsvLayout<String>();
    final TestReadEditor readEditor = new TestReadEditor();
    layout.setReaderHandler(readEditor);

    // ## Act ##
    final RecordReader<Map<String, String>> csvReader = layout.build().openReader(r);

    // ## Assert ##
    final Map<String, String> bean = CollectionsUtil.newHashMap();
    assertReadCustomLayout(csvReader, bean);
  }
예제 #8
0
  /** デフォルトではtrimしない。 */
  @Test
  public void read_trim_off() throws Throwable {
    // ## Arrange ##
    final MapCsvLayout<String> layout = new MapCsvLayout<String>();
    layout.setElementSeparator(CsvSetting.COMMA);

    // ## Act ##
    final RecordReader<Map<String, String>> csvReader =
        layout.build().openReader(new StringReader("aaa,bbb,ccc\n" + "  , b , \n"));

    // ## Assert ##
    final Map<String, String> bean = CollectionsUtil.newHashMap();
    assertEquals(true, csvReader.hasNext());
    csvReader.read(bean);
    assertEquals("  ", bean.get("aaa"));
    assertEquals(" b ", bean.get("bbb"));
    assertEquals(" ", bean.get("ccc"));

    assertEquals(false, csvReader.hasNext());
    csvReader.close();
  }
예제 #9
0
  /** setupしない列が入力ファイルに存在する場合は無視する。 */
  @Test
  public void read4() throws Throwable {
    // ## Arrange ##
    final Reader r = getResourceAsReader("-2", "tsv");

    final MapCsvLayout<String> layout = new MapCsvLayout<String>();
    layout.setupColumns(
        new SetupBlock<CsvColumnSetup>() {
          @Override
          public void setup(final CsvColumnSetup setup) {
            setup.column("あ").toProperty("aaa");
            setup.column("ううう").toProperty("ccc");
          }
        });

    // ## Act ##
    final RecordReader<Map<String, String>> csvReader = layout.build().openReader(r);

    // ## Assert ##
    final Map<String, String> bean = CollectionsUtil.newHashMap();
    assertRead4(csvReader, bean);
  }
예제 #10
0
  /** Bean側をBigDecimalで扱えること */
  @Test
  public void read_bigDecimal() throws Throwable {
    // ## Arrange ##
    final MapCsvLayout<Object> layout = new MapCsvLayout<Object>();
    layout.setupColumns(
        new SetupBlock<CsvColumnSetup>() {
          @Override
          public void setup(final CsvColumnSetup setup) {
            setup.column("aaa").withConverter(new BigDecimalConverter());
            setup.column("bbb");
          }
        });

    // ## Act ##
    final RecordReader<Map<String, Object>> csvReader =
        layout.build().openReader(getResourceAsReader("-12", "tsv"));

    // ## Assert ##
    final Map<String, Object> bean = CollectionsUtil.newHashMap();

    assertEquals(true, csvReader.hasNext());
    csvReader.read(bean);
    assertEquals("11.10", ((BigDecimal) bean.get("aaa")).toPlainString());
    assertEquals("21.02", bean.get("bbb"));

    assertEquals(true, csvReader.hasNext());
    csvReader.read(bean);
    assertEquals(null, bean.get("aaa"));
    assertEquals(null, bean.get("bbb"));

    assertEquals(true, csvReader.hasNext());
    csvReader.read(bean);
    assertEquals("1101.45", ((BigDecimal) bean.get("aaa")).toPlainString());
    assertEquals("1,201.56", bean.get("bbb"));

    assertEquals(false, csvReader.hasNext());
    csvReader.close();
  }
예제 #11
0
  /** CSV側の列がsetupした列より少ない場合、 CSV側に無い項目はnullセットされること。 */
  @Test
  public void read_smallColumns() throws Throwable {
    // ## Arrange ##
    final Reader r = BeanCsvReaderTest.getResourceAsReader("-9", "tsv", Charset.forName("UTF-8"));

    final MapCsvLayout<String> layout = new MapCsvLayout<String>();
    layout.setupColumns(
        new SetupBlock<CsvColumnSetup>() {
          @Override
          public void setup(final CsvColumnSetup setup) {
            setup.column("aaa");
            setup.column("bbb");
            setup.column("ccc");
          }
        });

    // ## Act ##
    final RecordReader<Map<String, String>> csvReader = layout.build().openReader(r);

    // ## Assert ##
    final Map<String, String> bean = CollectionsUtil.newHashMap();
    assertReadSmallColumns(csvReader, bean);
  }