9fb9ef1e8433241b3238799d205ddcb5cbb07a94
[vfc/nfvo/resmanagement.git] /
1 /*
2  * Copyright 2016-2017 Huawei Technologies Co., Ltd.
3  *
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
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.openo.nfvo.resmanagement.common.util;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertTrue;
22
23 import java.lang.reflect.Constructor;
24 import java.lang.reflect.Modifier;
25
26 import org.junit.Test;
27
28 public class StringUtilTest {
29
30     @Test
31     public void testisValidString() {
32         assertTrue(StringUtil.isValidString("abc"));
33     }
34
35     @Test
36     public void testisValidString1() {
37         assertFalse(StringUtil.isValidString(null));
38     }
39
40     @Test
41     public void testisValidString2() {
42         assertFalse(StringUtil.isValidString(""));
43     }
44
45     @Test
46     public void testIsAnyLargeThanZero() {
47         assertFalse(StringUtil.isAnyLargeThanZero(""));
48     }
49
50     @Test
51     public void testIsAnyLargeThanZero1() {
52         assertTrue(StringUtil.isAnyLargeThanZero("123"));
53     }
54
55     @Test
56     public void testIsIntegerExceptions() {
57         assertFalse(StringUtil.isInteger("asd"));
58     }
59
60     @Test
61     public void testIsInteger() {
62         assertTrue(StringUtil.isInteger("123"));
63     }
64
65     @Test
66     public void testIsInteger1() {
67         assertFalse(StringUtil.isInteger("-1"));
68     }
69
70     @Test
71     public void testIsNumericExceptions() {
72         assertFalse(StringUtil.isNumeric("abc"));
73     }
74
75     @Test
76     public void testIsNumeric() {
77         assertTrue(StringUtil.isNumeric("1.456"));
78     }
79
80     @Test
81     public void testIsNumeric1() {
82         assertFalse(StringUtil.isNumeric("-1.456"));
83     }
84
85     @Test
86     public void testCompareZeroByFloat() {
87         assertTrue(StringUtil.compareZeroByFloat("3.0", "1.0", "2.0"));
88     }
89
90     @Test
91     public void testCompareZeroByFloat1() {
92         assertFalse(StringUtil.compareZeroByFloat("3.0", "1.2", "2.5"));
93     }
94
95     @Test
96     public void testCompareZeroByInteger() {
97         assertTrue(StringUtil.compareZeroByInteger("3", "1", "2"));
98     }
99
100     @Test
101     public void testCompareZeroByInteger1() {
102         assertFalse(StringUtil.compareZeroByInteger("3", "1", "3"));
103     }
104
105     @Test
106     public void testNumFormatDataIsNull() {
107         String result = StringUtil.numFormat(null);
108         assertEquals(null, result);
109     }
110
111     @Test
112     public void testNumFormatDataIsEmpty() {
113         String result = StringUtil.numFormat("");
114         assertEquals(null, result);
115     }
116
117     @Test
118     public void testNumFormatInteger() {
119         String result = StringUtil.numFormat("12");
120         String expectedResult = "12";
121         assertEquals(expectedResult, result);
122     }
123
124     @Test
125     public void testNumFormatFloat() {
126         String result = StringUtil.numFormat("12.5");
127         String expectedResult = "12.5";
128         assertEquals(expectedResult, result);
129     }
130
131     @Test
132     public void testCheckXss() {
133         assertTrue(StringUtil.checkXss("123"));
134     }
135     @Test
136     public void testPrivateConstructor() throws Exception {
137         Constructor<StringUtil> constructor = StringUtil.class.getDeclaredConstructor();
138         assertTrue("Constructor is not private", Modifier.isPrivate(constructor.getModifiers()));
139
140         constructor.setAccessible(true);
141         constructor.newInstance();
142     }
143 }