Skip to content

Add unit tests for getConfigResources in ModuleDefinitionSet and improve context readability #11042

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,31 @@
*/
package org.apache.cloudstack.spring.module.factory;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.IOException;
import java.util.Collection;

import org.apache.cloudstack.spring.module.locator.impl.ClasspathModuleDefinitionLocator;
import org.apache.cloudstack.spring.module.model.ModuleDefinition;
import org.apache.cloudstack.spring.module.model.ModuleDefinitionSet;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.Resource;

import org.apache.cloudstack.spring.module.locator.impl.ClasspathModuleDefinitionLocator;
import org.apache.cloudstack.spring.module.model.ModuleDefinition;
import org.apache.cloudstack.spring.module.model.ModuleDefinitionSet;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class ModuleBasedContextFactoryTest {

Collection<ModuleDefinition> defs;
ModuleBasedContextFactory factory = new ModuleBasedContextFactory();

@Before
public void setUp() throws IOException {
Expand All @@ -51,8 +55,6 @@ public void setUp() throws IOException {
@Test
public void testLoad() throws IOException {

ModuleBasedContextFactory factory = new ModuleBasedContextFactory();

ModuleDefinitionSet set = factory.loadModules(defs, "base");

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

InitTest.initted = false;

ModuleBasedContextFactory factory = new ModuleBasedContextFactory();

ModuleDefinitionSet set = factory.loadModules(defs, "base");

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

@Test
public void testExcluded() throws IOException {
ModuleBasedContextFactory factory = new ModuleBasedContextFactory();
ModuleDefinitionSet set = factory.loadModules(defs, "base");

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

@Test
public void testBeans() throws IOException {
ModuleBasedContextFactory factory = new ModuleBasedContextFactory();
ModuleDefinitionSet set = factory.loadModules(defs, "base");

testBeansInContext(set, "base", 1, new String[] {"base"}, new String[] {"child1", "child2", "child1-1"});
Expand All @@ -92,6 +90,51 @@ public void testBeans() throws IOException {
testBeansInContext(set, "child1-1", 3, new String[] {"base", "child1", "child1-1"}, new String[] {"child2"});
}

@Test
public void testEmptyNameConfigResources() throws IOException {
ModuleDefinitionSet set = factory.loadModules(defs, "base");
testConfigResourcesArray(new String[] {}, set.getConfigResources(""));
}

@Test
public void testBaseConfigResources() throws IOException {
ModuleDefinitionSet set = factory.loadModules(defs, "base");
testConfigResourcesArray(new String[] {"base-context.xml", "base-context-inheritable.xml"}, set.getConfigResources("base"));
}

@Test
public void testChild1ConfigResources() throws IOException {
ModuleDefinitionSet set = factory.loadModules(defs, "base");
testConfigResourcesArray(new String[] {
"child1-context.xml", "child1-context-inheritable.xml",
"base-context-inheritable.xml", "chil1-context-override.xml"
}, set.getConfigResources("child1"));
}

@Test
public void testChild2ConfigResources() throws IOException {
ModuleDefinitionSet set = factory.loadModules(defs, "base");
testConfigResourcesArray(new String[] {
"child2-context.xml", "base-context-inheritable.xml"
}, set.getConfigResources("child2"));
}

@Test
public void testChild11ConfigResources() throws IOException {
ModuleDefinitionSet set = factory.loadModules(defs, "base");
testConfigResourcesArray(new String[] {
"chil1-1-context.xml", "child1-context-inheritable.xml", "base-context-inheritable.xml"
}, set.getConfigResources("child1-1"));
}

private void testConfigResourcesArray(String[] expected, Resource[] actual) {
assertEquals(expected.length, actual.length);
List<String> actualFileNameList = Arrays.stream(actual).map(Resource::getFilename).collect(Collectors.toList());
for (int i = 0; i < expected.length; i++) {
assertEquals(expected[i], actualFileNameList.get(i));
}
}

protected void testBeansInContext(ModuleDefinitionSet set, String name, int order, String[] parents, String[] notTheres) {
ApplicationContext context = set.getApplicationContext(name);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!--
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.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

</beans>
Loading