SpringContext { public BeanDefinition getBean(String name) { BeanDefinition def = beanDefinitionsById.get(name); if (def == null) def = beanDefinitionsByName.get(name); return def; } SpringContext(WarMachine war, String contextLocation);  SpringContext(SpringContext parent, WarMachine war, String contextLocation); Map<String,BeanDefinition> getBeans(); BeanDefinition getBean(String name); List<BeanDefinition> getBeansByClass(String className);  }
@Test public void testGetProperty() throws Exception { logger.info("testGetProperty()"); SpringContext context = new SpringContext(null, "classpath:contexts/propContext.xml"); BeanDefinition bean = context.getBean("example"); assertNotNull("able to find bean", bean); assertEquals("extracted property as string in attribute", "foo", bean.getPropertyAsString("propAsStringAttribute")); assertEquals("extracted property as string in value", "bar", bean.getPropertyAsString("propAsStringValue")); assertEquals("extracted property as ref", "example", bean.getPropertyAsRefId("propAsRefId")); Properties propsProp1 = bean.getPropertyAsProperties("propAsSingleValue"); assertNotNull("able to extract property with name-value props", propsProp1); assertEquals("extracted property for 'foo'", "bar", propsProp1.get("foo")); assertEquals("extracted property for 'argle'", "bargle", propsProp1.get("argle")); Properties propsProp2 = bean.getPropertyAsProperties("propAsExplicitProperties"); assertNotNull("able to extract property with explicit props", propsProp2); assertEquals("extracted property for 'foo'", "bar", propsProp2.get("foo")); assertEquals("extracted property for 'argle'", "bargle", propsProp2.get("argle")); }
@Test public void testInheritProperty() throws Exception { logger.info("testInheritProperty()"); SpringContext context = new SpringContext(null, "classpath:contexts/inheritProp.xml"); BeanDefinition bean = context.getBean("inherited"); assertNotNull("able to find bean", bean); assertEquals("extracted property as string in attribute", "foo", bean.getPropertyAsString("propAsStringAttribute")); assertEquals("extracted property as string in value", "bar", bean.getPropertyAsString("propAsStringValue")); assertEquals("extracted property as ref", "example", bean.getPropertyAsRefId("propAsRefId")); Properties propsProp1 = bean.getPropertyAsProperties("propAsSingleValue"); assertNotNull("able to extract property with name-value props", propsProp1); assertEquals("extracted property for 'foo'", "bar", propsProp1.get("foo")); assertEquals("extracted property for 'argle'", "bargle", propsProp1.get("argle")); Properties propsProp2 = bean.getPropertyAsProperties("propAsExplicitProperties"); assertNotNull("able to extract property with explicit props", propsProp2); assertEquals("extracted property for 'foo'", "bar", propsProp2.get("foo")); assertEquals("extracted property for 'argle'", "bargle", propsProp2.get("argle")); }
@Test public void testInheritClass() throws Exception { logger.info("testInheritClass()"); SpringContext ctx = new SpringContext(null, "classpath:contexts/inheritClass.xml"); assertEquals("com.example.SomeBean", ctx.getBean("concreteBean").getBeanClass()); }
@Test(expected=InvalidContextException.class) public void testInheritClassFail() throws Exception { logger.info("testInheritClassFail()"); SpringContext ctx = new SpringContext(null, "classpath:contexts/inheritClassFail.xml"); assertEquals("com.example.SomeBean", ctx.getBean("concreteBean").getBeanClass()); }
@Test public void testImportedContext() throws Exception { logger.info("testImportedContext()"); WarMachine war = TestHelpers.createWarMachine(WarNames.SPRING_SPLIT_CONFIG); SpringContext ctx = new SpringContext(war, "/WEB-INF/spring/servletContext.xml"); assertEquals("bean from base context", "com.kdgregory.pathfinder.test.spring2.ControllerA", ctx.getBean("controllerA").getBeanClass()); assertEquals("bean from imported context", "com.kdgregory.pathfinder.test.spring2.ControllerB", ctx.getBean("controllerB").getBeanClass()); }
@Test public void testImportedContextAbsolutePath() throws Exception { logger.info("testImportedContextAbsolutePath()"); WarMachine war = TestHelpers.createWarMachine(WarNames.SPRING_RESOURCES); SpringContext ctx = new SpringContext(war, "/WEB-INF/spring/servletContext.xml"); assertEquals("bean from base context", "com.kdgregory.pathfinder.test.spring3.pkg1.ControllerA", ctx.getBean("controllerA").getBeanClass()); assertEquals("bean from imported context", "com.kdgregory.pathfinder.test.spring3.pkg1.ControllerB", ctx.getBean("controllerB").getBeanClass()); }