Search in sources :

Example 71 with UrlResource

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

the class XmlBeanFactoryTests method urlResourceWithImport.

@Test
void urlResourceWithImport() {
    URL url = getClass().getResource(RESOURCE_CONTEXT.getPath());
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(new UrlResource(url));
    // comes from "resourceImport.xml"
    xbf.getBean("resource1", ResourceTestBean.class);
    // comes from "resource.xml"
    xbf.getBean("resource2", ResourceTestBean.class);
}
Also used : UrlResource(org.springframework.core.io.UrlResource) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) URL(java.net.URL) Test(org.junit.jupiter.api.Test)

Example 72 with UrlResource

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

the class ResourceWebHandlerTests method testResolvePathWithTraversal.

private void testResolvePathWithTraversal(HttpMethod httpMethod) throws Exception {
    Resource location = new ClassPathResource("test/", getClass());
    this.handler.setLocations(Collections.singletonList(location));
    testResolvePathWithTraversal(httpMethod, "../testsecret/secret.txt", location);
    testResolvePathWithTraversal(httpMethod, "test/../../testsecret/secret.txt", location);
    testResolvePathWithTraversal(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();
    testResolvePathWithTraversal(httpMethod, "file:" + secretPath, location);
    testResolvePathWithTraversal(httpMethod, "/file:" + secretPath, location);
    testResolvePathWithTraversal(httpMethod, "url:" + secretPath, location);
    testResolvePathWithTraversal(httpMethod, "/url:" + secretPath, location);
    testResolvePathWithTraversal(httpMethod, "////../.." + secretPath, location);
    testResolvePathWithTraversal(httpMethod, "/%2E%2E/testsecret/secret.txt", location);
    testResolvePathWithTraversal(httpMethod, "%2F%2F%2E%2E%2F%2Ftestsecret/secret.txt", location);
    testResolvePathWithTraversal(httpMethod, "url:" + secretPath, location);
// The following tests fail with a MalformedURLException on Windows
// testResolvePathWithTraversal(location, "/" + secretPath);
// testResolvePathWithTraversal(location, "/  " + secretPath);
}
Also used : UrlResource(org.springframework.core.io.UrlResource) UrlResource(org.springframework.core.io.UrlResource) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) FileSystemResource(org.springframework.core.io.FileSystemResource) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 73 with UrlResource

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

the class PathResourceResolverTests method checkResource.

@Test
public void checkResource() throws IOException {
    Resource location = new ClassPathResource("test/", PathResourceResolver.class);
    testCheckResource(location, "../testsecret/secret.txt");
    testCheckResource(location, "test/../../testsecret/secret.txt");
    location = new UrlResource(getClass().getResource("./test/"));
    String secretPath = new UrlResource(getClass().getResource("testsecret/secret.txt")).getURL().getPath();
    testCheckResource(location, "file:" + secretPath);
    testCheckResource(location, "/file:" + secretPath);
    testCheckResource(location, "/" + secretPath);
    testCheckResource(location, "////../.." + secretPath);
    testCheckResource(location, "/%2E%2E/testsecret/secret.txt");
    testCheckResource(location, "/%2e%2e/testsecret/secret.txt");
    testCheckResource(location, " " + secretPath);
    testCheckResource(location, "/  " + secretPath);
    testCheckResource(location, "url:" + secretPath);
}
Also used : UrlResource(org.springframework.core.io.UrlResource) FileUrlResource(org.springframework.core.io.FileUrlResource) UrlResource(org.springframework.core.io.UrlResource) ClassPathResource(org.springframework.core.io.ClassPathResource) FileUrlResource(org.springframework.core.io.FileUrlResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Example 74 with UrlResource

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

the class PersistenceXmlParsingTests method testPersistenceUnitRootUrlWithJar.

@Test
public void testPersistenceUnitRootUrlWithJar() throws Exception {
    ClassPathResource archive = new ClassPathResource("/org/springframework/orm/jpa/jpa-archive.jar");
    String newRoot = "jar:" + archive.getURL().toExternalForm() + "!/META-INF/persist.xml";
    Resource insideArchive = new UrlResource(newRoot);
    // make sure the location actually exists
    assertThat(insideArchive.exists()).isTrue();
    URL url = PersistenceUnitReader.determinePersistenceUnitRootUrl(insideArchive);
    assertThat(archive.getURL().sameFile(url)).as("the archive location should have been returned").isTrue();
}
Also used : UrlResource(org.springframework.core.io.UrlResource) UrlResource(org.springframework.core.io.UrlResource) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) URL(java.net.URL) Test(org.junit.jupiter.api.Test)

Example 75 with UrlResource

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

the class CachingMetadataReaderLeakTests method significantLoad.

@Test
void significantLoad() throws Exception {
    // the biggest public class in the JDK (>60k)
    URL url = getClass().getResource("/java/awt/Component.class");
    assertThat(url).isNotNull();
    // look at a LOT of items
    for (int i = 0; i < ITEMS_TO_LOAD; i++) {
        Resource resource = new UrlResource(url) {

            @Override
            public boolean equals(Object obj) {
                return (obj == this);
            }

            @Override
            public int hashCode() {
                return System.identityHashCode(this);
            }
        };
        MetadataReader reader = mrf.getMetadataReader(resource);
        assertThat(reader).isNotNull();
    }
// useful for profiling to take snapshots
// System.in.read();
}
Also used : UrlResource(org.springframework.core.io.UrlResource) UrlResource(org.springframework.core.io.UrlResource) Resource(org.springframework.core.io.Resource) MetadataReader(org.springframework.core.type.classreading.MetadataReader) URL(java.net.URL) Test(org.junit.jupiter.api.Test)

Aggregations

UrlResource (org.springframework.core.io.UrlResource)100 Resource (org.springframework.core.io.Resource)41 URL (java.net.URL)33 Test (org.junit.jupiter.api.Test)30 Test (org.junit.Test)24 ClassPathResource (org.springframework.core.io.ClassPathResource)19 IOException (java.io.IOException)14 ArrayList (java.util.ArrayList)14 FileSystemResource (org.springframework.core.io.FileSystemResource)13 MalformedURLException (java.net.MalformedURLException)11 Requisition (org.opennms.netmgt.provision.persist.requisition.Requisition)10 lombok.val (lombok.val)9 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)9 GenericBean (org.springframework.beans.testfixture.beans.GenericBean)9 File (java.io.File)8 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)8 InputStream (java.io.InputStream)6 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)6 Properties (java.util.Properties)5 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)5