public class TransitionManagerTest extends TestCase {
public void testProcessTransitions() {
DefaultModuleStateHolder moduleStateHolder = new DefaultModuleStateHolder();
DefaultTransitionManager transitionManager = new DefaultTransitionManager();
ModuleLoaderRegistry registry = new ModuleLoaderRegistry();
ModuleLocationResolver resolver = new StandaloneModuleLocationResolver();
CustomClassLoaderFactory classLoaderFactory = new CustomClassLoaderFactory();
classLoaderFactory.setModuleLocationResolver(resolver);
ApplicationModuleLoader rootModuleLoader = new ApplicationModuleLoader();
registry.addItem("spring-"+ModuleTypes.ROOT, rootModuleLoader);
ApplicationModuleLoader applicationModuleLoader = new ApplicationModuleLoader();
registry.addItem("spring-"+ModuleTypes.APPLICATION, applicationModuleLoader);
DefaultApplicationContextLoader contextLoader = new DefaultApplicationContextLoader();
contextLoader.setModuleLoaderRegistry(registry);
contextLoader.setDelegatingContextLoaderRegistry(new DelegatingContextLoaderRegistry());
TransitionProcessorRegistry transitionProcessors = new TransitionProcessorRegistry();
LoadTransitionProcessor loadTransitionProcessor = new LoadTransitionProcessor();
SpringModuleRuntime moduleRuntime = new SpringModuleRuntime();
moduleRuntime.setApplicationContextLoader(contextLoader);
moduleRuntime.setClassLoaderFactory(classLoaderFactory);
ModuleRuntime springModuleRuntime = moduleRuntime;
Map<String, ModuleRuntime> moduleRuntimes = Collections.singletonMap("spring", springModuleRuntime);
DefaultModuleRuntimeManager manager = new DefaultModuleRuntimeManager();
manager.setModuleRuntimes(moduleRuntimes);
loadTransitionProcessor.setModuleRuntimeManager(manager);
UnloadTransitionProcessor unloadTransitionProcessor = new UnloadTransitionProcessor();
transitionProcessors.addItem(Transition.UNLOADED_TO_LOADED, loadTransitionProcessor);
transitionProcessors.addItem(Transition.LOADED_TO_UNLOADED, unloadTransitionProcessor);
transitionManager.setTransitionProcessorRegistry(transitionProcessors);
ApplicationManager applicationManager = TestApplicationManager.newApplicationManager(new ModuleClassLoaderRegistry(), moduleStateHolder, new DelegatingServiceRegistry());
Application application = applicationManager.getCurrentApplication();
RootModuleDefinition test1Definition = newTest1().getModuleDefinition();
ModificationExtractor calculator = new StrictModificationExtractor();
TransitionSet transitions = calculator.getTransitions(application, null, test1Definition);
transitionManager.processTransitions(moduleStateHolder, application, transitions);
ConfigurableApplicationContext context = SpringModuleUtils.getRootSpringContext(moduleStateHolder);
service((FileMonitor) context.getBean("bean1"));
noService((FileMonitor) context.getBean("bean3"));
RootModuleDefinition test2Definition = newTest2().getModuleDefinition();
transitions = calculator.getTransitions(application, test1Definition, test2Definition);
transitionManager.processTransitions(moduleStateHolder, application, transitions);
context = SpringModuleUtils.getRootSpringContext(moduleStateHolder);
service((FileMonitor) context.getBean("bean1"));
//now we got bean3
service((FileMonitor) context.getBean("bean3"));
moduleStateHolder.setExternalRootModuleName("sample-module1");
SpringRuntimeModule module1 = (SpringRuntimeModule) moduleStateHolder.getModule("sample-module1");
ConfigurableApplicationContext newRootModule = SpringModuleUtils.getRootSpringContext(moduleStateHolder);
assertSame(module1.getApplicationContext(), newRootModule);
}