Search in sources :

Example 1 with ServletContextResourcePatternLoader

use of cn.taketoday.web.context.support.ServletContextResourcePatternLoader in project today-infrastructure by TAKETODAY.

the class ServletContextSupportTests method testServletContextResourcePatternLoaderWithUnboundedPatternPath.

@Test
public void testServletContextResourcePatternLoaderWithUnboundedPatternPath() throws IOException {
    final Set<String> dirs = new HashSet<>();
    dirs.add("/WEB-INF/mydir1/");
    dirs.add("/WEB-INF/mydir2/");
    final Set<String> paths = new HashSet<>();
    paths.add("/WEB-INF/mydir2/context2.xml");
    paths.add("/WEB-INF/mydir2/mydir3/");
    MockServletContext sc = new MockServletContext("classpath:cn/taketoday/web/context") {

        @Override
        public Set<String> getResourcePaths(String path) {
            if ("/WEB-INF/".equals(path)) {
                return dirs;
            }
            if ("/WEB-INF/mydir1/".equals(path)) {
                return Collections.singleton("/WEB-INF/mydir1/context1.xml");
            }
            if ("/WEB-INF/mydir2/".equals(path)) {
                return paths;
            }
            if ("/WEB-INF/mydir2/mydir3/".equals(path)) {
                return Collections.singleton("/WEB-INF/mydir2/mydir3/context3.xml");
            }
            return null;
        }
    };
    ServletContextResourcePatternLoader rpr = new ServletContextResourcePatternLoader(sc);
    Resource[] found = rpr.getResourcesArray("/WEB-INF/**/*.xml");
    Set<String> foundPaths = new HashSet<>();
    for (Resource resource : found) {
        foundPaths.add(((ServletContextResource) resource).getPath());
    }
    assertThat(foundPaths.size()).isEqualTo(3);
    assertThat(foundPaths.contains("/WEB-INF/mydir1/context1.xml")).isTrue();
    assertThat(foundPaths.contains("/WEB-INF/mydir2/context2.xml")).isTrue();
    assertThat(foundPaths.contains("/WEB-INF/mydir2/mydir3/context3.xml")).isTrue();
}
Also used : ServletContextResource(cn.taketoday.web.context.support.ServletContextResource) Resource(cn.taketoday.core.io.Resource) ServletContextResourcePatternLoader(cn.taketoday.web.context.support.ServletContextResourcePatternLoader) MockServletContext(cn.taketoday.web.mock.MockServletContext) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 2 with ServletContextResourcePatternLoader

use of cn.taketoday.web.context.support.ServletContextResourcePatternLoader in project today-infrastructure by TAKETODAY.

the class ServletContextSupportTests method testServletContextResourcePatternLoader.

@Test
public void testServletContextResourcePatternLoader() throws IOException {
    final Set<String> paths = new HashSet<>();
    paths.add("/WEB-INF/context1.xml");
    paths.add("/WEB-INF/context2.xml");
    MockServletContext sc = new MockServletContext("classpath:cn/taketoday/web/context") {

        @Override
        public Set<String> getResourcePaths(String path) {
            if ("/WEB-INF/".equals(path)) {
                return paths;
            }
            return null;
        }
    };
    ServletContextResourcePatternLoader rpr = new ServletContextResourcePatternLoader(sc);
    Resource[] found = rpr.getResourcesArray("/WEB-INF/*.xml");
    Set<String> foundPaths = new HashSet<>();
    for (Resource resource : found) {
        foundPaths.add(((ServletContextResource) resource).getPath());
    }
    assertThat(foundPaths.size()).isEqualTo(2);
    assertThat(foundPaths.contains("/WEB-INF/context1.xml")).isTrue();
    assertThat(foundPaths.contains("/WEB-INF/context2.xml")).isTrue();
}
Also used : ServletContextResource(cn.taketoday.web.context.support.ServletContextResource) Resource(cn.taketoday.core.io.Resource) ServletContextResourcePatternLoader(cn.taketoday.web.context.support.ServletContextResourcePatternLoader) MockServletContext(cn.taketoday.web.mock.MockServletContext) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 3 with ServletContextResourcePatternLoader

use of cn.taketoday.web.context.support.ServletContextResourcePatternLoader in project today-infrastructure by TAKETODAY.

the class ServletContextSupportTests method testServletContextResourcePatternLoaderWithPatternPath.

@Test
public void testServletContextResourcePatternLoaderWithPatternPath() throws IOException {
    final Set<String> dirs = new HashSet<>();
    dirs.add("/WEB-INF/mydir1/");
    dirs.add("/WEB-INF/mydir2/");
    MockServletContext sc = new MockServletContext("classpath:cn/taketoday/web/context") {

        @Override
        public Set<String> getResourcePaths(String path) {
            if ("/WEB-INF/".equals(path)) {
                return dirs;
            }
            if ("/WEB-INF/mydir1/".equals(path)) {
                return Collections.singleton("/WEB-INF/mydir1/context1.xml");
            }
            if ("/WEB-INF/mydir2/".equals(path)) {
                return Collections.singleton("/WEB-INF/mydir2/context2.xml");
            }
            return null;
        }
    };
    ServletContextResourcePatternLoader rpr = new ServletContextResourcePatternLoader(sc);
    Resource[] found = rpr.getResourcesArray("/WEB-INF/*/*.xml");
    Set<String> foundPaths = new HashSet<>();
    for (Resource resource : found) {
        foundPaths.add(((ServletContextResource) resource).getPath());
    }
    assertThat(foundPaths.size()).isEqualTo(2);
    assertThat(foundPaths.contains("/WEB-INF/mydir1/context1.xml")).isTrue();
    assertThat(foundPaths.contains("/WEB-INF/mydir2/context2.xml")).isTrue();
}
Also used : ServletContextResource(cn.taketoday.web.context.support.ServletContextResource) Resource(cn.taketoday.core.io.Resource) ServletContextResourcePatternLoader(cn.taketoday.web.context.support.ServletContextResourcePatternLoader) MockServletContext(cn.taketoday.web.mock.MockServletContext) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 4 with ServletContextResourcePatternLoader

use of cn.taketoday.web.context.support.ServletContextResourcePatternLoader in project today-infrastructure by TAKETODAY.

the class ServletContextSupportTests method testServletContextResourcePatternLoaderWithAbsolutePaths.

@Test
public void testServletContextResourcePatternLoaderWithAbsolutePaths() throws IOException {
    final Set<String> paths = new HashSet<>();
    paths.add("C:/webroot/WEB-INF/context1.xml");
    paths.add("C:/webroot/WEB-INF/context2.xml");
    paths.add("C:/webroot/someOtherDirThatDoesntContainPath");
    MockServletContext sc = new MockServletContext("classpath:cn/taketoday/web/context") {

        @Override
        public Set<String> getResourcePaths(String path) {
            if ("/WEB-INF/".equals(path)) {
                return paths;
            }
            return null;
        }
    };
    ServletContextResourcePatternLoader rpr = new ServletContextResourcePatternLoader(sc);
    Resource[] found = rpr.getResourcesArray("/WEB-INF/*.xml");
    Set<String> foundPaths = new HashSet<>();
    for (Resource resource : found) {
        foundPaths.add(((ServletContextResource) resource).getPath());
    }
    assertThat(foundPaths.size()).isEqualTo(2);
    assertThat(foundPaths.contains("/WEB-INF/context1.xml")).isTrue();
    assertThat(foundPaths.contains("/WEB-INF/context2.xml")).isTrue();
}
Also used : ServletContextResource(cn.taketoday.web.context.support.ServletContextResource) Resource(cn.taketoday.core.io.Resource) ServletContextResourcePatternLoader(cn.taketoday.web.context.support.ServletContextResourcePatternLoader) MockServletContext(cn.taketoday.web.mock.MockServletContext) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 5 with ServletContextResourcePatternLoader

use of cn.taketoday.web.context.support.ServletContextResourcePatternLoader in project today-framework by TAKETODAY.

the class ServletContextSupportTests method testServletContextResourcePatternLoaderWithPatternPath.

@Test
public void testServletContextResourcePatternLoaderWithPatternPath() throws IOException {
    final Set<String> dirs = new HashSet<>();
    dirs.add("/WEB-INF/mydir1/");
    dirs.add("/WEB-INF/mydir2/");
    MockServletContext sc = new MockServletContext("classpath:cn/taketoday/web/context") {

        @Override
        public Set<String> getResourcePaths(String path) {
            if ("/WEB-INF/".equals(path)) {
                return dirs;
            }
            if ("/WEB-INF/mydir1/".equals(path)) {
                return Collections.singleton("/WEB-INF/mydir1/context1.xml");
            }
            if ("/WEB-INF/mydir2/".equals(path)) {
                return Collections.singleton("/WEB-INF/mydir2/context2.xml");
            }
            return null;
        }
    };
    ServletContextResourcePatternLoader rpr = new ServletContextResourcePatternLoader(sc);
    Resource[] found = rpr.getResourcesArray("/WEB-INF/*/*.xml");
    Set<String> foundPaths = new HashSet<>();
    for (Resource resource : found) {
        foundPaths.add(((ServletContextResource) resource).getPath());
    }
    assertThat(foundPaths.size()).isEqualTo(2);
    assertThat(foundPaths.contains("/WEB-INF/mydir1/context1.xml")).isTrue();
    assertThat(foundPaths.contains("/WEB-INF/mydir2/context2.xml")).isTrue();
}
Also used : ServletContextResource(cn.taketoday.web.context.support.ServletContextResource) Resource(cn.taketoday.core.io.Resource) ServletContextResourcePatternLoader(cn.taketoday.web.context.support.ServletContextResourcePatternLoader) MockServletContext(cn.taketoday.web.testfixture.servlet.MockServletContext) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

Resource (cn.taketoday.core.io.Resource)8 ServletContextResource (cn.taketoday.web.context.support.ServletContextResource)8 ServletContextResourcePatternLoader (cn.taketoday.web.context.support.ServletContextResourcePatternLoader)8 HashSet (java.util.HashSet)8 Test (org.junit.jupiter.api.Test)8 MockServletContext (cn.taketoday.web.mock.MockServletContext)4 MockServletContext (cn.taketoday.web.testfixture.servlet.MockServletContext)4