Java Code Examples for au.com.bytecode.opencsv.bean.CsvToBean

The following code examples are extracted from open source projects. You can click to vote up the examples that are useful to you.

Example 1

From project opencsv, under directory /test/integrationTest/issue3189428/.

Source file: CsvSample.java

  25 
vote

/** 
 * This approach seems to fail with embedded newlines; that might be a weakness of the mapping strategy support classes.
 * @param originalCommentText
 * @throws FileNotFoundException
 */
protected void testMappingStrategyRead(String originalCommentText) throws FileNotFoundException {
  ColumnPositionMappingStrategy mappingStrategy=new ColumnPositionMappingStrategy();
  mappingStrategy.setType(MyBean.class);
  String[] columns=new String[]{"name","value","amount1","currency","comments"};
  mappingStrategy.setColumnMapping(columns);
  CsvToBean csv=new CsvToBean();
  CSVReader reader=new CSVReader(new FileReader(filePath),CSVParser.DEFAULT_SEPARATOR,CSVParser.DEFAULT_QUOTE_CHARACTER,CSVParser.DEFAULT_ESCAPE_CHARACTER,0,false,false);
  List<MyBean> list=csv.parse(mappingStrategy,reader);
  if (list.size() != 3) {
    System.out.println("Error - list size is wrong.");
  }
  MyBean myBean=list.get(2);
  if (!myBean.getName().equals("field1")) {
    System.out.println("MappingStrategy Assert Error: Name is wrong.");
  }
  if (!myBean.getValue().equals("3.0")) {
    System.out.println("MappingStrategy Assert Error: Value is wrong.");
  }
  if (!myBean.getAmount1().equals("3,147.25")) {
    System.out.println("MappingStrategy Assert Error: Amount1 is wrong.");
  }
  if (!myBean.getCurrency().equals("$3,147.26")) {
    System.out.println("MappingStrategy Assert Error: Currency is wrong.");
  }
  printfield("MyBeanComments:         ",myBean.getComments());
  printfield("OriginalCommentText:    ",originalCommentText);
  if (!myBean.getComments().equals(originalCommentText)) {
    System.out.println("MappingStrategy Assert Error: Comment is wrong.");
  }
}
 

Example 2

From project caseconductor-platform, under directory /utest-domain-services/src/main/java/com/utest/domain/service/impl/.

Source file: TestCaseServiceImpl.java

  23 
vote

@Override public void importSingleStepTestCasesFromCsv(final String cvs_,final Integer productId_) throws Exception {
  Product product=getRequiredEntityById(Product.class,productId_);
  final String[] columns={"testCaseName","externalAuthorEmail","createDate","description","bugList","instruction","expectedResult","tagList","testSuiteList","type"};
  final CSVReader reader=new CSVReader(new StringReader(cvs_));
  final ColumnPositionMappingStrategy<TestCaseExportSingleStepExtendedView> strat=new ColumnPositionMappingStrategy<TestCaseExportSingleStepExtendedView>();
  strat.setColumnMapping(columns);
  strat.setType(TestCaseExportSingleStepExtendedView.class);
  final CsvToBean<TestCaseExportSingleStepExtendedView> csvToBean=new CsvToBean<TestCaseExportSingleStepExtendedView>();
  final List<TestCaseExportSingleStepExtendedView> nodes=csvToBean.parse(strat,reader);
  TestCase testCase=null;
  TestCaseVersion testCaseVersion=null;
  Map<String,Set<Integer>> tagMap=new HashMap<String,Set<Integer>>();
  Map<String,Set<Integer>> suiteMap=new HashMap<String,Set<Integer>>();
  for (int i=1; i < nodes.size(); i++) {
    final TestCaseExportSingleStepExtendedView export=nodes.get(i);
    if (!TestCaseExportSingleStepExtendedView.HEADER_TYPE.equalsIgnoreCase(export.getType())) {
      throw new InvalidImportFileFormatException(InvalidImportFileFormatException.ERROR_INVALID_TEST_CASE_HEADER_TYPE + ", row: " + i);
    }
    testCase=addTestCase(productId_,10,3,export.getTestCaseName() + " : " + new Date().getTime(),export.getDescription(),export.getExternalAuthorEmail());
    testCaseVersion=testCase.getLatestVersion();
    addTestCaseStep(testCaseVersion.getId(),testCase.getName() + " : step 1",1,export.getInstruction(),export.getExpectedResult(),TestCase.DEFAULT_STEP_ESTIMATED_TIME_IN_MIN);
    if (export.getTagList() != null && export.getTagList().length() > 0) {
      loadTestCaseAssociations(tagMap,export.getTagList(),",",testCaseVersion.getId());
    }
    if (export.getTestSuiteList() != null && export.getTestSuiteList().length() > 0) {
      loadTestCaseAssociations(suiteMap,export.getTestSuiteList(),",",testCaseVersion.getId());
    }
  }
  createAssociatedTags(tagMap,product);
  createAssociatedTestSuites(suiteMap,product);
}
 

Example 3

From project onebusaway-nyc, under directory /onebusaway-nyc-transit-data-manager/onebusaway-nyc-tdm-adapters/src/main/java/org/onebusaway/nyc/transit_data_manager/adapters/input/readers/.

Source file: CSVCrewAssignsInputConverter.java

  23 
vote

public List<MtaUtsCrewAssignment> getCrewAssignments() throws FileNotFoundException {
  Map<String,String> colMap=new HashMap<String,String>();
  colMap.put("DEPOT","depotField");
  colMap.put("AUTH_ID","authIdField");
  colMap.put("PASS_NUMBER","passNumberField");
  colMap.put("ROUTE","routeField");
  colMap.put("RUN_NUMBER","runNumberField");
  colMap.put("SERV_ID","servIdField");
  colMap.put("DATE","dateField");
  colMap.put("TIMESTAMP","timestampField");
  HeaderColumnNameTranslateMappingStrategy<MtaUtsCrewAssignment> mapStrat=new HeaderColumnNameTranslateMappingStrategy<MtaUtsCrewAssignment>();
  mapStrat.setType(MtaUtsCrewAssignment.class);
  mapStrat.setColumnMapping(colMap);
  CsvToBean<MtaUtsCrewAssignment> crewAssignsCsv=new CsvToBean<MtaUtsCrewAssignment>();
  List<MtaUtsCrewAssignment> crewAssignments=null;
  Reader inputReader=null;
  try {
    inputReader=new FileReader(csvFile);
    crewAssignments=crewAssignsCsv.parse(mapStrat,inputReader);
  }
  finally {
    if (inputReader != null)     try {
      inputReader.close();
    }
 catch (    IOException e) {
    }
  }
  return crewAssignments;
}
 

Example 4

From project onebusaway-nyc, under directory /onebusaway-nyc-transit-data-manager/onebusaway-nyc-tdm-adapters/src/main/java/org/onebusaway/nyc/transit_data_manager/adapters/input/readers/.

Source file: CSVVehicleAssignsInputConverter.java

  23 
vote

public List<MtaUtsVehiclePullInPullOut> getVehicleAssignments() throws FileNotFoundException {
  Map<String,String> colMap=new HashMap<String,String>();
  colMap.put("ROUTE","routeField");
  colMap.put("DEPOT","depotField");
  colMap.put("RUN NUMBER","runNumberField");
  colMap.put("DATE","dateField");
  colMap.put("SCHED PO","schedPOField");
  colMap.put("ACTUAL PO","actualPOField");
  colMap.put("SCHED PI","schedPIField");
  colMap.put("ACTUAL PI","actualPIField");
  colMap.put("BUS NUMBER","busNumberField");
  colMap.put("BUS MILEAGE","busMileageField");
  colMap.put("PASS","passNumberField");
  colMap.put("AUTH ID","authIdField");
  HeaderColumnNameTranslateMappingStrategy<MtaUtsVehiclePullInPullOut> mapStrat=new HeaderColumnNameTranslateMappingStrategy<MtaUtsVehiclePullInPullOut>();
  mapStrat.setType(MtaUtsVehiclePullInPullOut.class);
  mapStrat.setColumnMapping(colMap);
  CsvToBean<MtaUtsVehiclePullInPullOut> crewAssignsCsv=new CsvToBean<MtaUtsVehiclePullInPullOut>();
  List<MtaUtsVehiclePullInPullOut> vehicleAssignments=null;
  Reader inputReader=null;
  try {
    inputReader=new FileReader(csvFile);
    vehicleAssignments=crewAssignsCsv.parse(mapStrat,inputReader);
  }
  finally {
    if (inputReader != null)     try {
      inputReader.close();
    }
 catch (    IOException e) {
    }
  }
  return vehicleAssignments;
}
 

Example 5

From project opencsv, under directory /test/integrationTest/issue3402853/.

Source file: HeaderColumnNameMappingStrategyUserTest.java

  23 
vote

private List<MockUserBean> createTestParseResult() throws FileNotFoundException {
  CSVReader reader=new CSVReader(new FileReader(USER_FILE));
  HeaderColumnNameMappingStrategy<MockUserBean> strat=new HeaderColumnNameMappingStrategy<MockUserBean>();
  strat.setType(MockUserBean.class);
  CsvToBean<MockUserBean> csv=new CsvToBean<MockUserBean>();
  return csv.parse(strat,reader);
}