KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > util > TestCmsBrowserMatcher


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/util/TestCmsBrowserMatcher.java,v $
3  * Date : $Date: 2005/06/26 11:23:00 $
4  * Version: $Revision: 1.8 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.util;
33
34 import java.util.ArrayList JavaDoc;
35 import java.util.regex.Pattern JavaDoc;
36
37 import junit.framework.TestCase;
38
39 /**
40  * Test case for browser matcher.<p>
41  *
42  * @author Alexander Kandzior
43  *
44  * @version $Revision: 1.8 $
45  *
46  * @since 6.0.0
47  */

48 public class TestCmsBrowserMatcher extends TestCase {
49
50     private static String JavaDoc[] 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 JavaDoc[] 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     // default browser match patterns (same as in "opencms.properties")
79
private static String JavaDoc[] 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     /**
90      * Default JUnit constructor.<p>
91      *
92      * @param arg0 JUnit parameters
93      */

94     public TestCmsBrowserMatcher(String JavaDoc arg0) {
95
96         super(arg0);
97     }
98
99     /**
100      * Tests the browser matching regular expressions.<p>
101      */

102     public void testMatchBrowser() {
103
104         ArrayList JavaDoc pattern = new ArrayList JavaDoc(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 JavaDoc)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     /**
120      * Tests for regular expressions.<p>
121      */

122     public void testRegex() {
123
124         String JavaDoc 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 JavaDoc 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 JavaDoc regex = "/WORKPLACE/";
127         String JavaDoc replace = "/system/workplace/jsp/";
128
129         String JavaDoc result = source.replaceAll(regex, replace);
130         System.out.println(result);
131         assertEquals(result, target);
132     }
133
134 }
135
Popular Tags