/** * Yields the tokens resulting from the splitting of the supplied * <code>line</code>. * * @param line the line to be tokenized (can be <code>null</code>) * * @return the resulting tokens */ @Override public FieldSet tokenize(String line) { if (line == null) { line = ""; } List<String> tokens = new ArrayList<>(doTokenize(line)); // if names are set and strict flag is false if ( ( names.length != 0 ) && ( ! strict ) ) { adjustTokenCountIfNecessary( tokens ); } String[] values = tokens.toArray(new String[tokens.size()]); if (names.length == 0) { return fieldSetFactory.create(values); } else if (values.length != names.length) { throw new IncorrectTokenCountException(names.length, values.length, line); } return fieldSetFactory.create(values, names); }
@Test public void testEmptyLineWithNames(){ tokenizer.setNames(new String[]{"A", "B"}); try{ tokenizer.tokenize(""); } catch(IncorrectTokenCountException ex){ assertEquals(2, ex.getExpectedCount()); assertEquals(0, ex.getActualCount()); assertEquals("", ex.getInput()); } }
@Test public void testTokenizerInvalidSetup() { tokenizer.setNames(new String[] { "a", "b" }); tokenizer.setColumns(new Range[] { new Range(1, 5) }); try { tokenizer.tokenize("12345"); fail("Exception was expected: too few names provided"); } catch (IncorrectTokenCountException e) { assertEquals(2, e.getExpectedCount()); assertEquals(1, e.getActualCount()); } }
@Test public void testTooManyNames() { tokenizer.setNames(new String[] {"A", "B", "C", "D"}); try{ tokenizer.tokenize("a,b,c"); } catch(IncorrectTokenCountException e){ assertEquals(4, e.getExpectedCount()); assertEquals(3, e.getActualCount()); assertEquals("a,b,c", e.getInput()); } }
/** * Yields the tokens resulting from the splitting of the supplied * <code>line</code>. * * @param line the line to be tokenized (can be <code>null</code>) * * @return the resulting tokens */ @Override public FieldSet tokenize(String line) { if (line == null) { line = ""; } List<String> tokens = new ArrayList<String>(doTokenize(line)); // if names are set and strict flag is false if ( ( names.length != 0 ) && ( ! strict ) ) { adjustTokenCountIfNecessary( tokens ); } String[] values = tokens.toArray(new String[tokens.size()]); if (names.length == 0) { return fieldSetFactory.create(values); } else if (values.length != names.length) { throw new IncorrectTokenCountException(names.length, values.length, line); } return fieldSetFactory.create(values, names); }
@Test public void testTooFewNames() { tokenizer.setNames(new String[] {"A", "B"}); try { tokenizer.tokenize("a,b,c"); fail("Expected IncorrectTokenCountException"); } catch (IncorrectTokenCountException e) { assertEquals(2, e.getExpectedCount()); assertEquals(3, e.getActualCount()); assertEquals("a,b,c", e.getInput()); } }