private boolean havingAdditionalFields(final TableSchema tableSchema) { for (final Field field : tableSchema.getFields()) { if (field.getName() == null) { return true; } } return false; }
private long runTestForFile(String fileName, SchemaFactory schemaFactory) { final Date startTime = new Date(); long linesTested = 0; if (fileName == null) { validationLog.executionError("Null file"); return linesTested; } if (!fileName.endsWith("txt")) { testReport.addError( "0-0", startTime, fileName, resourceManager.getFilePath(), "", FILE_NAME_TEST_TYPE, "RF2 Compilant filename", fileName, "Incorrect file extension, should end with a .txt"); return linesTested; } TableSchema tableSchema; try { tableSchema = schemaFactory.createSchemaBean(fileName); } catch (final FileRecognitionException e) { // log the problem and continue to the next file testReport.addError( "0-0", startTime, fileName, resourceManager.getFilePath(), "", FILE_NAME_TEST_TYPE, "RF2 Compilant filename", fileName, e.getMessage()); return linesTested; } if (tableSchema == null) { // log the problem and continue to the next file testReport.addError( "0-0", startTime, fileName, resourceManager.getFilePath(), "", FILE_NAME_TEST_TYPE, "RF2 Compilant filename", fileName, "unexpected filename format."); return linesTested; } final boolean releaseInputFile = fileName.startsWith("rel2"); List<Field> fields = tableSchema.getFields(); if (fields != null) { try (BufferedReader reader = resourceManager.getReader(fileName, Charset.forName(UTF_8))) { String line; long lineNumber = 0; String[] columnData; final int configColumnCount = fields.size(); while ((line = reader.readLine()) != null) { int columnIndex = 0; linesTested++; lineNumber++; columnData = line.split("\t"); final int dataColumnCount = columnData.length; if (!(validateRow( startTime, fileName, line, lineNumber, configColumnCount, dataColumnCount))) { continue; } // check whether header fields not containing null values due to specific additional // fields if ((lineNumber == 1) && havingAdditionalFields(tableSchema)) { schemaFactory.populateExtendedRefsetAdditionalFieldNames(tableSchema, line); fields = tableSchema.getFields(); } for (final Field column : fields) { final String value = columnData[columnIndex]; if (lineNumber == 1) { // Test header value testHeaderValue(value, column, startTime, fileName, columnIndex); } else { testDataValue( lineNumber + "-" + columnIndex, lineNumber, value, column, startTime, fileName, releaseInputFile); } columnIndex++; } } } catch (final IOException e) { validationLog.executionError("Problem reading file {}", fileName, e); testReport.addError( "0-0", startTime, fileName, resourceManager.getFilePath(), null, FILE_NAME_TEST_TYPE, "", fileName, "Unable to read the file"); } } else { validationLog.executionError( "Invalid fileName {} does not match the expected pattern ", fileName); testReport.addError( "0-0", startTime, fileName, resourceManager.getFilePath(), "", FILE_NAME_TEST_TYPE, "", fileName, "valid release 2 filename"); } return linesTested; }