forked from apache/dubbo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apache#1682: Enhance the test coverage part-4 (apache#1862)
- Loading branch information
Showing
5 changed files
with
350 additions
and
0 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/DubboAppenderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* 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 com.alibaba.dubbo.common.utils.DubboAppender; | ||
import com.alibaba.dubbo.common.utils.Log; | ||
import org.apache.log4j.Category; | ||
import org.apache.log4j.Level; | ||
import org.apache.log4j.spi.LoggingEvent; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.hasSize; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public class DubboAppenderTest { | ||
private LoggingEvent event; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
Level level = Mockito.mock(Level.class); | ||
Category category = Mockito.mock(Category.class); | ||
event = Mockito.mock(LoggingEvent.class); | ||
Mockito.when(event.getLogger()).thenReturn(category); | ||
Mockito.when(event.getLevel()).thenReturn(level); | ||
Mockito.when(event.getThreadName()).thenReturn("thread-name"); | ||
Mockito.when(event.getMessage()).thenReturn("message"); | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
DubboAppender.clear(); | ||
DubboAppender.doStop(); | ||
} | ||
|
||
@Test | ||
public void testAvailable() throws Exception { | ||
assertThat(DubboAppender.available, is(false)); | ||
DubboAppender.doStart(); | ||
assertThat(DubboAppender.available, is(true)); | ||
DubboAppender.doStop(); | ||
assertThat(DubboAppender.available, is(false)); | ||
} | ||
|
||
@Test | ||
public void testAppend() throws Exception { | ||
DubboAppender appender = new DubboAppender(); | ||
appender.append(event); | ||
assertThat(DubboAppender.logList, hasSize(0)); | ||
DubboAppender.doStart(); | ||
appender.append(event); | ||
assertThat(DubboAppender.logList, hasSize(1)); | ||
Log log = DubboAppender.logList.get(0); | ||
assertThat(log.getLogThread(), equalTo("thread-name")); | ||
} | ||
|
||
@Test | ||
public void testClear() throws Exception { | ||
DubboAppender.doStart(); | ||
DubboAppender appender = new DubboAppender(); | ||
appender.append(event); | ||
assertThat(DubboAppender.logList, hasSize(1)); | ||
DubboAppender.clear(); | ||
assertThat(DubboAppender.logList, hasSize(0)); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/ExecutorUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* 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 com.alibaba.dubbo.common.Constants; | ||
import com.alibaba.dubbo.common.URL; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
|
||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.Assert.assertThat; | ||
import static org.mockito.Mockito.atLeast; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class ExecutorUtilTest { | ||
@Test | ||
public void testIsTerminated() throws Exception { | ||
ExecutorService executor = Mockito.mock(ExecutorService.class); | ||
when(executor.isTerminated()).thenReturn(true); | ||
assertThat(ExecutorUtil.isTerminated(executor), is(true)); | ||
Executor executor2 = Mockito.mock(Executor.class); | ||
assertThat(ExecutorUtil.isTerminated(executor2), is(false)); | ||
} | ||
|
||
@Test | ||
public void testGracefulShutdown1() throws Exception { | ||
ExecutorService executor = Mockito.mock(ExecutorService.class); | ||
when(executor.isTerminated()).thenReturn(false, true); | ||
when(executor.awaitTermination(20, TimeUnit.MILLISECONDS)).thenReturn(false); | ||
ExecutorUtil.gracefulShutdown(executor, 20); | ||
verify(executor).shutdown(); | ||
verify(executor).shutdownNow(); | ||
} | ||
|
||
@Test | ||
public void testGracefulShutdown2() throws Exception { | ||
ExecutorService executor = Mockito.mock(ExecutorService.class); | ||
when(executor.isTerminated()).thenReturn(false, false, false); | ||
when(executor.awaitTermination(20, TimeUnit.MILLISECONDS)).thenReturn(false); | ||
when(executor.awaitTermination(10, TimeUnit.MILLISECONDS)).thenReturn(false, true); | ||
ExecutorUtil.gracefulShutdown(executor, 20); | ||
Thread.sleep(2000); | ||
verify(executor).shutdown(); | ||
verify(executor, atLeast(2)).shutdownNow(); | ||
} | ||
|
||
@Test | ||
public void testShutdownNow() throws Exception { | ||
ExecutorService executor = Mockito.mock(ExecutorService.class); | ||
when(executor.isTerminated()).thenReturn(false, true); | ||
ExecutorUtil.shutdownNow(executor, 20); | ||
verify(executor).shutdownNow(); | ||
verify(executor).awaitTermination(20, TimeUnit.MILLISECONDS); | ||
} | ||
|
||
@Test | ||
public void testSetThreadName() throws Exception { | ||
URL url = new URL("dubbo", "localhost", 1234).addParameter(Constants.THREAD_NAME_KEY, "custom-thread"); | ||
url = ExecutorUtil.setThreadName(url, "default-name"); | ||
assertThat(url.getParameter(Constants.THREAD_NAME_KEY), equalTo("custom-thread-localhost:1234")); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/HolderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* 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 org.hamcrest.Matchers.is; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public class HolderTest { | ||
@Test | ||
public void testSetAndGet() throws Exception { | ||
Holder<String> holder = new Holder<String>(); | ||
String message = "hello"; | ||
holder.set(message); | ||
assertThat(holder.get(), is(message)); | ||
} | ||
} |
128 changes: 128 additions & 0 deletions
128
dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/IOUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* | ||
* 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.After; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.TemporaryFolder; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.File; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.io.Reader; | ||
import java.io.StringReader; | ||
import java.io.StringWriter; | ||
import java.io.Writer; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public class IOUtilsTest { | ||
@Rule | ||
public TemporaryFolder tmpDir = new TemporaryFolder(); | ||
|
||
private static String TEXT = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"; | ||
private InputStream is; | ||
private OutputStream os; | ||
private Reader reader; | ||
private Writer writer; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
is = new ByteArrayInputStream(TEXT.getBytes("UTF-8")); | ||
os = new ByteArrayOutputStream(); | ||
reader = new StringReader(TEXT); | ||
writer = new StringWriter(); | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
is.close(); | ||
os.close(); | ||
reader.close(); | ||
writer.close(); | ||
} | ||
|
||
@Test | ||
public void testWrite1() throws Exception { | ||
assertThat((int) IOUtils.write(is, os, 16), equalTo(TEXT.length())); | ||
} | ||
|
||
@Test | ||
public void testWrite2() throws Exception { | ||
assertThat((int) IOUtils.write(reader, writer, 16), equalTo(TEXT.length())); | ||
} | ||
|
||
@Test | ||
public void testWrite3() throws Exception { | ||
assertThat((int) IOUtils.write(writer, TEXT), equalTo(TEXT.length())); | ||
} | ||
|
||
@Test | ||
public void testWrite4() throws Exception { | ||
assertThat((int) IOUtils.write(is, os), equalTo(TEXT.length())); | ||
} | ||
|
||
@Test | ||
public void testWrite5() throws Exception { | ||
assertThat((int) IOUtils.write(reader, writer), equalTo(TEXT.length())); | ||
} | ||
|
||
@Test | ||
public void testLines() throws Exception { | ||
File file = tmpDir.newFile(); | ||
IOUtils.writeLines(file, new String[]{TEXT}); | ||
String[] lines = IOUtils.readLines(file); | ||
assertThat(lines.length, equalTo(1)); | ||
assertThat(lines[0], equalTo(TEXT)); | ||
} | ||
|
||
@Test | ||
public void testReadLines() throws Exception { | ||
String[] lines = IOUtils.readLines(is); | ||
assertThat(lines.length, equalTo(1)); | ||
assertThat(lines[0], equalTo(TEXT)); | ||
} | ||
|
||
@Test | ||
public void testWriteLines() throws Exception { | ||
IOUtils.writeLines(os, new String[]{TEXT}); | ||
ByteArrayOutputStream bos = (ByteArrayOutputStream) os; | ||
assertThat(new String(bos.toByteArray()), equalTo(TEXT + "\n")); | ||
} | ||
|
||
@Test | ||
public void testRead() throws Exception { | ||
assertThat(IOUtils.read(reader), equalTo(TEXT)); | ||
} | ||
|
||
@Test | ||
public void testAppendLines() throws Exception { | ||
File file = tmpDir.newFile(); | ||
IOUtils.appendLines(file, new String[]{"a", "b", "c"}); | ||
String[] lines = IOUtils.readLines(file); | ||
assertThat(lines.length, equalTo(3)); | ||
assertThat(lines[0], equalTo("a")); | ||
assertThat(lines[1], equalTo("b")); | ||
assertThat(lines[2], equalTo("c")); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/JVMUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* 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; | ||
|
||
public class JVMUtilTest { | ||
} |