Search in sources :

Example 1 with VirtualDirContext

use of org.apache.naming.resources.VirtualDirContext in project tomcat70 by apache.

the class TestVirtualContext method testVirtualClassLoader.

@Test
public void testVirtualClassLoader() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    File appDir = new File("test/webapp-3.0-virtual-webapp/src/main/webapp");
    // app dir is relative to server home
    StandardContext ctx = (StandardContext) tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
    VirtualWebappLoader loader = new VirtualWebappLoader(ctx.getParentClassLoader());
    // 
    loader.setVirtualClasspath(// 
    "test/webapp-3.0-virtual-webapp/target/classes;" + // 
    "test/webapp-3.0-virtual-library/target/classes;" + // 
    "test/webapp-3.0-virtual-webapp/src/main/webapp/WEB-INF/classes;" + "test/webapp-3.0-virtual-webapp/src/main/webapp2/WEB-INF/classes");
    ctx.setLoader(loader);
    String extraMappings = // 
    "/=test/webapp-3.0-virtual-webapp/src/main/webapp2" + // 
    ",/other=test/webapp-3.0-virtual-webapp/src/main/misc" + ",/WEB-INF/classes=test/webapp-3.0-virtual-webapp/target/classes";
    VirtualDirContext resources = new VirtualDirContext();
    resources.setExtraResourcePaths(extraMappings);
    ctx.setResources(resources);
    StandardJarScanner jarScanner = new StandardJarScanner();
    jarScanner.setScanAllDirectories(true);
    ctx.setJarScanner(jarScanner);
    tomcat.start();
    assertPageContains("/test/classpathGetResourceAsStream.jsp?path=nonexistent", "resourceAInWebInfClasses=true", 404);
    assertPageContains("/test/classpathGetResourceAsStream.jsp?path=rsrc/resourceA.properties", "resourceAInWebInfClasses=true");
    assertPageContains("/test/classpathGetResourceUrlThenGetStream.jsp?path=rsrc/resourceA.properties", "resourceAInWebInfClasses=true");
    assertPageContains("/test/classpathGetResourceAsStream.jsp?path=rsrc/resourceB.properties", "resourceBInTargetClasses=true");
    assertPageContains("/test/classpathGetResourceUrlThenGetStream.jsp?path=rsrc/resourceB.properties", "resourceBInTargetClasses=true");
    assertPageContains("/test/classpathGetResourceAsStream.jsp?path=rsrc/resourceC.properties", "resourceCInDependentLibraryTargetClasses=true");
    assertPageContains("/test/classpathGetResourceUrlThenGetStream.jsp?path=rsrc/resourceC.properties", "resourceCInDependentLibraryTargetClasses=true");
    assertPageContains("/test/classpathGetResourceAsStream.jsp?path=rsrc/resourceD.properties", "resourceDInPackagedJarInWebInfLib=true");
    assertPageContains("/test/classpathGetResourceUrlThenGetStream.jsp?path=rsrc/resourceD.properties", "resourceDInPackagedJarInWebInfLib=true");
    assertPageContains("/test/classpathGetResourceAsStream.jsp?path=rsrc/resourceG.properties", "resourceGInWebInfClasses=true");
    assertPageContains("/test/classpathGetResourceUrlThenGetStream.jsp?path=rsrc/resourceG.properties", "resourceGInWebInfClasses=true");
    // test listing all possible paths for a classpath resource
    String allUrls = getUrl("http://localhost:" + getPort() + "/test/classpathGetResources.jsp?path=rsrc/").toString();
    Assert.assertTrue(allUrls, allUrls.indexOf("/test/webapp-3.0-virtual-webapp/src/main/webapp/WEB-INF/classes/rsrc") > 0);
    Assert.assertTrue(allUrls, allUrls.indexOf("/test/webapp-3.0-virtual-webapp/src/main/webapp2/WEB-INF/classes/rsrc") > 0);
    Assert.assertTrue(allUrls, allUrls.indexOf("/test/webapp-3.0-virtual-webapp/src/main/webapp/WEB-INF/lib/rsrc.jar!/rsrc") > 0);
    Assert.assertTrue(allUrls, allUrls.indexOf("/test/webapp-3.0-virtual-webapp/target/classes/rsrc") > 0);
    Assert.assertTrue(allUrls, allUrls.indexOf("/test/webapp-3.0-virtual-library/target/classes/rsrc") > 0);
    // check that there's no duplicate in the URLs
    String[] allUrlsArray = allUrls.split("\\s+");
    Assert.assertEquals(new HashSet<String>(Arrays.asList(allUrlsArray)).size(), allUrlsArray.length);
    String allRsrsc2ClasspathUrls = getUrl("http://localhost:" + getPort() + "/test/classpathGetResources.jsp?path=rsrc2/").toString();
    Assert.assertTrue(allRsrsc2ClasspathUrls, allRsrsc2ClasspathUrls.indexOf("/test/webapp-3.0-virtual-webapp/src/main/webapp2/WEB-INF/classes/rsrc2") > 0);
    // tests context.getRealPath
    // the following fails because getRealPath always return a non-null path
    // even if there's no such resource
    // assertPageContains("/test/contextGetRealPath.jsp?path=nonexistent",
    // "resourceAInWebInfClasses=true", 404);
    // Real paths depend on the OS and this test has to work on all
    // platforms so use File to convert the path to a platform specific form
    File f = new File("test/webapp-3.0-virtual-webapp/src/main/webapp/rsrc/resourceF.properties");
    assertPageContains("/test/contextGetRealPath.jsp?path=/rsrc/resourceF.properties", f.getPath());
    // tests context.getResource then the content
    assertPageContains("/test/contextGetResource.jsp?path=nonexistent", "resourceAInWebInfClasses=true", 404);
    assertPageContains("/test/contextGetResource.jsp?path=/WEB-INF/classes/rsrc/resourceA.properties", "resourceAInWebInfClasses=true");
    assertPageContains("/test/contextGetResource.jsp?path=/WEB-INF/classes/rsrc/resourceG.properties", "resourceGInWebInfClasses=true");
    assertPageContains("/test/contextGetResource.jsp?path=/rsrc/resourceE.properties", "resourceEInDependentLibraryTargetClasses=true");
    assertPageContains("/test/contextGetResource.jsp?path=/other/resourceI.properties", "resourceIInWebapp=true");
    assertPageContains("/test/contextGetResource.jsp?path=/rsrc2/resourceJ.properties", "resourceJInWebapp=true");
    String allRsrcPaths = getUrl("http://localhost:" + getPort() + "/test/contextGetResourcePaths.jsp?path=/rsrc/").toString();
    Assert.assertTrue(allRsrcPaths, allRsrcPaths.indexOf("/rsrc/resourceF.properties") > 0);
    Assert.assertTrue(allRsrcPaths, allRsrcPaths.indexOf("/rsrc/resourceE.properties") > 0);
    Assert.assertTrue(allRsrcPaths, allRsrcPaths.indexOf("/rsrc/resourceH.properties") > 0);
    // check that there's no duplicate in the URLs
    String[] allRsrcPathsArray = allRsrcPaths.split("\\s+");
    Assert.assertEquals(new HashSet<String>(Arrays.asList(allRsrcPathsArray)).size(), allRsrcPathsArray.length);
    String allRsrc2Paths = getUrl("http://localhost:" + getPort() + "/test/contextGetResourcePaths.jsp?path=/rsrc2/").toString();
    Assert.assertTrue(allRsrc2Paths, allRsrc2Paths.indexOf("/rsrc2/resourceJ.properties") > 0);
    assertPageContains("/test/testTlds.jsp", "worldA");
    assertPageContains("/test/testTlds.jsp", "worldB");
    assertPageContains("/test/testTlds.jsp", "worldC");
    assertPageContains("/test/testTlds.jsp", "worldD");
}
Also used : Tomcat(org.apache.catalina.startup.Tomcat) VirtualDirContext(org.apache.naming.resources.VirtualDirContext) StandardContext(org.apache.catalina.core.StandardContext) StandardJarScanner(org.apache.tomcat.util.scan.StandardJarScanner) File(java.io.File) HashSet(java.util.HashSet) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 2 with VirtualDirContext

use of org.apache.naming.resources.VirtualDirContext in project tomcat70 by apache.

the class TestVirtualContext method testAdditionalWebInfClassesPaths.

@Test
public void testAdditionalWebInfClassesPaths() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    File appDir = new File("test/webapp-3.0-virtual-webapp/src/main/webapp");
    // app dir is relative to server home
    StandardContext ctx = (StandardContext) tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
    File tempFile = File.createTempFile("virtualWebInfClasses", null);
    File additionWebInfClasses = new File(tempFile.getAbsolutePath() + ".dir");
    Assert.assertTrue(additionWebInfClasses.mkdirs());
    File targetPackageForAnnotatedClass = new File(additionWebInfClasses, MyAnnotatedServlet.class.getPackage().getName().replace('.', '/'));
    Assert.assertTrue(targetPackageForAnnotatedClass.mkdirs());
    InputStream annotatedServletClassInputStream = null;
    FileOutputStream annotatedServletClassOutputStream = null;
    try {
        annotatedServletClassInputStream = this.getClass().getResourceAsStream(MyAnnotatedServlet.class.getSimpleName() + ".class");
        annotatedServletClassOutputStream = new FileOutputStream(new File(targetPackageForAnnotatedClass, MyAnnotatedServlet.class.getSimpleName() + ".class"));
        IOUtils.copy(annotatedServletClassInputStream, annotatedServletClassOutputStream);
    } finally {
        if (annotatedServletClassInputStream != null) {
            try {
                annotatedServletClassInputStream.close();
            } catch (IOException e) {
            }
        }
        if (annotatedServletClassOutputStream != null) {
            try {
                annotatedServletClassOutputStream.close();
            } catch (IOException e) {
            }
        }
    }
    VirtualWebappLoader loader = new VirtualWebappLoader(ctx.getParentClassLoader());
    loader.setVirtualClasspath(// 
    "test/webapp-3.0-virtual-webapp/target/classes;" + // 
    "test/webapp-3.0-virtual-library/target/classes;" + additionWebInfClasses.getAbsolutePath());
    ctx.setLoader(loader);
    tomcat.start();
    // first test that without the setting on StandardContext the annotated
    // servlet is not detected
    assertPageContains("/test/annotatedServlet", MyAnnotatedServlet.MESSAGE, 404);
    tomcat.stop();
    // then test that if we configure StandardContext with the additional
    // path, the servlet is detected
    // ctx.setAdditionalVirtualWebInfClasses(additionWebInfClasses.getAbsolutePath());
    VirtualDirContext resources = new VirtualDirContext();
    resources.setExtraResourcePaths("/WEB-INF/classes=" + additionWebInfClasses);
    ctx.setResources(resources);
    tomcat.start();
    assertPageContains("/test/annotatedServlet", MyAnnotatedServlet.MESSAGE);
    tomcat.stop();
    FileUtils.deleteDirectory(additionWebInfClasses);
    tempFile.delete();
}
Also used : Tomcat(org.apache.catalina.startup.Tomcat) VirtualDirContext(org.apache.naming.resources.VirtualDirContext) InputStream(java.io.InputStream) StandardContext(org.apache.catalina.core.StandardContext) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Aggregations

File (java.io.File)2 StandardContext (org.apache.catalina.core.StandardContext)2 Tomcat (org.apache.catalina.startup.Tomcat)2 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)2 VirtualDirContext (org.apache.naming.resources.VirtualDirContext)2 Test (org.junit.Test)2 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HashSet (java.util.HashSet)1 StandardJarScanner (org.apache.tomcat.util.scan.StandardJarScanner)1