FileDocCategorySizeDatePackage
TextUtilTest.javaAPI DocExample3491Mon Jul 23 13:26:16 BST 2007org.apache.struts2.views.util

TextUtilTest

public class TextUtilTest extends TestCase
Unit test for {@link TextUtil}.

Fields Summary
private static char
EURO_SIGN
Constructors Summary
Methods Summary
public voidtestEscape()


         
        assertEquals("", TextUtil.escapeHTML(""));
        assertEquals("   ", TextUtil.escapeHTML("   "));

        assertEquals("Hello World", TextUtil.escapeHTML("Hello World"));
        assertEquals("Hello & World", TextUtil.escapeHTML("Hello & World"));

        assertEquals("Cost is 1999€ and this is cheap", TextUtil.escapeHTML("Cost is 1999" + EURO_SIGN + " and this is cheap"));

        assertEquals("Now some <> and < - > and we have </ and />", TextUtil.escapeHTML("Now some <> and < - > and we have </ and />"));
        assertEquals("<?xml version="1.0" encoding="UTF-8"?>", TextUtil.escapeHTML("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
    
public voidtestEscapeEmpty()

        assertEquals("", TextUtil.escapeHTML("", true));
        assertEquals("   ", TextUtil.escapeHTML("   ", true));

        assertEquals("Hello World", TextUtil.escapeHTML("Hello World", true));
        assertEquals("Hello & World", TextUtil.escapeHTML("Hello & World", true));

        assertEquals("Cost is 1999€ and this is cheap", TextUtil.escapeHTML("Cost is 1999" + EURO_SIGN + " and this is cheap", true));

        assertEquals("Now some <> and < - > and we have </ and />", TextUtil.escapeHTML("Now some <> and < - > and we have </ and />", true));
        assertEquals("<?xml version="1.0" encoding="UTF-8"?>", TextUtil.escapeHTML("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", true));
    
public voidtestLongText()

        // TextUtil behaves special internally for long texts 
        String s = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" +
                   "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" +
                   "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 and now s" +
                   "ome < that should be escaped. But this text is to long (> 300)";
        String res = TextUtil.escapeHTML(s);
        assertEquals(368, res.length());
        assertTrue(res.indexOf("<") == -1);
        assertTrue(res.indexOf(">") == -1);