@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
@Override public BodyContentSpec expectBody() { ByteArrayResource resource = this.response.bodyToMono(ByteArrayResource.class).block(this.timeout); byte[] body = (resource != null ? resource.getByteArray() : null); EntityExchangeResult<byte[]> entityResult = new EntityExchangeResult<>(this.exchangeResult, body); return new DefaultBodyContentSpec(entityResult); }
@Test(expected = IllegalArgumentException.class) public void toResourceRegionIllegalLength() { ByteArrayResource resource = mock(ByteArrayResource.class); given(resource.contentLength()).willReturn(-1L); HttpRange range = HttpRange.createByteRange(0, 9); range.toResourceRegion(resource); }
@Override protected Resource decodeDataBuffer(DataBuffer dataBuffer, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) { byte[] bytes = new byte[dataBuffer.readableByteCount()]; dataBuffer.read(bytes); DataBufferUtils.release(dataBuffer); if (logger.isDebugEnabled()) { logger.debug(Hints.getLogPrefix(hints) + "Read " + bytes.length + " bytes"); } Class<?> clazz = elementType.toClass(); if (clazz == InputStreamResource.class) { return new InputStreamResource(new ByteArrayInputStream(bytes)); } else if (Resource.class.isAssignableFrom(clazz)) { return new ByteArrayResource(bytes); } else { throw new IllegalStateException("Unsupported resource class: " + clazz); } }
@Override protected Resource decodeDataBuffer(DataBuffer dataBuffer, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) { byte[] bytes = new byte[dataBuffer.readableByteCount()]; dataBuffer.read(bytes); DataBufferUtils.release(dataBuffer); if (logger.isDebugEnabled()) { logger.debug(Hints.getLogPrefix(hints) + "Read " + bytes.length + " bytes"); } Class<?> clazz = elementType.toClass(); if (clazz == InputStreamResource.class) { return new InputStreamResource(new ByteArrayInputStream(bytes)); } else if (Resource.class.isAssignableFrom(clazz)) { return new ByteArrayResource(bytes); } else { throw new IllegalStateException("Unsupported resource class: " + clazz); } }
@Test public void testLoadResourcesWithOverride() { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResources( new ByteArrayResource("foo: bar\nspam:\n foo: baz".getBytes()), new ByteArrayResource("foo:\n bar: spam".getBytes())); Properties properties = factory.getObject(); assertThat(properties.getProperty("foo"), equalTo("bar")); assertThat(properties.getProperty("spam.foo"), equalTo("baz")); assertThat(properties.getProperty("foo.bar"), equalTo("spam")); }
@SuppressWarnings("unchecked") @Test public void testOverrideAndRemoveDefaults() { this.factory.setResources(new ByteArrayResource("foo:\n bar: spam".getBytes()), new ByteArrayResource("foo:\n spam: bar".getBytes())); assertEquals(1, this.factory.getObject().size()); assertEquals(2, ((Map<String, Object>) this.factory.getObject().get("foo")).size()); }
@Test(expected = DuplicateKeyException.class) public void testDuplicateKey() { this.factory.setResources(new ByteArrayResource("mymap:\n foo: bar\nmymap:\n bar: foo".getBytes())); this.factory.getObject().get("mymap"); }
@Test public void testByteArrayResource() throws IOException { Resource resource = new ByteArrayResource("testString".getBytes()); assertTrue(resource.exists()); assertFalse(resource.isOpen()); String content = FileCopyUtils.copyToString(new InputStreamReader(resource.getInputStream())); assertEquals("testString", content); assertEquals(resource, new ByteArrayResource("testString".getBytes())); }
@Test public void testLoadArrayOfInteger() { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResources(new ByteArrayResource("foo:\n- 1\n- 2".getBytes())); Properties properties = factory.getObject(); assertThat(properties.getProperty("foo[0]"), equalTo("1")); assertThat(properties.getProperty("foo[1]"), equalTo("2")); assertThat(properties.get("foo"), is(nullValue())); }
@Test public void testLoadArrayOfString() { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResources(new ByteArrayResource("foo:\n- bar\n- baz".getBytes())); Properties properties = factory.getObject(); assertThat(properties.getProperty("foo[0]"), equalTo("bar")); assertThat(properties.getProperty("foo[1]"), equalTo("baz")); assertThat(properties.get("foo"), is(nullValue())); }
@Test public void testLoadArrayOfObject() { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResources(new ByteArrayResource( "foo:\n- bar:\n spam: crap\n- baz\n- one: two\n three: four".getBytes() )); Properties properties = factory.getObject(); assertThat(properties.getProperty("foo[0].bar.spam"), equalTo("crap")); assertThat(properties.getProperty("foo[1]"), equalTo("baz")); assertThat(properties.getProperty("foo[2].one"), equalTo("two")); assertThat(properties.getProperty("foo[2].three"), equalTo("four")); assertThat(properties.get("foo"), is(nullValue())); }
@Test public void testLoadResourceWithSelectedDocuments() { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResources(new ByteArrayResource( "foo: bar\nspam: baz\n---\nfoo: bag\nspam: bad".getBytes())); factory.setDocumentMatchers(properties -> ("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")); }
@Test public void testLoadNull() { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResources(new ByteArrayResource("foo: bar\nspam:".getBytes())); Properties properties = factory.getObject(); assertThat(properties.getProperty("foo"), equalTo("bar")); assertThat(properties.getProperty("spam"), equalTo("")); }
@Test public void testLoadResourceWithMultipleDocuments() { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResources(new ByteArrayResource( "foo: bar\nspam: baz\n---\nfoo: bag".getBytes())); Properties properties = factory.getObject(); assertThat(properties.getProperty("foo"), equalTo("bag")); assertThat(properties.getProperty("spam"), equalTo("baz")); }
@Test public void testByteArrayResourceWithDescription() throws IOException { Resource resource = new ByteArrayResource("testString".getBytes(), "my description"); assertTrue(resource.exists()); assertFalse(resource.isOpen()); String content = FileCopyUtils.copyToString(new InputStreamReader(resource.getInputStream())); assertEquals("testString", content); assertTrue(resource.getDescription().contains("my description")); assertEquals(resource, new ByteArrayResource("testString".getBytes())); }
@Test public void testStringResource() { this.processor.setResources(new ByteArrayResource("foo # a document that is a literal".getBytes())); this.processor.process((properties, map) -> assertEquals("foo", map.get("document"))); }
@Test public void testLoadResource() { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResources(new ByteArrayResource("foo: bar\nspam:\n foo: baz".getBytes())); Properties properties = factory.getObject(); assertThat(properties.getProperty("foo"), equalTo("bar")); assertThat(properties.getProperty("spam.foo"), equalTo("baz")); }
@Test public void testLoadEmptyArrayValue() { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResources(new ByteArrayResource("a: alpha\ntest: []".getBytes())); Properties properties = factory.getObject(); assertThat(properties.getProperty("a"), equalTo("alpha")); assertThat(properties.getProperty("test"), equalTo("")); }
@Test public void testGetObject() { this.factory.setResources(new ByteArrayResource("foo: bar".getBytes())); assertEquals(1, this.factory.getObject().size()); }