Skip to content

Commit c3a797d

Browse files
committed
add test cases for getConfigResources
1 parent 6aaaa83 commit c3a797d

File tree

8 files changed

+59
-18
lines changed

8 files changed

+59
-18
lines changed

framework/spring/module/src/test/java/org/apache/cloudstack/spring/module/factory/ModuleBasedContextFactoryTest.java

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,31 @@
1818
*/
1919
package org.apache.cloudstack.spring.module.factory;
2020

21-
import static org.junit.Assert.assertEquals;
22-
import static org.junit.Assert.assertNotNull;
23-
import static org.junit.Assert.assertNull;
24-
import static org.junit.Assert.assertTrue;
25-
import static org.junit.Assert.fail;
26-
27-
import java.io.IOException;
28-
import java.util.Collection;
29-
21+
import org.apache.cloudstack.spring.module.locator.impl.ClasspathModuleDefinitionLocator;
22+
import org.apache.cloudstack.spring.module.model.ModuleDefinition;
23+
import org.apache.cloudstack.spring.module.model.ModuleDefinitionSet;
3024
import org.junit.Before;
3125
import org.junit.Test;
3226
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
3327
import org.springframework.context.ApplicationContext;
28+
import org.springframework.core.io.Resource;
3429

35-
import org.apache.cloudstack.spring.module.locator.impl.ClasspathModuleDefinitionLocator;
36-
import org.apache.cloudstack.spring.module.model.ModuleDefinition;
37-
import org.apache.cloudstack.spring.module.model.ModuleDefinitionSet;
30+
import java.io.IOException;
31+
import java.util.Arrays;
32+
import java.util.Collection;
33+
import java.util.List;
34+
import java.util.stream.Collectors;
35+
36+
import static org.junit.Assert.assertEquals;
37+
import static org.junit.Assert.assertNotNull;
38+
import static org.junit.Assert.assertNull;
39+
import static org.junit.Assert.assertTrue;
40+
import static org.junit.Assert.fail;
3841

3942
public class ModuleBasedContextFactoryTest {
4043

4144
Collection<ModuleDefinition> defs;
45+
ModuleBasedContextFactory factory = new ModuleBasedContextFactory();
4246

4347
@Before
4448
public void setUp() throws IOException {
@@ -51,8 +55,6 @@ public void setUp() throws IOException {
5155
@Test
5256
public void testLoad() throws IOException {
5357

54-
ModuleBasedContextFactory factory = new ModuleBasedContextFactory();
55-
5658
ModuleDefinitionSet set = factory.loadModules(defs, "base");
5759

5860
assertNotNull(set.getApplicationContext("base"));
@@ -63,8 +65,6 @@ public void testOverride() throws IOException {
6365

6466
InitTest.initted = false;
6567

66-
ModuleBasedContextFactory factory = new ModuleBasedContextFactory();
67-
6868
ModuleDefinitionSet set = factory.loadModules(defs, "base");
6969

7070
assertTrue(!InitTest.initted);
@@ -73,7 +73,6 @@ public void testOverride() throws IOException {
7373

7474
@Test
7575
public void testExcluded() throws IOException {
76-
ModuleBasedContextFactory factory = new ModuleBasedContextFactory();
7776
ModuleDefinitionSet set = factory.loadModules(defs, "base");
7877

7978
assertNull(set.getApplicationContext("excluded"));
@@ -83,7 +82,6 @@ public void testExcluded() throws IOException {
8382

8483
@Test
8584
public void testBeans() throws IOException {
86-
ModuleBasedContextFactory factory = new ModuleBasedContextFactory();
8785
ModuleDefinitionSet set = factory.loadModules(defs, "base");
8886

8987
testBeansInContext(set, "base", 1, new String[] {"base"}, new String[] {"child1", "child2", "child1-1"});
@@ -92,6 +90,25 @@ public void testBeans() throws IOException {
9290
testBeansInContext(set, "child1-1", 3, new String[] {"base", "child1", "child1-1"}, new String[] {"child2"});
9391
}
9492

93+
@Test
94+
public void testConfigResources() throws IOException {
95+
ModuleDefinitionSet set = factory.loadModules(defs, "base");
96+
97+
testConfigResourcesArray(new String[] {}, set.getConfigResources(""));
98+
testConfigResourcesArray(new String[] {"base-context.xml", "base-context-inheritable.xml"}, set.getConfigResources("base"));
99+
testConfigResourcesArray(new String[] {"child1-context.xml", "child1-context-inheritable.xml", "base-context-inheritable.xml", "chil1-context-override.xml"}, set.getConfigResources("child1"));
100+
testConfigResourcesArray(new String[] {"child2-context.xml", "base-context-inheritable.xml"}, set.getConfigResources("child2"));
101+
testConfigResourcesArray(new String[] {"chil1-1-context.xml", "child1-context-inheritable.xml", "base-context-inheritable.xml"}, set.getConfigResources("child1-1"));
102+
}
103+
104+
private void testConfigResourcesArray(String[] expected, Resource[] actual) {
105+
assertEquals(expected.length, actual.length);
106+
List<String> actualFileNameList = Arrays.stream(actual).map(Resource::getFilename).collect(Collectors.toList());
107+
for (int i = 0; i < expected.length; i++) {
108+
assertEquals(expected[i], actualFileNameList.get(i));
109+
}
110+
}
111+
95112
protected void testBeansInContext(ModuleDefinitionSet set, String name, int order, String[] parents, String[] notTheres) {
96113
ApplicationContext context = set.getApplicationContext(name);
97114

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!--
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
-->
19+
<beans xmlns="http://www.springframework.org/schema/beans"
20+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21+
xsi:schemaLocation="http://www.springframework.org/schema/beans
22+
http://www.springframework.org/schema/beans/spring-beans.xsd">
23+
24+
</beans>

0 commit comments

Comments
 (0)