use of org.springframework.tests.sample.beans.ResourceTestBean in project spring-framework by spring-projects.
the class XmlBeanFactoryTests method testResourceAndInputStream.
@Test
public void testResourceAndInputStream() throws IOException {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(RESOURCE_CONTEXT);
// comes from "resourceImport.xml"
ResourceTestBean resource1 = (ResourceTestBean) xbf.getBean("resource1");
// comes from "resource.xml"
ResourceTestBean resource2 = (ResourceTestBean) xbf.getBean("resource2");
assertTrue(resource1.getResource() instanceof ClassPathResource);
StringWriter writer = new StringWriter();
FileCopyUtils.copy(new InputStreamReader(resource1.getResource().getInputStream()), writer);
assertEquals("test", writer.toString());
writer = new StringWriter();
FileCopyUtils.copy(new InputStreamReader(resource1.getInputStream()), writer);
assertEquals("test", writer.toString());
writer = new StringWriter();
FileCopyUtils.copy(new InputStreamReader(resource2.getResource().getInputStream()), writer);
assertEquals("test", writer.toString());
writer = new StringWriter();
FileCopyUtils.copy(new InputStreamReader(resource2.getInputStream()), writer);
assertEquals("test", writer.toString());
}
use of org.springframework.tests.sample.beans.ResourceTestBean in project spring-framework by spring-projects.
the class ClassPathXmlApplicationContextTests method testResourceAndInputStream.
@Test
public void testResourceAndInputStream() throws IOException {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(RESOURCE_CONTEXT) {
@Override
public Resource getResource(String location) {
if (TEST_PROPERTIES.equals(location)) {
return new ClassPathResource(TEST_PROPERTIES, ClassPathXmlApplicationContextTests.class);
}
return super.getResource(location);
}
};
ResourceTestBean resource1 = (ResourceTestBean) ctx.getBean("resource1");
ResourceTestBean resource2 = (ResourceTestBean) ctx.getBean("resource2");
assertTrue(resource1.getResource() instanceof ClassPathResource);
StringWriter writer = new StringWriter();
FileCopyUtils.copy(new InputStreamReader(resource1.getResource().getInputStream()), writer);
assertEquals("contexttest", writer.toString());
writer = new StringWriter();
FileCopyUtils.copy(new InputStreamReader(resource1.getInputStream()), writer);
assertEquals("test", writer.toString());
writer = new StringWriter();
FileCopyUtils.copy(new InputStreamReader(resource2.getResource().getInputStream()), writer);
assertEquals("contexttest", writer.toString());
writer = new StringWriter();
FileCopyUtils.copy(new InputStreamReader(resource2.getInputStream()), writer);
assertEquals("test", writer.toString());
ctx.close();
}
use of org.springframework.tests.sample.beans.ResourceTestBean in project spring-framework by spring-projects.
the class ConversionServiceFactoryBeanTests method doTestConversionServiceInApplicationContext.
private void doTestConversionServiceInApplicationContext(String fileName, Class<?> resourceClass) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(fileName, getClass());
ResourceTestBean tb = ctx.getBean("resourceTestBean", ResourceTestBean.class);
assertTrue(resourceClass.isInstance(tb.getResource()));
assertTrue(tb.getResourceArray().length > 0);
assertTrue(resourceClass.isInstance(tb.getResourceArray()[0]));
assertTrue(tb.getResourceMap().size() == 1);
assertTrue(resourceClass.isInstance(tb.getResourceMap().get("key1")));
assertTrue(tb.getResourceArrayMap().size() == 1);
assertTrue(tb.getResourceArrayMap().get("key1").length > 0);
assertTrue(resourceClass.isInstance(tb.getResourceArrayMap().get("key1")[0]));
}
Aggregations