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
17package org.apache.harmony.tests.java.util.regex;
18
19import java.util.regex.Pattern;
20
21import junit.framework.TestCase;
22
23/**
24 * Test boundary and error conditions in java.util.regex.Pattern
25 */
26@SuppressWarnings("nls")
27public class PatternErrorTest extends TestCase {
28    public void testCompileErrors() throws Exception {
29        // null regex string - should get NullPointerException
30        try {
31            Pattern.compile(null);
32            fail("NullPointerException expected");
33        } catch (NullPointerException e) {
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        flags |= Pattern.CANON_EQ;
50        flags |= Pattern.COMMENTS;
51        flags |= Pattern.DOTALL;
52        flags |= Pattern.UNICODE_CASE;
53        flags &= ~Pattern.CANON_EQ; // Android always throws given this flag.
54        Pattern.compile("foo", flags);
55
56        // add invalid flags - should get IllegalArgumentException
57        // regression test for HARMONY-4248
58        flags |= 0xFFFFFFFF;
59        flags &= ~Pattern.CANON_EQ; // Android always throws given this flag.
60        try {
61            Pattern.compile("foo", flags);
62            fail("Expected IllegalArgumentException to be thrown");
63        } catch (IllegalArgumentException e) {
64            // This is the expected exception
65        }
66    }
67}
68