EmailAddressTokenizer { public static Collection<EmailAddressToken> tokenize(final CharSequence cs) { return tokenize(cs, false); }  static Collection<EmailAddressToken> tokenize(final CharSequence cs); static Collection<EmailAddressToken> tokenize(final CharSequence cs, final boolean requireExplicitDelimiter);  }
@Test public void emptySequence() { Assert.assertEquals(Collections.emptyList(), EmailAddressTokenizer.tokenize("")); }
@Test public void multipleWithQuotedLabel() { final String input = "\"Last, First\" <first.last@example.com>, Test <test@example.com>"; final Collection<EmailAddressToken> expected = ImmutableList.of( new EmailAddressToken( 0, 38, EmailAddress.builder().email("first.last@example.com").name("Last, First").build() ), new EmailAddressToken( 39, 62, EmailAddress.builder().email("test@example.com").name("Test").build() ) ); final Collection<EmailAddressToken> actual = EmailAddressTokenizer.tokenize(input); Assert.assertArrayEquals( expected.toArray(new EmailAddressToken[0]), actual.toArray(new EmailAddressToken[0]) ); }
@Test public void multipleWithQuotedLabelExplicitDelimiter() { final String input = "\"Last, First\" <first.last@example.com>, Test <test@example.com>"; final Collection<EmailAddressToken> expected = ImmutableList.of( new EmailAddressToken( 0, 38, EmailAddress.builder().email("first.last@example.com").name("Last, First").build() ) ); final Collection<EmailAddressToken> actual = EmailAddressTokenizer.tokenize(input, true); Assert.assertArrayEquals( expected.toArray(new EmailAddressToken[0]), actual.toArray(new EmailAddressToken[0]) ); }
@Test public void singleChar() { final String input = "s"; final Collection<EmailAddressToken> expected = ImmutableList.of( new EmailAddressToken( 0, 0, EmailAddress.builder().email("s").build() ) ); final Collection<EmailAddressToken> actual = EmailAddressTokenizer.tokenize(input); Assert.assertArrayEquals( expected.toArray(new EmailAddressToken[0]), actual.toArray(new EmailAddressToken[0]) ); }
@Test public void whitespaces() { final String input = " "; final Collection<EmailAddressToken> actual = EmailAddressTokenizer.tokenize(input); Assert.assertArrayEquals( new EmailAddressToken[0], actual.toArray(new EmailAddressToken[0]) ); }
@Test public void simpleStandAloneEmailAddress() { final String input = "test@example.com"; final Collection<EmailAddressToken> expected = ImmutableList.of( new EmailAddressToken( 0, 15, EmailAddress.builder().email("test@example.com").build() ) ); final Collection<EmailAddressToken> actual = EmailAddressTokenizer.tokenize(input); Assert.assertArrayEquals( expected.toArray(new EmailAddressToken[0]), actual.toArray(new EmailAddressToken[0]) ); }
@Test public void singleEmailWithExcessWhiteSpaceAfterDelimiter() { final String input = "test@example.com, "; final Collection<EmailAddressToken> expected = ImmutableList.of( new EmailAddressToken( 0, 16, EmailAddress.builder().email("test@example.com").build() ) ); final Collection<EmailAddressToken> actual = EmailAddressTokenizer.tokenize(input); Assert.assertArrayEquals( expected.toArray(new EmailAddressToken[0]), actual.toArray(new EmailAddressToken[0]) ); }
@Test public void simpleCommaSeparated() { final String input = "a@example.com, b@example.com"; final Collection<EmailAddressToken> expected = ImmutableList.of( new EmailAddressToken( 0, 13, EmailAddress.builder().email("a@example.com").build() ), new EmailAddressToken( 14, 27, EmailAddress.builder().email("b@example.com").build() ) ); final Collection<EmailAddressToken> actual = EmailAddressTokenizer.tokenize(input); Assert.assertArrayEquals( expected.toArray(new EmailAddressToken[0]), actual.toArray(new EmailAddressToken[0]) ); }
@Test public void bracketedCommaSeparated() { final String input = "<a@example.com>, <b@example.com>"; final Collection<EmailAddressToken> expected = ImmutableList.of( new EmailAddressToken( 0, 15, EmailAddress.builder().email("a@example.com").build() ), new EmailAddressToken( 16, 31, EmailAddress.builder().email("b@example.com").build() ) ); final Collection<EmailAddressToken> actual = EmailAddressTokenizer.tokenize(input); Assert.assertArrayEquals( expected.toArray(new EmailAddressToken[0]), actual.toArray(new EmailAddressToken[0]) ); }
@Test public void singleEmailWithLabel() { final String input = "A <a@example.com>"; final Collection<EmailAddressToken> expected = ImmutableList.of( new EmailAddressToken( 0, 16, EmailAddress.builder().email("a@example.com").name("A").build() ) ); final Collection<EmailAddressToken> actual = EmailAddressTokenizer.tokenize(input); Assert.assertArrayEquals( expected.toArray(new EmailAddressToken[0]), actual.toArray(new EmailAddressToken[0]) ); }
@Test public void singleEmailWithLabelExplicitDelimiter() { final String input = "A <a@example.com>"; final Collection<EmailAddressToken> actual = EmailAddressTokenizer.tokenize(input, true); Assert.assertArrayEquals( new EmailAddressToken[0], actual.toArray(new EmailAddressToken[0]) ); }
@Test public void singleEmailWithLabelExcessWhiteSpace() { final String input = " A <a@example.com>"; final Collection<EmailAddressToken> expected = ImmutableList.of( new EmailAddressToken( 0, 19, EmailAddress.builder().email("a@example.com").name("A").build() ) ); final Collection<EmailAddressToken> actual = EmailAddressTokenizer.tokenize(input); Assert.assertArrayEquals( expected.toArray(new EmailAddressToken[0]), actual.toArray(new EmailAddressToken[0]) ); }