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");
}
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");
}
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();
}
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();
}
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();
}
Aggregations