1 31 32 package org.opencms.util; 33 34 import java.util.ArrayList ; 35 import java.util.regex.Pattern ; 36 37 import junit.framework.TestCase; 38 39 48 public class TestCmsBrowserMatcher extends TestCase { 49 50 private static String [] browser = { 51 "MSIE_6.x", 52 "MSIE_5.5", 53 "MSIE_5.0", 54 "mozilla_1.4", 55 "mozilla_1.3", 56 "netscape_7.02", 57 "netscape_7.0", 58 "other___"}; 59 60 private static String [] sampleagent = { 61 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)", 62 "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4b) Gecko/20030507", 63 "Mozilla/4.0 (compatible; MSIE 5.0; AOL 7.0; Windows 95; DigExt)", 64 "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.0.2) Gecko/20030208 Netscape/7.02", 65 "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)", 66 "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.3) Gecko/20030312", 67 "Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.0.1) Gecko/20020823 Netscape/7.0", 68 "Mozilla/4.0 (compatible; MSIE 5.5; AOL 7.0; Windows 98)", 69 "Mozilla/5.0 (Macintosh; U; PPC; en-US; rv:1.0.2) Gecko/20030208 Netscape/7.02", 70 "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT)", 71 "Mozilla/4.0 (compatible; MSIE 6.0; MSN 2.6; Windows 98)", 72 "Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.4a) Gecko/20030419", 73 "Mozilla/5.0 (X11; U; SunOS sun4u; en-US; rv:1.0.1) Gecko/20020921 Netscape/7.0", 74 "Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.3) Gecko/20030411", 75 76 }; 77 78 private static String [] useragent = { 80 "^Mozilla/4\\.0 \\(compatible; MSIE 6\\.\\d*; .*\\)$", 81 "^Mozilla/4\\.0 \\(compatible; MSIE 5\\.[56789]\\d*; .*\\)$", 82 "^Mozilla/4\\.0 \\(compatible; MSIE 5\\.[01234]\\d*; .*\\)$", 83 "^Mozilla/5\\.0 \\(.* rv:1.4.*\\) Gecko/2003\\d*$", 84 "^Mozilla/5\\.0 \\(.* rv:1.3\\) Gecko/2003\\d*$", 85 "^Mozilla/5\\.0 \\(.* rv:1.0.2\\) Gecko/2003\\d* Netscape/.*$", 86 "^Mozilla/5\\.0 \\(.* rv:1.0.1\\) Gecko/2002\\d* Netscape/.*$", 87 ".*"}; 88 89 94 public TestCmsBrowserMatcher(String arg0) { 95 96 super(arg0); 97 } 98 99 102 public void testMatchBrowser() { 103 104 ArrayList pattern = new ArrayList (useragent.length); 105 for (int i = 0; i < useragent.length; i++) { 106 pattern.add(Pattern.compile(useragent[i])); 107 } 108 for (int i = 0; i < sampleagent.length; i++) { 109 for (int j = 0; j < pattern.size(); j++) { 110 boolean matches = ((Pattern )pattern.get(j)).matcher(sampleagent[i]).matches(); 111 if (matches) { 112 System.out.println(browser[j] + "\t matches " + sampleagent[i]); 113 break; 114 } 115 } 116 } 117 } 118 119 122 public void testRegex() { 123 124 String source = "addMenuEntry(resource_id, \"language_key(explorer.context.copy)\", \"/WORKPLACE/copy.html?initial=true\", \"''\", \"rules_key(d d aaai aaai dddd dddd dddd)\");"; 125 String target = "addMenuEntry(resource_id, \"language_key(explorer.context.copy)\", \"/system/workplace/jsp/copy.html?initial=true\", \"''\", \"rules_key(d d aaai aaai dddd dddd dddd)\");"; 126 String regex = "/WORKPLACE/"; 127 String replace = "/system/workplace/jsp/"; 128 129 String result = source.replaceAll(regex, replace); 130 System.out.println(result); 131 assertEquals(result, target); 132 } 133 134 } 135 | Popular Tags |