Skip to content
Permalink
Browse files
#1682: Enhance the test coverage part-4 (#1862)
  • Loading branch information
beiwei30 committed May 30, 2018
1 parent 259c072 commit 61727e1e657f0ada93d2b4695791074e51a64ff8
Show file tree
Hide file tree
Showing 5 changed files with 350 additions and 0 deletions.
@@ -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));
}
}
@@ -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"));
}
}
@@ -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));
}
}
@@ -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"));
}
}
@@ -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 {
}

0 comments on commit 61727e1

Please sign in to comment.