You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by il...@apache.org on 2018/05/30 02:10:15 UTC

[incubator-dubbo] branch master updated: #1682: Enhance the test coverage part-4 (#1862)

This is an automated email from the ASF dual-hosted git repository.

iluo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-dubbo.git


The following commit(s) were added to refs/heads/master by this push:
     new 61727e1      #1682: Enhance the test coverage part-4 (#1862)
61727e1 is described below

commit 61727e1e657f0ada93d2b4695791074e51a64ff8
Author: Ian Luo <ia...@gmail.com>
AuthorDate: Wed May 30 10:10:06 2018 +0800

        #1682: Enhance the test coverage part-4 (#1862)
---
 .../dubbo/common/utils/DubboAppenderTest.java      |  85 ++++++++++++++
 .../dubbo/common/utils/ExecutorUtilTest.java       |  83 +++++++++++++
 .../com/alibaba/dubbo/common/utils/HolderTest.java |  33 ++++++
 .../alibaba/dubbo/common/utils/IOUtilsTest.java    | 128 +++++++++++++++++++++
 .../alibaba/dubbo/common/utils/JVMUtilTest.java    |  21 ++++
 5 files changed, 350 insertions(+)

diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/DubboAppenderTest.java b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/DubboAppenderTest.java
new file mode 100644
index 0000000..3abede3
--- /dev/null
+++ b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/DubboAppenderTest.java
@@ -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));
+    }
+}
diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/ExecutorUtilTest.java b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/ExecutorUtilTest.java
new file mode 100644
index 0000000..4fc1984
--- /dev/null
+++ b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/ExecutorUtilTest.java
@@ -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"));
+    }
+}
diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/HolderTest.java b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/HolderTest.java
new file mode 100644
index 0000000..1fba4ba
--- /dev/null
+++ b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/HolderTest.java
@@ -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));
+    }
+}
diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/IOUtilsTest.java b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/IOUtilsTest.java
new file mode 100644
index 0000000..a6d7966
--- /dev/null
+++ b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/IOUtilsTest.java
@@ -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"));
+    }
+}
diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/JVMUtilTest.java b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/JVMUtilTest.java
new file mode 100644
index 0000000..ca90930
--- /dev/null
+++ b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/JVMUtilTest.java
@@ -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 {
+}

-- 
To stop receiving notification emails like this one, please contact
iluo@apache.org.