PatternsTest.java revision 52fc810f73e0d8e005281e80a981e1ceed855850
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package android.util;
17
18import android.test.suitebuilder.annotation.SmallTest;
19import android.util.Patterns;
20
21import java.util.regex.Matcher;
22
23import junit.framework.TestCase;
24
25public class PatternsTest extends TestCase {
26
27    @SmallTest
28    public void testTldPattern() throws Exception {
29        boolean t;
30
31        t = Patterns.TOP_LEVEL_DOMAIN.matcher("com").matches();
32        assertTrue("Missed valid TLD", t);
33
34        // One of the new top level domain.
35        t = Patterns.TOP_LEVEL_DOMAIN.matcher("me").matches();
36        assertTrue("Missed valid TLD", t);
37
38        // One of the new top level test domain.
39        t = Patterns.TOP_LEVEL_DOMAIN.matcher("xn--0zwm56d").matches();
40        assertTrue("Missed valid TLD", t);
41
42        // One of the new top level internationalized domain.
43        t = Patterns.TOP_LEVEL_DOMAIN.matcher("\uD55C\uAD6D").matches();
44        assertTrue("Missed valid TLD", t);
45
46        t = Patterns.TOP_LEVEL_DOMAIN.matcher("mem").matches();
47        assertFalse("Matched invalid TLD!", t);
48
49        t = Patterns.TOP_LEVEL_DOMAIN.matcher("xn").matches();
50        assertFalse("Matched invalid TLD!", t);
51
52        t = Patterns.TOP_LEVEL_DOMAIN.matcher("xer").matches();
53        assertFalse("Matched invalid TLD!", t);
54    }
55
56    @SmallTest
57    public void testUrlPattern() throws Exception {
58        boolean t;
59
60        t = Patterns.WEB_URL.matcher("http://www.google.com").matches();
61        assertTrue("Valid URL", t);
62
63        // Google in one of the new top level domain.
64        t = Patterns.WEB_URL.matcher("http://www.google.me").matches();
65        assertTrue("Valid URL", t);
66        t = Patterns.WEB_URL.matcher("google.me").matches();
67        assertTrue("Valid URL", t);
68
69        // Test url in Chinese: http://xn--fsqu00a.xn--0zwm56d
70        t = Patterns.WEB_URL.matcher("http://xn--fsqu00a.xn--0zwm56d").matches();
71        assertTrue("Valid URL", t);
72        t = Patterns.WEB_URL.matcher("xn--fsqu00a.xn--0zwm56d").matches();
73        assertTrue("Valid URL", t);
74
75        // Url for testing top level Arabic country code domain in Punycode:
76        //   http://xn--4gbrim.xn----rmckbbajlc6dj7bxne2c.xn--wgbh1c/ar/default.aspx
77        t = Patterns.WEB_URL.matcher("http://xn--4gbrim.xn----rmckbbajlc6dj7bxne2c.xn--wgbh1c/ar/default.aspx").matches();
78        assertTrue("Valid URL", t);
79        t = Patterns.WEB_URL.matcher("xn--4gbrim.xn----rmckbbajlc6dj7bxne2c.xn--wgbh1c/ar/default.aspx").matches();
80        assertTrue("Valid URL", t);
81
82        // Internationalized URL.
83        t = Patterns.WEB_URL.matcher("http://\uD604\uAE08\uC601\uC218\uC99D.kr").matches();
84        assertTrue("Valid URL", t);
85        t = Patterns.WEB_URL.matcher("\uD604\uAE08\uC601\uC218\uC99D.kr").matches();
86        assertTrue("Valid URL", t);
87        // URL with international TLD.
88        t = Patterns.WEB_URL.matcher("\uB3C4\uBA54\uC778.\uD55C\uAD6D").matches();
89        assertTrue("Valid URL", t);
90
91        t = Patterns.WEB_URL.matcher("http://brainstormtech.blogs.fortune.cnn.com/2010/03/11/" +
92            "top-five-moments-from-eric-schmidt\u2019s-talk-in-abu-dhabi/").matches();
93        assertTrue("Valid URL", t);
94
95        t = Patterns.WEB_URL.matcher("ftp://www.example.com").matches();
96        assertFalse("Matched invalid protocol", t);
97
98        t = Patterns.WEB_URL.matcher("http://www.example.com:8080").matches();
99        assertTrue("Didn't match valid URL with port", t);
100
101        t = Patterns.WEB_URL.matcher("http://www.example.com:8080/?foo=bar").matches();
102        assertTrue("Didn't match valid URL with port and query args", t);
103
104        t = Patterns.WEB_URL.matcher("http://www.example.com:8080/~user/?foo=bar").matches();
105        assertTrue("Didn't match valid URL with ~", t);
106    }
107
108    @SmallTest
109    public void testIpPattern() throws Exception {
110        boolean t;
111
112        t = Patterns.IP_ADDRESS.matcher("172.29.86.3").matches();
113        assertTrue("Valid IP", t);
114
115        t = Patterns.IP_ADDRESS.matcher("1234.4321.9.9").matches();
116        assertFalse("Invalid IP", t);
117    }
118
119    @SmallTest
120    public void testDomainPattern() throws Exception {
121        boolean t;
122
123        t = Patterns.DOMAIN_NAME.matcher("mail.example.com").matches();
124        assertTrue("Valid domain", t);
125
126        t = Patterns.DOMAIN_NAME.matcher("google.me").matches();
127        assertTrue("Valid domain", t);
128
129        // Internationalized domains.
130        t = Patterns.DOMAIN_NAME.matcher("\uD604\uAE08\uC601\uC218\uC99D.kr").matches();
131        assertTrue("Valid domain", t);
132
133        t = Patterns.DOMAIN_NAME.matcher("__+&42.xer").matches();
134        assertFalse("Invalid domain", t);
135
136        // Obsolete domain .yu
137        t = Patterns.DOMAIN_NAME.matcher("test.yu").matches();
138        assertFalse("Obsolete country code top level domain", t);
139
140        // Testing top level Arabic country code domain in Punycode:
141        t = Patterns.DOMAIN_NAME.matcher("xn--4gbrim.xn----rmckbbajlc6dj7bxne2c.xn--wgbh1c").matches();
142        assertTrue("Valid domain", t);
143    }
144
145    @SmallTest
146    public void testPhonePattern() throws Exception {
147        boolean t;
148
149        t = Patterns.PHONE.matcher("(919) 555-1212").matches();
150        assertTrue("Valid phone", t);
151
152        t = Patterns.PHONE.matcher("2334 9323/54321").matches();
153        assertFalse("Invalid phone", t);
154
155        String[] tests = {
156                "Me: 16505551212 this\n",
157                "Me: 6505551212 this\n",
158                "Me: 5551212 this\n",
159
160                "Me: 1-650-555-1212 this\n",
161                "Me: (650) 555-1212 this\n",
162                "Me: +1 (650) 555-1212 this\n",
163                "Me: +1-650-555-1212 this\n",
164                "Me: 650-555-1212 this\n",
165                "Me: 555-1212 this\n",
166
167                "Me: 1.650.555.1212 this\n",
168                "Me: (650) 555.1212 this\n",
169                "Me: +1 (650) 555.1212 this\n",
170                "Me: +1.650.555.1212 this\n",
171                "Me: 650.555.1212 this\n",
172                "Me: 555.1212 this\n",
173
174                "Me: 1 650 555 1212 this\n",
175                "Me: (650) 555 1212 this\n",
176                "Me: +1 (650) 555 1212 this\n",
177                "Me: +1 650 555 1212 this\n",
178                "Me: 650 555 1212 this\n",
179                "Me: 555 1212 this\n",
180        };
181
182        for (String test : tests) {
183            Matcher m = Patterns.PHONE.matcher(test);
184
185            assertTrue("Valid phone " + test, m.find());
186        }
187    }
188}
189