2 * Copyright 2016-2017 Huawei Technologies Co., Ltd.
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.
17 package org.openo.nfvo.resmanagement.common.util;
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertTrue;
23 import java.lang.reflect.Constructor;
24 import java.lang.reflect.Modifier;
26 import org.junit.Test;
28 public class StringUtilTest {
31 public void testisValidString() {
32 assertTrue(StringUtil.isValidString("abc"));
36 public void testisValidString1() {
37 assertFalse(StringUtil.isValidString(null));
41 public void testisValidString2() {
42 assertFalse(StringUtil.isValidString(""));
46 public void testIsAnyLargeThanZero() {
47 assertFalse(StringUtil.isAnyLargeThanZero(""));
51 public void testIsAnyLargeThanZero1() {
52 assertTrue(StringUtil.isAnyLargeThanZero("123"));
56 public void testIsIntegerExceptions() {
57 assertFalse(StringUtil.isInteger("asd"));
61 public void testIsInteger() {
62 assertTrue(StringUtil.isInteger("123"));
66 public void testIsInteger1() {
67 assertFalse(StringUtil.isInteger("-1"));
71 public void testIsNumericExceptions() {
72 assertFalse(StringUtil.isNumeric("abc"));
76 public void testIsNumeric() {
77 assertTrue(StringUtil.isNumeric("1.456"));
81 public void testIsNumeric1() {
82 assertFalse(StringUtil.isNumeric("-1.456"));
86 public void testCompareZeroByFloat() {
87 assertTrue(StringUtil.compareZeroByFloat("3.0", "1.0", "2.0"));
91 public void testCompareZeroByFloat1() {
92 assertFalse(StringUtil.compareZeroByFloat("3.0", "1.2", "2.5"));
96 public void testCompareZeroByInteger() {
97 assertTrue(StringUtil.compareZeroByInteger("3", "1", "2"));
101 public void testCompareZeroByInteger1() {
102 assertFalse(StringUtil.compareZeroByInteger("3", "1", "3"));
106 public void testNumFormatDataIsNull() {
107 String result = StringUtil.numFormat(null);
108 assertEquals(null, result);
112 public void testNumFormatDataIsEmpty() {
113 String result = StringUtil.numFormat("");
114 assertEquals(null, result);
118 public void testNumFormatInteger() {
119 String result = StringUtil.numFormat("12");
120 String expectedResult = "12";
121 assertEquals(expectedResult, result);
125 public void testNumFormatFloat() {
126 String result = StringUtil.numFormat("12.5");
127 String expectedResult = "12.5";
128 assertEquals(expectedResult, result);
132 public void testCheckXss() {
133 assertTrue(StringUtil.checkXss("123"));
136 public void testPrivateConstructor() throws Exception {
137 Constructor<StringUtil> constructor = StringUtil.class.getDeclaredConstructor();
138 assertTrue("Constructor is not private", Modifier.isPrivate(constructor.getModifiers()));
140 constructor.setAccessible(true);
141 constructor.newInstance();