2 * Copyright 2016-2017 ZTE, Inc. and others.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.msb.sdclient.wrapper.util;
18 import java.util.regex.Pattern;
20 import org.apache.commons.lang3.StringUtils;
22 public class RegExpTestUtil {
24 public static boolean httpUrlRegExpTest(String url){
28 String httpUrlReg = "^(|http:\\/\\/)(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
29 +"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
30 +"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
31 +"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)"
33 return Pattern.matches(httpUrlReg, url);
37 public static boolean hostRegExpTest(String host){
39 String hostReg = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
40 +"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
41 +"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
42 +"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)"
44 return Pattern.matches(hostReg, host);
48 public static boolean ipRegExpTest(String ip){
50 String hostReg = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
51 +"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
52 +"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
53 +"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
54 return Pattern.matches(hostReg, ip);
58 public static boolean portRegExpTest(String port){
60 String hostReg = "^\\d{1,5}$";
61 if(Pattern.matches(hostReg, port)){
62 int iport=Integer.parseInt(port);
63 if(iport>=1 && iport<=65535) return true;
70 public static boolean versionRegExpTest(String version){
72 String versionReg = "^v\\d+(\\.\\d+)?$";
73 return Pattern.matches(versionReg, version);
77 public static boolean urlRegExpTest(String url){
78 if(url.equals("/")) return true;
80 String urlReg = "^\\/.*((?!\\/).)$";
81 return Pattern.matches(urlReg, url);
85 public static boolean serviceNameRegExpTest(String serviceName){
87 String serviceNameReg = "^([0-9a-zA-Z]|-|_)*$";
88 return Pattern.matches(serviceNameReg, serviceName);
92 public static boolean apiRouteUrlRegExpTest(String url){
94 String urlReg = "^\\/api\\/.*$";
95 return Pattern.matches(urlReg, url);
99 public static boolean labelRegExpTest(String label){
101 String labelReg = "^\\S+:\\S+$";
102 String[] labelArray=StringUtils.split(label,",");
103 for(int i=0;i<labelArray.length;i++){
104 if(!Pattern.matches(labelReg, labelArray[i])){
113 public static void main(String[] args) {
115 System.out.println(httpUrlRegExpTest("/wqew"));