package org.apache.jasper.runtime; import org.junit.Assert; import org.junit.Test; public class EscapeTest { private static String oldXmlEscape(String s) { if (s == null) return null; StringBuilder sb = new StringBuilder(); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c == '<') { sb.append("<"); } else if (c == '>') { sb.append(">"); } else if (c == '\'') { sb.append("'"); // ' } else if (c == '&') { sb.append("&"); } else if (c == '"') { sb.append("""); // " } else { sb.append(c); } } return sb.toString(); } private static String[] array = new String[] { "Hello World!", "", "This connection has limited network connectivity. ", "Please use this web page & to access file server resources. " }; @Test public void testComparison() { for (int i = 0; i < 10; i++) { doComparison(); } } private static void doComparison() { int count = 1000000; for (int j = 0; j < array.length; j++) { Assert.assertEquals(oldXmlEscape(array[j]), PageContextImpl.XmlEscape(array[j])); } for (int i = 0; i < 100; i++) { for (int j = 0; j < array.length; j++) { oldXmlEscape(array[j]); } } for (int i = 0; i < 100; i++) { for (int j = 0; j < array.length; j++) { PageContextImpl.XmlEscape(array[j]); } } long start = System.currentTimeMillis(); for (int i = 0; i < count; i++) { for (int j = 0; j < array.length; j++) { oldXmlEscape(array[j]); } } System.out .println("Old escape:" + (System.currentTimeMillis() - start)); start = System.currentTimeMillis(); for (int i = 0; i < count; i++) { for (int j = 0; j < array.length; j++) { PageContextImpl.XmlEscape(array[j]); } } System.out .println("New escape:" + (System.currentTimeMillis() - start)); } }