Skip to content

Commit 8d623d5

Browse files
authored
Adding JUnit. Fixes #3874 (#4271)
Adding JUnit. Fixes #3874
1 parent 672320f commit 8d623d5

File tree

2 files changed

+112
-20
lines changed

2 files changed

+112
-20
lines changed

apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/utils/ConfigChangeContentBuilderTest.java

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,79 @@
11
/*
22
* Copyright 2022 Apollo Authors
33
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
76
*
87
* http://www.apache.org/licenses/LICENSE-2.0
98
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
1513
*
1614
*/
1715
package com.ctrip.framework.apollo.biz.utils;
1816

1917
import static org.junit.Assert.assertNotNull;
20-
import static org.junit.Assert.assertTrue;
18+
import static org.junit.jupiter.api.Assertions.assertFalse;
19+
import static org.junit.jupiter.api.Assertions.assertTrue;
2120

22-
import com.ctrip.framework.apollo.biz.MockBeanFactory;
23-
import com.ctrip.framework.apollo.biz.entity.Item;
2421
import org.junit.Before;
2522
import org.junit.Test;
2623

24+
import com.ctrip.framework.apollo.biz.MockBeanFactory;
25+
import com.ctrip.framework.apollo.biz.entity.Item;
26+
2727
/**
2828
* @author jian.tan
2929
*/
3030

3131
public class ConfigChangeContentBuilderTest {
3232

33-
private final ConfigChangeContentBuilder configChangeContentBuilder = new ConfigChangeContentBuilder();
33+
private ConfigChangeContentBuilder configChangeContentBuilder;
3434
private String configString;
35+
private Item createdItem;
36+
private Item updatedItem;
37+
private Item updatedItemFalseCheck;
38+
private Item createdItemFalseCheck;
3539

3640
@Before
3741
public void initConfig() {
38-
39-
Item createdItem = MockBeanFactory.mockItem(1, 1, "timeout", "100", 1);
40-
Item updatedItem = MockBeanFactory.mockItem(1, 1, "timeout", "1001", 1);
41-
42+
configChangeContentBuilder = new ConfigChangeContentBuilder();
43+
createdItem = MockBeanFactory.mockItem(1, 1, "timeout", "100", 1);
44+
updatedItem = MockBeanFactory.mockItem(1, 1, "timeout", "1001", 1);
45+
updatedItemFalseCheck = MockBeanFactory.mockItem(1, 1, "timeout", "100", 1);
46+
createdItemFalseCheck = MockBeanFactory.mockItem(1, 1, "", "100", 1);
4247
configChangeContentBuilder.createItem(createdItem);
48+
configChangeContentBuilder.createItem(createdItemFalseCheck);
4349
configChangeContentBuilder.updateItem(createdItem, updatedItem);
50+
configChangeContentBuilder.updateItem(createdItem, updatedItemFalseCheck);
4451
configChangeContentBuilder.deleteItem(updatedItem);
45-
52+
configChangeContentBuilder.deleteItem(createdItemFalseCheck);
4653
configString = configChangeContentBuilder.build();
4754
}
4855

4956
@Test
5057
public void testHasContent() {
5158
assertTrue(configChangeContentBuilder.hasContent());
59+
configChangeContentBuilder.getCreateItems().clear();
60+
assertTrue(configChangeContentBuilder.hasContent());
61+
configChangeContentBuilder.getUpdateItems().clear();
62+
assertTrue(configChangeContentBuilder.hasContent());
5263
}
5364

5465
@Test
55-
public void testConvertJsonString() {
56-
ConfigChangeContentBuilder contentBuilder = ConfigChangeContentBuilder
57-
.convertJsonString(configString);
66+
public void testHasContentFalseCheck() {
67+
configChangeContentBuilder.getCreateItems().clear();
68+
configChangeContentBuilder.getUpdateItems().clear();
69+
configChangeContentBuilder.getDeleteItems().clear();
70+
assertFalse(configChangeContentBuilder.hasContent());
71+
}
5872

73+
@Test
74+
public void testConvertJsonString() {
75+
ConfigChangeContentBuilder contentBuilder =
76+
ConfigChangeContentBuilder.convertJsonString(configString);
5977
assertNotNull(contentBuilder.getCreateItems());
6078
assertNotNull(contentBuilder.getUpdateItems().get(0).oldItem);
6179
assertNotNull(contentBuilder.getUpdateItems().get(0).newItem);
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2022 Apollo Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*
14+
*/
15+
package com.ctrip.framework.apollo.spring.util;
16+
17+
import static org.junit.Assert.assertFalse;
18+
import static org.junit.Assert.assertTrue;
19+
20+
import java.util.Map;
21+
import java.util.concurrent.ConcurrentHashMap;
22+
23+
import org.junit.Before;
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
import org.mockito.InjectMocks;
27+
import org.mockito.Mockito;
28+
import org.mockito.junit.MockitoJUnitRunner;
29+
import org.springframework.beans.factory.config.BeanDefinition;
30+
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
31+
import org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry;
32+
33+
@RunWith(MockitoJUnitRunner.class)
34+
public class BeanRegistrationUtilTest {
35+
36+
@InjectMocks
37+
private BeanRegistrationUtil beanRegistrationUtil;
38+
private BeanDefinitionRegistry someRegistry;
39+
private String someBeanName = "someBean";
40+
41+
@Before
42+
public void setUp() {
43+
someRegistry = new SimpleBeanDefinitionRegistry();
44+
}
45+
46+
@Test
47+
public void registerBeanDefinitionIfNotExistsTest() {
48+
someRegistry.registerBeanDefinition(someBeanName, Mockito.mock(BeanDefinition.class));
49+
assertFalse(BeanRegistrationUtil.registerBeanDefinitionIfNotExists(someRegistry, someBeanName,
50+
getClass(), null));
51+
assertFalse(BeanRegistrationUtil.registerBeanDefinitionIfNotExists(someRegistry, someBeanName,
52+
getClass()));
53+
54+
}
55+
56+
@Test
57+
public void registerBeanDefinitionIfNotExistsBeanNotPresentTest() {
58+
someRegistry.registerBeanDefinition("someAnotherBean", Mockito.mock(BeanDefinition.class));
59+
assertTrue(BeanRegistrationUtil.registerBeanDefinitionIfNotExists(someRegistry, someBeanName,
60+
getClass(), null));
61+
62+
}
63+
64+
@Test
65+
public void registerBeanDefinitionIfNotExistsWithExtPropTest() {
66+
someRegistry.registerBeanDefinition("someAnotherBean", Mockito.mock(BeanDefinition.class));
67+
Map<String, Object> extraPropertyValues = new ConcurrentHashMap<>();
68+
extraPropertyValues.put(someBeanName, "someProperty");
69+
assertTrue(BeanRegistrationUtil.registerBeanDefinitionIfNotExists(someRegistry, someBeanName,
70+
getClass(), extraPropertyValues));
71+
72+
}
73+
74+
}

0 commit comments

Comments
 (0)