Search in sources :

Example 1 with ResourceTestBean

use of cn.taketoday.tests.sample.beans.ResourceTestBean in project today-infrastructure by TAKETODAY.

the class XmlBeanFactoryTests method resourceAndInputStream.

@Test
void resourceAndInputStream() throws IOException {
    StandardBeanFactory xbf = new StandardBeanFactory();
    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");
    assertThat(resource1.getResource() instanceof ClassPathResource).isTrue();
    StringWriter writer = new StringWriter();
    FileCopyUtils.copy(new InputStreamReader(resource1.getResource().getInputStream()), writer);
    assertThat(writer.toString()).isEqualTo("test");
    writer = new StringWriter();
    FileCopyUtils.copy(new InputStreamReader(resource1.getInputStream()), writer);
    assertThat(writer.toString()).isEqualTo("test");
    writer = new StringWriter();
    FileCopyUtils.copy(new InputStreamReader(resource2.getResource().getInputStream()), writer);
    assertThat(writer.toString()).isEqualTo("test");
    writer = new StringWriter();
    FileCopyUtils.copy(new InputStreamReader(resource2.getInputStream()), writer);
    assertThat(writer.toString()).isEqualTo("test");
}
Also used : ResourceTestBean(cn.taketoday.tests.sample.beans.ResourceTestBean) StringWriter(java.io.StringWriter) InputStreamReader(java.io.InputStreamReader) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) ClassPathResource(cn.taketoday.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Example 2 with ResourceTestBean

use of cn.taketoday.tests.sample.beans.ResourceTestBean in project today-framework by TAKETODAY.

the class XmlBeanFactoryTests method resourceAndInputStream.

@Test
void resourceAndInputStream() throws IOException {
    StandardBeanFactory xbf = new StandardBeanFactory();
    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");
    assertThat(resource1.getResource() instanceof ClassPathResource).isTrue();
    StringWriter writer = new StringWriter();
    FileCopyUtils.copy(new InputStreamReader(resource1.getResource().getInputStream()), writer);
    assertThat(writer.toString()).isEqualTo("test");
    writer = new StringWriter();
    FileCopyUtils.copy(new InputStreamReader(resource1.getInputStream()), writer);
    assertThat(writer.toString()).isEqualTo("test");
    writer = new StringWriter();
    FileCopyUtils.copy(new InputStreamReader(resource2.getResource().getInputStream()), writer);
    assertThat(writer.toString()).isEqualTo("test");
    writer = new StringWriter();
    FileCopyUtils.copy(new InputStreamReader(resource2.getInputStream()), writer);
    assertThat(writer.toString()).isEqualTo("test");
}
Also used : ResourceTestBean(cn.taketoday.tests.sample.beans.ResourceTestBean) StringWriter(java.io.StringWriter) InputStreamReader(java.io.InputStreamReader) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) ClassPathResource(cn.taketoday.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Example 3 with ResourceTestBean

use of cn.taketoday.tests.sample.beans.ResourceTestBean in project today-framework by TAKETODAY.

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");
    boolean condition = resource1.getResource() instanceof ClassPathResource;
    assertThat(condition).isTrue();
    StringWriter writer = new StringWriter();
    FileCopyUtils.copy(new InputStreamReader(resource1.getResource().getInputStream()), writer);
    assertThat(writer.toString()).isEqualTo("contexttest");
    writer = new StringWriter();
    FileCopyUtils.copy(new InputStreamReader(resource1.getInputStream()), writer);
    assertThat(writer.toString()).isEqualTo("test");
    writer = new StringWriter();
    FileCopyUtils.copy(new InputStreamReader(resource2.getResource().getInputStream()), writer);
    assertThat(writer.toString()).isEqualTo("contexttest");
    writer = new StringWriter();
    FileCopyUtils.copy(new InputStreamReader(resource2.getInputStream()), writer);
    assertThat(writer.toString()).isEqualTo("test");
    ctx.close();
}
Also used : ResourceTestBean(cn.taketoday.tests.sample.beans.ResourceTestBean) StringWriter(java.io.StringWriter) InputStreamReader(java.io.InputStreamReader) ClassPathResource(cn.taketoday.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Example 4 with ResourceTestBean

use of cn.taketoday.tests.sample.beans.ResourceTestBean in project today-framework by TAKETODAY.

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);
    assertThat(resourceClass.isInstance(tb.getResource())).isTrue();
    assertThat(tb.getResourceArray().length > 0).isTrue();
    assertThat(resourceClass.isInstance(tb.getResourceArray()[0])).isTrue();
    assertThat(tb.getResourceMap().size() == 1).isTrue();
    assertThat(resourceClass.isInstance(tb.getResourceMap().get("key1"))).isTrue();
    assertThat(tb.getResourceArrayMap().size() == 1).isTrue();
    assertThat(tb.getResourceArrayMap().get("key1").length > 0).isTrue();
    assertThat(resourceClass.isInstance(tb.getResourceArrayMap().get("key1")[0])).isTrue();
}
Also used : ResourceTestBean(cn.taketoday.tests.sample.beans.ResourceTestBean) ApplicationContext(cn.taketoday.context.ApplicationContext)

Example 5 with ResourceTestBean

use of cn.taketoday.tests.sample.beans.ResourceTestBean in project today-infrastructure by TAKETODAY.

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");
    boolean condition = resource1.getResource() instanceof ClassPathResource;
    assertThat(condition).isTrue();
    StringWriter writer = new StringWriter();
    FileCopyUtils.copy(new InputStreamReader(resource1.getResource().getInputStream()), writer);
    assertThat(writer.toString()).isEqualTo("contexttest");
    writer = new StringWriter();
    FileCopyUtils.copy(new InputStreamReader(resource1.getInputStream()), writer);
    assertThat(writer.toString()).isEqualTo("test");
    writer = new StringWriter();
    FileCopyUtils.copy(new InputStreamReader(resource2.getResource().getInputStream()), writer);
    assertThat(writer.toString()).isEqualTo("contexttest");
    writer = new StringWriter();
    FileCopyUtils.copy(new InputStreamReader(resource2.getInputStream()), writer);
    assertThat(writer.toString()).isEqualTo("test");
    ctx.close();
}
Also used : ResourceTestBean(cn.taketoday.tests.sample.beans.ResourceTestBean) StringWriter(java.io.StringWriter) InputStreamReader(java.io.InputStreamReader) ClassPathResource(cn.taketoday.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Aggregations

ResourceTestBean (cn.taketoday.tests.sample.beans.ResourceTestBean)6 ClassPathResource (cn.taketoday.core.io.ClassPathResource)4 InputStreamReader (java.io.InputStreamReader)4 StringWriter (java.io.StringWriter)4 Test (org.junit.jupiter.api.Test)4 StandardBeanFactory (cn.taketoday.beans.factory.support.StandardBeanFactory)2 ApplicationContext (cn.taketoday.context.ApplicationContext)2