Search in sources :

Example 1 with ResourceTestBean

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());
}
Also used : ResourceTestBean(org.springframework.tests.sample.beans.ResourceTestBean) StringWriter(java.io.StringWriter) InputStreamReader(java.io.InputStreamReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 2 with ResourceTestBean

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();
}
Also used : ResourceTestBean(org.springframework.tests.sample.beans.ResourceTestBean) StringWriter(java.io.StringWriter) InputStreamReader(java.io.InputStreamReader) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 3 with ResourceTestBean

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]));
}
Also used : ResourceTestBean(org.springframework.tests.sample.beans.ResourceTestBean) ApplicationContext(org.springframework.context.ApplicationContext)

Aggregations

ResourceTestBean (org.springframework.tests.sample.beans.ResourceTestBean)3 InputStreamReader (java.io.InputStreamReader)2 StringWriter (java.io.StringWriter)2 Test (org.junit.Test)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)1 ApplicationContext (org.springframework.context.ApplicationContext)1