@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
@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 mapConvertedToIndexedBeanReference() { this.processor.setResources(new ByteArrayResource("foo: bar\nbar:\n spam: bucket".getBytes())); this.processor.process((properties, map) -> { assertEquals("bucket", properties.get("bar.spam")); assertEquals(2, properties.size()); }); }
@Test public void integerKeyBehaves() { this.processor.setResources(new ByteArrayResource("foo: bar\n1: bar".getBytes())); this.processor.process((properties, map) -> { assertEquals("bar", properties.get("[1]")); assertEquals(2, properties.size()); }); }
@Test public void withResourceHavingNoDescription() throws IOException { PropertySource<?> ps = new ResourcePropertySource(new ByteArrayResource("foo=bar".getBytes(), "")); assertEquals("bar", ps.getProperty("foo")); assertEquals("Byte array resource []", ps.getName()); }
@Test(expected = DuplicateKeyException.class) public void testLoadResourcesWithInternalOverride() { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResources(new ByteArrayResource( "foo: bar\nspam:\n foo: baz\nfoo: bucket".getBytes())); factory.getObject(); }
@Test public void testBadDocumentStart() { this.processor.setResources(new ByteArrayResource("foo # a document\nbar: baz".getBytes())); this.exception.expect(ParserException.class); this.exception.expectMessage("line 2, column 1"); this.processor.process((properties, map) -> {}); }
@Test public void testBadResource() { this.processor.setResources(new ByteArrayResource("foo: bar\ncd\nspam:\n foo: baz".getBytes())); this.exception.expect(ScannerException.class); this.exception.expectMessage("line 3, column 1"); this.processor.process((properties, map) -> {}); }
@Test public void testBadResource() { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResources(new ByteArrayResource( "foo: bar\ncd\nspam:\n foo: baz".getBytes())); this.exception.expect(ScannerException.class); this.exception.expectMessage("line 3, column 1"); factory.getObject(); }
@Override public void encode() { Flux<Resource> input = Flux.just(new ByteArrayResource(this.bytes)); testEncodeAll(input, Resource.class, step -> step .consumeNextWith(expectBytes(this.bytes)) .verifyComplete()); }
@Test public void readByteArrayResourcePositionAndTakeUntil() throws Exception { Resource resource = new ByteArrayResource("foobarbazqux" .getBytes()); Flux<DataBuffer> flux = DataBufferUtils.read(resource, 3, this.bufferFactory, 3); flux = DataBufferUtils.takeUntilByteCount(flux, 5); StepVerifier.create(flux) .consumeNextWith(stringConsumer("bar")) .consumeNextWith(stringConsumer("ba")) .expectComplete() .verify(Duration.ofSeconds(5)); }
@Test public void toResourceRegion() { byte[] bytes = "Spring Framework".getBytes(StandardCharsets.UTF_8); ByteArrayResource resource = new ByteArrayResource(bytes); HttpRange range = HttpRange.createByteRange(0, 5); ResourceRegion region = range.toResourceRegion(resource); assertEquals(resource, region.getResource()); assertEquals(0L, region.getPosition()); assertEquals(6L, region.getCount()); }
@Test public void shouldHandleResource() throws Exception { ResponseEntity<Resource> returnValue = ResponseEntity .ok(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, APPLICATION_OCTET_STREAM)).willReturn(true); processor.handleReturnValue(returnValue, returnTypeResponseEntityResource, mavContainer, webRequest); then(resourceMessageConverter).should(times(1)).write( any(ByteArrayResource.class), eq(APPLICATION_OCTET_STREAM), any(HttpOutputMessage.class)); assertEquals(200, servletResponse.getStatus()); }
@Test public void lastModifiedWorksWithResourceThatDoesNotSupportFileBasedAccessAtAll() throws Exception { Resource resource = new ByteArrayResource(new byte[0]); ResourceScriptSource scriptSource = new ResourceScriptSource(resource); assertTrue("ResourceScriptSource must start off in the 'isModified' state (it obviously isn't).", scriptSource.isModified()); scriptSource.getScriptAsString(); assertFalse("ResourceScriptSource must not report back as being modified if the underlying File resource is not reporting a changed lastModified time.", scriptSource.isModified()); // Must now continue to report back as not having been modified 'cos the Resource does not support access as a File (and so the lastModified date cannot be determined). assertFalse("ResourceScriptSource must not report back as being modified if the underlying File resource is not reporting a changed lastModified time.", scriptSource.isModified()); }
@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()); }
@Test public void handleReturnTypeResourceIllegalByteRange() throws Exception { ResponseEntity<Resource> returnValue = ResponseEntity .ok(new ByteArrayResource("Content".getBytes(StandardCharsets.UTF_8))); servletRequest.addHeader("Range", "illegal"); given(resourceRegionMessageConverter.canWrite(any(), eq(null))).willReturn(true); given(resourceRegionMessageConverter.canWrite(any(), eq(APPLICATION_OCTET_STREAM))).willReturn(true); processor.handleReturnValue(returnValue, returnTypeResponseEntityResource, mavContainer, webRequest); then(resourceRegionMessageConverter).should(never()).write( anyCollection(), eq(APPLICATION_OCTET_STREAM), any(HttpOutputMessage.class)); assertEquals(416, servletResponse.getStatus()); }
@Test //SPR-16921 public void disableRangeSupportIfContentRangePresent() throws Exception { ResponseEntity<Resource> returnValue = ResponseEntity .status(HttpStatus.PARTIAL_CONTENT) .header(HttpHeaders.RANGE, "bytes=0-5") .body(new ByteArrayResource("Content".getBytes(StandardCharsets.UTF_8))); given(resourceRegionMessageConverter.canWrite(any(), eq(null))).willReturn(true); given(resourceRegionMessageConverter.canWrite(any(), eq(APPLICATION_OCTET_STREAM))).willReturn(true); processor.handleReturnValue(returnValue, returnTypeResponseEntityResource, mavContainer, webRequest); then(resourceRegionMessageConverter).should(never()).write(anyCollection(), any(), any()); assertEquals(206, servletResponse.getStatus()); }