use of org.springframework.core.io.ClassPathResource in project spring-framework by spring-projects.
the class GzipResourceResolverTests method resolveGzippedFile.
@Test
public void resolveGzippedFile() throws IOException {
MockServerWebExchange exchange = MockServerHttpRequest.get("").header("Accept-Encoding", "gzip").toExchange();
String file = "js/foo.js";
Resource resolved = this.resolver.resolveResource(exchange, file, this.locations).block(TIMEOUT);
String gzFile = file + ".gz";
Resource resource = new ClassPathResource("test/" + gzFile, getClass());
assertEquals(resource.getDescription(), resolved.getDescription());
assertEquals(new ClassPathResource("test/" + file).getFilename(), resolved.getFilename());
assertTrue("Expected " + resolved + " to be of type " + HttpResource.class, resolved instanceof HttpResource);
}
use of org.springframework.core.io.ClassPathResource in project spring-framework by spring-projects.
the class ResourceWebHandlerTests method testInvalidPath.
private void testInvalidPath(HttpMethod httpMethod) throws Exception {
Resource location = new ClassPathResource("test/", getClass());
this.handler.setLocations(Collections.singletonList(location));
testInvalidPath(httpMethod, "../testsecret/secret.txt", location);
testInvalidPath(httpMethod, "test/../../testsecret/secret.txt", location);
testInvalidPath(httpMethod, ":/../../testsecret/secret.txt", location);
location = new UrlResource(getClass().getResource("./test/"));
this.handler.setLocations(Collections.singletonList(location));
Resource secretResource = new UrlResource(getClass().getResource("testsecret/secret.txt"));
String secretPath = secretResource.getURL().getPath();
testInvalidPath(httpMethod, "file:" + secretPath, location);
testInvalidPath(httpMethod, "/file:" + secretPath, location);
testInvalidPath(httpMethod, "url:" + secretPath, location);
testInvalidPath(httpMethod, "/url:" + secretPath, location);
testInvalidPath(httpMethod, "////../.." + secretPath, location);
testInvalidPath(httpMethod, "/%2E%2E/testsecret/secret.txt", location);
testInvalidPath(httpMethod, "url:" + secretPath, location);
// The following tests fail with a MalformedURLException on Windows
// testInvalidPath(location, "/" + secretPath);
// testInvalidPath(location, "/ " + secretPath);
}
use of org.springframework.core.io.ClassPathResource in project spring-framework by spring-projects.
the class CastorUnmarshallerTests method setBothTargetClassesAndMapping.
@Test
public void setBothTargetClassesAndMapping() throws IOException {
CastorMarshaller unmarshaller = new CastorMarshaller();
unmarshaller.setMappingLocation(new ClassPathResource("order-mapping.xml", CastorMarshaller.class));
unmarshaller.setTargetClasses(new Class[] { Order.class });
unmarshaller.afterPropertiesSet();
String xml = "<order>" + "<order-item id=\"1\" quantity=\"15\"/>" + "<order-item id=\"3\" quantity=\"20\"/>" + "</order>";
Order order = (Order) unmarshaller.unmarshal(new StreamSource(new StringReader(xml)));
assertEquals("Invalid amount of items", 2, order.getOrderItemCount());
OrderItem item = order.getOrderItem(0);
assertEquals("Invalid items", "1", item.getId());
assertThat("Invalid items", item.getQuantity(), equalTo(15));
item = order.getOrderItem(1);
assertEquals("Invalid items", "3", item.getId());
assertThat("Invalid items", item.getQuantity(), equalTo(20));
}
use of org.springframework.core.io.ClassPathResource in project spring-boot by spring-projects.
the class HazelcastAutoConfigurationTests method explicitConfigFile.
@Test
public void explicitConfigFile() throws IOException {
load("spring.hazelcast.config=org/springframework/boot/autoconfigure/hazelcast/" + "hazelcast-specific.xml");
HazelcastInstance hazelcastInstance = this.context.getBean(HazelcastInstance.class);
assertThat(hazelcastInstance.getConfig().getConfigurationFile()).isEqualTo(new ClassPathResource("org/springframework/boot/autoconfigure/hazelcast" + "/hazelcast-specific.xml").getFile());
}
use of org.springframework.core.io.ClassPathResource in project spring-boot by spring-projects.
the class WebFluxAnnotationAutoConfigurationTests method shouldRegisterResourceHandlerMapping.
@Test
public void shouldRegisterResourceHandlerMapping() throws Exception {
load();
SimpleUrlHandlerMapping hm = this.context.getBean("resourceHandlerMapping", SimpleUrlHandlerMapping.class);
assertThat(hm.getUrlMap().get("/**")).isInstanceOf(ResourceWebHandler.class);
ResourceWebHandler staticHandler = (ResourceWebHandler) hm.getUrlMap().get("/**");
assertThat(staticHandler.getLocations()).hasSize(5);
assertThat(hm.getUrlMap().get("/webjars/**")).isInstanceOf(ResourceWebHandler.class);
ResourceWebHandler webjarsHandler = (ResourceWebHandler) hm.getUrlMap().get("/webjars/**");
assertThat(webjarsHandler.getLocations()).hasSize(1);
assertThat(webjarsHandler.getLocations().get(0)).isEqualTo(new ClassPathResource("/META-INF/resources/webjars/"));
}
Aggregations