• 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 java.util.regex.Pattern;
20 import junit.framework.TestCase;
21 
22 /**
23  * Test boundary and error conditions in java.util.regex.Pattern
24  *
25  */
26 public class PatternErrorTest extends TestCase {
testCompileErrors()27     public void testCompileErrors() throws Exception {
28         // null regex string - should get NullPointerException
29         try {
30             Pattern.compile(null);
31             fail("NullPointerException expected");
32         } catch (NullPointerException e) {
33         }
34 
35 
36         // empty regex string - no exception should be thrown
37         Pattern.compile("");
38 
39         // note: invalid regex syntax checked in PatternSyntaxExceptionTest
40 
41         // flags = 0 should raise no exception
42         int flags = 0;
43         Pattern.compile("foo", flags);
44 
45         // check that all valid flags accepted without exception
46         flags |= Pattern.UNIX_LINES;
47         flags |= Pattern.CASE_INSENSITIVE;
48         flags |= Pattern.MULTILINE;
49         // BEGIN android-changed
50         // We don't support that flag.
51         // flags |= Pattern.CANON_EQ;
52         // END android-changed
53         flags |= Pattern.COMMENTS;
54         flags |= Pattern.DOTALL;
55         flags |= Pattern.UNICODE_CASE;
56         Pattern.compile("foo", flags);
57 
58         // add invalid flags - should get IllegalArgumentException
59         /*
60          * TODO: Inconsistency between the reference JDK behaviour and spec - exception is
61          * not thrown
62          */
63         /*
64          * Valid test is:
65          * flags |= 0xFFFFFFFF;
66          * try {
67          *   Pattern.compile("foo",flags);
68          * } catch (IllegalArgumentException e) {
69          *   // This is the expected exception
70          * } catch (Exception e) {
71          *   fail();
72          * }
73          */
74 
75         /* Workaround test is: */
76         // BEGIN android-changed
77         // We don't support that flag.
78         flags |= ~Pattern.CANON_EQ;
79         // END android-changed
80         // No exception expected to match incorrect the reference behaviour
81         Pattern.compile("foo", flags);
82     }
83 }
84