/** * Quote a string, if necessary, based upon the definition of an "atom," as defined by RFC2822 * (http://tools.ietf.org/html/rfc2822#section-3.2.4). Strings that consist purely of atoms are * left unquoted; anything else is returned as a quoted string. * @param text String to quote. * @return Possibly quoted string. */ public static String quoteAtoms(final String text) { if (ATOM.matcher(text).matches()) { return text; } else { return quoteString(text); } }
@Test public void stringQuotationShouldCorrectlyQuote() { assertEquals("\"sample\"", Address.quoteString("sample")); assertEquals("\"\"sample\"\"", Address.quoteString("\"\"sample\"\"")); assertEquals("\"sample\"", Address.quoteString("\"sample\"")); assertEquals("\"sa\"mp\"le\"", Address.quoteString("sa\"mp\"le")); assertEquals("\"sa\"mp\"le\"", Address.quoteString("\"sa\"mp\"le\"")); assertEquals("\"\"\"", Address.quoteString("\"")); }