Skip to content

Commit 0028774

Browse files
authored
apache#1682: Enhance the test coverage part-4 (apache#1862)
1 parent bee28e8 commit 0028774

File tree

5 files changed

+350
-0
lines changed

5 files changed

+350
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 com.alibaba.dubbo.common.utils.DubboAppender;
21+
import com.alibaba.dubbo.common.utils.Log;
22+
import org.apache.log4j.Category;
23+
import org.apache.log4j.Level;
24+
import org.apache.log4j.spi.LoggingEvent;
25+
import org.junit.After;
26+
import org.junit.Before;
27+
import org.junit.Test;
28+
import org.mockito.Mockito;
29+
30+
import static org.hamcrest.Matchers.equalTo;
31+
import static org.hamcrest.Matchers.hasSize;
32+
import static org.hamcrest.Matchers.is;
33+
import static org.junit.Assert.assertThat;
34+
35+
public class DubboAppenderTest {
36+
private LoggingEvent event;
37+
38+
@Before
39+
public void setUp() throws Exception {
40+
Level level = Mockito.mock(Level.class);
41+
Category category = Mockito.mock(Category.class);
42+
event = Mockito.mock(LoggingEvent.class);
43+
Mockito.when(event.getLogger()).thenReturn(category);
44+
Mockito.when(event.getLevel()).thenReturn(level);
45+
Mockito.when(event.getThreadName()).thenReturn("thread-name");
46+
Mockito.when(event.getMessage()).thenReturn("message");
47+
}
48+
49+
@After
50+
public void tearDown() throws Exception {
51+
DubboAppender.clear();
52+
DubboAppender.doStop();
53+
}
54+
55+
@Test
56+
public void testAvailable() throws Exception {
57+
assertThat(DubboAppender.available, is(false));
58+
DubboAppender.doStart();
59+
assertThat(DubboAppender.available, is(true));
60+
DubboAppender.doStop();
61+
assertThat(DubboAppender.available, is(false));
62+
}
63+
64+
@Test
65+
public void testAppend() throws Exception {
66+
DubboAppender appender = new DubboAppender();
67+
appender.append(event);
68+
assertThat(DubboAppender.logList, hasSize(0));
69+
DubboAppender.doStart();
70+
appender.append(event);
71+
assertThat(DubboAppender.logList, hasSize(1));
72+
Log log = DubboAppender.logList.get(0);
73+
assertThat(log.getLogThread(), equalTo("thread-name"));
74+
}
75+
76+
@Test
77+
public void testClear() throws Exception {
78+
DubboAppender.doStart();
79+
DubboAppender appender = new DubboAppender();
80+
appender.append(event);
81+
assertThat(DubboAppender.logList, hasSize(1));
82+
DubboAppender.clear();
83+
assertThat(DubboAppender.logList, hasSize(0));
84+
}
85+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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 com.alibaba.dubbo.common.Constants;
21+
import com.alibaba.dubbo.common.URL;
22+
import org.junit.Test;
23+
import org.mockito.Mockito;
24+
25+
import java.util.concurrent.Executor;
26+
import java.util.concurrent.ExecutorService;
27+
import java.util.concurrent.TimeUnit;
28+
29+
import static org.hamcrest.Matchers.equalTo;
30+
import static org.hamcrest.Matchers.is;
31+
import static org.junit.Assert.assertThat;
32+
import static org.mockito.Mockito.atLeast;
33+
import static org.mockito.Mockito.verify;
34+
import static org.mockito.Mockito.when;
35+
36+
public class ExecutorUtilTest {
37+
@Test
38+
public void testIsTerminated() throws Exception {
39+
ExecutorService executor = Mockito.mock(ExecutorService.class);
40+
when(executor.isTerminated()).thenReturn(true);
41+
assertThat(ExecutorUtil.isTerminated(executor), is(true));
42+
Executor executor2 = Mockito.mock(Executor.class);
43+
assertThat(ExecutorUtil.isTerminated(executor2), is(false));
44+
}
45+
46+
@Test
47+
public void testGracefulShutdown1() throws Exception {
48+
ExecutorService executor = Mockito.mock(ExecutorService.class);
49+
when(executor.isTerminated()).thenReturn(false, true);
50+
when(executor.awaitTermination(20, TimeUnit.MILLISECONDS)).thenReturn(false);
51+
ExecutorUtil.gracefulShutdown(executor, 20);
52+
verify(executor).shutdown();
53+
verify(executor).shutdownNow();
54+
}
55+
56+
@Test
57+
public void testGracefulShutdown2() throws Exception {
58+
ExecutorService executor = Mockito.mock(ExecutorService.class);
59+
when(executor.isTerminated()).thenReturn(false, false, false);
60+
when(executor.awaitTermination(20, TimeUnit.MILLISECONDS)).thenReturn(false);
61+
when(executor.awaitTermination(10, TimeUnit.MILLISECONDS)).thenReturn(false, true);
62+
ExecutorUtil.gracefulShutdown(executor, 20);
63+
Thread.sleep(2000);
64+
verify(executor).shutdown();
65+
verify(executor, atLeast(2)).shutdownNow();
66+
}
67+
68+
@Test
69+
public void testShutdownNow() throws Exception {
70+
ExecutorService executor = Mockito.mock(ExecutorService.class);
71+
when(executor.isTerminated()).thenReturn(false, true);
72+
ExecutorUtil.shutdownNow(executor, 20);
73+
verify(executor).shutdownNow();
74+
verify(executor).awaitTermination(20, TimeUnit.MILLISECONDS);
75+
}
76+
77+
@Test
78+
public void testSetThreadName() throws Exception {
79+
URL url = new URL("dubbo", "localhost", 1234).addParameter(Constants.THREAD_NAME_KEY, "custom-thread");
80+
url = ExecutorUtil.setThreadName(url, "default-name");
81+
assertThat(url.getParameter(Constants.THREAD_NAME_KEY), equalTo("custom-thread-localhost:1234"));
82+
}
83+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 org.hamcrest.Matchers.is;
23+
import static org.junit.Assert.assertThat;
24+
25+
public class HolderTest {
26+
@Test
27+
public void testSetAndGet() throws Exception {
28+
Holder<String> holder = new Holder<String>();
29+
String message = "hello";
30+
holder.set(message);
31+
assertThat(holder.get(), is(message));
32+
}
33+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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.After;
21+
import org.junit.Before;
22+
import org.junit.Rule;
23+
import org.junit.Test;
24+
import org.junit.rules.TemporaryFolder;
25+
26+
import java.io.ByteArrayInputStream;
27+
import java.io.ByteArrayOutputStream;
28+
import java.io.File;
29+
import java.io.InputStream;
30+
import java.io.OutputStream;
31+
import java.io.Reader;
32+
import java.io.StringReader;
33+
import java.io.StringWriter;
34+
import java.io.Writer;
35+
36+
import static org.hamcrest.Matchers.equalTo;
37+
import static org.junit.Assert.assertThat;
38+
39+
public class IOUtilsTest {
40+
@Rule
41+
public TemporaryFolder tmpDir = new TemporaryFolder();
42+
43+
private static String TEXT = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
44+
private InputStream is;
45+
private OutputStream os;
46+
private Reader reader;
47+
private Writer writer;
48+
49+
@Before
50+
public void setUp() throws Exception {
51+
is = new ByteArrayInputStream(TEXT.getBytes("UTF-8"));
52+
os = new ByteArrayOutputStream();
53+
reader = new StringReader(TEXT);
54+
writer = new StringWriter();
55+
}
56+
57+
@After
58+
public void tearDown() throws Exception {
59+
is.close();
60+
os.close();
61+
reader.close();
62+
writer.close();
63+
}
64+
65+
@Test
66+
public void testWrite1() throws Exception {
67+
assertThat((int) IOUtils.write(is, os, 16), equalTo(TEXT.length()));
68+
}
69+
70+
@Test
71+
public void testWrite2() throws Exception {
72+
assertThat((int) IOUtils.write(reader, writer, 16), equalTo(TEXT.length()));
73+
}
74+
75+
@Test
76+
public void testWrite3() throws Exception {
77+
assertThat((int) IOUtils.write(writer, TEXT), equalTo(TEXT.length()));
78+
}
79+
80+
@Test
81+
public void testWrite4() throws Exception {
82+
assertThat((int) IOUtils.write(is, os), equalTo(TEXT.length()));
83+
}
84+
85+
@Test
86+
public void testWrite5() throws Exception {
87+
assertThat((int) IOUtils.write(reader, writer), equalTo(TEXT.length()));
88+
}
89+
90+
@Test
91+
public void testLines() throws Exception {
92+
File file = tmpDir.newFile();
93+
IOUtils.writeLines(file, new String[]{TEXT});
94+
String[] lines = IOUtils.readLines(file);
95+
assertThat(lines.length, equalTo(1));
96+
assertThat(lines[0], equalTo(TEXT));
97+
}
98+
99+
@Test
100+
public void testReadLines() throws Exception {
101+
String[] lines = IOUtils.readLines(is);
102+
assertThat(lines.length, equalTo(1));
103+
assertThat(lines[0], equalTo(TEXT));
104+
}
105+
106+
@Test
107+
public void testWriteLines() throws Exception {
108+
IOUtils.writeLines(os, new String[]{TEXT});
109+
ByteArrayOutputStream bos = (ByteArrayOutputStream) os;
110+
assertThat(new String(bos.toByteArray()), equalTo(TEXT + "\n"));
111+
}
112+
113+
@Test
114+
public void testRead() throws Exception {
115+
assertThat(IOUtils.read(reader), equalTo(TEXT));
116+
}
117+
118+
@Test
119+
public void testAppendLines() throws Exception {
120+
File file = tmpDir.newFile();
121+
IOUtils.appendLines(file, new String[]{"a", "b", "c"});
122+
String[] lines = IOUtils.readLines(file);
123+
assertThat(lines.length, equalTo(3));
124+
assertThat(lines[0], equalTo("a"));
125+
assertThat(lines[1], equalTo("b"));
126+
assertThat(lines[2], equalTo("c"));
127+
}
128+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
public class JVMUtilTest {
21+
}

0 commit comments

Comments
 (0)