This commit is contained in:
2024-11-30 19:03:49 +08:00
commit 1e6763c160
3806 changed files with 737676 additions and 0 deletions

View File

@@ -0,0 +1,82 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.webresources;
import java.io.File;
import org.junit.Test;
import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.WebResourceSet;
public abstract class AbstractTestFileResourceSet extends AbstractTestResourceSet {
private final boolean readOnly;
protected AbstractTestFileResourceSet(boolean readOnly) {
this.readOnly = readOnly;
}
protected abstract File getDir2();
@Override
public WebResourceRoot getWebResourceRoot() {
TesterWebResourceRoot root = new TesterWebResourceRoot();
WebResourceSet webResourceSet = new DirResourceSet(root, "/", getBaseDir().getAbsolutePath(), "/");
webResourceSet.setReadOnly(readOnly);
root.setMainResources(webResourceSet);
WebResourceSet f1 = new FileResourceSet(root, "/f1.txt",
"test/webresources/dir1/f1.txt", "/");
f1.setReadOnly(readOnly);
root.addPreResources(f1);
WebResourceSet f2 = new FileResourceSet(root, "/f2.txt",
"test/webresources/dir1/f2.txt", "/");
f2.setReadOnly(readOnly);
root.addPreResources(f2);
WebResourceSet d1f1 = new FileResourceSet(root, "/d1/d1-f1.txt",
"test/webresources/dir1/d1/d1-f1.txt", "/");
d1f1.setReadOnly(readOnly);
root.addPreResources(d1f1);
WebResourceSet d2f1 = new FileResourceSet(root, "/d2/d2-f1.txt",
"test/webresources/dir1/d2/d2-f1.txt", "/");
d2f1.setReadOnly(readOnly);
root.addPreResources(d2f1);
return root;
}
@Override
protected boolean isWriteable() {
return !readOnly;
}
@Override
public File getBaseDir() {
return getDir2();
}
@Override
@Test
public void testNoArgConstructor() {
@SuppressWarnings("unused")
Object obj = new FileResourceSet();
}
}

View File

@@ -0,0 +1,552 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.webresources;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;
import java.util.jar.Manifest;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.WebResource;
import org.apache.catalina.WebResourceRoot;
public abstract class AbstractTestResourceSet {
protected WebResourceRoot resourceRoot;
protected abstract WebResourceRoot getWebResourceRoot();
protected abstract boolean isWriteable();
public String getMount() {
return "";
}
public abstract File getBaseDir();
@Before
public final void setup() throws LifecycleException {
resourceRoot = getWebResourceRoot();
resourceRoot.start();
}
@After
public final void teardown() throws LifecycleException {
resourceRoot.stop();
resourceRoot.destroy();
}
@Test(expected = IllegalArgumentException.class)
public final void testGetResourceEmpty() {
resourceRoot.getResource("");
}
//------------------------------------------------------------ getResource()
@Test
public final void testGetResourceRoot() {
doTestGetResourceRoot(true);
}
@Test
public final void testGetResourceRootNoSlash() {
doTestGetResourceRoot(false);
}
private void doTestGetResourceRoot(boolean slash) {
String mount = getMount();
if (!slash && mount.length() == 0) {
return;
}
mount = mount + (slash ? "/" : "");
WebResource webResource = resourceRoot.getResource(mount);
Assert.assertTrue(webResource.isDirectory());
String expected;
if (getMount().length() > 0) {
expected = getMount().substring(1);
} else {
expected = "";
}
Assert.assertEquals(expected, webResource.getName());
Assert.assertEquals(mount + (!slash ? "/" : ""), webResource.getWebappPath());
}
@Test
public final void testGetResourceDirA() {
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());
}
@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());
}
@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());
}
@Test
public final void testGetResourceFileWithTrailingSlash() {
WebResource webResource =
resourceRoot.getResource(getMount() + "/d1/d1-f1.txt/");
Assert.assertFalse(webResource.exists());
}
@Test
public final void testGetResourceCaseSensitive() {
WebResource webResource =
resourceRoot.getResource(getMount() + "/d1/d1-F1.txt");
Assert.assertFalse(webResource.exists());
}
@Test
public final void testGetResourceTraversal() {
WebResource webResource = null;
try {
webResource = resourceRoot.getResource(getMount() + "/../");
} catch (IllegalArgumentException iae) {
// Expected if mount point is zero length
Assert.assertTrue(getMount().length() == 0);
return;
}
Assert.assertFalse(webResource.exists());
}
//------------------------------------------------------------------- list()
@Test(expected = IllegalArgumentException.class)
public final void testListEmpty() {
resourceRoot.list("");
}
@Test
public final void testListRoot() {
doTestListRoot(true);
}
@Test
public final void testListRootNoSlash() {
doTestListRoot(false);
}
private void doTestListRoot(boolean slash) {
String mount = getMount();
if (!slash && mount.length() == 0) {
return;
}
String[] results = resourceRoot.list(mount + (slash ? "/" : ""));
Set<String> expected = new HashSet<>();
expected.add("d1");
expected.add("d2");
expected.add("f1.txt");
expected.add("f2.txt");
// Directories created by Subversion 1.6 and earlier clients
Set<String> optional = new HashSet<>();
optional.add(".svn");
// Files visible in some tests only
optional.add(getMount() + ".ignore-me.txt");
optional.add("META-INF");
for (String result : results) {
Assert.assertTrue(result,
expected.remove(result) || optional.remove(result));
}
Assert.assertEquals(0, expected.size());
}
@Test
public final void testListDirA() {
String[] results = resourceRoot.list(getMount() + "/d1");
Set<String> expected = new HashSet<>();
expected.add("d1-f1.txt");
// Directories created by Subversion 1.6 and earlier clients
Set<String> optional = new HashSet<>();
optional.add(".svn");
// Files visible in some tests only
optional.add(".ignore-me.txt");
for (String result : results) {
Assert.assertTrue(result,
expected.remove(result) || optional.remove(result));
}
Assert.assertEquals(0, expected.size());
}
@Test
public final void testListDirB() {
String[] results = resourceRoot.list(getMount() + "/d1/");
Set<String> expected = new HashSet<>();
expected.add("d1-f1.txt");
// Directories created by Subversion 1.6 and earlier clients
Set<String> optional = new HashSet<>();
optional.add(".svn");
// Files visible in some tests only
optional.add(".ignore-me.txt");
for (String result : results) {
Assert.assertTrue(result,
expected.remove(result) || optional.remove(result));
}
Assert.assertEquals(0, expected.size());
}
@Test
public final void testListFile() {
String[] results = resourceRoot.list(getMount() + "/d1/d1-f1.txt");
Assert.assertNotNull(results);
Assert.assertEquals(0, results.length);
}
//-------------------------------------------------------- listWebAppPaths()
@Test(expected = IllegalArgumentException.class)
public final void testListWebAppPathsEmpty() {
resourceRoot.listWebAppPaths("");
}
@Test
public final void testListWebAppPathsRoot() {
doTestListWebAppPathsRoot(true);
}
@Test
public final void testListWebAppPathsRootNoSlash() {
doTestListWebAppPathsRoot(false);
}
private void doTestListWebAppPathsRoot(boolean slash) {
String mount = getMount();
if (!slash && mount.length() == 0) {
return;
}
Set<String> results = resourceRoot.listWebAppPaths(mount + (slash ? "/" : ""));
Set<String> expected = new HashSet<>();
expected.add(getMount() + "/d1/");
expected.add(getMount() + "/d2/");
expected.add(getMount() + "/f1.txt");
expected.add(getMount() + "/f2.txt");
// Directories created by Subversion 1.6 and earlier clients
Set<String> optional = new HashSet<>();
optional.add(getMount() + "/.svn/");
// Files visible in some tests only
optional.add(getMount() + "/.ignore-me.txt");
// Files visible in some configurations only
optional.add(getMount() + "/META-INF/");
for (String result : results) {
Assert.assertTrue(result,
expected.remove(result) || optional.remove(result));
}
Assert.assertEquals(0, expected.size());
}
@Test
public final void testListWebAppPathsDirA() {
Set<String> results = resourceRoot.listWebAppPaths(getMount() + "/d1");
Set<String> expected = new HashSet<>();
expected.add(getMount() + "/d1/d1-f1.txt");
// Directories created by Subversion 1.6 and earlier clients
Set<String> optional = new HashSet<>();
optional.add(getMount() + "/d1/.svn/");
// Files visible in some tests only
optional.add(getMount() + "/d1/.ignore-me.txt");
for (String result : results) {
Assert.assertTrue(result,
expected.remove(result) || optional.remove(result));
}
Assert.assertEquals(0, expected.size());
}
@Test
public final void testListWebAppPathsDirB() {
Set<String> results = resourceRoot.listWebAppPaths(getMount() + "/d1/");
Set<String> expected = new HashSet<>();
expected.add(getMount() + "/d1/d1-f1.txt");
// Directories created by Subversion 1.6 and earlier clients
Set<String> optional = new HashSet<>();
optional.add(getMount() + "/d1/.svn/");
// Files visible in some tests only
optional.add(getMount() + "/d1/.ignore-me.txt");
for (String result : results) {
Assert.assertTrue(result,
expected.remove(result) || optional.remove(result));
}
Assert.assertEquals(0, expected.size());
}
@Test
public final void testListWebAppPathsFile() {
Set<String> results =
resourceRoot.listWebAppPaths(getMount() + "/d1/d1-f1.txt");
Assert.assertNull(results);
}
//------------------------------------------------------------------ mkdir()
@Test(expected = IllegalArgumentException.class)
public final void testMkdirEmpty() {
resourceRoot.mkdir("");
}
@Test
public final void testMkdirRoot() {
Assert.assertFalse(resourceRoot.mkdir(getMount() + "/"));
}
@Test
public final void testMkdirDirA() {
WebResource d1 = resourceRoot.getResource(getMount() + "/d1");
if (d1.exists()) {
Assert.assertFalse(resourceRoot.mkdir(getMount() + "/d1"));
} else if (d1.isVirtual()) {
Assert.assertTrue(resourceRoot.mkdir(getMount() + "/d1"));
File file = new File(getBaseDir(), "d1");
Assert.assertTrue(file.isDirectory());
Assert.assertTrue(file.delete());
} else {
Assert.fail("Unhandled condition in unit test");
}
}
@Test
public final void testMkdirDirB() {
WebResource d1 = resourceRoot.getResource(getMount() + "/d1/");
if (d1.exists()) {
Assert.assertFalse(resourceRoot.mkdir(getMount() + "/d1/"));
} else if (d1.isVirtual()) {
Assert.assertTrue(resourceRoot.mkdir(getMount() + "/d1/"));
File file = new File(getBaseDir(), "d1");
Assert.assertTrue(file.isDirectory());
Assert.assertTrue(file.delete());
} else {
Assert.fail("Unhandled condition in unit test");
}
}
@Test
public final void testMkdirFile() {
Assert.assertFalse(resourceRoot.mkdir(getMount() + "/d1/d1-f1.txt"));
}
@Test
public final void testMkdirNew() {
String newDirName = getNewDirName();
if (isWriteable()) {
Assert.assertTrue(resourceRoot.mkdir(getMount() + "/" + newDirName));
File file = new File(getBaseDir(), newDirName);
Assert.assertTrue(file.isDirectory());
Assert.assertTrue(file.delete());
} else {
Assert.assertFalse(resourceRoot.mkdir(getMount() + "/" + newDirName));
}
}
protected abstract String getNewDirName();
//------------------------------------------------------------------ write()
@Test(expected = IllegalArgumentException.class)
public final void testWriteEmpty() {
InputStream is = new ByteArrayInputStream("test".getBytes());
resourceRoot.write("", is, false);
}
@Test
public final void testWriteRoot() {
InputStream is = new ByteArrayInputStream("test".getBytes());
Assert.assertFalse(resourceRoot.write(getMount() + "/", is, false));
}
@Test
public final void testWriteDirA() {
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");
}
}
@Test
public final void testWriteDirB() {
WebResource d1 = resourceRoot.getResource(getMount() + "/d1/");
InputStream is = new ByteArrayInputStream("test".getBytes());
if (d1.exists() || d1.isVirtual()) {
Assert.assertFalse(resourceRoot.write(getMount() + "/d1/", is, false));
} else {
Assert.fail("Unhandled condition in unit test");
}
}
@Test
public final void testWriteFile() {
InputStream is = new ByteArrayInputStream("test".getBytes());
Assert.assertFalse(resourceRoot.write(
getMount() + "/d1/d1-f1.txt", is, false));
}
@Test(expected = NullPointerException.class)
public final void testWriteNull() {
resourceRoot.write(getMount() + "/" + getNewFileNameNull(), null, false);
}
protected abstract String getNewFileNameNull();
@Test
public final void testWrite() {
String newFileName = getNewFileName();
InputStream is = new ByteArrayInputStream("test".getBytes());
if (isWriteable()) {
Assert.assertTrue(resourceRoot.write(
getMount() + "/" + newFileName, is, false));
File file = new File(getBaseDir(), newFileName);
Assert.assertTrue(file.exists());
Assert.assertTrue(file.delete());
} else {
Assert.assertFalse(resourceRoot.write(
getMount() + "/" + newFileName, is, false));
}
}
@Test
public final void testWriteWithTrailingSlash() {
String newFileName = getNewFileName() + "/";
InputStream is = new ByteArrayInputStream("test".getBytes());
Assert.assertFalse(resourceRoot.write(
getMount() + "/" + newFileName, is, false));
}
protected abstract String getNewFileName();
// ------------------------------------------------------ getCanonicalPath()
@Test
public final void testGetCanonicalPathExists() {
WebResource exists =
resourceRoot.getResource(getMount() + "/d1/d1-f1.txt");
String existsCanonicalPath = exists.getCanonicalPath();
URL existsUrl = exists.getURL();
if ("file".equals(existsUrl.getProtocol())) {
// Should have a canonical path
Assert.assertNotNull(existsCanonicalPath);
} else {
Assert.assertNull(existsCanonicalPath);
}
}
@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);
}
}
// ----------------------------------------------------------- getManifest()
@Test
public final void testGetManifest() {
WebResource exists = resourceRoot.getResource(getMount() + "/d1/d1-f1.txt");
boolean manifestExists = resourceRoot.getResource("/META-INF/MANIFEST.MF").exists();
Manifest m = exists.getManifest();
if (getMount().equals("") && manifestExists) {
Assert.assertNotNull(m);
} else {
Assert.assertNull(m);
}
}
// ------------------------------------------------------------ constructors
public abstract void testNoArgConstructor();
}

View File

@@ -0,0 +1,75 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.webresources;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Set;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.WebResource;
public abstract class AbstractTestResourceSetMount
extends AbstractTestResourceSet {
@Override
public final String getMount() {
return "/mount";
}
@Test
public final void testGetResourceAbove() {
WebResource webResource = resourceRoot.getResource("/");
Assert.assertFalse(webResource.exists());
}
@Test
public final void testListAbove() {
String[] results = resourceRoot.list("/");
Assert.assertNotNull(results);
Assert.assertEquals(1, results.length);
Assert.assertEquals(getMount().substring(1), results[0]);
}
@Test
public final void testListWebAppPathsAbove() {
Set<String> results = resourceRoot.listWebAppPaths("/");
Assert.assertNotNull(results);
Assert.assertEquals(1, results.size());
Assert.assertTrue(results.contains(getMount() + "/"));
}
@Test
public void testMkdirAbove() {
Assert.assertFalse(resourceRoot.mkdir("/"));
}
@Test
public void testWriteAbove() {
InputStream is = new ByteArrayInputStream("test".getBytes());
Assert.assertFalse(resourceRoot.write("/", is, false));
}
@Override
public void testNoArgConstructor() {
// NO-OP
}
}

View File

@@ -0,0 +1,77 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.webresources;
import java.io.File;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.Context;
import org.apache.catalina.WebResource;
import org.apache.catalina.core.StandardHost;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.TomcatBaseTest;
public class TestAbstractArchiveResource extends TomcatBaseTest {
@Test
public void testNestedJarGetURL() throws Exception {
Tomcat tomcat = getTomcatInstance();
File docBase = new File("test/webresources/war-url-connection.war");
Context ctx = tomcat.addWebapp("/test", docBase.getAbsolutePath());
skipTldsForResourceJars(ctx);
((StandardHost) tomcat.getHost()).setUnpackWARs(false);
tomcat.start();
WebResource webResource =
ctx.getResources().getClassLoaderResource("/META-INF/resources/index.html");
StringBuilder expectedURL = new StringBuilder("jar:war:");
expectedURL.append(docBase.getAbsoluteFile().toURI().toURL().toString());
expectedURL.append("*/WEB-INF/lib/test.jar!/META-INF/resources/index.html");
Assert.assertEquals(expectedURL.toString(), webResource.getURL().toString());
}
@Test
public void testJarGetURL() throws Exception {
Tomcat tomcat = getTomcatInstance();
File docBase = new File("test/webapp");
Context ctx = tomcat.addWebapp("/test", docBase.getAbsolutePath());
skipTldsForResourceJars(ctx);
((StandardHost) tomcat.getHost()).setUnpackWARs(false);
tomcat.start();
WebResource webResource =
ctx.getResources().getClassLoaderResource("/META-INF/tags/echo.tag");
StringBuilder expectedURL = new StringBuilder("jar:");
expectedURL.append(docBase.getAbsoluteFile().toURI().toURL().toString());
expectedURL.append("WEB-INF/lib/test-lib.jar!/META-INF/tags/echo.tag");
Assert.assertEquals(expectedURL.toString(), webResource.getURL().toString());
}
}

View File

@@ -0,0 +1,90 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.webresources;
import java.util.regex.Pattern;
import org.junit.Test;
public class TestAbstractFileResourceSetPerformance {
private static final Pattern UNSAFE_WINDOWS_FILENAME_PATTERN = Pattern.compile(" $|[\"<>]");
private static final int LOOPS = 10_000_000;
/*
* Checking individual characters is about 10 times faster on markt's dev
* PC for typical length file names. The file names need to get to ~65
* characters before the Pattern matching is faster.
*/
@Test
public void testFileNameFiltering() {
long start = System.nanoTime();
for (int i = 0; i < LOOPS; i++) {
UNSAFE_WINDOWS_FILENAME_PATTERN.matcher("testfile.jsp ").find();
}
long end = System.nanoTime();
System.out.println("Regular expression took " + (end - start) + "ns or " +
(end-start)/LOOPS + "ns per iteration");
start = System.nanoTime();
for (int i = 0; i < LOOPS; i++) {
checkForBadCharsArray("testfile.jsp ");
}
end = System.nanoTime();
System.out.println("char[] check took " + (end - start) + "ns or " +
(end-start)/LOOPS + "ns per iteration");
start = System.nanoTime();
for (int i = 0; i < LOOPS; i++) {
checkForBadCharsAt("testfile.jsp ");
}
end = System.nanoTime();
System.out.println("charAt() check took " + (end - start) + "ns or " +
(end-start)/LOOPS + "ns per iteration");
}
private boolean checkForBadCharsArray(String filename) {
char[] chars = filename.toCharArray();
for (char c : chars) {
if (c == '\"' || c == '<' || c == '>') {
return false;
}
}
if (chars[chars.length -1] == ' ') {
return false;
}
return true;
}
private boolean checkForBadCharsAt(String filename) {
final int len = filename.length();
for (int i = 0; i < len; i++) {
char c = filename.charAt(i);
if (c == '\"' || c == '<' || c == '>') {
return false;
}
}
if (filename.charAt(len - 1) == ' ') {
return false;
}
return true;
}
}

View File

@@ -0,0 +1,134 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.webresources;
import java.io.File;
import java.io.InputStream;
import java.net.JarURLConnection;
import java.net.URL;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.Context;
import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.core.StandardHost;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.TomcatBaseTest;
public class TestCachedResource extends TomcatBaseTest {
// https://bz.apache.org/bugzilla/show_bug.cgi?id=63872
@Test
public void testUrlFileFromDirectory() throws Exception {
Tomcat tomcat = getTomcatInstance();
File docBase = new File("test/webresources/dir1");
Context ctx = tomcat.addWebapp("/test", docBase.getAbsolutePath());
tomcat.start();
WebResourceRoot root = ctx.getResources();
URL d1 = root.getResource("/d1").getURL();
URL d1f1 = new URL(d1, "d1-f1.txt");
try (InputStream is = d1f1.openStream()) {
Assert.assertNotNull(is);
}
}
// https://bz.apache.org/bugzilla/show_bug.cgi?id=63970
@Test
public void testCachedJarUrlConnection() throws Exception {
Tomcat tomcat = getTomcatInstance();
File docBase = new File("test/webresources/war-url-connection.war");
Context ctx = tomcat.addWebapp("/test", docBase.getAbsolutePath());
tomcat.start();
WebResourceRoot root = ctx.getResources();
// WAR contains a resources JAR so this should return a JAR URL
URL webinf = root.getResource("/index.html").getURL();
Assert.assertEquals("jar", webinf.getProtocol());
JarURLConnection jarConn = null;
try {
jarConn = (JarURLConnection) webinf.openConnection();
} catch (ClassCastException e) {
// Ignore
}
Assert.assertNotNull(jarConn);
}
@Test
public void testDirectoryListingsPackedWar() throws Exception {
Tomcat tomcat = getTomcatInstance();
File docBase = new File("test/webresources/war-url-connection.war");
Context ctx = tomcat.addWebapp("/test", docBase.getAbsolutePath());
((StandardHost) tomcat.getHost()).setUnpackWARs(false);
tomcat.start();
WebResourceRoot root = ctx.getResources();
URL d1 = root.getResource("/").getURL();
try (InputStream is = d1.openStream()) {
Assert.assertNotNull(is);
}
}
@Test
public void testDirectoryListingsWar() throws Exception {
Tomcat tomcat = getTomcatInstance();
File docBase = new File("test/webresources/war-url-connection.war");
Context ctx = tomcat.addWebapp("/test", docBase.getAbsolutePath());
tomcat.start();
WebResourceRoot root = ctx.getResources();
URL d1 = root.getResource("/").getURL();
try (InputStream is = d1.openStream()) {
Assert.assertNotNull(is);
}
}
@Test
public void testDirectoryListingsDir() throws Exception {
Tomcat tomcat = getTomcatInstance();
File docBase = new File("test/webresources/dir1");
Context ctx = tomcat.addWebapp("/test", docBase.getAbsolutePath());
tomcat.start();
WebResourceRoot root = ctx.getResources();
URL d1 = root.getResource("/d1").getURL();
try (InputStream is = d1.openStream()) {
Assert.assertNotNull(is);
}
}
}

View File

@@ -0,0 +1,44 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.webresources;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
public class TestClasspathUrlStreamHandler {
@BeforeClass
public static void setup() {
TomcatURLStreamHandlerFactory.getInstance();
}
@Test
public void testClasspathURL01() throws IOException {
URL u = new URL("classpath:/org/apache/catalina/webresources/LocalStrings.properties");
InputStream is = u.openStream();
Properties p = new Properties();
p.load(is);
String msg = (String) p.get("dirResourceSet.writeNpe");
Assert.assertEquals("The input stream may not be null", msg);
}
}

View File

@@ -0,0 +1,93 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.webresources;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileAttribute;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.WebResourceSet;
import org.apache.catalina.startup.ExpandWar;
import org.apache.catalina.startup.TomcatBaseTest;
public class TestDirResourceSet extends AbstractTestResourceSet {
private static Path tempDir;
private static File dir1;
@BeforeClass
public static void before() throws IOException {
tempDir = Files.createTempDirectory("test", new FileAttribute[0]);
dir1 = new File(tempDir.toFile(), "dir1");
TomcatBaseTest.recursiveCopy(new File("test/webresources/dir1").toPath(), dir1.toPath());
}
@AfterClass
public static void after() {
ExpandWar.delete(tempDir.toFile());
}
@Override
public WebResourceRoot getWebResourceRoot() {
TesterWebResourceRoot root = new TesterWebResourceRoot();
WebResourceSet webResourceSet =
new DirResourceSet(root, "/", getBaseDir().getAbsolutePath(), "/");
webResourceSet.setReadOnly(false);
root.setMainResources(webResourceSet);
return root;
}
@Override
protected boolean isWriteable() {
return true;
}
@Override
public File getBaseDir() {
return dir1;
}
@Override
@Test
public void testNoArgConstructor() {
@SuppressWarnings("unused")
Object obj = new DirResourceSet();
}
@Override
protected String getNewDirName() {
return "test-dir-01";
}
@Override
protected String getNewFileNameNull() {
return "test-null-01";
}
@Override
protected String getNewFileName() {
return "test-file-01";
}
}

View File

@@ -0,0 +1,89 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.webresources;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileAttribute;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.WebResourceSet;
import org.apache.catalina.startup.ExpandWar;
import org.apache.catalina.startup.TomcatBaseTest;
public class TestDirResourceSetInternal extends AbstractTestResourceSet {
private static Path tempDir;
private static File dir1;
@BeforeClass
public static void before() throws IOException {
tempDir = Files.createTempDirectory("test", new FileAttribute[0]);
dir1 = new File(tempDir.toFile(), "dir1");
TomcatBaseTest.recursiveCopy(new File("test/webresources/dir1").toPath(), dir1.toPath());
}
@AfterClass
public static void after() {
ExpandWar.delete(tempDir.toFile());
}
@Override
public WebResourceRoot getWebResourceRoot() {
TesterWebResourceRoot root = new TesterWebResourceRoot();
WebResourceSet webResourceSet =
new DirResourceSet(root, "/", tempDir.toAbsolutePath().toString(), "/dir1");
root.setMainResources(webResourceSet);
return root;
}
@Override
protected boolean isWriteable() {
return true;
}
@Override
public File getBaseDir() {
return dir1;
}
@Override
public void testNoArgConstructor() {
// NO-OP. Tested in TestDirResource
}
@Override
protected String getNewDirName() {
return "test-dir-02";
}
@Override
protected String getNewFileNameNull() {
return "test-null-02";
}
@Override
protected String getNewFileName() {
return "test-file-02";
}
}

View File

@@ -0,0 +1,85 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.webresources;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileAttribute;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.WebResourceSet;
import org.apache.catalina.startup.ExpandWar;
import org.apache.catalina.startup.TomcatBaseTest;
public class TestDirResourceSetMount extends AbstractTestResourceSetMount {
private static Path tempDir;
private static File dir1;
@BeforeClass
public static void before() throws IOException {
tempDir = Files.createTempDirectory("test", new FileAttribute[0]);
dir1 = new File(tempDir.toFile(), "dir1");
TomcatBaseTest.recursiveCopy(new File("test/webresources/dir1").toPath(), dir1.toPath());
}
@AfterClass
public static void after() {
ExpandWar.delete(tempDir.toFile());
}
@Override
public WebResourceRoot getWebResourceRoot() {
TesterWebResourceRoot root = new TesterWebResourceRoot();
WebResourceSet webResourceSet =
new DirResourceSet(new TesterWebResourceRoot(), getMount(),
getBaseDir().getAbsolutePath(), "/");
root.setMainResources(webResourceSet);
return root;
}
@Override
protected boolean isWriteable() {
return true;
}
@Override
public File getBaseDir() {
return dir1;
}
@Override
protected String getNewDirName() {
return "test-dir-03";
}
@Override
protected String getNewFileNameNull() {
return "test-null-03";
}
@Override
protected String getNewFileName() {
return "test-file-03";
}
}

View File

@@ -0,0 +1,65 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.webresources;
import java.io.File;
import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.WebResourceSet;
public class TestDirResourceSetReadOnly extends AbstractTestResourceSet {
@Override
public WebResourceRoot getWebResourceRoot() {
TesterWebResourceRoot root = new TesterWebResourceRoot();
WebResourceSet webResourceSet =
new DirResourceSet(root, "/", getBaseDir().getAbsolutePath(), "/");
webResourceSet.setReadOnly(true);
root.setMainResources(webResourceSet);
return root;
}
@Override
protected boolean isWriteable() {
return false;
}
@Override
public File getBaseDir() {
return new File("test/webresources/dir1");
}
@Override
public void testNoArgConstructor() {
// NO-OP. Tested in TestDirResource
}
@Override
protected String getNewDirName() {
return "test-dir-04";
}
@Override
protected String getNewFileNameNull() {
return "test-null-04";
}
@Override
protected String getNewFileName() {
return "test-file-04";
}
}

View File

@@ -0,0 +1,107 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.webresources;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileAttribute;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.WebResourceSet;
import org.apache.catalina.startup.ExpandWar;
import org.apache.catalina.startup.TomcatBaseTest;
public class TestDirResourceSetVirtual extends AbstractTestResourceSet {
private static Path tempDir;
private static File dir1;
@BeforeClass
public static void before() throws IOException {
tempDir = Files.createTempDirectory("test", new FileAttribute[0]);
dir1 = new File(tempDir.toFile(), "dir1");
TomcatBaseTest.recursiveCopy(new File("test/webresources/dir1").toPath(), dir1.toPath());
}
@AfterClass
public static void after() {
ExpandWar.delete(tempDir.toFile());
}
@Override
public WebResourceRoot getWebResourceRoot() {
TesterWebResourceRoot root = new TesterWebResourceRoot();
WebResourceSet webResourceSet =
new DirResourceSet(new TesterWebResourceRoot(), "/",
getBaseDir().getAbsolutePath(), "/");
root.setMainResources(webResourceSet);
WebResourceSet f1 = new FileResourceSet(root, "/f1.txt",
dir1.getAbsolutePath() + "/f1.txt", "/");
root.addPreResources(f1);
WebResourceSet f2 = new FileResourceSet(root, "/f2.txt",
dir1.getAbsolutePath() + "/f2.txt", "/");
root.addPreResources(f2);
WebResourceSet d1 = new DirResourceSet(root, "/d1",
dir1.getAbsolutePath() + "/d1", "/");
root.addPreResources(d1);
WebResourceSet d2 = new DirResourceSet(root, "/d2",
dir1.getAbsolutePath() + "/d2", "/");
root.addPreResources(d2);
return root;
}
@Override
protected boolean isWriteable() {
return true;
}
@Override
public File getBaseDir() {
return new File("test/webresources/dir3");
}
@Override
public void testNoArgConstructor() {
// NO-OP. Tested in TestDirResource
}
@Override
protected String getNewDirName() {
return "test-dir-05";
}
@Override
protected String getNewFileNameNull() {
return "test-null-05";
}
@Override
protected String getNewFileName() {
return "test-file-05";
}
}

View File

@@ -0,0 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.webresources;
import java.io.File;
import javax.servlet.http.HttpServletResponse;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.startup.TomcatBaseTest;
import org.apache.tomcat.util.buf.ByteChunk;
public class TestFileResource extends TomcatBaseTest {
@Test
public void testGetCodePath() throws Exception {
getTomcatInstanceTestWebapp(false, true);
ByteChunk out = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() + "/test/bug5nnnn/bug58096.jsp", out, null);
Assert.assertEquals(HttpServletResponse.SC_OK, rc);
// Build the expected location the same way the webapp base dir is built
File f = new File("test/webapp/WEB-INF/classes");
Assert.assertEquals(f.toURI().toURL().toString(), out.toString().trim());
}
}

View File

@@ -0,0 +1,72 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.webresources;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileAttribute;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.apache.catalina.startup.ExpandWar;
import org.apache.catalina.startup.TomcatBaseTest;
public class TestFileResourceSet extends AbstractTestFileResourceSet {
private static Path tempDir;
private static File dir2;
@BeforeClass
public static void before() throws IOException {
tempDir = Files.createTempDirectory("test", new FileAttribute[0]);
dir2 = new File(tempDir.toFile(), "dir2");
TomcatBaseTest.recursiveCopy(new File("test/webresources/dir2").toPath(), dir2.toPath());
}
@AfterClass
public static void after() {
ExpandWar.delete(tempDir.toFile());
}
public TestFileResourceSet() {
super(false);
}
@Override
protected File getDir2() {
return dir2;
}
@Override
protected String getNewDirName() {
return "test-dir-06";
}
@Override
protected String getNewFileNameNull() {
return "test-null-06";
}
@Override
protected String getNewFileName() {
return "test-file-06";
}
}

View File

@@ -0,0 +1,72 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.webresources;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileAttribute;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.apache.catalina.startup.ExpandWar;
import org.apache.catalina.startup.TomcatBaseTest;
public class TestFileResourceSetReadOnly extends AbstractTestFileResourceSet {
private static Path tempDir;
private static File dir2;
@BeforeClass
public static void before() throws IOException {
tempDir = Files.createTempDirectory("test", new FileAttribute[0]);
dir2 = new File(tempDir.toFile(), "dir2");
TomcatBaseTest.recursiveCopy(new File("test/webresources/dir2").toPath(), dir2.toPath());
}
@AfterClass
public static void after() {
ExpandWar.delete(tempDir.toFile());
}
public TestFileResourceSetReadOnly() {
super(true);
}
@Override
protected File getDir2() {
return dir2;
}
@Override
protected String getNewDirName() {
return "test-dir-07";
}
@Override
protected String getNewFileNameNull() {
return "test-null-07";
}
@Override
protected String getNewFileName() {
return "test-file-07";
}
}

View File

@@ -0,0 +1,77 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.webresources;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileAttribute;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.apache.catalina.startup.ExpandWar;
import org.apache.catalina.startup.TomcatBaseTest;
/**
* Mounts file resources in sub directories that do not exist in the main
* resources.
*/
public class TestFileResourceSetVirtual extends TestFileResourceSet {
private static Path tempDir;
private static File dir2;
@BeforeClass
public static void before() throws IOException {
tempDir = Files.createTempDirectory("test", new FileAttribute[0]);
dir2 = new File(tempDir.toFile(), "dir2");
TomcatBaseTest.recursiveCopy(new File("test/webresources/dir2").toPath(), dir2.toPath());
}
@AfterClass
public static void after() {
ExpandWar.delete(tempDir.toFile());
}
@Override
public File getBaseDir() {
return new File("test/webresources/dir3");
}
@Override
protected File getDir2() {
return dir2;
}
@Override
protected String getNewDirName() {
return "test-dir-11";
}
@Override
protected String getNewFileNameNull() {
return "test-null-11";
}
@Override
protected String getNewFileName() {
return "test-file-11";
}
}

View File

@@ -0,0 +1,141 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.webresources;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.WebResource;
import org.apache.tomcat.util.compat.JreCompat;
public class TestJarInputStreamWrapper {
@Test
public void testReadAfterClose() throws Exception {
Method m = InputStream.class.getMethod("read", (Class<?>[]) null);
testMethodAfterClose(m, (Object[]) null);
}
@Test
public void testSkipAfterClose() throws Exception {
Method m = InputStream.class.getMethod("skip", long.class);
testMethodAfterClose(m, Long.valueOf(1));
}
@Test
public void testAvailableAfterClose() throws Exception {
Method m = InputStream.class.getMethod("available", (Class<?>[]) null);
testMethodAfterClose(m, (Object[]) null);
}
@Test
public void testCloseAfterClose() throws Exception {
Method m = InputStream.class.getMethod("close", (Class<?>[]) null);
testMethodAfterClose(m, (Object[]) null);
}
@Test
public void testMarkAfterClose() throws Exception {
Method m = InputStream.class.getMethod("mark", int.class);
testMethodAfterClose(m, Integer.valueOf(1));
}
@Test
public void testResetAfterClose() throws Exception {
Method m = InputStream.class.getMethod("reset", (Class<?>[]) null);
testMethodAfterClose(m, (Object[]) null);
}
@Test
public void testMarkSupportedAfterClose() throws Exception {
Method m = InputStream.class.getMethod("markSupported", (Class<?>[]) null);
testMethodAfterClose(m, (Object[]) null);
}
private void testMethodAfterClose(Method m, Object... params) throws IOException {
InputStream unwrapped = getUnwrappedClosedInputStream();
InputStream wrapped = getWrappedClosedInputStream();
Object unwrappedReturn = null;
Exception unwrappedException = null;
Object wrappedReturn = null;
Exception wrappedException = null;
try {
unwrappedReturn = m.invoke(unwrapped, params);
} catch (Exception e) {
unwrappedException = e;
}
try {
wrappedReturn = m.invoke(wrapped, params);
} catch (Exception e) {
wrappedException = e;
}
if (unwrappedReturn == null) {
Assert.assertNull(wrappedReturn);
} else {
Assert.assertNotNull(wrappedReturn);
Assert.assertEquals(unwrappedReturn, wrappedReturn);
}
if (unwrappedException == null) {
Assert.assertNull(wrappedException);
} else {
Assert.assertNotNull(wrappedException);
Assert.assertEquals(unwrappedException.getClass(), wrappedException.getClass());
}
}
private InputStream getUnwrappedClosedInputStream() throws IOException {
File file = new File("test/webresources/non-static-resources.jar");
JarFile jarFile = JreCompat.getInstance().jarFileNewInstance(file);
ZipEntry jarEntry = jarFile.getEntry("META-INF/MANIFEST.MF");
InputStream unwrapped = jarFile.getInputStream(jarEntry);
unwrapped.close();
jarFile.close();
return unwrapped;
}
private InputStream getWrappedClosedInputStream() throws IOException {
StandardRoot root = new StandardRoot();
root.setCachingAllowed(false);
JarResourceSet jarResourceSet =
new JarResourceSet(root, "/", "test/webresources/non-static-resources.jar", "/");
WebResource webResource = jarResourceSet.getResource("/META-INF/MANIFEST.MF");
InputStream wrapped = webResource.getInputStream();
wrapped.close();
return wrapped;
}
}

View File

@@ -0,0 +1,69 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.webresources;
import java.io.File;
import org.junit.Test;
import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.WebResourceSet;
public class TestJarResourceSet extends AbstractTestResourceSet {
@Override
public WebResourceRoot getWebResourceRoot() {
File f = new File("test/webresources/dir1.jar");
TesterWebResourceRoot root = new TesterWebResourceRoot();
WebResourceSet webResourceSet =
new JarResourceSet(root, "/", f.getAbsolutePath(), "/");
root.setMainResources(webResourceSet);
return root;
}
@Override
protected boolean isWriteable() {
return false;
}
@Override
public File getBaseDir() {
return new File("test/webresources");
}
@Override
@Test
public void testNoArgConstructor() {
@SuppressWarnings("unused")
Object obj = new JarResourceSet();
}
@Override
protected String getNewDirName() {
return "test-dir-08";
}
@Override
protected String getNewFileNameNull() {
return "test-null-08";
}
@Override
protected String getNewFileName() {
return "test-file-08";
}
}

View File

@@ -0,0 +1,69 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.webresources;
import java.io.File;
import org.junit.Test;
import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.WebResourceSet;
public class TestJarResourceSetInternal extends AbstractTestResourceSet {
@Override
public WebResourceRoot getWebResourceRoot() {
File f = new File("test/webresources/dir1-internal.jar");
TesterWebResourceRoot root = new TesterWebResourceRoot();
WebResourceSet webResourceSet =
new JarResourceSet(root, "/", f.getAbsolutePath(), "/dir1");
root.setMainResources(webResourceSet);
return root;
}
@Override
protected boolean isWriteable() {
return false;
}
@Override
public File getBaseDir() {
return new File("test/webresources");
}
@Override
@Test
public void testNoArgConstructor() {
@SuppressWarnings("unused")
Object obj = new JarResourceSet();
}
@Override
protected String getNewDirName() {
return "test-dir-09";
}
@Override
protected String getNewFileNameNull() {
return "test-null-09";
}
@Override
protected String getNewFileName() {
return "test-file-09";
}
}

View File

@@ -0,0 +1,60 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.webresources;
import java.io.File;
import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.WebResourceSet;
public class TestJarResourceSetMount extends AbstractTestResourceSetMount {
@Override
public WebResourceRoot getWebResourceRoot() {
File f = new File("test/webresources/dir1.jar");
TesterWebResourceRoot root = new TesterWebResourceRoot();
WebResourceSet webResourceSet =
new JarResourceSet(root, getMount(), f.getAbsolutePath(), "/");
root.setMainResources(webResourceSet);
return root;
}
@Override
protected boolean isWriteable() {
return false;
}
@Override
public File getBaseDir() {
return new File("test/webresources");
}
@Override
protected String getNewDirName() {
return "test-dir-10";
}
@Override
protected String getNewFileNameNull() {
return "test-null-10";
}
@Override
protected String getNewFileName() {
return "test-file-10";
}
}

View File

@@ -0,0 +1,56 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.webresources;
import java.io.File;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.WebResource;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.TomcatBaseTest;
public class TestJarWarResourceSet extends TomcatBaseTest {
@Before
public void register() {
TomcatURLStreamHandlerFactory.register();
}
@Test
public void testJarWarMetaInf() throws LifecycleException {
Tomcat tomcat = getTomcatInstance();
File warFile = new File("test/webresources/war-url-connection.war");
Context ctx = tomcat.addContext("", warFile.getAbsolutePath());
tomcat.start();
StandardRoot root = (StandardRoot) ctx.getResources();
WebResource[] results = root.getClassLoaderResources("/META-INF");
Assert.assertNotNull(results);
Assert.assertEquals(1, results.length);
Assert.assertNotNull(results[0].getURL());
}
}

View File

@@ -0,0 +1,51 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.webresources;
import java.io.File;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.WebResource;
import org.apache.catalina.WebResourceSet;
public class TestResourceJars {
@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());
}
}

View File

@@ -0,0 +1,80 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.webresources;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.webresources.StandardRoot.BaseLocation;
public class TestStandardRoot {
private static final File file;
private static final String fileUrl;
static {
file = new File("/foo");
String url = null;
try {
url = file.toURI().toURL().toExternalForm();
} catch (MalformedURLException e) {
// Ignore
}
fileUrl = url;
}
@Test
public void testBaseLocation01() throws Exception {
doTestBaseLocation(new URL (fileUrl),
file.getAbsolutePath(), null);
}
@Test
public void testBaseLocation02() throws Exception {
doTestBaseLocation(new URL ("jar:" + fileUrl + "!/"),
file.getAbsolutePath(), null);
}
@Test
public void testBaseLocation03() throws Exception {
doTestBaseLocation(new URL ("jar:" + fileUrl + "!/bar"),
file.getAbsolutePath(), "bar");
}
@Test
public void testBaseLocation04() throws Exception {
doTestBaseLocation(new URL ("jar:" + fileUrl + "!/bar/bar"),
file.getAbsolutePath(), "bar/bar");
}
@Test(expected=IllegalArgumentException.class)
public void testBaseLocation05() throws Exception {
doTestBaseLocation(new URL ("http://localhost:8080/foo"),
null, null);
}
private void doTestBaseLocation(URL url, String expectedBasePath,
String expectedArchivePath) {
BaseLocation baseLocation = new BaseLocation(url);
Assert.assertEquals(expectedBasePath, baseLocation.getBasePath());
Assert.assertEquals(expectedArchivePath, baseLocation.getArchivePath());
}
}

View File

@@ -0,0 +1,43 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.webresources;
import java.net.URLStreamHandler;
import java.net.URLStreamHandlerFactory;
import org.junit.Before;
import org.junit.Test;
public class TestTomcatURLStreamHandlerFactory {
@Before
public void register() {
TomcatURLStreamHandlerFactory.register();
}
@Test
public void testUserFactory() throws Exception {
URLStreamHandlerFactory factory = new URLStreamHandlerFactory() {
@Override
public URLStreamHandler createURLStreamHandler(String protocol) {
return null;
}
};
TomcatURLStreamHandlerFactory.getInstance().addUserFactory(factory);
TomcatURLStreamHandlerFactory.release(factory.getClass().getClassLoader());
}
}

View File

@@ -0,0 +1,159 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.webresources;
import java.net.URL;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.LifecycleListener;
import org.apache.catalina.LifecycleState;
import org.apache.tomcat.unittest.TesterContext;
/**
* Minimal implementation for use in unit tests that supports main and pre
* resources.
*/
public class TesterWebResourceRoot extends StandardRoot {
public TesterWebResourceRoot() {
super();
setCachingAllowed(false);
}
@Override
public void addLifecycleListener(LifecycleListener listener) {
// NO-OP
}
@Override
public LifecycleListener[] findLifecycleListeners() {
return null;
}
@Override
public void removeLifecycleListener(LifecycleListener listener) {
// NO-OP
}
@Override
public void initInternal() throws LifecycleException {
// NO-OP
}
@Override
public void startInternal() throws LifecycleException {
setState(LifecycleState.STARTING);
}
@Override
public void stopInternal() throws LifecycleException {
setState(LifecycleState.STOPPING);
}
@Override
public void destroyInternal() throws LifecycleException {
// NO-OP
}
@Override
public LifecycleState getState() {
return LifecycleState.STARTED;
}
@Override
public String getStateName() {
return null;
}
@Override
public Context getContext() {
return new TesterContext();
}
@Override
public void setContext(Context context) {
// NO-OP
}
@Override
public void createWebResourceSet(ResourceSetType type, String webAppPath,
URL url, String internalPath) {
// NO-OP
}
@Override
public void createWebResourceSet(ResourceSetType type, String webAppPath,
String base, String archivePath, String internalPath) {
// NO-OP
}
@Override
public void setAllowLinking(boolean allowLinking) {
// NO-OP
}
@Override
public boolean getAllowLinking() {
return false;
}
@Override
public void setCachingAllowed(boolean cachingAllowed) {
// NO-OP
}
@Override
public boolean isCachingAllowed() {
return false;
}
@Override
public void setCacheTtl(long ttl) {
// NO-OP
}
@Override
public long getCacheTtl() {
return 0;
}
@Override
public void setCacheMaxSize(long cacheMaxSize) {
// NO-OP
}
@Override
public long getCacheMaxSize() {
return 0;
}
@Override
public void setCacheObjectMaxSize(int cacheObjectMaxSize) {
// NO-OP
}
@Override
public int getCacheObjectMaxSize() {
return 0;
}
@Override
public void backgroundProcess() {
// NO-OP
}
}

View File

@@ -0,0 +1,81 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.webresources.war;
import java.io.File;
import java.net.URL;
import java.net.URLConnection;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.apache.catalina.webresources.TomcatURLStreamHandlerFactory;
public class TestHandler {
@Before
public void register() {
TomcatURLStreamHandlerFactory.register();
}
@Test
public void testUrlFileInJarInWar() throws Exception {
doTestUrl("jar:war:", "*/WEB-INF/lib/test.jar!/META-INF/resources/index.html");
}
@Test
public void testUrlJarInWar() throws Exception {
doTestUrl("war:", "*/WEB-INF/lib/test.jar");
}
@Test
public void testUrlWar() throws Exception {
doTestUrl("", "");
}
private void doTestUrl(String prefix, String suffix) throws Exception {
File f = new File("test/webresources/war-url-connection.war");
String fileUrl = f.toURI().toURL().toString();
String urlString = prefix + fileUrl + suffix;
URL url = new URL(urlString);
Assert.assertEquals(urlString, url.toExternalForm());
}
@Test
public void testOldFormat() throws Exception {
File f = new File("test/webresources/war-url-connection.war");
String fileUrl = f.toURI().toURL().toString();
URL indexHtmlUrl = new URL("jar:war:" + fileUrl +
"^/WEB-INF/lib/test.jar!/META-INF/resources/index.html");
URLConnection urlConn = indexHtmlUrl.openConnection();
urlConn.connect();
int size = urlConn.getContentLength();
Assert.assertEquals(137, size);
}
}

View File

@@ -0,0 +1,52 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.webresources.war;
import java.io.File;
import java.net.URL;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.Context;
import org.apache.catalina.core.StandardHost;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.TomcatBaseTest;
public class TestHandlerIntegration extends TomcatBaseTest {
@Test
public void testToURI() throws Exception {
Tomcat tomcat = getTomcatInstance();
File docBase = new File("test/webresources/war-url-connection.war");
Context ctx = tomcat.addWebapp("/test", docBase.getAbsolutePath());
skipTldsForResourceJars(ctx);
((StandardHost) tomcat.getHost()).setUnpackWARs(false);
tomcat.start();
URL url = ctx.getServletContext().getResource("/index.html");
try {
url.toURI();
} catch (Exception e) {
e.printStackTrace();
Assert.fail();
}
}
}

View File

@@ -0,0 +1,52 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.webresources.war;
import java.io.File;
import java.net.URL;
import java.net.URLConnection;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.apache.catalina.webresources.TomcatURLStreamHandlerFactory;
public class TestWarURLConnection {
@Before
public void register() {
TomcatURLStreamHandlerFactory.register();
}
@Test
public void testContentLength() throws Exception {
File f = new File("test/webresources/war-url-connection.war");
String fileUrl = f.toURI().toURL().toString();
URL indexHtmlUrl = new URL("jar:war:" + fileUrl +
"*/WEB-INF/lib/test.jar!/META-INF/resources/index.html");
URLConnection urlConn = indexHtmlUrl.openConnection();
urlConn.connect();
int size = urlConn.getContentLength();
Assert.assertEquals(137, size);
}
}