Search in sources :

Example 76 with ByteArrayResource

use of org.springframework.core.io.ByteArrayResource in project spring-framework by spring-projects.

the class YamlPropertiesFactoryBeanTests method testLoadResourceWithoutDefaultMatch.

@Test
public void testLoadResourceWithoutDefaultMatch() throws Exception {
    YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
    factory.setMatchDefault(false);
    factory.setResources(new ByteArrayResource("one: two\n---\nfoo: bar\nspam: baz\n---\nfoo: bag\nspam: bad".getBytes()));
    factory.setDocumentMatchers(new DocumentMatcher() {

        @Override
        public MatchStatus matches(Properties properties) {
            if (!properties.containsKey("foo")) {
                return MatchStatus.ABSTAIN;
            }
            return ("bag".equals(properties.getProperty("foo")) ? MatchStatus.FOUND : MatchStatus.NOT_FOUND);
        }
    });
    Properties properties = factory.getObject();
    assertThat(properties.getProperty("foo"), equalTo("bag"));
    assertThat(properties.getProperty("spam"), equalTo("bad"));
    assertThat(properties.getProperty("one"), nullValue());
}
Also used : ByteArrayResource(org.springframework.core.io.ByteArrayResource) Properties(java.util.Properties) Test(org.junit.Test)

Example 77 with ByteArrayResource

use of org.springframework.core.io.ByteArrayResource in project spring-framework by spring-projects.

the class YamlPropertiesFactoryBeanTests method testLoadResourcesWithInternalOverride.

@Test
public void testLoadResourcesWithInternalOverride() throws Exception {
    YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
    factory.setResources(new ByteArrayResource("foo: bar\nspam:\n  foo: baz\nfoo: bucket".getBytes()));
    Properties properties = factory.getObject();
    assertThat(properties.getProperty("foo"), equalTo("bucket"));
}
Also used : ByteArrayResource(org.springframework.core.io.ByteArrayResource) Properties(java.util.Properties) Test(org.junit.Test)

Example 78 with ByteArrayResource

use of org.springframework.core.io.ByteArrayResource in project spring-framework by spring-projects.

the class YamlMapFactoryBeanTests method testFirstFound.

@Test
public void testFirstFound() throws Exception {
    this.factory.setResolutionMethod(YamlProcessor.ResolutionMethod.FIRST_FOUND);
    this.factory.setResources(new AbstractResource() {

        @Override
        public String getDescription() {
            return "non-existent";
        }

        @Override
        public InputStream getInputStream() throws IOException {
            throw new IOException("planned");
        }
    }, new ByteArrayResource("foo:\n  spam: bar".getBytes()));
    assertEquals(1, this.factory.getObject().size());
}
Also used : InputStream(java.io.InputStream) AbstractResource(org.springframework.core.io.AbstractResource) IOException(java.io.IOException) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Test(org.junit.Test)

Example 79 with ByteArrayResource

use of org.springframework.core.io.ByteArrayResource in project spring-framework by spring-projects.

the class RequestResponseBodyMethodProcessorMockTests method handleReturnTypeResource.

@Test
public void handleReturnTypeResource() throws Exception {
    Resource returnValue = new ByteArrayResource("Content".getBytes(StandardCharsets.UTF_8));
    given(resourceMessageConverter.canWrite(ByteArrayResource.class, null)).willReturn(true);
    given(resourceMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.ALL));
    given(resourceMessageConverter.canWrite(ByteArrayResource.class, MediaType.APPLICATION_OCTET_STREAM)).willReturn(true);
    processor.handleReturnValue(returnValue, returnTypeResource, mavContainer, webRequest);
    then(resourceMessageConverter).should(times(1)).write(any(ByteArrayResource.class), eq(MediaType.APPLICATION_OCTET_STREAM), any(HttpOutputMessage.class));
    assertEquals(200, servletResponse.getStatus());
}
Also used : HttpOutputMessage(org.springframework.http.HttpOutputMessage) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Resource(org.springframework.core.io.Resource) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Test(org.junit.Test)

Example 80 with ByteArrayResource

use of org.springframework.core.io.ByteArrayResource in project spring-framework by spring-projects.

the class FreeMarkerConfigurerTests method freeMarkerConfigurationFactoryBeanWithNonFileResourceLoaderPath.

@Test
@SuppressWarnings("rawtypes")
public void freeMarkerConfigurationFactoryBeanWithNonFileResourceLoaderPath() throws Exception {
    FreeMarkerConfigurationFactoryBean fcfb = new FreeMarkerConfigurationFactoryBean();
    fcfb.setTemplateLoaderPath("file:/mydir");
    Properties settings = new Properties();
    settings.setProperty("localized_lookup", "false");
    fcfb.setFreemarkerSettings(settings);
    fcfb.setResourceLoader(new ResourceLoader() {

        @Override
        public Resource getResource(String location) {
            if (!("file:/mydir".equals(location) || "file:/mydir/test".equals(location))) {
                throw new IllegalArgumentException(location);
            }
            return new ByteArrayResource("test".getBytes(), "test");
        }

        @Override
        public ClassLoader getClassLoader() {
            return getClass().getClassLoader();
        }
    });
    fcfb.afterPropertiesSet();
    assertThat(fcfb.getObject(), instanceOf(Configuration.class));
    Configuration fc = fcfb.getObject();
    Template ft = fc.getTemplate("test");
    assertEquals("test", FreeMarkerTemplateUtils.processTemplateIntoString(ft, new HashMap()));
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader) FreeMarkerConfigurationFactoryBean(org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean) Configuration(freemarker.template.Configuration) HashMap(java.util.HashMap) FileSystemResource(org.springframework.core.io.FileSystemResource) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Resource(org.springframework.core.io.Resource) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Properties(java.util.Properties) Template(freemarker.template.Template) Test(org.junit.Test)

Aggregations

ByteArrayResource (org.springframework.core.io.ByteArrayResource)95 Test (org.junit.Test)67 Resource (org.springframework.core.io.Resource)32 Properties (java.util.Properties)26 IOException (java.io.IOException)6 InputStream (java.io.InputStream)6 JmsNorthbounderConfig (org.opennms.netmgt.alarmd.northbounder.jms.JmsNorthbounderConfig)6 JmsNorthbounderConfigDao (org.opennms.netmgt.alarmd.northbounder.jms.JmsNorthbounderConfigDao)6 InetAddress (java.net.InetAddress)5 ArrayList (java.util.ArrayList)5 LinkedList (java.util.LinkedList)5 Before (org.junit.Before)5 NorthboundAlarm (org.opennms.netmgt.alarmd.api.NorthboundAlarm)5 JmsDestination (org.opennms.netmgt.alarmd.northbounder.jms.JmsDestination)5 OnmsAlarm (org.opennms.netmgt.model.OnmsAlarm)5 OnmsIpInterface (org.opennms.netmgt.model.OnmsIpInterface)5 OnmsNode (org.opennms.netmgt.model.OnmsNode)5 LinkedHashMap (java.util.LinkedHashMap)4 Map (java.util.Map)4 MutablePropertySources (org.springframework.core.env.MutablePropertySources)4