Skip to content

Commit

Permalink
#1682: Enhance the test coverage part-4: dubbo-common/src/main/java/…
Browse files Browse the repository at this point in the history
…com/alibaba/dubbo/common/status(store|threadpoolutils) modules (#1806)
  • Loading branch information
beiwei30 authored May 16, 2018
1 parent 3d2a0f8 commit 59f2b8b
Show file tree
Hide file tree
Showing 7 changed files with 445 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public static Properties loadProperties(String fileName, boolean allowMultiFile,
input.close();
}
} catch (Throwable e) {
logger.warn("Failed to load " + fileName + " file from " + fileName + "(ingore this file): " + e.getMessage(), e);
logger.warn("Failed to load " + fileName + " file from " + fileName + "(ignore this file): " + e.getMessage(), e);
}
return properties;
}
Expand Down Expand Up @@ -261,7 +261,7 @@ public static Properties loadProperties(String fileName, boolean allowMultiFile,
try {
properties.load(ClassHelper.getClassLoader().getResourceAsStream(fileName));
} catch (Throwable e) {
logger.warn("Failed to load " + fileName + " file from " + fileName + "(ingore this file): " + e.getMessage(), e);
logger.warn("Failed to load " + fileName + " file from " + fileName + "(ignore this file): " + e.getMessage(), e);
}
return properties;
}
Expand All @@ -284,7 +284,7 @@ public static Properties loadProperties(String fileName, boolean allowMultiFile,
}
}
} catch (Throwable e) {
logger.warn("Fail to load " + fileName + " file from " + url + "(ingore this file): " + e.getMessage(), e);
logger.warn("Fail to load " + fileName + " file from " + url + "(ignore this file): " + e.getMessage(), e);
}
}

Expand Down Expand Up @@ -312,13 +312,15 @@ public static int getServerShutdownTimeout() {
try {
timeout = Integer.parseInt(value);
} catch (Exception e) {
// ignore
}
} else {
value = ConfigUtils.getProperty(Constants.SHUTDOWN_WAIT_SECONDS_KEY);
if (value != null && value.length() > 0) {
try {
timeout = Integer.parseInt(value) * 1000;
} catch (Exception e) {
// ignore
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.dubbo.common.utils;

import org.junit.Test;

import static com.alibaba.dubbo.common.utils.Assert.notNull;

public class AssertTest {
@Test(expected = IllegalArgumentException.class)
public void testNotNull1() throws Exception {
notNull(null, "null object");
}

@Test(expected = IllegalStateException.class)
public void testNotNull2() throws Exception {
notNull(null, new IllegalStateException("null object"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,29 @@
import org.junit.Test;

import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.junit.matchers.JUnitMatchers.containsString;

public class AtomicPositiveIntegerTest {
AtomicPositiveInteger i1 = new AtomicPositiveInteger();
private AtomicPositiveInteger i1 = new AtomicPositiveInteger();

AtomicPositiveInteger i2 = new AtomicPositiveInteger(127);
private AtomicPositiveInteger i2 = new AtomicPositiveInteger(127);

AtomicPositiveInteger i3 = new AtomicPositiveInteger(Integer.MAX_VALUE);
private AtomicPositiveInteger i3 = new AtomicPositiveInteger(Integer.MAX_VALUE);

@Test
public void test_get() throws Exception {
public void testGet() throws Exception {
assertEquals(0, i1.get());
assertEquals(127, i2.get());
assertEquals(Integer.MAX_VALUE, i3.get());
}

@Test
public void test_set() throws Exception {
public void testSet() throws Exception {
i1.set(100);
assertEquals(100, i1.get());

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

@Test
public void test_getAndIncrement() throws Exception {
public void testGetAndIncrement() throws Exception {
int get = i1.getAndIncrement();
assertEquals(0, get);
assertEquals(1, i1.get());
Expand All @@ -68,7 +70,7 @@ public void test_getAndIncrement() throws Exception {
}

@Test
public void test_getAndDecrement() throws Exception {
public void testGetAndDecrement() throws Exception {
int get = i1.getAndDecrement();
assertEquals(0, get);
assertEquals(Integer.MAX_VALUE, i1.get());
Expand All @@ -83,7 +85,7 @@ public void test_getAndDecrement() throws Exception {
}

@Test
public void test_incrementAndGet() throws Exception {
public void testIncrementAndGet() throws Exception {
int get = i1.incrementAndGet();
assertEquals(1, get);
assertEquals(1, i1.get());
Expand All @@ -98,7 +100,7 @@ public void test_incrementAndGet() throws Exception {
}

@Test
public void test_decrementAndGet() throws Exception {
public void testDecrementAndGet() throws Exception {
int get = i1.decrementAndGet();
assertEquals(Integer.MAX_VALUE, get);
assertEquals(Integer.MAX_VALUE, i1.get());
Expand All @@ -113,7 +115,7 @@ public void test_decrementAndGet() throws Exception {
}

@Test
public void test_getAndSet() throws Exception {
public void testGetAndSet() throws Exception {
int get = i1.getAndSet(100);
assertEquals(0, get);
assertEquals(100, i1.get());
Expand All @@ -127,7 +129,7 @@ public void test_getAndSet() throws Exception {
}

@Test
public void test_getAndAnd() throws Exception {
public void testGetAndAnd() throws Exception {
int get = i1.getAndAdd(3);
assertEquals(0, get);
assertEquals(3, i1.get());
Expand All @@ -143,7 +145,7 @@ public void test_getAndAnd() throws Exception {


@Test
public void test_addAndGet() throws Exception {
public void testAddAndGet() throws Exception {
int get = i1.addAndGet(3);
assertEquals(3, get);
assertEquals(3, i1.get());
Expand All @@ -157,8 +159,42 @@ public void test_addAndGet() throws Exception {
assertEquals(2, i3.get());
}

@Test(expected = IllegalArgumentException.class)
public void testCompareAndSet1() throws Exception {
i1.compareAndSet(i1.get(), -1);
}

@Test
public void testCompareAndSet2() throws Exception {
assertThat(i1.compareAndSet(i1.get(), 2), is(true));
assertThat(i1.get(), is(2));
}

@Test(expected = IllegalArgumentException.class)
public void testWeakCompareAndSet1() throws Exception {
i1.weakCompareAndSet(i1.get(), -1);
}

@Test
public void testWeakCompareAndSet2() throws Exception {
assertThat(i1.weakCompareAndSet(i1.get(), 2), is(true));
assertThat(i1.get(), is(2));
}

@Test
public void testValues() throws Exception {
Integer i = i1.get();
assertThat(i1.byteValue(), equalTo(i.byteValue()));
assertThat(i1.shortValue(), equalTo(i.shortValue()));
assertThat(i1.intValue(), equalTo(i.intValue()));
assertThat(i1.longValue(), equalTo(i.longValue()));
assertThat(i1.floatValue(), equalTo(i.floatValue()));
assertThat(i1.doubleValue(), equalTo(i.doubleValue()));
assertThat(i1.toString(), equalTo(i.toString()));
}

@Test
public void test_equals() {
public void testEquals() {
assertEquals(new AtomicPositiveInteger(), new AtomicPositiveInteger());
assertEquals(new AtomicPositiveInteger(1), new AtomicPositiveInteger(1));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.dubbo.common.utils;

import org.junit.Test;
import org.mockito.Mockito;

import static com.alibaba.dubbo.common.utils.ClassHelper.forName;
import static com.alibaba.dubbo.common.utils.ClassHelper.getCallerClassLoader;
import static com.alibaba.dubbo.common.utils.ClassHelper.getClassLoader;
import static com.alibaba.dubbo.common.utils.ClassHelper.resolvePrimitiveClassName;
import static com.alibaba.dubbo.common.utils.ClassHelper.toShortString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.sameInstance;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.verify;

public class ClassHelperTest {
@Test
public void testForNameWithThreadContextClassLoader() throws Exception {
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
try {
ClassLoader classLoader = Mockito.mock(ClassLoader.class);
Thread.currentThread().setContextClassLoader(classLoader);
ClassHelper.forNameWithThreadContextClassLoader("a.b.c.D");
verify(classLoader).loadClass("a.b.c.D");
} finally {
Thread.currentThread().setContextClassLoader(oldClassLoader);
}
}

@Test
public void tetForNameWithCallerClassLoader() throws Exception {
Class c = ClassHelper.forNameWithCallerClassLoader(ClassHelper.class.getName(), ClassHelperTest.class);
assertThat(c == ClassHelper.class, is(true));
}

@Test
public void testGetCallerClassLoader() throws Exception {
assertThat(getCallerClassLoader(ClassHelperTest.class), sameInstance(ClassHelperTest.class.getClassLoader()));
}

@Test
public void testGetClassLoader1() throws Exception {
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
try {
assertThat(getClassLoader(ClassHelperTest.class), sameInstance(oldClassLoader));
Thread.currentThread().setContextClassLoader(null);
assertThat(getClassLoader(ClassHelperTest.class), sameInstance(ClassHelperTest.class.getClassLoader()));
} finally {
Thread.currentThread().setContextClassLoader(oldClassLoader);
}
}

@Test
public void testGetClassLoader2() throws Exception {
assertThat(getClassLoader(), sameInstance(ClassHelper.class.getClassLoader()));
}

@Test
public void testForName1() throws Exception {
assertThat(forName(ClassHelperTest.class.getName()) == ClassHelperTest.class, is(true));
}

@Test
public void testForName2() throws Exception {
assertThat(forName("byte") == byte.class, is(true));
assertThat(forName("java.lang.String[]") == String[].class, is(true));
assertThat(forName("[Ljava.lang.String;") == String[].class, is(true));
}

@Test
public void testForName3() throws Exception {
ClassLoader classLoader = Mockito.mock(ClassLoader.class);
forName("a.b.c.D", classLoader);
verify(classLoader).loadClass("a.b.c.D");
}

@Test
public void testResolvePrimitiveClassName() throws Exception {
assertThat(resolvePrimitiveClassName("boolean") == boolean.class, is(true));
assertThat(resolvePrimitiveClassName("byte") == byte.class, is(true));
assertThat(resolvePrimitiveClassName("char") == char.class, is(true));
assertThat(resolvePrimitiveClassName("double") == double.class, is(true));
assertThat(resolvePrimitiveClassName("float") == float.class, is(true));
assertThat(resolvePrimitiveClassName("int") == int.class, is(true));
assertThat(resolvePrimitiveClassName("long") == long.class, is(true));
assertThat(resolvePrimitiveClassName("short") == short.class, is(true));
assertThat(resolvePrimitiveClassName("[Z") == boolean[].class, is(true));
assertThat(resolvePrimitiveClassName("[B") == byte[].class, is(true));
assertThat(resolvePrimitiveClassName("[C") == char[].class, is(true));
assertThat(resolvePrimitiveClassName("[D") == double[].class, is(true));
assertThat(resolvePrimitiveClassName("[F") == float[].class, is(true));
assertThat(resolvePrimitiveClassName("[I") == int[].class, is(true));
assertThat(resolvePrimitiveClassName("[J") == long[].class, is(true));
assertThat(resolvePrimitiveClassName("[S") == short[].class, is(true));
}

@Test
public void testToShortString() throws Exception {
assertThat(toShortString(null), equalTo("null"));
assertThat(toShortString(new ClassHelperTest()), startsWith("ClassHelperTest@"));
}
}
Loading

0 comments on commit 59f2b8b

Please sign in to comment.