Skip to content

Commit 59f2b8b

Browse files
authored
#1682: Enhance the test coverage part-4: dubbo-common/src/main/java/com/alibaba/dubbo/common/status(store|threadpoolutils) modules (#1806)
1 parent 3d2a0f8 commit 59f2b8b

File tree

7 files changed

+445
-61
lines changed

7 files changed

+445
-61
lines changed

dubbo-common/src/main/java/com/alibaba/dubbo/common/utils/ConfigUtils.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ public static Properties loadProperties(String fileName, boolean allowMultiFile,
226226
input.close();
227227
}
228228
} catch (Throwable e) {
229-
logger.warn("Failed to load " + fileName + " file from " + fileName + "(ingore this file): " + e.getMessage(), e);
229+
logger.warn("Failed to load " + fileName + " file from " + fileName + "(ignore this file): " + e.getMessage(), e);
230230
}
231231
return properties;
232232
}
@@ -261,7 +261,7 @@ public static Properties loadProperties(String fileName, boolean allowMultiFile,
261261
try {
262262
properties.load(ClassHelper.getClassLoader().getResourceAsStream(fileName));
263263
} catch (Throwable e) {
264-
logger.warn("Failed to load " + fileName + " file from " + fileName + "(ingore this file): " + e.getMessage(), e);
264+
logger.warn("Failed to load " + fileName + " file from " + fileName + "(ignore this file): " + e.getMessage(), e);
265265
}
266266
return properties;
267267
}
@@ -284,7 +284,7 @@ public static Properties loadProperties(String fileName, boolean allowMultiFile,
284284
}
285285
}
286286
} catch (Throwable e) {
287-
logger.warn("Fail to load " + fileName + " file from " + url + "(ingore this file): " + e.getMessage(), e);
287+
logger.warn("Fail to load " + fileName + " file from " + url + "(ignore this file): " + e.getMessage(), e);
288288
}
289289
}
290290

@@ -312,13 +312,15 @@ public static int getServerShutdownTimeout() {
312312
try {
313313
timeout = Integer.parseInt(value);
314314
} catch (Exception e) {
315+
// ignore
315316
}
316317
} else {
317318
value = ConfigUtils.getProperty(Constants.SHUTDOWN_WAIT_SECONDS_KEY);
318319
if (value != null && value.length() > 0) {
319320
try {
320321
timeout = Integer.parseInt(value) * 1000;
321322
} catch (Exception e) {
323+
// ignore
322324
}
323325
}
324326
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the 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 writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.alibaba.dubbo.common.utils;
19+
20+
import org.junit.Test;
21+
22+
import static com.alibaba.dubbo.common.utils.Assert.notNull;
23+
24+
public class AssertTest {
25+
@Test(expected = IllegalArgumentException.class)
26+
public void testNotNull1() throws Exception {
27+
notNull(null, "null object");
28+
}
29+
30+
@Test(expected = IllegalStateException.class)
31+
public void testNotNull2() throws Exception {
32+
notNull(null, new IllegalStateException("null object"));
33+
}
34+
}

dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/AtomicPositiveIntegerTest.java

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,29 @@
1919
import org.junit.Test;
2020

2121
import static org.hamcrest.CoreMatchers.allOf;
22+
import static org.hamcrest.CoreMatchers.containsString;
23+
import static org.hamcrest.CoreMatchers.equalTo;
24+
import static org.hamcrest.CoreMatchers.is;
2225
import static org.junit.Assert.assertEquals;
2326
import static org.junit.Assert.assertThat;
2427
import static org.junit.Assert.fail;
25-
import static org.junit.matchers.JUnitMatchers.containsString;
2628

2729
public class AtomicPositiveIntegerTest {
28-
AtomicPositiveInteger i1 = new AtomicPositiveInteger();
30+
private AtomicPositiveInteger i1 = new AtomicPositiveInteger();
2931

30-
AtomicPositiveInteger i2 = new AtomicPositiveInteger(127);
32+
private AtomicPositiveInteger i2 = new AtomicPositiveInteger(127);
3133

32-
AtomicPositiveInteger i3 = new AtomicPositiveInteger(Integer.MAX_VALUE);
34+
private AtomicPositiveInteger i3 = new AtomicPositiveInteger(Integer.MAX_VALUE);
3335

3436
@Test
35-
public void test_get() throws Exception {
37+
public void testGet() throws Exception {
3638
assertEquals(0, i1.get());
3739
assertEquals(127, i2.get());
3840
assertEquals(Integer.MAX_VALUE, i3.get());
3941
}
4042

4143
@Test
42-
public void test_set() throws Exception {
44+
public void testSet() throws Exception {
4345
i1.set(100);
4446
assertEquals(100, i1.get());
4547

@@ -53,7 +55,7 @@ public void test_set() throws Exception {
5355
}
5456

5557
@Test
56-
public void test_getAndIncrement() throws Exception {
58+
public void testGetAndIncrement() throws Exception {
5759
int get = i1.getAndIncrement();
5860
assertEquals(0, get);
5961
assertEquals(1, i1.get());
@@ -68,7 +70,7 @@ public void test_getAndIncrement() throws Exception {
6870
}
6971

7072
@Test
71-
public void test_getAndDecrement() throws Exception {
73+
public void testGetAndDecrement() throws Exception {
7274
int get = i1.getAndDecrement();
7375
assertEquals(0, get);
7476
assertEquals(Integer.MAX_VALUE, i1.get());
@@ -83,7 +85,7 @@ public void test_getAndDecrement() throws Exception {
8385
}
8486

8587
@Test
86-
public void test_incrementAndGet() throws Exception {
88+
public void testIncrementAndGet() throws Exception {
8789
int get = i1.incrementAndGet();
8890
assertEquals(1, get);
8991
assertEquals(1, i1.get());
@@ -98,7 +100,7 @@ public void test_incrementAndGet() throws Exception {
98100
}
99101

100102
@Test
101-
public void test_decrementAndGet() throws Exception {
103+
public void testDecrementAndGet() throws Exception {
102104
int get = i1.decrementAndGet();
103105
assertEquals(Integer.MAX_VALUE, get);
104106
assertEquals(Integer.MAX_VALUE, i1.get());
@@ -113,7 +115,7 @@ public void test_decrementAndGet() throws Exception {
113115
}
114116

115117
@Test
116-
public void test_getAndSet() throws Exception {
118+
public void testGetAndSet() throws Exception {
117119
int get = i1.getAndSet(100);
118120
assertEquals(0, get);
119121
assertEquals(100, i1.get());
@@ -127,7 +129,7 @@ public void test_getAndSet() throws Exception {
127129
}
128130

129131
@Test
130-
public void test_getAndAnd() throws Exception {
132+
public void testGetAndAnd() throws Exception {
131133
int get = i1.getAndAdd(3);
132134
assertEquals(0, get);
133135
assertEquals(3, i1.get());
@@ -143,7 +145,7 @@ public void test_getAndAnd() throws Exception {
143145

144146

145147
@Test
146-
public void test_addAndGet() throws Exception {
148+
public void testAddAndGet() throws Exception {
147149
int get = i1.addAndGet(3);
148150
assertEquals(3, get);
149151
assertEquals(3, i1.get());
@@ -157,8 +159,42 @@ public void test_addAndGet() throws Exception {
157159
assertEquals(2, i3.get());
158160
}
159161

162+
@Test(expected = IllegalArgumentException.class)
163+
public void testCompareAndSet1() throws Exception {
164+
i1.compareAndSet(i1.get(), -1);
165+
}
166+
167+
@Test
168+
public void testCompareAndSet2() throws Exception {
169+
assertThat(i1.compareAndSet(i1.get(), 2), is(true));
170+
assertThat(i1.get(), is(2));
171+
}
172+
173+
@Test(expected = IllegalArgumentException.class)
174+
public void testWeakCompareAndSet1() throws Exception {
175+
i1.weakCompareAndSet(i1.get(), -1);
176+
}
177+
178+
@Test
179+
public void testWeakCompareAndSet2() throws Exception {
180+
assertThat(i1.weakCompareAndSet(i1.get(), 2), is(true));
181+
assertThat(i1.get(), is(2));
182+
}
183+
184+
@Test
185+
public void testValues() throws Exception {
186+
Integer i = i1.get();
187+
assertThat(i1.byteValue(), equalTo(i.byteValue()));
188+
assertThat(i1.shortValue(), equalTo(i.shortValue()));
189+
assertThat(i1.intValue(), equalTo(i.intValue()));
190+
assertThat(i1.longValue(), equalTo(i.longValue()));
191+
assertThat(i1.floatValue(), equalTo(i.floatValue()));
192+
assertThat(i1.doubleValue(), equalTo(i.doubleValue()));
193+
assertThat(i1.toString(), equalTo(i.toString()));
194+
}
195+
160196
@Test
161-
public void test_equals() {
197+
public void testEquals() {
162198
assertEquals(new AtomicPositiveInteger(), new AtomicPositiveInteger());
163199
assertEquals(new AtomicPositiveInteger(1), new AtomicPositiveInteger(1));
164200
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the 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 writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.alibaba.dubbo.common.utils;
19+
20+
import org.junit.Test;
21+
import org.mockito.Mockito;
22+
23+
import static com.alibaba.dubbo.common.utils.ClassHelper.forName;
24+
import static com.alibaba.dubbo.common.utils.ClassHelper.getCallerClassLoader;
25+
import static com.alibaba.dubbo.common.utils.ClassHelper.getClassLoader;
26+
import static com.alibaba.dubbo.common.utils.ClassHelper.resolvePrimitiveClassName;
27+
import static com.alibaba.dubbo.common.utils.ClassHelper.toShortString;
28+
import static org.hamcrest.Matchers.equalTo;
29+
import static org.hamcrest.Matchers.is;
30+
import static org.hamcrest.Matchers.sameInstance;
31+
import static org.hamcrest.Matchers.startsWith;
32+
import static org.junit.Assert.assertThat;
33+
import static org.mockito.Mockito.verify;
34+
35+
public class ClassHelperTest {
36+
@Test
37+
public void testForNameWithThreadContextClassLoader() throws Exception {
38+
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
39+
try {
40+
ClassLoader classLoader = Mockito.mock(ClassLoader.class);
41+
Thread.currentThread().setContextClassLoader(classLoader);
42+
ClassHelper.forNameWithThreadContextClassLoader("a.b.c.D");
43+
verify(classLoader).loadClass("a.b.c.D");
44+
} finally {
45+
Thread.currentThread().setContextClassLoader(oldClassLoader);
46+
}
47+
}
48+
49+
@Test
50+
public void tetForNameWithCallerClassLoader() throws Exception {
51+
Class c = ClassHelper.forNameWithCallerClassLoader(ClassHelper.class.getName(), ClassHelperTest.class);
52+
assertThat(c == ClassHelper.class, is(true));
53+
}
54+
55+
@Test
56+
public void testGetCallerClassLoader() throws Exception {
57+
assertThat(getCallerClassLoader(ClassHelperTest.class), sameInstance(ClassHelperTest.class.getClassLoader()));
58+
}
59+
60+
@Test
61+
public void testGetClassLoader1() throws Exception {
62+
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
63+
try {
64+
assertThat(getClassLoader(ClassHelperTest.class), sameInstance(oldClassLoader));
65+
Thread.currentThread().setContextClassLoader(null);
66+
assertThat(getClassLoader(ClassHelperTest.class), sameInstance(ClassHelperTest.class.getClassLoader()));
67+
} finally {
68+
Thread.currentThread().setContextClassLoader(oldClassLoader);
69+
}
70+
}
71+
72+
@Test
73+
public void testGetClassLoader2() throws Exception {
74+
assertThat(getClassLoader(), sameInstance(ClassHelper.class.getClassLoader()));
75+
}
76+
77+
@Test
78+
public void testForName1() throws Exception {
79+
assertThat(forName(ClassHelperTest.class.getName()) == ClassHelperTest.class, is(true));
80+
}
81+
82+
@Test
83+
public void testForName2() throws Exception {
84+
assertThat(forName("byte") == byte.class, is(true));
85+
assertThat(forName("java.lang.String[]") == String[].class, is(true));
86+
assertThat(forName("[Ljava.lang.String;") == String[].class, is(true));
87+
}
88+
89+
@Test
90+
public void testForName3() throws Exception {
91+
ClassLoader classLoader = Mockito.mock(ClassLoader.class);
92+
forName("a.b.c.D", classLoader);
93+
verify(classLoader).loadClass("a.b.c.D");
94+
}
95+
96+
@Test
97+
public void testResolvePrimitiveClassName() throws Exception {
98+
assertThat(resolvePrimitiveClassName("boolean") == boolean.class, is(true));
99+
assertThat(resolvePrimitiveClassName("byte") == byte.class, is(true));
100+
assertThat(resolvePrimitiveClassName("char") == char.class, is(true));
101+
assertThat(resolvePrimitiveClassName("double") == double.class, is(true));
102+
assertThat(resolvePrimitiveClassName("float") == float.class, is(true));
103+
assertThat(resolvePrimitiveClassName("int") == int.class, is(true));
104+
assertThat(resolvePrimitiveClassName("long") == long.class, is(true));
105+
assertThat(resolvePrimitiveClassName("short") == short.class, is(true));
106+
assertThat(resolvePrimitiveClassName("[Z") == boolean[].class, is(true));
107+
assertThat(resolvePrimitiveClassName("[B") == byte[].class, is(true));
108+
assertThat(resolvePrimitiveClassName("[C") == char[].class, is(true));
109+
assertThat(resolvePrimitiveClassName("[D") == double[].class, is(true));
110+
assertThat(resolvePrimitiveClassName("[F") == float[].class, is(true));
111+
assertThat(resolvePrimitiveClassName("[I") == int[].class, is(true));
112+
assertThat(resolvePrimitiveClassName("[J") == long[].class, is(true));
113+
assertThat(resolvePrimitiveClassName("[S") == short[].class, is(true));
114+
}
115+
116+
@Test
117+
public void testToShortString() throws Exception {
118+
assertThat(toShortString(null), equalTo("null"));
119+
assertThat(toShortString(new ClassHelperTest()), startsWith("ClassHelperTest@"));
120+
}
121+
}

0 commit comments

Comments
 (0)