From ee994b344457b3fa36b9c46e550e7bfd6f99854f Mon Sep 17 00:00:00 2001 From: he1l0world Date: Tue, 17 Jun 2025 00:41:59 -0400 Subject: [PATCH 1/2] add test cases for getConfigResources --- .../ModuleBasedContextFactoryTest.java | 53 ++++++++++++------- ...table.xml => base-context-inheritable.xml} | 0 .../{test-context.xml => base-context.xml} | 0 .../{test-context.xml => chil1-1-context.xml} | 0 ...verride.xml => chil1-context-override.xml} | 0 .../child1/child1-context-inheritable.xml | 24 +++++++++ .../{test-context.xml => child1-context.xml} | 0 .../{test-context.xml => child2-context.xml} | 0 8 files changed, 59 insertions(+), 18 deletions(-) rename framework/spring/module/src/test/resources/testhierarchy/base/{test-context-inheritable.xml => base-context-inheritable.xml} (100%) rename framework/spring/module/src/test/resources/testhierarchy/base/{test-context.xml => base-context.xml} (100%) rename framework/spring/module/src/test/resources/testhierarchy/child1-1/{test-context.xml => chil1-1-context.xml} (100%) rename framework/spring/module/src/test/resources/testhierarchy/child1/{test-context-override.xml => chil1-context-override.xml} (100%) create mode 100644 framework/spring/module/src/test/resources/testhierarchy/child1/child1-context-inheritable.xml rename framework/spring/module/src/test/resources/testhierarchy/child1/{test-context.xml => child1-context.xml} (100%) rename framework/spring/module/src/test/resources/testhierarchy/child2/{test-context.xml => child2-context.xml} (100%) diff --git a/framework/spring/module/src/test/java/org/apache/cloudstack/spring/module/factory/ModuleBasedContextFactoryTest.java b/framework/spring/module/src/test/java/org/apache/cloudstack/spring/module/factory/ModuleBasedContextFactoryTest.java index d8d109f65758..f4f257b518d1 100644 --- a/framework/spring/module/src/test/java/org/apache/cloudstack/spring/module/factory/ModuleBasedContextFactoryTest.java +++ b/framework/spring/module/src/test/java/org/apache/cloudstack/spring/module/factory/ModuleBasedContextFactoryTest.java @@ -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 defs; + ModuleBasedContextFactory factory = new ModuleBasedContextFactory(); @Before public void setUp() throws IOException { @@ -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")); @@ -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); @@ -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")); @@ -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"}); @@ -92,6 +90,25 @@ public void testBeans() throws IOException { testBeansInContext(set, "child1-1", 3, new String[] {"base", "child1", "child1-1"}, new String[] {"child2"}); } + @Test + public void testConfigResources() throws IOException { + ModuleDefinitionSet set = factory.loadModules(defs, "base"); + + testConfigResourcesArray(new String[] {}, set.getConfigResources("")); + testConfigResourcesArray(new String[] {"base-context.xml", "base-context-inheritable.xml"}, set.getConfigResources("base")); + testConfigResourcesArray(new String[] {"child1-context.xml", "child1-context-inheritable.xml", "base-context-inheritable.xml", "chil1-context-override.xml"}, set.getConfigResources("child1")); + testConfigResourcesArray(new String[] {"child2-context.xml", "base-context-inheritable.xml"}, set.getConfigResources("child2")); + 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 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); diff --git a/framework/spring/module/src/test/resources/testhierarchy/base/test-context-inheritable.xml b/framework/spring/module/src/test/resources/testhierarchy/base/base-context-inheritable.xml similarity index 100% rename from framework/spring/module/src/test/resources/testhierarchy/base/test-context-inheritable.xml rename to framework/spring/module/src/test/resources/testhierarchy/base/base-context-inheritable.xml diff --git a/framework/spring/module/src/test/resources/testhierarchy/base/test-context.xml b/framework/spring/module/src/test/resources/testhierarchy/base/base-context.xml similarity index 100% rename from framework/spring/module/src/test/resources/testhierarchy/base/test-context.xml rename to framework/spring/module/src/test/resources/testhierarchy/base/base-context.xml diff --git a/framework/spring/module/src/test/resources/testhierarchy/child1-1/test-context.xml b/framework/spring/module/src/test/resources/testhierarchy/child1-1/chil1-1-context.xml similarity index 100% rename from framework/spring/module/src/test/resources/testhierarchy/child1-1/test-context.xml rename to framework/spring/module/src/test/resources/testhierarchy/child1-1/chil1-1-context.xml diff --git a/framework/spring/module/src/test/resources/testhierarchy/child1/test-context-override.xml b/framework/spring/module/src/test/resources/testhierarchy/child1/chil1-context-override.xml similarity index 100% rename from framework/spring/module/src/test/resources/testhierarchy/child1/test-context-override.xml rename to framework/spring/module/src/test/resources/testhierarchy/child1/chil1-context-override.xml diff --git a/framework/spring/module/src/test/resources/testhierarchy/child1/child1-context-inheritable.xml b/framework/spring/module/src/test/resources/testhierarchy/child1/child1-context-inheritable.xml new file mode 100644 index 000000000000..2cea17e97e35 --- /dev/null +++ b/framework/spring/module/src/test/resources/testhierarchy/child1/child1-context-inheritable.xml @@ -0,0 +1,24 @@ + + + + diff --git a/framework/spring/module/src/test/resources/testhierarchy/child1/test-context.xml b/framework/spring/module/src/test/resources/testhierarchy/child1/child1-context.xml similarity index 100% rename from framework/spring/module/src/test/resources/testhierarchy/child1/test-context.xml rename to framework/spring/module/src/test/resources/testhierarchy/child1/child1-context.xml diff --git a/framework/spring/module/src/test/resources/testhierarchy/child2/test-context.xml b/framework/spring/module/src/test/resources/testhierarchy/child2/child2-context.xml similarity index 100% rename from framework/spring/module/src/test/resources/testhierarchy/child2/test-context.xml rename to framework/spring/module/src/test/resources/testhierarchy/child2/child2-context.xml From 390ac7b2fe9d9335ccde46b2fbbb394ed090a175 Mon Sep 17 00:00:00 2001 From: he1l0world Date: Tue, 17 Jun 2025 03:17:59 -0400 Subject: [PATCH 2/2] split tests for each cases --- .../ModuleBasedContextFactoryTest.java | 36 ++++++++++++++++--- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/framework/spring/module/src/test/java/org/apache/cloudstack/spring/module/factory/ModuleBasedContextFactoryTest.java b/framework/spring/module/src/test/java/org/apache/cloudstack/spring/module/factory/ModuleBasedContextFactoryTest.java index f4f257b518d1..42884e1764e9 100644 --- a/framework/spring/module/src/test/java/org/apache/cloudstack/spring/module/factory/ModuleBasedContextFactoryTest.java +++ b/framework/spring/module/src/test/java/org/apache/cloudstack/spring/module/factory/ModuleBasedContextFactoryTest.java @@ -91,14 +91,40 @@ public void testBeans() throws IOException { } @Test - public void testConfigResources() throws IOException { + 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")); - testConfigResourcesArray(new String[] {"child1-context.xml", "child1-context-inheritable.xml", "base-context-inheritable.xml", "chil1-context-override.xml"}, set.getConfigResources("child1")); - testConfigResourcesArray(new String[] {"child2-context.xml", "base-context-inheritable.xml"}, set.getConfigResources("child2")); - testConfigResourcesArray(new String[] {"chil1-1-context.xml", "child1-context-inheritable.xml", "base-context-inheritable.xml"}, set.getConfigResources("child1-1")); + } + + @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) {