Coverage Summary for Class: SchemaNameValidator (com.acciente.oacc.sql.internal)

Class Class, % Method, % Line, %
SchemaNameValidator 100% (1/ 1) 75% (3/ 4) 83.3% (5/ 6)


1 /* 2  * Copyright 2009-2018, Acciente LLC 3  * 4  * Acciente LLC licenses this file to you under the 5  * Apache License, Version 2.0 (the "License"); you 6  * may not use this file except in compliance with the 7  * License. You may obtain a copy of the License at 8  * 9  * http://www.apache.org/licenses/LICENSE-2.0 10  * 11  * Unless required by applicable law or agreed to in 12  * writing, software distributed under the License is 13  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES 14  * OR CONDITIONS OF ANY KIND, either express or implied. 15  * See the License for the specific language governing 16  * permissions and limitations under the License. 17  */ 18 package com.acciente.oacc.sql.internal; 19  20 import java.util.regex.Pattern; 21  22 public class SchemaNameValidator { 23  // breakdown of the regex: (?U)^\w+|^"\w+"|^'\w+'|^\[\w+\]|^`\w+` 24  // ------------------------------------------------------------ 25  // (?U) embedded flag to set UNICODE_CHARACTER_CLASS 26  // ^\w+ match sequence of unicode word characters 27  // |^"\w+" or match sequence of unicode word characters delimited by double quotes 28  // |^'\w+' or match sequence of unicode word characters delimited by single quotes 29  // |^\[\w+\] or match sequence of unicode word characters delimited by square brackets 30  // |^`\w+` or match sequence of unicode word characters delimited by backticks 31  public static final String OPTIONALLY_DELIMITED_UNICODE_WORD_REGEX = "(?U)^\\w+|^\"\\w+\"|^'\\w+'|^\\[\\w+\\]|^`\\w+`"; 32  public static final Pattern OPTIONALLY_DELIMITED_UNICODE_WORD_PATTERN = Pattern.compile(OPTIONALLY_DELIMITED_UNICODE_WORD_REGEX); 33  34  /** 35  * Validates the specified schema name. 36  * <p/> 37  * The specified schema name is only considered valid if it consists of a single sequence of unicode word characters, 38  * or is null. Blank schema names are not valid. 39  * The unicode word can optionally be surrounded by SQL delimiters. 40  * Valid SQL delimiters include the following characters: 41  * <ul> 42  * <li>double-quote (<code>"</code>)</li> 43  * <li>single-quote (<code>'</code>)</li> 44  * <li>square brackets (<code>[]</code>)</li> 45  * <li>backtick (<code>`</code>)</li> 46  * </ul> 47  * A unicode word can consist of the following 48  * <a href="https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#ubpc">[Javadoc Reference]</a>: 49  * <ul> 50  * <li>Alphabetical letter characters</li> 51  * <li>Digits</li> 52  * <li>Connector punctuation (e.g. underscore "<code>_</code>")</li> 53  * <li>Nonspacing marks</li> 54  * <li>Enclosing marks</li> 55  * <li>Spacing combining marks</li> 56  * </ul> 57  * 58  * @param schemaName the schema name String to be validated 59  * @return true if the specified schema name is null or a (optionally delimited) unicode word, false otherwise 60  */ 61  public static boolean isValid(String schemaName) { 62  return schemaName == null || OPTIONALLY_DELIMITED_UNICODE_WORD_PATTERN.matcher(schemaName).matches(); 63  } 64  65  /** 66  * Asserts the specified schema name is valid. 67  * 68  * See {@link #isValid(String)}. 69  * @param schemaName the schema name String to be validated 70  * @throws IllegalArgumentException if the specified schema name is not valid according to {@link #isValid(String)}} 71  */ 72  public static void assertValid(String schemaName) { 73  if (!isValid(schemaName)) { 74  throw new IllegalArgumentException("Invalid database schema name - it can only consist of (optionally delimited) " 75  + "unicode word characters, or be null, but it was: " + schemaName); 76  } 77  } 78 }