Search in sources :

Example 16 with WebResource

use of org.apache.catalina.WebResource in project tomcat by apache.

the class TestResourceJars method testNonStaticResources.

@Test
public void testNonStaticResources() {
    File empty = new File("test/webresources/dir3");
    File jar = new File("test/webresources/non-static-resources.jar");
    TesterWebResourceRoot root = new TesterWebResourceRoot();
    // Use empty dir for root of web app.
    WebResourceSet webResourceSet = new DirResourceSet(root, "/", empty.getAbsolutePath(), "/");
    root.setMainResources(webResourceSet);
    // If this JAR was in a web application, this is equivalent to how it
    // would be added
    JarResourceSet test = new JarResourceSet(root, "/", jar.getAbsolutePath(), "/META-INF/resources");
    test.setStaticOnly(true);
    root.addJarResources(test);
    WebResource resource = root.getClassLoaderResource("/org/apache/tomcat/unittest/foo.txt");
    Assert.assertFalse(resource.exists());
}
Also used : WebResourceSet(org.apache.catalina.WebResourceSet) WebResource(org.apache.catalina.WebResource) File(java.io.File) Test(org.junit.Test)

Example 17 with WebResource

use of org.apache.catalina.WebResource in project tomcat by apache.

the class AbstractTestResourceSet method testWriteDirB.

@Test
public final void testWriteDirB() {
    WebResource d1 = resourceRoot.getResource(getMount() + "/d1/");
    InputStream is = new ByteArrayInputStream("test".getBytes());
    if (d1.exists()) {
        Assert.assertFalse(resourceRoot.write(getMount() + "/d1/", is, false));
    } else if (d1.isVirtual()) {
        Assert.assertTrue(resourceRoot.write(getMount() + "/d1/", is, false));
        File file = new File(getBaseDir(), "d1");
        Assert.assertTrue(file.exists());
        Assert.assertTrue(file.delete());
    } else {
        Assert.fail("Unhandled condition in unit test");
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) WebResource(org.apache.catalina.WebResource) File(java.io.File) Test(org.junit.Test)

Example 18 with WebResource

use of org.apache.catalina.WebResource in project tomcat by apache.

the class AbstractTestResourceSet method testGetCanonicalPathDoesNotExist.

@Test
public final void testGetCanonicalPathDoesNotExist() {
    WebResource exists = resourceRoot.getResource(getMount() + "/d1/d1-f1.txt");
    WebResource doesNotExist = resourceRoot.getResource(getMount() + "/d1/dummy.txt");
    String doesNotExistCanonicalPath = doesNotExist.getCanonicalPath();
    URL existsUrl = exists.getURL();
    if ("file".equals(existsUrl.getProtocol())) {
        // Should be possible to construct a canonical path for a resource
        // that doesn't exist given that a resource that does exist in the
        // same directory has a URL with the file protocol
        Assert.assertNotNull(doesNotExistCanonicalPath);
    } else {
        Assert.assertNull(doesNotExistCanonicalPath);
    }
}
Also used : WebResource(org.apache.catalina.WebResource) URL(java.net.URL) Test(org.junit.Test)

Example 19 with WebResource

use of org.apache.catalina.WebResource in project tomcat by apache.

the class AbstractTestResourceSet method testGetResourceDirB.

@Test
public final void testGetResourceDirB() {
    WebResource webResource = resourceRoot.getResource(getMount() + "/d1/");
    Assert.assertTrue(webResource.isDirectory());
    Assert.assertEquals("d1", webResource.getName());
    Assert.assertEquals(getMount() + "/d1/", webResource.getWebappPath());
    Assert.assertEquals(-1, webResource.getContentLength());
    Assert.assertNull(webResource.getContent());
    Assert.assertNull(webResource.getInputStream());
}
Also used : WebResource(org.apache.catalina.WebResource) Test(org.junit.Test)

Example 20 with WebResource

use of org.apache.catalina.WebResource in project tomcat by apache.

the class AbstractTestResourceSet method testGetResourceFile.

@Test
public final void testGetResourceFile() {
    WebResource webResource = resourceRoot.getResource(getMount() + "/d1/d1-f1.txt");
    Assert.assertTrue(webResource.isFile());
    Assert.assertEquals("d1-f1.txt", webResource.getName());
    Assert.assertEquals(getMount() + "/d1/d1-f1.txt", webResource.getWebappPath());
    Assert.assertEquals(0, webResource.getContentLength());
    Assert.assertEquals(0, webResource.getContent().length);
    Assert.assertNotNull(webResource.getInputStream());
}
Also used : WebResource(org.apache.catalina.WebResource) Test(org.junit.Test)

Aggregations

WebResource (org.apache.catalina.WebResource)47 Test (org.junit.Test)14 InputStream (java.io.InputStream)10 File (java.io.File)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 IOException (java.io.IOException)7 URL (java.net.URL)7 BufferedInputStream (java.io.BufferedInputStream)5 FileInputStream (java.io.FileInputStream)5 InputSource (org.xml.sax.InputSource)5 PrintWriter (java.io.PrintWriter)3 RandomAccessFile (java.io.RandomAccessFile)3 Manifest (java.util.jar.Manifest)3 DocumentBuilder (javax.xml.parsers.DocumentBuilder)3 WebResourceSet (org.apache.catalina.WebResourceSet)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 StringWriter (java.io.StringWriter)2 Hashtable (java.util.Hashtable)2 LinkedHashSet (java.util.LinkedHashSet)2