• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  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  */
16 
17 package org.apache.harmony.regex.tests.java.util.regex;
18 
19 import dalvik.annotation.TestTargetClass;
20 import dalvik.annotation.TestTargets;
21 import dalvik.annotation.TestTargetNew;
22 import dalvik.annotation.TestLevel;
23 
24 import java.util.regex.Pattern;
25 import junit.framework.TestCase;
26 
27 @TestTargetClass(Pattern.class)
28 /**
29  * Test boundary and error conditions in java.util.regex.Pattern
30  *
31  */
32 public class PatternErrorTest extends TestCase {
33     @TestTargets({
34         @TestTargetNew(
35             level = TestLevel.PARTIAL_COMPLETE,
36             notes = "Verifies compile(String regex) and compile(String regex, int flag) method with invalid parameters. Doesn't verify IllegalArgumentException, PatternSyntaxException.",
37             method = "compile",
38             args = {java.lang.String.class}
39         ),
40         @TestTargetNew(
41             level = TestLevel.PARTIAL_COMPLETE,
42             notes = "Verifies compile(String regex) and compile(String regex, int flag) method with invalid parameters. Doesn't verify IllegalArgumentException, PatternSyntaxException.",
43             method = "compile",
44             args = {java.lang.String.class, int.class}
45         )
46     })
testCompileErrors()47     public void testCompileErrors() throws Exception {
48         // null regex string - should get NullPointerException
49         try {
50             Pattern.compile(null);
51             fail("NullPointerException expected");
52         } catch (NullPointerException e) {
53         }
54 
55 
56         // empty regex string - no exception should be thrown
57         Pattern.compile("");
58 
59         // note: invalid regex syntax checked in PatternSyntaxExceptionTest
60 
61         // flags = 0 should raise no exception
62         int flags = 0;
63         Pattern.compile("foo", flags);
64 
65         // check that all valid flags accepted without exception
66         flags |= Pattern.UNIX_LINES;
67         flags |= Pattern.CASE_INSENSITIVE;
68         flags |= Pattern.MULTILINE;
69         // BEGIN android-changed
70         // We don't support that flag.
71         // flags |= Pattern.CANON_EQ;
72         // END android-changed
73         flags |= Pattern.COMMENTS;
74         flags |= Pattern.DOTALL;
75         flags |= Pattern.UNICODE_CASE;
76         Pattern.compile("foo", flags);
77 
78         // add invalid flags - should get IllegalArgumentException
79         /*
80          * TODO: Inconsistency between the reference JDK behaviour and spec - exception is
81          * not thrown
82          */
83         /*
84          * Valid test is:
85          * flags |= 0xFFFFFFFF;
86          * try {
87          *   Pattern.compile("foo",flags);
88          * } catch (IllegalArgumentException e) {
89          *   // This is the expected exception
90          * } catch (Exception e) {
91          *   fail();
92          * }
93          */
94 
95         /* Workaround test is: */
96         // BEGIN android-changed
97         // We don't support that flag.
98         flags |= ~Pattern.CANON_EQ;
99         // END android-changed
100         // No exception expected to match incorrect the reference behaviour
101         Pattern.compile("foo", flags);
102     }
103 }
104