Source Code Cross Referenced for MatcherTest.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » tests » java » util » regex » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » org package » org.apache.harmony.tests.java.util.regex 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:
018:        package org.apache.harmony.tests.java.util.regex;
019:
020:        import java.util.regex.Matcher;
021:        import java.util.regex.Pattern;
022:
023:        import junit.framework.TestCase;
024:
025:        @SuppressWarnings("nls")
026:        public class MatcherTest extends TestCase {
027:            String[] testPatterns = {
028:                    "(a|b)*abb",
029:                    "(1*2*3*4*)*567",
030:                    "(a|b|c|d)*aab",
031:                    "(1|2|3|4|5|6|7|8|9|0)(1|2|3|4|5|6|7|8|9|0)*",
032:                    "(abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ)*",
033:                    "(a|b)*(a|b)*A(a|b)*lice.*",
034:                    "(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z)(a|b|c|d|e|f|g|h|"
035:                            + "i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z)*(1|2|3|4|5|6|7|8|9|0)*|while|for|struct|if|do" };
036:
037:            String[] groupPatterns = { "(a|b)*aabb", "((a)|b)*aabb",
038:                    "((a|b)*)a(abb)", "(((a)|(b))*)aabb", "(((a)|(b))*)aa(b)b",
039:                    "(((a)|(b))*)a(a(b)b)" };
040:
041:            public void testAppendReplacement() {
042:                Pattern pat = Pattern.compile("XX");
043:                Matcher m = pat.matcher("Today is XX-XX-XX ...");
044:                StringBuffer sb = new StringBuffer();
045:
046:                for (int i = 0; m.find(); i++) {
047:                    m.appendReplacement(sb, new Integer(i * 10 + i).toString());
048:                }
049:                m.appendTail(sb);
050:                assertEquals("Today is 0-11-22 ...", sb.toString());
051:            }
052:
053:            public void testAppendReplacementRef() {
054:                Pattern p = Pattern.compile("xx (rur|\\$)");
055:                Matcher m = p.matcher("xx $ equals to xx rur.");
056:                StringBuffer sb = new StringBuffer();
057:                for (int i = 1; m.find(); i *= 30) {
058:                    String rep = new Integer(i).toString() + " $1";
059:                    m.appendReplacement(sb, rep);
060:                }
061:                m.appendTail(sb);
062:                assertEquals("1 $ equals to 30 rur.", sb.toString());
063:            }
064:
065:            public void testReplaceAll() {
066:                String input = "aabfooaabfooabfoob";
067:                String pattern = "a*b";
068:                Pattern pat = Pattern.compile(pattern);
069:                Matcher mat = pat.matcher(input);
070:
071:                assertEquals("-foo-foo-foo-", mat.replaceAll("-"));
072:            }
073:
074:            /*
075:             * Class under test for Matcher reset(CharSequence)
076:             */
077:            public void testResetCharSequence() {
078:            }
079:
080:            public void testAppendSlashes() {
081:                Pattern p = Pattern.compile("\\\\");
082:                Matcher m = p.matcher("one\\cat\\two\\cats\\in\\the\\yard");
083:                StringBuffer sb = new StringBuffer();
084:                while (m.find()) {
085:                    m.appendReplacement(sb, "\\\\");
086:                }
087:                m.appendTail(sb);
088:                assertEquals("one\\cat\\two\\cats\\in\\the\\yard", sb
089:                        .toString());
090:
091:            }
092:
093:            public void testReplaceFirst() {
094:                String input = "zzzdogzzzdogzzz";
095:                String pattern = "dog";
096:                Pattern pat = Pattern.compile(pattern);
097:                Matcher mat = pat.matcher(input);
098:
099:                assertEquals("zzzcatzzzdogzzz", mat.replaceFirst("cat"));
100:            }
101:
102:            public void testPattern() {
103:                for (String element : testPatterns) {
104:                    Pattern test = Pattern.compile(element);
105:                    assertEquals(test, test.matcher("aaa").pattern());
106:                }
107:
108:                for (String element : testPatterns) {
109:                    assertEquals(element, Pattern.compile(element).matcher(
110:                            "aaa").pattern().toString());
111:                }
112:            }
113:
114:            /*
115:             * Class under test for Matcher reset()
116:             */
117:            public void testReset() {
118:            }
119:
120:            /*
121:             * Class under test for String group(int)
122:             */
123:            public void testGroupint() {
124:                String positiveTestString = "ababababbaaabb";
125:
126:                // test IndexOutOfBoundsException
127:                // //
128:                for (int i = 0; i < groupPatterns.length; i++) {
129:                    Pattern test = Pattern.compile(groupPatterns[i]);
130:                    Matcher mat = test.matcher(positiveTestString);
131:                    mat.matches();
132:                    try {
133:                        // groupPattern <index + 1> equals to number of groups
134:                        // of the specified pattern
135:                        // //
136:                        mat.group(i + 2);
137:                        fail("IndexOutBoundsException expected");
138:                        mat.group(i + 100);
139:                        fail("IndexOutBoundsException expected");
140:                        mat.group(-1);
141:                        fail("IndexOutBoundsException expected");
142:                        mat.group(-100);
143:                        fail("IndexOutBoundsException expected");
144:                    } catch (IndexOutOfBoundsException iobe) {
145:                    }
146:                }
147:
148:                String[][] groupResults = { { "a" }, { "a", "a" },
149:                        { "ababababba", "a", "abb" },
150:                        { "ababababba", "a", "a", "b" },
151:                        { "ababababba", "a", "a", "b", "b" },
152:                        { "ababababba", "a", "a", "b", "abb", "b" }, };
153:
154:                for (int i = 0; i < groupPatterns.length; i++) {
155:                    Pattern test = Pattern.compile(groupPatterns[i]);
156:                    Matcher mat = test.matcher(positiveTestString);
157:                    mat.matches();
158:                    for (int j = 0; j < groupResults[i].length; j++) {
159:                        assertEquals("i: " + i + " j: " + j,
160:                                groupResults[i][j], mat.group(j + 1));
161:                    }
162:
163:                }
164:
165:            }
166:
167:            public void testGroup() {
168:                String positiveTestString = "ababababbaaabb";
169:                String negativeTestString = "gjhfgdsjfhgcbv";
170:                for (String element : groupPatterns) {
171:                    Pattern test = Pattern.compile(element);
172:                    Matcher mat = test.matcher(positiveTestString);
173:                    mat.matches();
174:                    // test result
175:                    assertEquals(positiveTestString, mat.group());
176:
177:                    // test equal to group(0) result
178:                    assertEquals(mat.group(0), mat.group());
179:                }
180:
181:                for (String element : groupPatterns) {
182:                    Pattern test = Pattern.compile(element);
183:                    Matcher mat = test.matcher(negativeTestString);
184:                    mat.matches();
185:                    try {
186:                        mat.group();
187:                        fail("IllegalStateException expected for <false> matches result");
188:                    } catch (IllegalStateException ise) {
189:                    }
190:                }
191:            }
192:
193:            public void testGroupPossessive() {
194:                Pattern pat = Pattern.compile("((a)|(b))++c");
195:                Matcher mat = pat.matcher("aac");
196:
197:                mat.matches();
198:                assertEquals("a", mat.group(1));
199:            }
200:
201:            /*
202:             * Class under test for boolean find(int)
203:             */
204:            public void testFindint() {
205:            }
206:
207:            /*
208:             * Class under test for int start(int)
209:             */
210:            public void testStartint() {
211:            }
212:
213:            /*
214:             * Class under test for int end(int)
215:             */
216:            public void testEndint() {
217:            }
218:
219:            public void testMatchesMisc() {
220:                String[][] posSeq = {
221:                        { "abb", "ababb", "abababbababb",
222:                                "abababbababbabababbbbbabb" },
223:                        { "213567", "12324567", "1234567", "213213567",
224:                                "21312312312567", "444444567" },
225:                        { "abcdaab", "aab", "abaab", "cdaab", "acbdadcbaab" },
226:                        { "213234567", "3458", "0987654", "7689546432",
227:                                "0398576", "98432", "5" },
228:                        {
229:                                "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
230:                                "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
231:                                        + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" },
232:                        { "ababbaAabababblice", "ababbaAliceababab",
233:                                "ababbAabliceaaa", "abbbAbbbliceaaa", "Alice" },
234:                        { "a123", "bnxnvgds156", "for", "while", "if", "struct" }
235:
236:                };
237:
238:                for (int i = 0; i < testPatterns.length; i++) {
239:                    Pattern pat = Pattern.compile(testPatterns[i]);
240:                    for (int j = 0; j < posSeq[i].length; j++) {
241:                        Matcher mat = pat.matcher(posSeq[i][j]);
242:                        assertTrue("Incorrect match: " + testPatterns[i]
243:                                + " vs " + posSeq[i][j], mat.matches());
244:                    }
245:                }
246:            }
247:
248:            public void testMatchesQuantifiers() {
249:                String[] testPatternsSingles = { "a{5}", "a{2,4}", "a{3,}" };
250:                String[] testPatternsMultiple = { "((a)|(b)){1,2}abb",
251:                        "((a)|(b)){2,4}", "((a)|(b)){3,}" };
252:
253:                String[][] stringSingles = { { "aaaaa", "aaa" },
254:                        { "aa", "a", "aaa", "aaaaaa", "aaaa", "aaaaa" },
255:                        { "aaa", "a", "aaaa", "aa" }, };
256:
257:                String[][] stringMultiples = { { "ababb", "aba" },
258:                        { "ab", "b", "bab", "ababa", "abba", "abababbb" },
259:                        { "aba", "b", "abaa", "ba" }, };
260:
261:                for (int i = 0; i < testPatternsSingles.length; i++) {
262:                    Pattern pat = Pattern.compile(testPatternsSingles[i]);
263:                    for (int j = 0; j < stringSingles.length / 2; j++) {
264:                        assertTrue("Match expected, but failed: "
265:                                + pat.pattern() + " : " + stringSingles[i][j],
266:                                pat.matcher(stringSingles[i][j * 2]).matches());
267:                        assertFalse(
268:                                "Match failure expected, but match succeed: "
269:                                        + pat.pattern() + " : "
270:                                        + stringSingles[i][j * 2 + 1], pat
271:                                        .matcher(stringSingles[i][j * 2 + 1])
272:                                        .matches());
273:                    }
274:                }
275:
276:                for (int i = 0; i < testPatternsMultiple.length; i++) {
277:                    Pattern pat = Pattern.compile(testPatternsMultiple[i]);
278:                    for (int j = 0; j < stringMultiples.length / 2; j++) {
279:                        assertTrue(
280:                                "Match expected, but failed: " + pat.pattern()
281:                                        + " : " + stringMultiples[i][j], pat
282:                                        .matcher(stringMultiples[i][j * 2])
283:                                        .matches());
284:                        assertFalse(
285:                                "Match failure expected, but match succeed: "
286:                                        + pat.pattern() + " : "
287:                                        + stringMultiples[i][j * 2 + 1], pat
288:                                        .matcher(stringMultiples[i][j * 2 + 1])
289:                                        .matches());
290:                    }
291:                }
292:            }
293:
294:            public void testQuantVsGroup() {
295:                String patternString = "(d{1,3})((a|c)*)(d{1,3})((a|c)*)(d{1,3})";
296:                String testString = "dacaacaacaaddaaacaacaaddd";
297:
298:                Pattern pat = Pattern.compile(patternString);
299:                Matcher mat = pat.matcher(testString);
300:
301:                mat.matches();
302:                assertEquals("dacaacaacaaddaaacaacaaddd", mat.group());
303:                assertEquals("d", mat.group(1));
304:                assertEquals("acaacaacaa", mat.group(2));
305:                assertEquals("dd", mat.group(4));
306:                assertEquals("aaacaacaa", mat.group(5));
307:                assertEquals("ddd", mat.group(7));
308:            }
309:
310:            public void testLookingAt() {
311:            }
312:
313:            /*
314:             * Class under test for boolean find()
315:             */
316:            public void testFind() {
317:                String testPattern = "(abb)";
318:                String testString = "cccabbabbabbabbabb";
319:                Pattern pat = Pattern.compile(testPattern);
320:                Matcher mat = pat.matcher(testString);
321:                int start = 3;
322:                int end = 6;
323:                while (mat.find()) {
324:                    assertEquals(start, mat.start(1));
325:                    assertEquals(end, mat.end(1));
326:
327:                    start = end;
328:                    end += 3;
329:                }
330:
331:                testPattern = "(\\d{1,3})";
332:                testString = "aaaa123456789045";
333:
334:                Pattern pat2 = Pattern.compile(testPattern);
335:                Matcher mat2 = pat2.matcher(testString);
336:                start = 4;
337:                int length = 3;
338:                while (mat2.find()) {
339:                    assertEquals(testString.substring(start, start + length),
340:                            mat2.group(1));
341:                    start += length;
342:                }
343:            }
344:
345:            public void testSEOLsymbols() {
346:                Pattern pat = Pattern.compile("^a\\(bb\\[$");
347:                Matcher mat = pat.matcher("a(bb[");
348:
349:                assertTrue(mat.matches());
350:            }
351:
352:            /*
353:             * Class under test for int start()
354:             */
355:            public void testStart() {
356:            }
357:
358:            public void testGroupCount() {
359:                for (int i = 0; i < groupPatterns.length; i++) {
360:                    Pattern test = Pattern.compile(groupPatterns[i]);
361:                    Matcher mat = test.matcher("ababababbaaabb");
362:                    mat.matches();
363:                    assertEquals(i + 1, mat.groupCount());
364:
365:                }
366:            }
367:
368:            public void testRelactantQuantifiers() {
369:                Pattern pat = Pattern.compile("(ab*)*b");
370:                Matcher mat = pat.matcher("abbbb");
371:
372:                if (mat.matches()) {
373:                    assertEquals("abbb", mat.group(1));
374:                } else {
375:                    fail("Match expected: (ab*)*b vs abbbb");
376:                }
377:            }
378:
379:            public void testEnhancedFind() {
380:                String input = "foob";
381:                String pattern = "a*b";
382:                Pattern pat = Pattern.compile(pattern);
383:                Matcher mat = pat.matcher(input);
384:
385:                mat.find();
386:                assertEquals("b", mat.group());
387:            }
388:
389:            public void _testMatchesURI() {
390:                final Pattern pat = Pattern
391:                        .compile("^(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?");
392:                Runnable r1 = new Runnable() {
393:                    public void run() {
394:                        Matcher mat = pat
395:                                .matcher("file:/c:/workspace/api/build.win32/classes/META-INF/"
396:                                        + "services/javax.xml.parsers.DocumentBuilderFactory");
397:                        while (mat.matches()) {
398:                            // Do nothing
399:                        }
400:                        System.out.println("1: fail");
401:                    }
402:                };
403:
404:                Runnable r2 = new Runnable() {
405:                    public void run() {
406:                        Matcher mat = pat
407:                                .matcher("file:/c:/workspace/"
408:                                        + "services/javax.xml.parsers.DocumentBuilderFactory");
409:                        while (mat.matches()) {
410:                            // Do nothing
411:                        }
412:                        System.out.println("2: fail");
413:                    }
414:                };
415:
416:                Thread t1 = new Thread(r1);
417:                Thread t2 = new Thread(r2);
418:
419:                t1.start();
420:                t2.start();
421:
422:                try {
423:                    t1.join();
424:                    t2.join();
425:                } catch (Exception e) {
426:                }
427:
428:            }
429:
430:            public void _testUnifiedQuantifiers() {
431:                // Pattern pat1 = Pattern.compile("\\s+a");
432:                Pattern pat2 = Pattern.compile(" *a");
433:
434:                Matcher mat = pat2.matcher("      a");
435:
436:                System.out.println("unified: " + mat.find());
437:            }
438:
439:            public void _testCompositeGroupQuantifiers() {
440:                Pattern pat = Pattern.compile("(a|b){0,3}abb");
441:                Matcher mat = pat.matcher("ababababababababababaab");
442:
443:                System.out.println("composite: " + mat.find());
444:            }
445:
446:            public void testPosCompositeGroup() {
447:                String[] posExamples = { "aabbcc", "aacc", "bbaabbcc" };
448:                String[] negExamples = { "aabb", "bb", "bbaabb" };
449:                Pattern posPat = Pattern.compile("(aa|bb){1,3}+cc");
450:                Pattern negPat = Pattern.compile("(aa|bb){1,3}+bb");
451:
452:                Matcher mat;
453:                for (String element : posExamples) {
454:                    mat = posPat.matcher(element);
455:                    assertTrue(mat.matches());
456:                }
457:
458:                for (String element : negExamples) {
459:                    mat = negPat.matcher(element);
460:                    assertFalse(mat.matches());
461:                }
462:
463:                assertTrue(Pattern.matches("(aa|bb){1,3}+bb", "aabbaabb"));
464:
465:            }
466:
467:            public void testPosAltGroup() {
468:                String[] posExamples = { "aacc", "bbcc", "cc" };
469:                String[] negExamples = { "bb", "aa" };
470:                Pattern posPat = Pattern.compile("(aa|bb)?+cc");
471:                Pattern negPat = Pattern.compile("(aa|bb)?+bb");
472:
473:                Matcher mat;
474:                for (String element : posExamples) {
475:                    mat = posPat.matcher(element);
476:                    assertTrue(posPat.toString() + " vs: " + element, mat
477:                            .matches());
478:                }
479:
480:                for (String element : negExamples) {
481:                    mat = negPat.matcher(element);
482:                    assertFalse(mat.matches());
483:                }
484:
485:                assertTrue(Pattern.matches("(aa|bb)?+bb", "aabb"));
486:            }
487:
488:            public void testRelCompGroup() {
489:
490:                Matcher mat;
491:                Pattern pat;
492:                String res = "";
493:                for (int i = 0; i < 4; i++) {
494:                    pat = Pattern.compile("((aa|bb){" + i + ",3}?).*cc");
495:                    mat = pat.matcher("aaaaaacc");
496:                    assertTrue(pat.toString() + " vs: " + "aaaaaacc", mat
497:                            .matches());
498:                    assertEquals(res, mat.group(1));
499:                    res += "aa";
500:                }
501:            }
502:
503:            public void testRelAltGroup() {
504:
505:                Matcher mat;
506:                Pattern pat;
507:
508:                pat = Pattern.compile("((aa|bb)??).*cc");
509:                mat = pat.matcher("aacc");
510:                assertTrue(pat.toString() + " vs: " + "aacc", mat.matches());
511:                assertEquals("", mat.group(1));
512:
513:                pat = Pattern.compile("((aa|bb)??)cc");
514:                mat = pat.matcher("aacc");
515:                assertTrue(pat.toString() + " vs: " + "aacc", mat.matches());
516:                assertEquals("aa", mat.group(1));
517:            }
518:
519:            public void testIgnoreCase() {
520:                Pattern pat = Pattern.compile("(aa|bb)*",
521:                        Pattern.CASE_INSENSITIVE);
522:                Matcher mat = pat.matcher("aAbb");
523:
524:                assertTrue(mat.matches());
525:
526:                pat = Pattern.compile("(a|b|c|d|e)*", Pattern.CASE_INSENSITIVE);
527:                mat = pat
528:                        .matcher("aAebbAEaEdebbedEccEdebbedEaedaebEbdCCdbBDcdcdADa");
529:                assertTrue(mat.matches());
530:
531:                pat = Pattern.compile("[a-e]*", Pattern.CASE_INSENSITIVE);
532:                mat = pat
533:                        .matcher("aAebbAEaEdebbedEccEdebbedEaedaebEbdCCdbBDcdcdADa");
534:                assertTrue(mat.matches());
535:
536:            }
537:
538:            public void testQuoteReplacement() {
539:                assertEquals("\\\\aaCC\\$1", Matcher
540:                        .quoteReplacement("\\aaCC$1"));
541:            }
542:
543:            public void testOverFlow() {
544:                Pattern tp = Pattern.compile("(a*)*");
545:                Matcher tm = tp.matcher("aaa");
546:                assertTrue(tm.matches());
547:                assertEquals("", tm.group(1));
548:
549:                assertTrue(Pattern.matches("(1+)\\1+", "11"));
550:                assertTrue(Pattern.matches("(1+)(2*)\\2+", "11"));
551:
552:                Pattern pat = Pattern.compile("(1+)\\1*");
553:                Matcher mat = pat.matcher("11");
554:
555:                assertTrue(mat.matches());
556:                assertEquals("11", mat.group(1));
557:
558:                pat = Pattern.compile("((1+)|(2+))(\\2+)");
559:                mat = pat.matcher("11");
560:
561:                assertTrue(mat.matches());
562:                assertEquals("1", mat.group(2));
563:                assertEquals("1", mat.group(1));
564:                assertEquals("1", mat.group(4));
565:                assertNull(mat.group(3));
566:
567:            }
568:
569:            public void testUnicode() {
570:
571:                assertTrue(Pattern.matches("\\x61a", "aa"));
572:                assertTrue(Pattern.matches("\\u0061a", "aa"));
573:                assertTrue(Pattern.matches("\\0141a", "aa"));
574:                assertTrue(Pattern.matches("\\0777", "?7"));
575:
576:            }
577:
578:            public void testUnicodeCategory() {
579:                assertTrue(Pattern.matches("\\p{Ll}", "k")); // Unicode lower case
580:                assertTrue(Pattern.matches("\\P{Ll}", "K")); // Unicode non-lower
581:                // case
582:                assertTrue(Pattern.matches("\\p{Lu}", "K")); // Unicode upper case
583:                assertTrue(Pattern.matches("\\P{Lu}", "k")); // Unicode non-upper
584:                // case
585:                // combinations
586:                assertTrue(Pattern.matches("[\\p{L}&&[^\\p{Lu}]]", "k"));
587:                assertTrue(Pattern.matches("[\\p{L}&&[^\\p{Ll}]]", "K"));
588:                assertFalse(Pattern.matches("[\\p{L}&&[^\\p{Lu}]]", "K"));
589:                assertFalse(Pattern.matches("[\\p{L}&&[^\\p{Ll}]]", "k"));
590:
591:                // category/character combinations
592:                assertFalse(Pattern.matches("[\\p{L}&&[^a-z]]", "k"));
593:                assertTrue(Pattern.matches("[\\p{L}&&[^a-z]]", "K"));
594:
595:                assertTrue(Pattern.matches("[\\p{Lu}a-z]", "k"));
596:                assertTrue(Pattern.matches("[a-z\\p{Lu}]", "k"));
597:
598:                assertFalse(Pattern.matches("[\\p{Lu}a-d]", "k"));
599:                assertTrue(Pattern.matches("[a-d\\p{Lu}]", "K"));
600:
601:                // assertTrue(Pattern.matches("[\\p{L}&&[^\\p{Lu}&&[^K]]]", "K"));
602:                assertFalse(Pattern.matches("[\\p{L}&&[^\\p{Lu}&&[^G]]]", "K"));
603:
604:            }
605:
606:            public void testSplitEmpty() {
607:
608:                Pattern pat = Pattern.compile("");
609:                String[] s = pat.split("", -1);
610:
611:                assertEquals(1, s.length);
612:                assertEquals("", s[0]);
613:            }
614:
615:            public void testFindDollar() {
616:                Matcher mat = Pattern.compile("a$").matcher("a\n");
617:                assertTrue(mat.find());
618:                assertEquals("a", mat.group());
619:            }
620:
621:            /*
622:             * Verify if the Matcher can match the input when region is changed
623:             */
624:            public void testMatchesRegionChanged() {
625:                // Regression for HARMONY-610
626:                String input = " word ";
627:                Pattern pattern = Pattern.compile("\\w+");
628:                Matcher matcher = pattern.matcher(input);
629:                matcher.region(1, 5);
630:                assertTrue(matcher.matches());
631:            }
632:
633:            public void testAllCodePoints() {
634:                // Regression for HARMONY-3145
635:                int[] codePoint = new int[1];
636:                Pattern p = Pattern.compile("(\\p{all})+");
637:                boolean res = true;
638:                int cnt = 0;
639:                String s;
640:                for (int i = 0; i < 0x110000; i++) {
641:                    codePoint[0] = i;
642:                    s = new String(codePoint, 0, 1);
643:                    if (!s.matches(p.toString())) {
644:                        cnt++;
645:                        res = false;
646:                    }
647:                }
648:                assertTrue(res);
649:                assertEquals(0, cnt);
650:
651:                p = Pattern.compile("(\\P{all})+");
652:                res = true;
653:                cnt = 0;
654:
655:                for (int i = 0; i < 0x110000; i++) {
656:                    codePoint[0] = i;
657:                    s = new String(codePoint, 0, 1);
658:                    if (!s.matches(p.toString())) {
659:                        cnt++;
660:                        res = false;
661:                    }
662:                }
663:
664:                assertFalse(res);
665:                assertEquals(0x110000, cnt);
666:            }
667:
668:            /*
669:             * Verify if the Matcher behaves correct when region is changed
670:             */
671:            public void testFindRegionChanged() {
672:                // Regression for HARMONY-625
673:                Pattern pattern = Pattern.compile("(?s).*");
674:                Matcher matcher = pattern.matcher("abcde");
675:                matcher.find();
676:                assertEquals("abcde", matcher.group());
677:
678:                matcher = pattern.matcher("abcde");
679:                matcher.region(0, 2);
680:                matcher.find();
681:                assertEquals("ab", matcher.group());
682:
683:            }
684:
685:            /*
686:             * Verify if the Matcher behaves correct with pattern "c" when region is
687:             * changed
688:             */
689:            public void testFindRegionChanged2() {
690:                // Regression for HARMONY-713
691:                Pattern pattern = Pattern.compile("c");
692:
693:                String inputStr = "aabb.c";
694:                Matcher matcher = pattern.matcher(inputStr);
695:                matcher.region(0, 3);
696:
697:                assertFalse(matcher.find());
698:            }
699:
700:            /*
701:             * Regression test for HARMONY-674
702:             */
703:            public void testPatternMatcher() throws Exception {
704:                Pattern pattern = Pattern.compile("(?:\\d+)(?:pt)");
705:                assertTrue(pattern.matcher("14pt").matches());
706:            }
707:
708:            /**
709:             * Inspired by HARMONY-3360
710:             */
711:            public void test3360() {
712:                String str = "!\"#%&'(),-./";
713:                Pattern p = Pattern.compile("\\s");
714:                Matcher m = p.matcher(str);
715:
716:                assertFalse(m.find());
717:            }
718:
719:            /**
720:             * Regression test for HARMONY-3360
721:             */
722:            public void testGeneralPunctuationCategory() {
723:                String[] s = { ",", "!", "\"", "#", "%", "&", "'", "(", ")",
724:                        "-", ".", "/" };
725:                String regexp = "\\p{P}";
726:
727:                for (int i = 0; i < s.length; i++) {
728:                    Pattern pattern = Pattern.compile(regexp);
729:                    Matcher matcher = pattern.matcher(s[i]);
730:                    assertTrue(matcher.find());
731:                }
732:            }
733:
734:            /**
735:             * Regression test for HARMONY-4396
736:             */
737:            public void testHitEndAfterFind() {
738:                hitEndTest(true, "#01.0", "r((ege)|(geg))x", "regexx", false);
739:                hitEndTest(true, "#01.1", "r((ege)|(geg))x", "regex", false);
740:                hitEndTest(true, "#01.2", "r((ege)|(geg))x", "rege", true);
741:                hitEndTest(true, "#01.2", "r((ege)|(geg))x", "xregexx", false);
742:
743:                hitEndTest(true, "#02.0", "regex", "rexreger", true);
744:                hitEndTest(true, "#02.1", "regex", "raxregexr", false);
745:
746:                String floatRegex = getHexFloatRegex();
747:                hitEndTest(true, "#03.0", floatRegex, Double
748:                        .toHexString(-1.234d), true);
749:                hitEndTest(true, "#03.1", floatRegex, "1 ABC"
750:                        + Double.toHexString(Double.NaN) + "buhuhu", false);
751:                hitEndTest(true, "#03.2", floatRegex, Double.toHexString(-0.0)
752:                        + "--", false);
753:                hitEndTest(true, "#03.3", floatRegex, "--"
754:                        + Double.toHexString(Double.MIN_VALUE) + "--", false);
755:
756:                hitEndTest(true, "#04.0",
757:                        "(\\d+) fish (\\d+) fish (\\w+) fish (\\d+)",
758:                        "1 fish 2 fish red fish 5", true);
759:                hitEndTest(true, "#04.1",
760:                        "(\\d+) fish (\\d+) fish (\\w+) fish (\\d+)",
761:                        "----1 fish 2 fish red fish 5----", false);
762:            }
763:
764:            private void hitEndTest(boolean callFind, String testNo,
765:                    String regex, String input, boolean hit) {
766:                Pattern pattern = Pattern.compile(regex);
767:                Matcher matcher = pattern.matcher(input);
768:                if (callFind) {
769:                    matcher.find();
770:                } else {
771:                    matcher.matches();
772:                }
773:                boolean h = matcher.hitEnd();
774:
775:                assertTrue(testNo, h == hit);
776:            }
777:
778:            private String getHexFloatRegex() {
779:                String hexDecimal = "(-|\\+)?0[xX][0-9a-fA-F]*\\.[0-9a-fA-F]+([pP](-|\\+)?[0-9]+)?";
780:                String notANumber = "((-|\\+)?Infinity)|([nN]a[nN])";
781:                return new StringBuilder("((").append(hexDecimal).append(")|(")
782:                        .append(notANumber).append("))").toString();
783:            }
784:
785:            public static void main(String[] args) {
786:                junit.textui.TestRunner.run(MatcherTest.class);
787:            }
788:        }
w___w_w__.__j_a__v__a___2___s__._c__o_m___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.