Skip to content

Commit 6f3fcab

Browse files
authored
unit test for com.alibaba.dubbo.common.status.support (#1796)
* unit test for Status * remove unnecessary 'static' * unit test for StatusUtils * unit test for LoadStatusChecker * reformat the code * unit test for MemoryStatusChecker
1 parent ddc1ea4 commit 6f3fcab

File tree

6 files changed

+207
-2
lines changed

6 files changed

+207
-2
lines changed

dubbo-common/src/main/java/com/alibaba/dubbo/common/status/Status.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public String getDescription() {
5454
/**
5555
* Level
5656
*/
57-
public static enum Level {
57+
public enum Level {
5858
/**
5959
* OK
6060
*/

dubbo-common/src/main/java/com/alibaba/dubbo/common/status/support/LoadStatusChecker.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ public Status check() {
4141
load = -1;
4242
}
4343
int cpu = operatingSystemMXBean.getAvailableProcessors();
44-
return new Status(load < 0 ? Status.Level.UNKNOWN : (load < cpu ? Status.Level.OK : Status.Level.WARN), (load < 0 ? "" : "load:" + load + ",") + "cpu:" + cpu);
44+
return new Status(load < 0 ? Status.Level.UNKNOWN : (load < cpu ? Status.Level.OK : Status.Level.WARN),
45+
(load < 0 ? "" : "load:" + load + ",") + "cpu:" + cpu);
4546
}
4647

4748
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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.status;
19+
20+
import org.junit.Test;
21+
22+
import static com.alibaba.dubbo.common.status.Status.Level.OK;
23+
import static org.hamcrest.Matchers.equalTo;
24+
import static org.hamcrest.Matchers.is;
25+
import static org.hamcrest.Matchers.isEmptyOrNullString;
26+
import static org.junit.Assert.assertThat;
27+
28+
public class StatusTest {
29+
@Test
30+
public void testConstructor1() throws Exception {
31+
Status status = new Status(OK, "message", "description");
32+
assertThat(status.getLevel(), is(OK));
33+
assertThat(status.getMessage(), equalTo("message"));
34+
assertThat(status.getDescription(), equalTo("description"));
35+
}
36+
37+
@Test
38+
public void testConstructor2() throws Exception {
39+
Status status = new Status(OK, "message");
40+
assertThat(status.getLevel(), is(OK));
41+
assertThat(status.getMessage(), equalTo("message"));
42+
assertThat(status.getDescription(), isEmptyOrNullString());
43+
}
44+
45+
@Test
46+
public void testConstructor3() throws Exception {
47+
Status status = new Status(OK);
48+
assertThat(status.getLevel(), is(OK));
49+
assertThat(status.getMessage(), isEmptyOrNullString());
50+
assertThat(status.getDescription(), isEmptyOrNullString());
51+
}
52+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.status.support;
19+
20+
import com.alibaba.dubbo.common.logger.Logger;
21+
import com.alibaba.dubbo.common.logger.LoggerFactory;
22+
import com.alibaba.dubbo.common.status.Status;
23+
import org.junit.Test;
24+
25+
import static org.hamcrest.Matchers.notNullValue;
26+
import static org.junit.Assert.assertThat;
27+
28+
public class LoadStatusCheckerTest {
29+
private static Logger logger = LoggerFactory.getLogger(LoadStatusCheckerTest.class);
30+
31+
@Test
32+
public void test() throws Exception {
33+
LoadStatusChecker statusChecker = new LoadStatusChecker();
34+
Status status = statusChecker.check();
35+
assertThat(status, notNullValue());
36+
logger.info("load status level: " + status.getLevel());
37+
logger.info("load status message: " + status.getMessage());
38+
}
39+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.status.support;
19+
20+
import com.alibaba.dubbo.common.logger.Logger;
21+
import com.alibaba.dubbo.common.logger.LoggerFactory;
22+
import com.alibaba.dubbo.common.status.Status;
23+
import org.junit.Test;
24+
25+
import static com.alibaba.dubbo.common.status.Status.Level.OK;
26+
import static com.alibaba.dubbo.common.status.Status.Level.WARN;
27+
import static org.hamcrest.CoreMatchers.anyOf;
28+
import static org.hamcrest.Matchers.is;
29+
import static org.junit.Assert.assertThat;
30+
31+
public class MemoryStatusCheckerTest {
32+
private static final Logger logger = LoggerFactory.getLogger(MemoryStatusCheckerTest.class);
33+
34+
@Test
35+
public void test() throws Exception {
36+
MemoryStatusChecker statusChecker = new MemoryStatusChecker();
37+
Status status = statusChecker.check();
38+
assertThat(status.getLevel(), anyOf(is(OK), is(WARN)));
39+
logger.info("memory status level: " + status.getLevel());
40+
logger.info("memory status message: " + status.getMessage());
41+
}
42+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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.status.support;
19+
20+
import com.alibaba.dubbo.common.status.Status;
21+
import org.junit.Test;
22+
23+
import java.util.HashMap;
24+
import java.util.Map;
25+
26+
import static org.hamcrest.Matchers.containsString;
27+
import static org.hamcrest.Matchers.is;
28+
import static org.hamcrest.Matchers.isEmptyOrNullString;
29+
import static org.hamcrest.Matchers.not;
30+
import static org.junit.Assert.assertThat;
31+
32+
public class StatusUtilsTest {
33+
@Test
34+
public void testGetSummaryStatus1() throws Exception {
35+
Status status1 = new Status(Status.Level.ERROR);
36+
Status status2 = new Status(Status.Level.WARN);
37+
Status status3 = new Status(Status.Level.OK);
38+
Map<String, Status> statuses = new HashMap<String, Status>();
39+
statuses.put("status1", status1);
40+
statuses.put("status2", status2);
41+
statuses.put("status3", status3);
42+
Status status = StatusUtils.getSummaryStatus(statuses);
43+
assertThat(status.getLevel(), is(Status.Level.ERROR));
44+
assertThat(status.getMessage(), containsString("status1"));
45+
assertThat(status.getMessage(), containsString("status2"));
46+
assertThat(status.getMessage(), not(containsString("status3")));
47+
}
48+
49+
@Test
50+
public void testGetSummaryStatus2() throws Exception {
51+
Status status1 = new Status(Status.Level.WARN);
52+
Status status2 = new Status(Status.Level.OK);
53+
Map<String, Status> statuses = new HashMap<String, Status>();
54+
statuses.put("status1", status1);
55+
statuses.put("status2", status2);
56+
Status status = StatusUtils.getSummaryStatus(statuses);
57+
assertThat(status.getLevel(), is(Status.Level.WARN));
58+
assertThat(status.getMessage(), containsString("status1"));
59+
assertThat(status.getMessage(), not(containsString("status2")));
60+
}
61+
62+
@Test
63+
public void testGetSummaryStatus3() throws Exception {
64+
Status status1 = new Status(Status.Level.OK);
65+
Map<String, Status> statuses = new HashMap<String, Status>();
66+
statuses.put("status1", status1);
67+
Status status = StatusUtils.getSummaryStatus(statuses);
68+
assertThat(status.getLevel(), is(Status.Level.OK));
69+
assertThat(status.getMessage(), isEmptyOrNullString());
70+
}
71+
}

0 commit comments

Comments
 (0)