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

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,40 @@
/*
* 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.tomcat.unittest;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
public class TesterCounter {
private static final Log log = LogFactory.getLog(TesterCounter.class);
static {
log.info("TestCounter loaded by " + TesterCounter.class.getClassLoader() +
" in thread " + Thread.currentThread().getName());
}
private int count = 0;
public void increment() {
count++;
}
public int getCount() {
return count;
}
}

View File

@@ -0,0 +1,37 @@
/*
* 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.tomcat.unittest;
public class TesterData {
public static String string(char c, int count) {
StringBuilder sb = new StringBuilder(count);
for (int i = 0; i < count; i++) {
sb.append(c);
}
return sb.toString();
}
public static String string(String str, int count) {
StringBuilder sb = new StringBuilder(str.length() * count);
for (int i = 0; i < count; i++) {
sb.append(str);
}
return sb.toString();
}
}

View File

@@ -0,0 +1,377 @@
/*
* 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.tomcat.unittest;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.concurrent.ExecutorService;
import java.util.regex.Pattern;
import javax.management.ObjectName;
import org.apache.catalina.AccessLog;
import org.apache.catalina.Cluster;
import org.apache.catalina.Container;
import org.apache.catalina.ContainerListener;
import org.apache.catalina.Host;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.LifecycleListener;
import org.apache.catalina.LifecycleState;
import org.apache.catalina.Pipeline;
import org.apache.catalina.Realm;
import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;
import org.apache.juli.logging.Log;
public class TesterHost implements Host {
@Override
public Log getLogger() {
return null;
}
@Override
public String getLogName() {
return null;
}
@Override
public ObjectName getObjectName() {
return null;
}
@Override
public String getDomain() {
return null;
}
@Override
public String getMBeanKeyProperties() {
return null;
}
@Override
public Pipeline getPipeline() {
return null;
}
@Override
public Cluster getCluster() {
return null;
}
@Override
public void setCluster(Cluster cluster) {
// NO-OP
}
@Override
public int getBackgroundProcessorDelay() {
return 0;
}
@Override
public void setBackgroundProcessorDelay(int delay) {
// NO-OP
}
private String name = "TestHost";
@Override
public String getName() {
return name;
}
@Override
public void setName(String name) {
this.name = name;
}
@Override
public Container getParent() {
return null;
}
@Override
public void setParent(Container container) {
// NO-OP
}
@Override
public ClassLoader getParentClassLoader() {
return null;
}
@Override
public void setParentClassLoader(ClassLoader parent) {
// NO-OP
}
@Override
public Realm getRealm() {
return null;
}
@Override
public void setRealm(Realm realm) {
// NO-OP
}
@Override
public void backgroundProcess() {
// NO-OP
}
@Override
public void addChild(Container child) {
// NO-OP
}
@Override
public void addContainerListener(ContainerListener listener) {
// NO-OP
}
@Override
public void addPropertyChangeListener(PropertyChangeListener listener) {
// NO-OP
}
@Override
public Container findChild(String name) {
return null;
}
@Override
public Container[] findChildren() {
return null;
}
@Override
public ContainerListener[] findContainerListeners() {
return null;
}
@Override
public void removeChild(Container child) {
// NO-OP
}
@Override
public void removeContainerListener(ContainerListener listener) {
// NO-OP
}
@Override
public void removePropertyChangeListener(PropertyChangeListener listener) {
// NO-OP
}
@Override
public void fireContainerEvent(String type, Object data) {
// NO-OP
}
@Override
public void logAccess(Request request, Response response, long time, boolean useDefault) {
// NO-OP
}
@Override
public AccessLog getAccessLog() {
return null;
}
@Override
public int getStartStopThreads() {
return 0;
}
@Override
public void setStartStopThreads(int startStopThreads) {
// NO-OP
}
@Override
public File getCatalinaBase() {
return null;
}
@Override
public File getCatalinaHome() {
return null;
}
@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 init() throws LifecycleException {
// NO-OP
}
@Override
public void start() throws LifecycleException {
// NO-OP
}
@Override
public void stop() throws LifecycleException {
// NO-OP
}
@Override
public void destroy() throws LifecycleException {
// NO-OP
}
@Override
public LifecycleState getState() {
return null;
}
@Override
public String getStateName() {
return null;
}
@Override
public String getXmlBase() {
return null;
}
@Override
public void setXmlBase(String xmlBase) {
// NO-OP
}
@Override
public File getConfigBaseFile() {
return null;
}
@Override
public String getAppBase() {
return null;
}
@Override
public File getAppBaseFile() {
return null;
}
@Override
public void setAppBase(String appBase) {
// NO-OP
}
@Override
public boolean getAutoDeploy() {
return false;
}
@Override
public void setAutoDeploy(boolean autoDeploy) {
// NO-OP
}
@Override
public String getConfigClass() {
return null;
}
@Override
public void setConfigClass(String configClass) {
// NO-OP
}
@Override
public boolean getDeployOnStartup() {
return false;
}
@Override
public void setDeployOnStartup(boolean deployOnStartup) {
// NO-OP
}
@Override
public String getDeployIgnore() {
return null;
}
@Override
public Pattern getDeployIgnorePattern() {
return null;
}
@Override
public void setDeployIgnore(String deployIgnore) {
// NO-OP
}
@Override
public ExecutorService getStartStopExecutor() {
return null;
}
@Override
public boolean getCreateDirs() {
return false;
}
@Override
public void setCreateDirs(boolean createDirs) {
// NO-OP
}
@Override
public boolean getUndeployOldVersions() {
return false;
}
@Override
public void setUndeployOldVersions(boolean undeployOldVersions) {
// NO-OP
}
@Override
public void addAlias(String alias) {
// NO-OP
}
@Override
public String[] findAliases() {
return null;
}
@Override
public void removeAlias(String alias) {
// NO-OP
}
}

View File

@@ -0,0 +1,54 @@
/*
* 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.tomcat.unittest;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
public class TesterLeakingServlet1 extends HttpServlet {
private static final Log log = LogFactory.getLog(TesterLeakingServlet1.class);
private static final long serialVersionUID = 1L;
private static ThreadLocal<TesterCounter> myThreadLocal = new ThreadLocal<>();
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
TesterCounter counter = myThreadLocal.get();
if (counter == null) {
log.info("Adding thread local to thread " + Thread.currentThread().getName());
counter = new TesterCounter();
myThreadLocal.set(counter);
}
counter.increment();
response.getWriter().println(
"The current thread served this servlet "
+ counter.getCount() + " times");
}
}

View File

@@ -0,0 +1,58 @@
/*
* 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.tomcat.unittest;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
public class TesterLeakingServlet2 extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final Log log = LogFactory.getLog(TesterLeakingServlet2.class);
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
List<TesterCounter> counterList = TesterThreadScopedHolder.getFromHolder();
TesterCounter counter;
if (counterList == null) {
log.info("Adding thread local to thread " + Thread.currentThread().getName());
counter = new TesterCounter();
TesterThreadScopedHolder.saveInHolder(Arrays.asList(counter));
} else {
counter = counterList.get(0);
}
counter.increment();
response.getWriter().println(
"The current thread served this servlet "
+ counter.getCount() + " times");
}
}

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.tomcat.unittest;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Filter;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
public class TesterLogValidationFilter implements Filter {
private final Level targetLevel;
private final String targetMessage;
private final String targetThrowableString;
private final AtomicInteger messageCount = new AtomicInteger(0);
public TesterLogValidationFilter(Level targetLevel, String targetMessage,
String targetThrowableString) {
this.targetLevel = targetLevel;
this.targetMessage = targetMessage;
this.targetThrowableString = targetThrowableString;
}
public int getMessageCount() {
return messageCount.get();
}
@Override
public boolean isLoggable(LogRecord record) {
if (targetLevel != null) {
Level level = record.getLevel();
if (targetLevel != level) {
return true;
}
}
if (targetMessage != null) {
String msg = record.getMessage();
if (msg == null || !msg.contains(targetMessage)) {
return true;
}
}
if (targetThrowableString != null) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
record.getThrown().printStackTrace(pw);
pw.close();
String throwableString = sw.toString();
if (!throwableString.contains(targetThrowableString)) {
return true;
}
}
messageCount.incrementAndGet();
return true;
}
public static TesterLogValidationFilter add(Level targetLevel, String targetMessage,
String targetThrowableString, String loggerName) {
TesterLogValidationFilter f = new TesterLogValidationFilter(targetLevel, targetMessage,
targetThrowableString);
LogManager.getLogManager().getLogger(loggerName).setFilter(f);
return f;
}
}

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.tomcat.unittest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletContext;
import javax.servlet.SessionTrackingMode;
import org.apache.catalina.Context;
import org.apache.catalina.connector.Request;
import org.apache.catalina.session.StandardSession;
public class TesterRequest extends Request {
private final TesterContext context;
private final TesterServletContext servletContext;
public TesterRequest() {
this(false);
}
public TesterRequest(boolean withSession) {
context = new TesterContext();
servletContext = new TesterServletContext();
context.setServletContext(servletContext);
if (withSession) {
Set<SessionTrackingMode> modes = new HashSet<>();
modes.add(SessionTrackingMode.URL);
modes.add(SessionTrackingMode.COOKIE);
servletContext.setSessionTrackingModes(modes);
session = new StandardSession(null);
session.setId("1234", false);
session.setValid(true);
}
}
@Override
public String getScheme() {
return "http";
}
@Override
public String getServerName() {
return "localhost";
}
@Override
public int getServerPort() {
return 8080;
}
@Override
public String getRequestURI() {
return "/level1/level2/foo.html";
}
@Override
public String getDecodedRequestURI() {
// Decoding not required
return getRequestURI();
}
@Override
public Context getContext() {
return context;
}
@Override
public ServletContext getServletContext() {
return servletContext;
}
private String method;
public void setMethod(String method) {
this.method = method;
}
@Override
public String getMethod() {
return method;
}
private final Map<String,List<String>> headers = new HashMap<>();
public void addHeader(String name, String value) {
List<String> values = headers.get(name);
if (values == null) {
values = new ArrayList<>();
headers.put(name, values);
}
values.add(value);
}
@Override
public String getHeader(String name) {
List<String> values = headers.get(name);
if (values == null || values.size() == 0) {
return null;
}
return values.get(0);
}
@Override
public Enumeration<String> getHeaders(String name) {
List<String> values = headers.get(name);
if (values == null || values.size() == 0) {
return Collections.emptyEnumeration();
}
return Collections.enumeration(headers.get(name));
}
@Override
public Enumeration<String> getHeaderNames() {
return Collections.enumeration(headers.keySet());
}
}

View File

@@ -0,0 +1,73 @@
/*
* 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.tomcat.unittest;
import java.io.IOException;
import org.apache.catalina.connector.Response;
/**
* Minimal changes to Response to enable tests that use this class to operate
* correctly.
*/
public class TesterResponse extends Response {
@Override
public boolean isCommitted() {
return false;
}
@Override
public void sendError(int status, String message) throws IOException {
// NO-OP by default.
/*
System.out.println("TesterResponse.sendError(" + status + ", \"" +
message + "\")");
*/
}
@Override
public void resetBuffer(boolean resetWriterStreamFlags) {
// NO-OP by default.
// There is no buffer created for this test object since no test depends
// on one being present or on this method resetting it.
}
@Override
public org.apache.coyote.Response getCoyoteResponse() {
// Lazy init
if (super.getCoyoteResponse() == null) {
this.coyoteResponse = new org.apache.coyote.Response();
}
return super.getCoyoteResponse();
}
@Override
public void setSuspended(boolean suspended) {
// NO-OP by default.
// There is no buffer created for this test object since no test depends
// on one being present or on this method suspending it.
}
@Override
public void reset() {
// Minimal implementation for tests that avoids using OutputBuffer
if (super.getCoyoteResponse() != null) {
super.getCoyoteResponse().reset();
}
}
}

View File

@@ -0,0 +1,345 @@
/*
* 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.tomcat.unittest;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.Enumeration;
import java.util.EventListener;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.servlet.Filter;
import javax.servlet.FilterRegistration;
import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import javax.servlet.ServletRegistration.Dynamic;
import javax.servlet.SessionCookieConfig;
import javax.servlet.SessionTrackingMode;
import javax.servlet.descriptor.JspConfigDescriptor;
import org.apache.catalina.core.ApplicationFilterRegistration;
import org.apache.tomcat.util.descriptor.web.FilterDef;
public class TesterServletContext implements ServletContext {
/**
* {@inheritDoc}
* <p>
* This test implementation is hard coded to return an empty String.
*/
@Override
public String getContextPath() {
return "";
}
/**
* {@inheritDoc}
* <p>
* This test implementation is hard coded to return an empty Set.
*/
@Override
public Set<String> getResourcePaths(String path) {
return Collections.emptySet();
}
/**
* {@inheritDoc}
* <p>
* This test implementation is hard coded to return the class loader that
* loaded this class.
*/
@Override
public ClassLoader getClassLoader() {
return getClass().getClassLoader();
}
@Override
public ServletContext getContext(String uripath) {
throw new RuntimeException("Not implemented");
}
@Override
public int getMajorVersion() {
throw new RuntimeException("Not implemented");
}
@Override
public int getMinorVersion() {
throw new RuntimeException("Not implemented");
}
@Override
public String getMimeType(String file) {
throw new RuntimeException("Not implemented");
}
@Override
public URL getResource(String path) throws MalformedURLException {
return null;
}
@Override
public InputStream getResourceAsStream(String path) {
throw new RuntimeException("Not implemented");
}
@Override
public RequestDispatcher getRequestDispatcher(String path) {
throw new RuntimeException("Not implemented");
}
@Override
public RequestDispatcher getNamedDispatcher(String name) {
throw new RuntimeException("Not implemented");
}
@Override
public Servlet getServlet(String name) throws ServletException {
throw new RuntimeException("Not implemented");
}
@Override
public Enumeration<Servlet> getServlets() {
throw new RuntimeException("Not implemented");
}
@Override
public Enumeration<String> getServletNames() {
throw new RuntimeException("Not implemented");
}
@Override
public void log(String msg) {
// NOOP
}
@Override
public void log(Exception exception, String msg) {
// NOOP
}
@Override
public void log(String message, Throwable throwable) {
// NOOP
}
@Override
public String getRealPath(String path) {
throw new RuntimeException("Not implemented");
}
@Override
public String getServerInfo() {
throw new RuntimeException("Not implemented");
}
@Override
public String getInitParameter(String name) {
return null;
}
@Override
public Enumeration<String> getInitParameterNames() {
throw new RuntimeException("Not implemented");
}
@Override
public Object getAttribute(String name) {
// Used by websockets
return null;
}
@Override
public Enumeration<String> getAttributeNames() {
throw new RuntimeException("Not implemented");
}
@Override
public void setAttribute(String name, Object object) {
throw new RuntimeException("Not implemented");
}
@Override
public void removeAttribute(String name) {
throw new RuntimeException("Not implemented");
}
@Override
public String getServletContextName() {
throw new RuntimeException("Not implemented");
}
@Override
public int getEffectiveMajorVersion() {
throw new RuntimeException("Not implemented");
}
@Override
public int getEffectiveMinorVersion() {
throw new RuntimeException("Not implemented");
}
@Override
public boolean setInitParameter(String name, String value) {
throw new RuntimeException("Not implemented");
}
@Override
public Dynamic addServlet(String servletName, String className) {
throw new RuntimeException("Not implemented");
}
@Override
public Dynamic addServlet(String servletName, Servlet servlet) {
throw new RuntimeException("Not implemented");
}
@Override
public Dynamic addServlet(String servletName,
Class<? extends Servlet> servletClass) {
throw new RuntimeException("Not implemented");
}
@Override
public <T extends Servlet> T createServlet(Class<T> c)
throws ServletException {
throw new RuntimeException("Not implemented");
}
@Override
public ServletRegistration getServletRegistration(String servletName) {
throw new RuntimeException("Not implemented");
}
@Override
public Map<String, ? extends ServletRegistration> getServletRegistrations() {
throw new RuntimeException("Not implemented");
}
@Override
public javax.servlet.FilterRegistration.Dynamic addFilter(
String filterName, String className) {
throw new RuntimeException("Not implemented");
}
@Override
public javax.servlet.FilterRegistration.Dynamic addFilter(
String filterName, Filter filter) {
return new ApplicationFilterRegistration(
new FilterDef(), new TesterContext());
}
@Override
public javax.servlet.FilterRegistration.Dynamic addFilter(
String filterName, Class<? extends Filter> filterClass) {
return new ApplicationFilterRegistration(
new FilterDef(), new TesterContext());
}
@Override
public <T extends Filter> T createFilter(Class<T> c)
throws ServletException {
throw new RuntimeException("Not implemented");
}
@Override
public FilterRegistration getFilterRegistration(String filterName) {
throw new RuntimeException("Not implemented");
}
@Override
public Map<String, ? extends FilterRegistration> getFilterRegistrations() {
throw new RuntimeException("Not implemented");
}
private SessionCookieConfig sessionCookieConfig = new TesterSessionCookieConfig();
@Override
public SessionCookieConfig getSessionCookieConfig() {
return sessionCookieConfig;
}
private final Set<SessionTrackingMode> sessionTrackingModes = new HashSet<>();
@Override
public void setSessionTrackingModes(
Set<SessionTrackingMode> sessionTrackingModes) {
this.sessionTrackingModes.clear();
this.sessionTrackingModes.addAll(sessionTrackingModes);
}
@Override
public Set<SessionTrackingMode> getDefaultSessionTrackingModes() {
throw new RuntimeException("Not implemented");
}
@Override
public Set<SessionTrackingMode> getEffectiveSessionTrackingModes() {
return sessionTrackingModes;
}
@Override
public void addListener(String className) {
throw new RuntimeException("Not implemented");
}
@Override
public <T extends EventListener> void addListener(T t) {
throw new RuntimeException("Not implemented");
}
@Override
public void addListener(Class<? extends EventListener> listenerClass) {
throw new RuntimeException("Not implemented");
}
@Override
public <T extends EventListener> T createListener(Class<T> c)
throws ServletException {
throw new RuntimeException("Not implemented");
}
@Override
public JspConfigDescriptor getJspConfigDescriptor() {
throw new RuntimeException("Not implemented");
}
@Override
public void declareRoles(String... roleNames) {
throw new RuntimeException("Not implemented");
}
/**
* {@inheritDoc}
* <p>
* This test implementation is hard coded to return <code>localhost</code>.
*/
@Override
public String getVirtualServerName() {
return "localhost";
}
}

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.tomcat.unittest;
import javax.servlet.SessionCookieConfig;
public class TesterSessionCookieConfig implements SessionCookieConfig {
private String name;
@Override
public void setName(String name) {
this.name = name;
}
@Override
public String getName() {
return name;
}
@Override
public void setDomain(String domain) {
throw new UnsupportedOperationException();
}
@Override
public String getDomain() {
throw new UnsupportedOperationException();
}
@Override
public void setPath(String path) {
throw new UnsupportedOperationException();
}
@Override
public String getPath() {
throw new UnsupportedOperationException();
}
@Override
public void setComment(String comment) {
throw new UnsupportedOperationException();
}
@Override
public String getComment() {
throw new UnsupportedOperationException();
}
@Override
public void setHttpOnly(boolean httpOnly) {
throw new UnsupportedOperationException();
}
@Override
public boolean isHttpOnly() {
throw new UnsupportedOperationException();
}
@Override
public void setSecure(boolean secure) {
throw new UnsupportedOperationException();
}
@Override
public boolean isSecure() {
throw new UnsupportedOperationException();
}
@Override
public void setMaxAge(int MaxAge) {
throw new UnsupportedOperationException();
}
@Override
public int getMaxAge() {
throw new UnsupportedOperationException();
}
}

View File

@@ -0,0 +1,32 @@
/*
* 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.tomcat.unittest;
import java.util.List;
public class TesterThreadScopedHolder {
private static final ThreadLocal<List<TesterCounter>> threadLocal =
new ThreadLocal<>();
public static void saveInHolder(List<TesterCounter> o) {
threadLocal.set(o);
}
public static List<TesterCounter> getFromHolder() {
return threadLocal.get();
}
}

View File

@@ -0,0 +1,23 @@
/*
* 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.tomcat.unittest.tags;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class Bug53545 extends BodyTagSupport {
private static final long serialVersionUID = 1L;
}

View File

@@ -0,0 +1,112 @@
/*
* 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.tomcat.util;
import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.core.StandardContext;
public class TestIntrospectionUtils {
// Test for all the classes and interfaces in StandardContext's type hierarchy
@Test
public void testIsInstanceStandardContext01() {
Assert.assertTrue(IntrospectionUtils.isInstance(
StandardContext.class, "org.apache.catalina.core.StandardContext"));
}
@Test
public void testIsInstanceStandardContext02() {
Assert.assertTrue(IntrospectionUtils.isInstance(
StandardContext.class, "org.apache.catalina.util.LifecycleMBeanBase"));
}
@Test
public void testIsInstanceStandardContext03() {
Assert.assertTrue(IntrospectionUtils.isInstance(
StandardContext.class, "org.apache.catalina.util.LifecycleBase"));
}
@Test
public void testIsInstanceStandardContext04() {
Assert.assertTrue(IntrospectionUtils.isInstance(
StandardContext.class, "java.lang.Object"));
}
@Test
public void testIsInstanceStandardContext05() {
Assert.assertTrue(IntrospectionUtils.isInstance(
StandardContext.class, "org.apache.catalina.Lifecycle"));
}
@Test
public void testIsInstanceStandardContext06() {
Assert.assertTrue(IntrospectionUtils.isInstance(
StandardContext.class, "org.apache.catalina.JmxEnabled"));
}
@Test
public void testIsInstanceStandardContext07() {
Assert.assertTrue(IntrospectionUtils.isInstance(
StandardContext.class, "javax.management.MBeanRegistration"));
}
@Test
public void testIsInstanceStandardContext08() {
Assert.assertTrue(IntrospectionUtils.isInstance(
StandardContext.class, "org.apache.catalina.Container"));
}
@Test
public void testIsInstanceStandardContext09() {
Assert.assertTrue(IntrospectionUtils.isInstance(
StandardContext.class, "org.apache.tomcat.ContextBind"));
}
@Test
public void testIsInstanceStandardContext10() {
Assert.assertTrue(IntrospectionUtils.isInstance(
StandardContext.class, "javax.management.NotificationEmitter"));
}
@Test
public void testIsInstanceStandardContext11() {
Assert.assertTrue(IntrospectionUtils.isInstance(
StandardContext.class, "javax.management.NotificationBroadcaster"));
}
// And one to check that non-matches return false
@Test
public void testIsInstanceStandardContext12() {
Assert.assertFalse(IntrospectionUtils.isInstance(
StandardContext.class, "com.example.Other"));
}
}

View File

@@ -0,0 +1,76 @@
/*
* 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.tomcat.util.bcel;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import org.junit.Assert;
import org.junit.Test;
import org.apache.tomcat.Jar;
import org.apache.tomcat.util.bcel.classfile.ClassParser;
import org.apache.tomcat.util.scan.JarFactory;
public class TesterPerformance {
private static final String JAR_LOCATION = "/tmp/jira-libs";
@Test
public void testClassParserPerformance() throws IOException {
File libDir = new File(JAR_LOCATION);
String[] libs = libDir.list();
Assert.assertNotNull(libs);
Set<URL> jarURLs = new HashSet<>();
for (String lib : libs) {
if (!lib.toLowerCase(Locale.ENGLISH).endsWith(".jar")) {
continue;
}
jarURLs.add(new URL("jar:" + new File (libDir, lib).toURI().toURL().toExternalForm() + "!/"));
}
long duration = 0;
for (URL jarURL : jarURLs) {
try (Jar jar = JarFactory.newInstance(jarURL)) {
jar.nextEntry();
String jarEntryName = jar.getEntryName();
while (jarEntryName != null) {
if (jarEntryName.endsWith(".class")) {
InputStream is = jar.getEntryInputStream();
long start = System.nanoTime();
ClassParser cp = new ClassParser(is);
cp.parse();
duration += System.nanoTime() - start;
}
jar.nextEntry();
jarEntryName = jar.getEntryName();
}
}
}
System.out.println("ClassParser performance test took: " + duration + " ns");
}
}

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.tomcat.util.buf;
import java.math.BigInteger;
import org.junit.Assert;
import org.junit.Test;
public class TestAscii {
@Test
public void testParseLong1() {
String value = "9223372036854775807"; // Long.MAX_VALUE
byte[] bytes = value.getBytes();
long result = Ascii.parseLong(bytes, 0, bytes.length);
Assert.assertEquals(value, String.valueOf(result));
}
@Test(expected = NumberFormatException.class)
public void testParseLong2() {
byte[] bytes = "9223372036854775808".getBytes(); // Long.MAX_VALUE + 1
long result = Ascii.parseLong(bytes, 0, bytes.length);
Assert.fail("NumberFormatException expected, got: " + result);
}
@Test(expected = NumberFormatException.class)
public void testParseLong3() {
byte[] bytes = "9223372036854775810".getBytes(); // Long.MAX_VALUE + 3
long result = Ascii.parseLong(bytes, 0, bytes.length);
Assert.fail("NumberFormatException expected, got: " + result);
}
@Test(expected = NumberFormatException.class)
public void testParseLong4() {
BigInteger x = BigInteger.valueOf(5000000000L).shiftLeft(32);
byte[] bytes = String.valueOf(x).getBytes();
long result = Ascii.parseLong(bytes, 0, bytes.length);
Assert.fail("NumberFormatException expected, got: " + result);
}
@Test
public void testParseLong5() {
String value = "9223372036854775806"; // Long.MAX_VALUE - 1
byte[] bytes = value.getBytes();
long result = Ascii.parseLong(bytes, 0, bytes.length);
Assert.assertEquals(value, String.valueOf(result));
}
}

View File

@@ -0,0 +1,143 @@
/*
* 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.tomcat.util.buf;
import java.nio.charset.Charset;
import java.nio.charset.MalformedInputException;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import org.junit.Assert;
import org.junit.Test;
public class TestB2CConverter {
private static final byte[] UTF16_MESSAGE =
new byte[] {-2, -1, 0, 65, 0, 66, 0, 67};
private static final byte[] UTF8_INVALID = new byte[] {-8, -69, -73, -77};
private static final byte[] UTF8_PARTIAL = new byte[] {-50};
@Test
public void testSingleMessage() throws Exception {
testMessages(1);
}
@Test
public void testTwoMessage() throws Exception {
testMessages(2);
}
@Test
public void testManyMessage() throws Exception {
testMessages(10);
}
private void testMessages(int msgCount) throws Exception {
B2CConverter conv = new B2CConverter(StandardCharsets.UTF_16);
ByteChunk bc = new ByteChunk();
CharChunk cc = new CharChunk(32);
for (int i = 0; i < msgCount; i++) {
bc.append(UTF16_MESSAGE, 0, UTF16_MESSAGE.length);
conv.convert(bc, cc, true);
Assert.assertEquals("ABC", cc.toString());
bc.recycle();
cc.recycle();
conv.recycle();
}
System.out.println(cc);
}
@Test
public void testLeftoverSize() {
float maxLeftover = 0;
String charsetName = "UNSET";
for (Charset charset : Charset.availableCharsets().values()) {
float leftover;
if (charset.name().toLowerCase(Locale.ENGLISH).startsWith("x-")) {
// Non-standard charset that browsers won't be using
// Likely something used internally by the JRE
continue;
}
try {
leftover = charset.newEncoder().maxBytesPerChar();
} catch (UnsupportedOperationException uoe) {
// Skip it
continue;
}
if (leftover > maxLeftover) {
maxLeftover = leftover;
charsetName = charset.name();
}
}
Assert.assertTrue("Limit needs to be at least " + maxLeftover +
" (used in charset '" + charsetName + "')",
maxLeftover <= B2CConverter.LEFTOVER_SIZE);
}
@Test(expected=MalformedInputException.class)
public void testBug54602a() throws Exception {
// Check invalid input is rejected straight away
B2CConverter conv = new B2CConverter(StandardCharsets.UTF_8);
ByteChunk bc = new ByteChunk();
CharChunk cc = new CharChunk();
bc.append(UTF8_INVALID, 0, UTF8_INVALID.length);
cc.allocate(bc.getLength(), -1);
conv.convert(bc, cc, false);
}
@Test(expected=MalformedInputException.class)
public void testBug54602b() throws Exception {
// Check partial input is rejected
B2CConverter conv = new B2CConverter(StandardCharsets.UTF_8);
ByteChunk bc = new ByteChunk();
CharChunk cc = new CharChunk();
bc.append(UTF8_PARTIAL, 0, UTF8_PARTIAL.length);
cc.allocate(bc.getLength(), -1);
conv.convert(bc, cc, true);
}
@Test
public void testBug54602c() throws Exception {
// Check partial input is rejected once it is known to be all available
B2CConverter conv = new B2CConverter(StandardCharsets.UTF_8);
ByteChunk bc = new ByteChunk();
CharChunk cc = new CharChunk();
bc.append(UTF8_PARTIAL, 0, UTF8_PARTIAL.length);
cc.allocate(bc.getLength(), -1);
conv.convert(bc, cc, false);
Exception e = null;
try {
conv.convert(bc, cc, true);
} catch (MalformedInputException mie) {
e = mie;
}
Assert.assertNotNull(e);
}
}

View File

@@ -0,0 +1,176 @@
/*
* 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.tomcat.util.buf;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.apache.tomcat.util.buf.ByteChunk.ByteOutputChannel;
/**
* Test cases for {@link ByteChunk}.
*/
public class TestByteChunk {
@Test
public void testConvertToBytes() throws UnsupportedEncodingException {
String string = "HTTP/1.1 100 \r\n\r\n";
byte[] bytes = ByteChunk.convertToBytes(string);
byte[] expected = string.getBytes("ISO-8859-1");
Assert.assertTrue(Arrays.equals(bytes, expected));
}
/*
* Test for {@code findByte} vs. {@code indexOf} methods difference.
*
* <p>
* As discussed in the "Re: r944918" thread on dev@, {@code
* ByteChunk.indexOf()} works for 0-127 ASCII chars only, and cannot find
* any chars outside of the range. {@code ByteChunk.findByte()} works for
* any ISO-8859-1 chars.
*/
@Test
public void testFindByte() throws UnsupportedEncodingException {
// 0xa0 = 160 = &nbsp; character
byte[] bytes = "Hello\u00a0world".getBytes("ISO-8859-1");
final int len = bytes.length;
// indexOf() does not work outside of 0-127
Assert.assertEquals(5, ByteChunk.findByte(bytes, 0, len, (byte) '\u00a0'));
Assert.assertEquals(-1, ByteChunk.indexOf(bytes, 0, len, '\u00a0'));
Assert.assertEquals(0, ByteChunk.findByte(bytes, 0, len, (byte) 'H'));
Assert.assertEquals(0, ByteChunk.indexOf(bytes, 0, len, 'H'));
Assert.assertEquals(len - 1, ByteChunk.findByte(bytes, 0, len, (byte) 'd'));
Assert.assertEquals(len - 1, ByteChunk.indexOf(bytes, 0, len, 'd'));
Assert.assertEquals(-1, ByteChunk.findByte(bytes, 0, len, (byte) 'x'));
Assert.assertEquals(-1, ByteChunk.indexOf(bytes, 0, len, 'x'));
Assert.assertEquals(7, ByteChunk.findByte(bytes, 5, len, (byte) 'o'));
Assert.assertEquals(7, ByteChunk.indexOf(bytes, 5, len, 'o'));
Assert.assertEquals(-1, ByteChunk.findByte(bytes, 2, 5, (byte) 'w'));
Assert.assertEquals(-1, ByteChunk.indexOf(bytes, 5, 5, 'w'));
}
@Test
public void testIndexOf_Char() throws UnsupportedEncodingException {
byte[] bytes = "Hello\u00a0world".getBytes("ISO-8859-1");
final int len = bytes.length;
ByteChunk bc = new ByteChunk();
bc.setBytes(bytes, 0, len);
Assert.assertEquals(0, bc.indexOf('H', 0));
Assert.assertEquals(6, bc.indexOf('w', 0));
// Does not work outside of 0-127
Assert.assertEquals(-1, bc.indexOf('\u00a0', 0));
bc.setBytes(bytes, 6, 5);
Assert.assertEquals(1, bc.indexOf('o', 0));
bc.setBytes(bytes, 6, 2);
Assert.assertEquals(0, bc.indexOf('w', 0));
Assert.assertEquals(-1, bc.indexOf('d', 0));
}
@Test
public void testIndexOf_String() throws UnsupportedEncodingException {
byte[] bytes = "Hello\u00a0world".getBytes("ISO-8859-1");
final int len = bytes.length;
ByteChunk bc = new ByteChunk();
bc.setBytes(bytes, 0, len);
Assert.assertEquals(0, bc.indexOf("Hello", 0, "Hello".length(), 0));
Assert.assertEquals(2, bc.indexOf("ll", 0, 2, 0));
Assert.assertEquals(2, bc.indexOf("Hello", 2, 2, 0));
Assert.assertEquals(7, bc.indexOf("o", 0, 1, 5));
// Does not work outside of 0-127
Assert.assertEquals(-1, bc.indexOf("\u00a0", 0, 1, 0));
bc.setBytes(bytes, 6, 5);
Assert.assertEquals(1, bc.indexOf("o", 0, 1, 0));
bc.setBytes(bytes, 6, 2);
Assert.assertEquals(0, bc.indexOf("wo", 0, 1, 0));
Assert.assertEquals(-1, bc.indexOf("d", 0, 1, 0));
}
@Test
public void testFindBytes() throws UnsupportedEncodingException {
byte[] bytes = "Hello\u00a0world".getBytes("ISO-8859-1");
final int len = bytes.length;
Assert.assertEquals(0, ByteChunk.findBytes(bytes, 0, len, new byte[] { 'H' }));
Assert.assertEquals(5, ByteChunk.findBytes(bytes, 0, len, new byte[] {
(byte) '\u00a0', 'x' }));
Assert.assertEquals(5, ByteChunk.findBytes(bytes, 0, len - 4, new byte[] {
'x', (byte) '\u00a0' }));
Assert.assertEquals(len - 1, ByteChunk.findBytes(bytes, 2, len, new byte[] {
'x', 'd' }));
Assert.assertEquals(1, ByteChunk.findBytes(bytes, 0, len, new byte[] { 'o',
'e' }));
Assert.assertEquals(-1, ByteChunk.findBytes(bytes, 2, 5, new byte[] { 'w' }));
}
@Ignore // Requires a 6GB heap (on markt's desktop - YMMV)
@Test
public void testAppend() throws Exception {
ByteChunk bc = new ByteChunk();
bc.setByteOutputChannel(new Sink());
// Defaults to no limit
byte data[] = new byte[32 * 1024 * 1024];
for (int i = 0; i < 100; i++) {
bc.append(data, 0, data.length);
}
Assert.assertEquals(AbstractChunk.ARRAY_MAX_SIZE, bc.getBuffer().length);
}
public class Sink implements ByteOutputChannel {
@Override
public void realWriteBytes(byte[] cbuf, int off, int len) throws IOException {
// NO-OP
}
@Override
public void realWriteBytes(ByteBuffer from) throws IOException {
// NO-OP
}
}
}

View File

@@ -0,0 +1,96 @@
/*
* 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.tomcat.util.buf;
import java.io.IOException;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.apache.tomcat.util.buf.CharChunk.CharOutputChannel;
/**
* Test cases for {@link CharChunk}.
*/
public class TestCharChunk {
@Test
public void testEndsWith() {
CharChunk cc = new CharChunk();
Assert.assertFalse(cc.endsWith("test"));
cc.setChars("xxtestxx".toCharArray(), 2, 4);
Assert.assertTrue(cc.endsWith(""));
Assert.assertTrue(cc.endsWith("t"));
Assert.assertTrue(cc.endsWith("st"));
Assert.assertTrue(cc.endsWith("test"));
Assert.assertFalse(cc.endsWith("x"));
Assert.assertFalse(cc.endsWith("xxtest"));
}
@Test
public void testIndexOf_String() {
char[] chars = "Hello\u00a0world".toCharArray();
final int len = chars.length;
CharChunk cc = new CharChunk();
cc.setChars(chars, 0, len);
Assert.assertEquals(0, cc.indexOf("Hello", 0, "Hello".length(), 0));
Assert.assertEquals(2, cc.indexOf("ll", 0, 2, 0));
Assert.assertEquals(2, cc.indexOf("Hello", 2, 2, 0));
Assert.assertEquals(7, cc.indexOf("o", 0, 1, 5));
// Does work outside of 0-127 (unlike ByteChunk)
Assert.assertEquals(5, cc.indexOf("\u00a0", 0, 1, 0));
cc.setChars(chars, 6, 5);
Assert.assertEquals(1, cc.indexOf("o", 0, 1, 0));
cc.setChars(chars, 6, 2);
Assert.assertEquals(0, cc.indexOf("wo", 0, 1, 0));
Assert.assertEquals(-1, cc.indexOf("d", 0, 1, 0));
}
@Ignore // Requires an 11GB heap (on markt's desktop - YMMV)
@Test
public void testAppend() throws Exception {
CharChunk cc = new CharChunk();
cc.setCharOutputChannel(new Sink());
// Defaults to no limit
char data[] = new char[32 * 1024 * 1024];
for (int i = 0; i < 100; i++) {
cc.append(data, 0, data.length);
}
Assert.assertEquals(AbstractChunk.ARRAY_MAX_SIZE, cc.getBuffer().length);
}
public class Sink implements CharOutputChannel {
@Override
public void realWriteChars(char[] cbuf, int off, int len) throws IOException {
// NO-OP
}
}
}

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.tomcat.util.buf;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import org.junit.Assert;
import org.junit.Test;
public class TestCharsetCache {
@Test
public void testAllKnownCharsets() {
Set<String> known = new HashSet<>();
known.addAll(Arrays.asList(CharsetCache.LAZY_CHARSETS));
Set<String> initial = new HashSet<>();
initial.addAll(Arrays.asList(CharsetCache.INITIAL_CHARSETS));
List<String> cacheMisses = new ArrayList<>();
for (Charset charset: Charset.availableCharsets().values()) {
String name = charset.name().toLowerCase(Locale.ENGLISH);
// No need to test the charsets that are pre-loaded
if (initial.contains(name)) {
continue;
}
if (!known.contains(name)) {
cacheMisses.add(name);
}
for (String alias : charset.aliases()) {
alias = alias.toLowerCase(Locale.ENGLISH);
if (!known.contains(alias)) {
cacheMisses.add(alias);
}
}
}
if (cacheMisses.size() != 0) {
StringBuilder sb = new StringBuilder();
Collections.sort(cacheMisses);
for (String name : cacheMisses) {
if (sb.length() == 0) {
sb.append('"');
} else {
sb.append(", \"");
}
sb.append(name.toLowerCase(Locale.ENGLISH));
sb.append('"');
}
System.out.println(sb.toString());
}
Assert.assertTrue(cacheMisses.size() == 0);
}
}

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.tomcat.util.buf;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import org.junit.Test;
public class TestCharsetCachePerformance {
@Test
public void testNoCsCache() throws Exception {
doTest(new NoCsCache());
}
@Test
public void testFullCsCache() throws Exception {
doTest(new FullCsCache());
}
@Test
public void testLazyCsCache() throws Exception {
doTest(new LazyCsCache());
}
private void doTest(CsCache cache) throws Exception {
int threadCount = 10;
int iterations = 10000000;
String[] lookupNames = new String[] {
"ISO-8859-1", "ISO-8859-2", "ISO-8859-3", "ISO-8859-4", "ISO-8859-5" };
Thread[] threads = new Thread[threadCount];
for (int i = 0; i < threadCount; i++) {
threads[i] = new TestCsCacheThread(iterations, cache, lookupNames);
}
long startTime = System.nanoTime();
for (int i = 0; i < threadCount; i++) {
threads[i].start();
}
for (int i = 0; i < threadCount; i++) {
threads[i].join();
}
long endTime = System.nanoTime();
System.out.println(cache.getClass().getName() + ": " + (endTime - startTime) + "ns");
}
private static interface CsCache {
Charset getCharset(String charsetName);
}
private static class NoCsCache implements CsCache {
@Override
public Charset getCharset(String charsetName) {
return Charset.forName(charsetName);
}
}
private static class FullCsCache implements CsCache {
private static final Map<String,Charset> cache = new HashMap<>();
static {
for (Charset charset: Charset.availableCharsets().values()) {
cache.put(charset.name().toLowerCase(Locale.ENGLISH), charset);
for (String alias : charset.aliases()) {
cache.put(alias.toLowerCase(Locale.ENGLISH), charset);
}
}
}
@Override
public Charset getCharset(String charsetName) {
return cache.get(charsetName.toLowerCase(Locale.ENGLISH));
}
}
private static class LazyCsCache implements CsCache {
private CharsetCache cache = new CharsetCache();
@Override
public Charset getCharset(String charsetName) {
return cache.getCharset(charsetName);
}
}
private static class TestCsCacheThread extends Thread {
private final int iterations;
private final CsCache cache;
private final String[] lookupNames;
private final int lookupNamesCount;
public TestCsCacheThread(int iterations, CsCache cache, String[] lookupNames) {
this.iterations = iterations;
this.cache = cache;
this.lookupNames = lookupNames;
this.lookupNamesCount = lookupNames.length;
}
@Override
public void run() {
for (int i = 0; i < iterations; i++) {
cache.getCharset(lookupNames[i % lookupNamesCount]);
}
}
}
}

View File

@@ -0,0 +1,71 @@
/*
* 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.tomcat.util.buf;
import java.nio.charset.StandardCharsets;
import org.junit.Assert;
import org.junit.Test;
/**
* Test cases for {@link HexUtils}.
*/
public class TestHexUtils {
private static final String TEST01_STRING = "Hello World";
private static final byte[] TEST01_BYTES = TEST01_STRING.getBytes(StandardCharsets.UTF_8);
private static final String TEST02_STRING = "foo";
private static final byte[] TEST02_BYTES = TEST02_STRING.getBytes(StandardCharsets.UTF_8);
@Test
public void testGetDec() {
Assert.assertEquals(0, HexUtils.getDec('0'));
Assert.assertEquals(9, HexUtils.getDec('9'));
Assert.assertEquals(10, HexUtils.getDec('a'));
Assert.assertEquals(15, HexUtils.getDec('f'));
Assert.assertEquals(10, HexUtils.getDec('A'));
Assert.assertEquals(15, HexUtils.getDec('F'));
Assert.assertEquals(-1, HexUtils.getDec(0));
Assert.assertEquals(-1, HexUtils.getDec('Z'));
Assert.assertEquals(-1, HexUtils.getDec(255));
Assert.assertEquals(-1, HexUtils.getDec(-60));
}
@Test
public void testRoundTrip01() {
Assert.assertArrayEquals(TEST01_STRING, TEST01_BYTES,
HexUtils.fromHexString(HexUtils.toHexString(TEST01_BYTES)));
}
@Test
public void testRoundTrip02() {
Assert.assertArrayEquals(TEST02_STRING, TEST02_BYTES,
HexUtils.fromHexString(HexUtils.toHexString(TEST02_BYTES)));
}
@Test(expected=IllegalArgumentException.class)
public void testFromHex01() {
HexUtils.fromHexString("Not a hex string");
}
@Test(expected=IllegalArgumentException.class)
public void testFromHex02() {
// Odd number of hex characters
HexUtils.fromHexString("aaa");
}
}

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.tomcat.util.buf;
import org.junit.Test;
public class TestMessageBytes {
@Test
public void testToStringFromNull() {
MessageBytes mb = MessageBytes.newInstance();
mb.toString();
}
@Test
public void testToBytesFromNull() {
MessageBytes mb = MessageBytes.newInstance();
mb.toBytes();
}
@Test
public void testToCharsFromNull() {
MessageBytes mb = MessageBytes.newInstance();
mb.toChars();
}
@Test
public void testToStringAfterRecycle() {
MessageBytes mb = MessageBytes.newInstance();
mb.setString("foo");
mb.recycle();
mb.toString();
}
@Test
public void testToBytesAfterRecycle() {
MessageBytes mb = MessageBytes.newInstance();
mb.setString("foo");
mb.recycle();
mb.toBytes();
}
@Test
public void testToCharsAfterRecycle() {
MessageBytes mb = MessageBytes.newInstance();
mb.setString("foo");
mb.recycle();
mb.toChars();
}
}

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.tomcat.util.buf;
import java.util.Collection;
import org.junit.Assert;
import org.junit.Test;
/*
* None of these tests should throw a NPE.
*/
public class TestStringUtils {
@Test
public void testNullArray() {
Assert.assertEquals("", StringUtils.join((String[]) null));
}
@Test
public void testNullArrayCharStringBuilder() {
StringBuilder sb = new StringBuilder();
StringUtils.join((String[]) null, ',', sb);
Assert.assertEquals("", sb.toString());
}
@Test
public void testNullCollection() {
Assert.assertEquals("", StringUtils.join((Collection<String>) null));
}
@Test
public void testNullCollectionChar() {
Assert.assertEquals("", StringUtils.join(null, ','));
}
@Test
public void testNullIterableCharStringBuilder() {
StringBuilder sb = new StringBuilder();
StringUtils.join((Iterable<String>) null, ',', sb);
Assert.assertEquals("", sb.toString());
}
@Test
public void testNullArrayCharFunctionStringBuilder() {
StringBuilder sb = new StringBuilder();
StringUtils.join((String[]) null, ',', null, sb);
Assert.assertEquals("", sb.toString());
}
@Test
public void testNullIterableCharFunctionStringBuilder() {
StringBuilder sb = new StringBuilder();
StringUtils.join((Iterable<String>) null, ',', null, sb);
Assert.assertEquals("", sb.toString());
}
}

View File

@@ -0,0 +1,102 @@
/*
* 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.tomcat.util.buf;
import java.nio.charset.StandardCharsets;
import org.junit.Assert;
import org.junit.Test;
public class TestUDecoder {
@Test
public void testURLDecodeStringInvalid() {
// %n rather than %nn should throw an IAE according to the Javadoc
Exception exception = null;
try {
UDecoder.URLDecode("%5xxxxx");
} catch (Exception e) {
exception = e;
}
Assert.assertTrue(exception instanceof IllegalArgumentException);
// Edge case trying to trigger ArrayIndexOutOfBoundsException
exception = null;
try {
UDecoder.URLDecode("%5");
} catch (Exception e) {
exception = e;
}
Assert.assertTrue(exception instanceof IllegalArgumentException);
}
@Test
public void testURLDecodeStringValidIso88591Start() {
String result = UDecoder.URLDecode("%41xxxx", StandardCharsets.ISO_8859_1);
Assert.assertEquals("Axxxx", result);
}
@Test
public void testURLDecodeStringValidIso88591Middle() {
String result = UDecoder.URLDecode("xx%41xx", StandardCharsets.ISO_8859_1);
Assert.assertEquals("xxAxx", result);
}
@Test
public void testURLDecodeStringValidIso88591End() {
String result = UDecoder.URLDecode("xxxx%41", StandardCharsets.ISO_8859_1);
Assert.assertEquals("xxxxA", result);
}
@Test
public void testURLDecodeStringValidUtf8Start() {
String result = UDecoder.URLDecode("%c3%aaxxxx", StandardCharsets.UTF_8);
Assert.assertEquals("\u00eaxxxx", result);
}
@Test
public void testURLDecodeStringValidUtf8Middle() {
String result = UDecoder.URLDecode("xx%c3%aaxx", StandardCharsets.UTF_8);
Assert.assertEquals("xx\u00eaxx", result);
}
@Test
public void testURLDecodeStringValidUtf8End() {
String result = UDecoder.URLDecode("xxxx%c3%aa", StandardCharsets.UTF_8);
Assert.assertEquals("xxxx\u00ea", result);
}
@Test
public void testURLDecodeStringNonAsciiValidNone() {
String result = UDecoder.URLDecode("\u00eaxxxx", StandardCharsets.UTF_8);
Assert.assertEquals("\u00eaxxxx", result);
}
@Test
public void testURLDecodeStringNonAsciiValidUtf8() {
String result = UDecoder.URLDecode("\u00ea%c3%aa", StandardCharsets.UTF_8);
Assert.assertEquals("\u00ea\u00ea", result);
}
}

View File

@@ -0,0 +1,46 @@
/*
* 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.tomcat.util.buf;
import java.io.IOException;
import org.junit.Assert;
import org.junit.Test;
import org.apache.tomcat.util.buf.UEncoder.SafeCharsSet;
/**
* Test cases for {@link UEncoder}.
*/
public class TestUEncoder {
@Test
public void testEncodeURLWithSlashInit() throws IOException {
UEncoder urlEncoder = new UEncoder(SafeCharsSet.WITH_SLASH);
String s = "a+b/c/d+e.class";
Assert.assertTrue(urlEncoder.encodeURL(s, 0, s.length()).equals(
"a%2bb/c/d%2be.class"));
Assert.assertTrue(urlEncoder.encodeURL(s, 2, s.length() - 2).equals(
"b/c/d%2be.cla"));
s = new String(new char[] { 0xD801, 0xDC01 });
Assert.assertTrue(urlEncoder.encodeURL(s, 0, s.length())
.equals("%f0%90%90%81"));
}
}

View File

@@ -0,0 +1,24 @@
/*
* 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.tomcat.util.buf;
public class TestUriUtil24 extends TesterUriUtilBase {
public TestUriUtil24() {
super("$");
}
}

View File

@@ -0,0 +1,24 @@
/*
* 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.tomcat.util.buf;
public class TestUriUtil26 extends TesterUriUtilBase {
public TestUriUtil26() {
super("&");
}
}

View File

@@ -0,0 +1,24 @@
/*
* 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.tomcat.util.buf;
public class TestUriUtil2A extends TesterUriUtilBase {
public TestUriUtil2A() {
super("*");
}
}

View File

@@ -0,0 +1,24 @@
/*
* 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.tomcat.util.buf;
public class TestUriUtil40 extends TesterUriUtilBase {
public TestUriUtil40() {
super("@");
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,136 @@
/*
* 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.tomcat.util.buf;
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.TomcatURLStreamHandlerFactory;
public abstract class TesterUriUtilBase {
private final String separator;
protected TesterUriUtilBase(String separator) {
this.separator = separator;
TomcatURLStreamHandlerFactory.register();
System.setProperty("org.apache.tomcat.util.buf.UriUtil.WAR_SEPARATOR", separator);
}
@Test
public void testBuildJarUrl01() throws MalformedURLException {
File jarFile = new File("/patha/pathb!/pathc");
String result = UriUtil.buildJarUrl(jarFile).toString();
int index = result.indexOf("!/");
Assert.assertEquals(result, result.length() - 2, index);
}
@Test
public void testBuildJarUrl02() throws MalformedURLException {
File jarFile = new File("/patha/pathb*/pathc");
String result = UriUtil.buildJarUrl(jarFile).toString();
int index = result.indexOf("!/");
Assert.assertEquals(result, result.length() - 2, index);
index = result.indexOf("*/");
Assert.assertEquals(result, -1, index);
}
@Test
public void testBuildJarUrl03() throws MalformedURLException {
File jarFile = new File("/patha/pathb^/pathc");
String result = UriUtil.buildJarUrl(jarFile).toString();
int index = result.indexOf("!/");
Assert.assertEquals(result, result.length() - 2, index);
index = result.indexOf("^/");
Assert.assertEquals(result, -1, index);
}
@Test
public void testBuildJarUrl04() throws MalformedURLException {
File jarFile = new File("/patha/pathb" + separator + "/pathc");
String result = UriUtil.buildJarUrl(jarFile).toString();
int index = result.indexOf("!/");
Assert.assertEquals(result, result.length() - 2, index);
index = result.indexOf(separator + "/");
Assert.assertEquals(result, -1, index);
}
@Test
public void testWarToJar01() throws MalformedURLException {
doTestWarToJar("^");
}
@Test
public void testWarToJar02() throws MalformedURLException {
doTestWarToJar("*");
}
@Test
public void testWarToJar03() throws MalformedURLException {
doTestWarToJar(separator);
}
private void doTestWarToJar(String separator) throws MalformedURLException {
URL warUrl = new URL("war:file:/external/path" + separator + "/internal/path");
URL jarUrl = UriUtil.warToJar(warUrl);
Assert.assertEquals("jar:file:/external/path!/internal/path", jarUrl.toString());
}
// @Test /* Uncomment to test performance for different implementations. */
public void performanceTestBuildJarUrl() throws MalformedURLException {
File jarFile = new File("/patha/pathb^/pathc");
URL url = null;
int count = 1000000;
// Warm up
for (int i = 0; i < count / 10; i++) {
url = UriUtil.buildJarUrl(jarFile);
}
// Test
long start = System.nanoTime();
for (int i = 0; i < count / 10; i++) {
url = UriUtil.buildJarUrl(jarFile);
}
long duration = System.nanoTime() - start;
System.out.println("[" + count + "] iterations took [" +
duration + "] ns for [" + url + "]");
}
}

View File

@@ -0,0 +1,215 @@
/*
* 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.tomcat.util.collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.junit.Assert;
import org.junit.Test;
public class TestCaseInsensitiveKeyMap {
@Test
public void testPut() {
Object o1 = new Object();
Object o2 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.put("a", o1);
Object o = map.put("A", o2);
Assert.assertEquals(o1, o);
Assert.assertEquals(o2, map.get("a"));
Assert.assertEquals(o2, map.get("A"));
}
@Test(expected=NullPointerException.class)
public void testPutNullKey() {
Object o1 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.put(null, o1);
}
@Test
public void testGet() {
Object o1 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.put("a", o1);
Assert.assertEquals(o1, map.get("a"));
Assert.assertEquals(o1, map.get("A"));
}
@Test
public void testGetNullKey() {
Object o1 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.put("a", o1);
Assert.assertNull(map.get(null));
}
@Test
public void testContainsKey() {
Object o1 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.put("a", o1);
Assert.assertTrue(map.containsKey("a"));
Assert.assertTrue(map.containsKey("A"));
}
@Test
public void testContainsKeyNonString() {
Object o1 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.put("a", o1);
Assert.assertFalse(map.containsKey(o1));
}
@Test
public void testContainsKeyNull() {
Object o1 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.put("a", o1);
Assert.assertFalse(map.containsKey(null));
}
@Test
public void testContainsValue() {
Object o1 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.put("a", o1);
Assert.assertTrue(map.containsValue(o1));
}
@Test
public void testRemove() {
Object o1 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.put("a", o1);
Assert.assertFalse(map.isEmpty());
map.remove("A");
Assert.assertTrue(map.isEmpty());
map.put("A", o1);
Assert.assertFalse(map.isEmpty());
map.remove("a");
Assert.assertTrue(map.isEmpty());
}
@Test
public void testClear() {
Object o1 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
for (int i = 0; i < 10; i++) {
map.put(Integer.toString(i), o1);
}
Assert.assertEquals(10, map.size());
map.clear();
Assert.assertEquals(0, map.size());
}
@Test
public void testPutAll() {
Object o1 = new Object();
Object o2 = new Object();
Map<String,Object> source = new HashMap<>();
source.put("a", o1);
source.put("A", o2);
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.putAll(source);
Assert.assertEquals(1, map.size());
Assert.assertTrue(map.containsValue(o1) != map.containsValue(o2));
}
@Test
public void testKeySetContains() {
Object o1 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.put("a", o1);
Set<String> keys = map.keySet();
Assert.assertTrue(keys.contains("a"));
Assert.assertTrue(keys.contains("A"));
}
@Test
public void testKeySetRemove() {
Object o1 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.put("a", o1);
Iterator<String> iter = map.keySet().iterator();
Assert.assertTrue(iter.hasNext());
iter.next();
iter.remove();
Assert.assertTrue(map.isEmpty());
}
@Test
public void testEntrySetRemove() {
Object o1 = new Object();
CaseInsensitiveKeyMap<Object> map = new CaseInsensitiveKeyMap<>();
map.put("a", o1);
Iterator<Entry<String,Object>> iter = map.entrySet().iterator();
Assert.assertTrue(iter.hasNext());
Entry<String,Object> entry = iter.next();
Assert.assertEquals("a", entry.getKey());
Assert.assertEquals(o1, entry.getValue());
iter.remove();
Assert.assertTrue(map.isEmpty());
}
}

View File

@@ -0,0 +1,117 @@
/*
* 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.tomcat.util.collections;
import org.junit.Assert;
import org.junit.Test;
public class TestSynchronizedQueue {
public void testPollEmpty() {
SynchronizedQueue<Object> queue = new SynchronizedQueue<>();
Assert.assertNull(queue.poll());
}
@Test
public void testOfferPollOrder() {
SynchronizedQueue<Object> queue = new SynchronizedQueue<>();
Object o1 = new Object();
Object o2 = new Object();
Object o3 = new Object();
Object o4 = new Object();
queue.offer(o1);
queue.offer(o2);
queue.offer(o3);
queue.offer(o4);
Assert.assertSame(queue.poll(), o1);
Assert.assertSame(queue.poll(), o2);
Assert.assertSame(queue.poll(), o3);
Assert.assertSame(queue.poll(), o4);
Assert.assertNull(queue.poll());
}
@Test
public void testExpandOfferPollOrder() {
SynchronizedQueue<Object> queue = new SynchronizedQueue<>();
Object o1 = new Object();
Object o2 = new Object();
Object o3 = new Object();
Object o4 = new Object();
for (int i = 0; i < 300; i++) {
queue.offer(o1);
queue.offer(o2);
queue.offer(o3);
queue.offer(o4);
}
for (int i = 0; i < 300; i++) {
Assert.assertSame(queue.poll(), o1);
Assert.assertSame(queue.poll(), o2);
Assert.assertSame(queue.poll(), o3);
Assert.assertSame(queue.poll(), o4);
}
Assert.assertNull(queue.poll());
}
@Test
public void testExpandOfferPollOrder2() {
SynchronizedQueue<Object> queue = new SynchronizedQueue<>();
Object o1 = new Object();
Object o2 = new Object();
Object o3 = new Object();
Object o4 = new Object();
for (int i = 0; i < 100; i++) {
queue.offer(o1);
queue.offer(o2);
queue.offer(o3);
queue.offer(o4);
}
for (int i = 0; i < 50; i++) {
Assert.assertSame(queue.poll(), o1);
Assert.assertSame(queue.poll(), o2);
Assert.assertSame(queue.poll(), o3);
Assert.assertSame(queue.poll(), o4);
}
for (int i = 0; i < 200; i++) {
queue.offer(o1);
queue.offer(o2);
queue.offer(o3);
queue.offer(o4);
}
for (int i = 0; i < 250; i++) {
Assert.assertSame(queue.poll(), o1);
Assert.assertSame(queue.poll(), o2);
Assert.assertSame(queue.poll(), o3);
Assert.assertSame(queue.poll(), o4);
}
Assert.assertNull(queue.poll());
}
}

View File

@@ -0,0 +1,119 @@
/*
* 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.tomcat.util.collections;
import org.junit.Assert;
import org.junit.Test;
public class TestSynchronizedStack {
@Test
public void testPopEmpty() {
SynchronizedStack<Object> stack = new SynchronizedStack<>();
Assert.assertNull(stack.pop());
}
@Test
public void testPushPopOrder() {
SynchronizedStack<Object> stack = new SynchronizedStack<>();
Object o1 = new Object();
Object o2 = new Object();
Object o3 = new Object();
Object o4 = new Object();
stack.push(o1);
stack.push(o2);
stack.push(o3);
stack.push(o4);
Assert.assertSame(stack.pop(), o4);
Assert.assertSame(stack.pop(), o3);
Assert.assertSame(stack.pop(), o2);
Assert.assertSame(stack.pop(), o1);
Assert.assertNull(stack.pop());
}
@Test
public void testExpandPushPopOrder() {
SynchronizedStack<Object> stack = new SynchronizedStack<>();
Object o1 = new Object();
Object o2 = new Object();
Object o3 = new Object();
Object o4 = new Object();
for (int i = 0; i < 300; i++) {
stack.push(o1);
stack.push(o2);
stack.push(o3);
stack.push(o4);
}
for (int i = 0; i < 300; i++) {
Assert.assertSame(stack.pop(), o4);
Assert.assertSame(stack.pop(), o3);
Assert.assertSame(stack.pop(), o2);
Assert.assertSame(stack.pop(), o1);
}
Assert.assertNull(stack.pop());
}
@Test
public void testLimit() {
SynchronizedStack<Object> stack = new SynchronizedStack<>(2,2);
Object o1 = new Object();
Object o2 = new Object();
Object o3 = new Object();
Object o4 = new Object();
stack.push(o1);
stack.push(o2);
stack.push(o3);
stack.push(o4);
Assert.assertSame(stack.pop(), o2);
Assert.assertSame(stack.pop(), o1);
Assert.assertNull(stack.pop());
}
@Test
public void testLimitExpand() {
SynchronizedStack<Object> stack = new SynchronizedStack<>(1,3);
Object o1 = new Object();
Object o2 = new Object();
Object o3 = new Object();
Object o4 = new Object();
stack.push(o1);
stack.push(o2);
stack.push(o3);
stack.push(o4);
Assert.assertSame(stack.pop(), o3);
Assert.assertSame(stack.pop(), o2);
Assert.assertSame(stack.pop(), o1);
Assert.assertNull(stack.pop());
}
}

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.tomcat.util.collections;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.junit.Test;
public class TesterPerformanceSynchronizedQueue {
private static final int THREAD_COUNT = 4;
private static final int ITERATIONS = 1000000;
private static final SynchronizedQueue<Object> S_QUEUE =
new SynchronizedQueue<>();
private static final Queue<Object> QUEUE = new ConcurrentLinkedQueue<>();
@Test
public void testSynchronizedQueue() throws InterruptedException {
Thread[] threads = new Thread[THREAD_COUNT];
for (int i = 0; i < THREAD_COUNT; i++) {
threads[i] = new StackThread();
}
long start = System.currentTimeMillis();
for (int i = 0; i < THREAD_COUNT; i++) {
threads[i].start();
}
for (int i = 0; i < THREAD_COUNT; i++) {
threads[i].join();
}
long end = System.currentTimeMillis();
System.out.println("SynchronizedQueue: " + (end - start) + "ms");
}
public static class StackThread extends Thread {
@Override
public void run() {
for(int i = 0; i < ITERATIONS; i++) {
Object obj = S_QUEUE.poll();
if (obj == null) {
obj = new Object();
}
S_QUEUE.offer(obj);
}
super.run();
}
}
@Test
public void testConcurrentQueue() throws InterruptedException {
Thread[] threads = new Thread[THREAD_COUNT];
for (int i = 0; i < THREAD_COUNT; i++) {
threads[i] = new QueueThread();
}
long start = System.currentTimeMillis();
for (int i = 0; i < THREAD_COUNT; i++) {
threads[i].start();
}
for (int i = 0; i < THREAD_COUNT; i++) {
threads[i].join();
}
long end = System.currentTimeMillis();
System.out.println("ConcurrentLinkedQueue: " + (end - start) + "ms");
}
public static class QueueThread extends Thread {
@Override
public void run() {
for(int i = 0; i < ITERATIONS; i++) {
Object obj = QUEUE.poll();
if (obj == null) {
obj = new Object();
}
QUEUE.offer(obj);
}
super.run();
}
}
}

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.tomcat.util.collections;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.junit.Test;
public class TesterPerformanceSynchronizedStack {
private static final int THREAD_COUNT = 4;
private static final int ITERATIONS = 1000000;
private static final SynchronizedStack<Object> STACK =
new SynchronizedStack<>();
private static final Queue<Object> QUEUE = new ConcurrentLinkedQueue<>();
@Test
public void testSynchronizedStack() throws InterruptedException {
Thread[] threads = new Thread[THREAD_COUNT];
for (int i = 0; i < THREAD_COUNT; i++) {
threads[i] = new StackThread();
}
long start = System.currentTimeMillis();
for (int i = 0; i < THREAD_COUNT; i++) {
threads[i].start();
}
for (int i = 0; i < THREAD_COUNT; i++) {
threads[i].join();
}
long end = System.currentTimeMillis();
System.out.println("SynchronizedStack: " + (end - start) + "ms");
}
public static class StackThread extends Thread {
@Override
public void run() {
for(int i = 0; i < ITERATIONS; i++) {
Object obj = STACK.pop();
if (obj == null) {
obj = new Object();
}
STACK.push(obj);
}
super.run();
}
}
@Test
public void testConcurrentQueue() throws InterruptedException {
Thread[] threads = new Thread[THREAD_COUNT];
for (int i = 0; i < THREAD_COUNT; i++) {
threads[i] = new QueueThread();
}
long start = System.currentTimeMillis();
for (int i = 0; i < THREAD_COUNT; i++) {
threads[i].start();
}
for (int i = 0; i < THREAD_COUNT; i++) {
threads[i].join();
}
long end = System.currentTimeMillis();
System.out.println("ConcurrentLinkedQueue: " + (end - start) + "ms");
}
public static class QueueThread extends Thread {
@Override
public void run() {
for(int i = 0; i < ITERATIONS; i++) {
Object obj = QUEUE.poll();
if (obj == null) {
obj = new Object();
}
QUEUE.offer(obj);
}
super.run();
}
}
}

View File

@@ -0,0 +1,125 @@
/*
* 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.tomcat.util.descriptor;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class TestLocalResolver {
private final Map<String, String> publicIds = new HashMap<>();
private final Map<String, String> systemIds = new HashMap<>();
private LocalResolver resolver = new LocalResolver(publicIds, systemIds, true);
private String WEB_22_LOCAL;
private String WEB_31_LOCAL;
private String WEBCOMMON_31_LOCAL;
@Before
public void init() {
WEB_22_LOCAL = urlFor("resources/web-app_2_2.dtd");
WEB_31_LOCAL = urlFor("resources/web-app_3_1.xsd");
WEBCOMMON_31_LOCAL = urlFor("resources/web-common_3_1.xsd");
publicIds.put(XmlIdentifiers.WEB_22_PUBLIC, WEB_22_LOCAL);
systemIds.put(XmlIdentifiers.WEB_31_XSD, WEB_31_LOCAL);
systemIds.put(WEBCOMMON_31_LOCAL, WEBCOMMON_31_LOCAL);
}
public String urlFor(String id) {
return ServletContext.class.getResource(id).toExternalForm();
}
@Test(expected = FileNotFoundException.class)
public void unknownNullId() throws IOException, SAXException {
Assert.assertNull(resolver.resolveEntity(null, null));
}
@Test(expected = FileNotFoundException.class)
public void unknownPublicId() throws IOException, SAXException {
Assert.assertNull(resolver.resolveEntity("unknown", null));
}
@Test(expected = FileNotFoundException.class)
public void unknownSystemId() throws IOException, SAXException {
InputSource source = resolver.resolveEntity(null, "unknown");
Assert.assertEquals(null, source.getPublicId());
Assert.assertEquals("unknown", source.getSystemId());
}
@Test(expected = FileNotFoundException.class)
public void unknownRelativeSystemId()
throws IOException, SAXException {
InputSource source = resolver.resolveEntity(
null, null, "http://example.com/home.html", "unknown");
Assert.assertEquals(null, source.getPublicId());
Assert.assertEquals("http://example.com/unknown", source.getSystemId());
}
@Test
public void publicIdIsResolved() throws IOException, SAXException {
InputSource source = resolver.resolveEntity(
XmlIdentifiers.WEB_22_PUBLIC, XmlIdentifiers.WEB_22_SYSTEM);
Assert.assertEquals(XmlIdentifiers.WEB_22_PUBLIC, source.getPublicId());
Assert.assertEquals(WEB_22_LOCAL, source.getSystemId());
}
@Test
public void systemIdIsIgnoredWhenPublicIdIsResolved()
throws IOException, SAXException {
InputSource source = resolver.resolveEntity(
XmlIdentifiers.WEB_22_PUBLIC, "unknown");
Assert.assertEquals(XmlIdentifiers.WEB_22_PUBLIC, source.getPublicId());
Assert.assertEquals(WEB_22_LOCAL, source.getSystemId());
}
@Test
public void systemIdIsResolved() throws IOException, SAXException {
InputSource source =
resolver.resolveEntity(null, XmlIdentifiers.WEB_31_XSD);
Assert.assertEquals(null, source.getPublicId());
Assert.assertEquals(WEB_31_LOCAL, source.getSystemId());
}
@Test
public void relativeSystemIdIsResolvedAgainstBaseURI()
throws IOException, SAXException {
InputSource source = resolver.resolveEntity(
null, null, WEB_31_LOCAL, "web-common_3_1.xsd");
Assert.assertEquals(null, source.getPublicId());
Assert.assertEquals(WEBCOMMON_31_LOCAL, source.getSystemId());
}
@Test
public void absoluteSystemIdOverridesBaseURI()
throws IOException, SAXException {
InputSource source = resolver.resolveEntity(null, null,
"http://example.com/home.html", XmlIdentifiers.WEB_31_XSD);
Assert.assertEquals(null, source.getPublicId());
Assert.assertEquals(WEB_31_LOCAL, source.getSystemId());
}
}

View File

@@ -0,0 +1,59 @@
/*
* 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.tomcat.util.descriptor.tld;
import java.io.File;
import java.io.IOException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class TestImplicitTldParser {
private TldParser parser;
@Before
public void init() {
parser = new TldParser(true, true, new ImplicitTldRuleSet(), true);
}
@Test
public void testImplicitTldGood() throws Exception {
TaglibXml xml = parse("test/tld/implicit-good.tld");
Assert.assertEquals("1.0", xml.getTlibVersion());
Assert.assertEquals("2.1", xml.getJspVersion());
Assert.assertEquals("Ignored", xml.getShortName());
}
@Test(expected=SAXParseException.class)
public void testImplicitTldBad() throws Exception {
TaglibXml xml = parse("test/tld/implicit-bad.tld");
Assert.assertEquals("1.0", xml.getTlibVersion());
Assert.assertEquals("2.1", xml.getJspVersion());
Assert.assertEquals("Ignored", xml.getShortName());
}
private TaglibXml parse(String pathname) throws IOException, SAXException {
File file = new File(pathname);
TldResourcePath path = new TldResourcePath(file.toURI().toURL(), null);
return parser.parse(path);
}
}

View File

@@ -0,0 +1,173 @@
/*
* 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.tomcat.util.descriptor.tld;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.servlet.jsp.tagext.FunctionInfo;
import javax.servlet.jsp.tagext.TagAttributeInfo;
import javax.servlet.jsp.tagext.TagVariableInfo;
import javax.servlet.jsp.tagext.VariableInfo;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.xml.sax.SAXException;
public class TestTldParser {
private TldParser parser;
@Before
public void init() {
parser = new TldParser(true, true, new TldRuleSet(), true);
}
@Test
public void testTld() throws Exception {
TaglibXml xml = parse("test/tld/test.tld");
Assert.assertEquals("1.0", xml.getTlibVersion());
Assert.assertEquals("2.1", xml.getJspVersion());
Assert.assertEquals("test", xml.getShortName());
Assert.assertEquals("http://tomcat.apache.org/TldTests", xml.getUri());
Assert.assertEquals(1, xml.getFunctions().size());
ValidatorXml validator = xml.getValidator();
Assert.assertEquals("com.example.Validator", validator.getValidatorClass());
Assert.assertEquals(1, validator.getInitParams().size());
Assert.assertEquals("value", validator.getInitParams().get("name"));
Assert.assertEquals(1, xml.getTags().size());
TagXml tag = xml.getTags().get(0);
Assert.assertEquals("org.apache.jasper.compiler.TestValidator$Echo", tag.getTagClass());
Assert.assertEquals("empty", tag.getBodyContent());
Assert.assertTrue(tag.hasDynamicAttributes());
Assert.assertEquals(1, tag.getVariables().size());
TagVariableInfo variableInfo = tag.getVariables().get(0);
Assert.assertEquals("var", variableInfo.getNameGiven());
Assert.assertEquals("java.lang.Object", variableInfo.getClassName());
Assert.assertTrue(variableInfo.getDeclare());
Assert.assertEquals(VariableInfo.AT_END, variableInfo.getScope());
Assert.assertEquals(4, tag.getAttributes().size());
TagAttributeInfo attributeInfo = tag.getAttributes().get(0);
Assert.assertEquals("Echo Tag", tag.getInfo());
Assert.assertEquals("Echo", tag.getDisplayName());
Assert.assertEquals("small", tag.getSmallIcon());
Assert.assertEquals("large", tag.getLargeIcon());
Assert.assertEquals("echo", attributeInfo.getName());
Assert.assertTrue(attributeInfo.isRequired());
Assert.assertTrue(attributeInfo.canBeRequestTime());
attributeInfo = tag.getAttributes().get(1);
Assert.assertEquals("fragment", attributeInfo.getName());
Assert.assertTrue(attributeInfo.isFragment());
Assert.assertTrue(attributeInfo.canBeRequestTime());
Assert.assertEquals("javax.servlet.jsp.tagext.JspFragment", attributeInfo.getTypeName());
attributeInfo = tag.getAttributes().get(2);
Assert.assertEquals("deferredValue", attributeInfo.getName());
Assert.assertEquals("javax.el.ValueExpression", attributeInfo.getTypeName());
Assert.assertEquals("java.util.Date", attributeInfo.getExpectedTypeName());
attributeInfo = tag.getAttributes().get(3);
Assert.assertEquals("deferredMethod", attributeInfo.getName());
Assert.assertEquals("javax.el.MethodExpression", attributeInfo.getTypeName());
Assert.assertEquals("java.util.Date getDate()", attributeInfo.getMethodSignature());
Assert.assertEquals(1, xml.getTagFiles().size());
TagFileXml tagFile = xml.getTagFiles().get(0);
Assert.assertEquals("Echo", tag.getDisplayName());
Assert.assertEquals("small", tag.getSmallIcon());
Assert.assertEquals("large", tag.getLargeIcon());
Assert.assertEquals("Echo2", tagFile.getName());
Assert.assertEquals("/echo.tag", tagFile.getPath());
Assert.assertEquals(1, xml.getFunctions().size());
FunctionInfo fn = xml.getFunctions().get(0);
Assert.assertEquals("trim", fn.getName());
Assert.assertEquals("org.apache.el.TesterFunctions", fn.getFunctionClass());
Assert.assertEquals("java.lang.String trim(java.lang.String)", fn.getFunctionSignature());
}
@Test
public void testParseTld21() throws Exception {
TaglibXml xml = parse("test/tld/tags21.tld");
Assert.assertEquals("1.0", xml.getTlibVersion());
Assert.assertEquals("2.1", xml.getJspVersion());
Assert.assertEquals("Tags21", xml.getShortName());
Assert.assertEquals("http://tomcat.apache.org/tags21", xml.getUri());
verifyTags(xml.getTags());
}
@Test
public void testParseTld20() throws Exception {
TaglibXml xml = parse("test/tld/tags20.tld");
Assert.assertEquals("1.0", xml.getTlibVersion());
Assert.assertEquals("2.0", xml.getJspVersion());
Assert.assertEquals("Tags20", xml.getShortName());
Assert.assertEquals("http://tomcat.apache.org/tags20", xml.getUri());
verifyTags(xml.getTags());
}
@Test
public void testParseTld12() throws Exception {
TaglibXml xml = parse("test/tld/tags12.tld");
Assert.assertEquals("1.0", xml.getTlibVersion());
Assert.assertEquals("1.2", xml.getJspVersion());
Assert.assertEquals("Tags12", xml.getShortName());
Assert.assertEquals("http://tomcat.apache.org/tags12", xml.getUri());
verifyTags(xml.getTags());
}
@Test
public void testParseTld11() throws Exception {
TaglibXml xml = parse("test/tld/tags11.tld");
Assert.assertEquals("1.0", xml.getTlibVersion());
Assert.assertEquals("1.1", xml.getJspVersion());
Assert.assertEquals("Tags11", xml.getShortName());
Assert.assertEquals("http://tomcat.apache.org/tags11", xml.getUri());
verifyTags(xml.getTags());
}
private void verifyTags(List<TagXml> tags) {
Assert.assertEquals(1, tags.size());
TagXml tag = tags.get(0);
Assert.assertEquals("Echo", tag.getName());
Assert.assertEquals("org.apache.jasper.compiler.TestValidator$Echo", tag.getTagClass());
Assert.assertEquals("empty", tag.getBodyContent());
}
@Test
public void testListener() throws Exception {
TaglibXml xml = parse("test/tld/listener.tld");
Assert.assertEquals("1.0", xml.getTlibVersion());
List<String> listeners = xml.getListeners();
Assert.assertEquals(1, listeners.size());
Assert.assertEquals("org.apache.catalina.core.TesterTldListener", listeners.get(0));
}
private TaglibXml parse(String pathname) throws IOException, SAXException {
File file = new File(pathname);
TldResourcePath path = new TldResourcePath(file.toURI().toURL(), null);
return parser.parse(path);
}
}

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.tomcat.util.descriptor.web;
import org.junit.Assert;
import org.junit.Test;
/**
* Test case for {@link FilterDef}.
*/
public class TestFilterDef {
@Test(expected = IllegalArgumentException.class)
public void testSetFilterNameNull() {
new FilterDef().setFilterName(null);
}
@Test(expected = IllegalArgumentException.class)
public void testSetFilterNameEmptyString() {
new FilterDef().setFilterName("");
}
@Test
public void testSetFilterName() {
FilterDef filterDef = new FilterDef();
filterDef.setFilterName("test");
Assert.assertEquals("'test' is expected as filter name",
"test", filterDef.getFilterName());
}
}

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.tomcat.util.descriptor.web;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.servlet.descriptor.JspConfigDescriptor;
import javax.servlet.descriptor.JspPropertyGroupDescriptor;
import javax.servlet.descriptor.TaglibDescriptor;
import org.junit.Assert;
import org.junit.Test;
public class TestJspConfigDescriptorImpl {
@Test
public void testTaglibsAreIsolate() {
List<TaglibDescriptor> taglibs = new ArrayList<>();
taglibs.add(new TaglibDescriptorImpl("location", "uri"));
List<JspPropertyGroupDescriptor> propertyGroups = Collections.emptyList();
JspConfigDescriptor descriptor = new JspConfigDescriptorImpl(propertyGroups, taglibs);
descriptor.getTaglibs().clear();
Assert.assertEquals(taglibs, descriptor.getTaglibs());
}
@Test
public void testPropertyGroupsAreIsolate() {
List<TaglibDescriptor> taglibs = Collections.emptyList();
List<JspPropertyGroupDescriptor> propertyGroups = new ArrayList<>();
propertyGroups.add(new JspPropertyGroupDescriptorImpl(new JspPropertyGroup()));
JspConfigDescriptor descriptor = new JspConfigDescriptorImpl(propertyGroups, taglibs);
descriptor.getJspPropertyGroups().clear();
Assert.assertEquals(propertyGroups, descriptor.getJspPropertyGroups());
}
}

View File

@@ -0,0 +1,35 @@
/*
* 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.tomcat.util.descriptor.web;
import org.junit.Assert;
import org.junit.Test;
public class TestJspPropertyGroup {
private JspPropertyGroup group = new JspPropertyGroup();
@Test
public void testBug55262() {
group.addIncludePrelude("/prelude");
group.addIncludePrelude("/prelude");
group.addIncludeCoda("/coda");
group.addIncludeCoda("/coda");
Assert.assertEquals(2, group.getIncludePreludes().size());
Assert.assertEquals(2, group.getIncludeCodas().size());
}
}

View File

@@ -0,0 +1,50 @@
/*
* 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.tomcat.util.descriptor.web;
import org.junit.Assert;
import org.junit.Test;
public class TestJspPropertyGroupDescriptorImpl {
@Test
public void testPreludesAreIsolated() {
JspPropertyGroup jpg = new JspPropertyGroup();
jpg.addIncludePrelude("prelude");
JspPropertyGroupDescriptorImpl descriptor = new JspPropertyGroupDescriptorImpl(jpg);
descriptor.getIncludePreludes().clear();
Assert.assertEquals(1, descriptor.getIncludePreludes().size());
}
@Test
public void testCodasAreIsolated() {
JspPropertyGroup jpg = new JspPropertyGroup();
jpg.addIncludeCoda("coda");
JspPropertyGroupDescriptorImpl descriptor = new JspPropertyGroupDescriptorImpl(jpg);
descriptor.getIncludeCodas().clear();
Assert.assertEquals(1, descriptor.getIncludeCodas().size());
}
@Test
public void testUrlPatternsAreIsolated() {
JspPropertyGroup jpg = new JspPropertyGroup();
jpg.addUrlPatternDecoded("pattern");
JspPropertyGroupDescriptorImpl descriptor = new JspPropertyGroupDescriptorImpl(jpg);
descriptor.getUrlPatterns().clear();
Assert.assertEquals(1, descriptor.getUrlPatterns().size());
}
}

View File

@@ -0,0 +1,453 @@
/*
* 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.tomcat.util.descriptor.web;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.HttpConstraintElement;
import javax.servlet.HttpMethodConstraintElement;
import javax.servlet.ServletSecurityElement;
import javax.servlet.annotation.ServletSecurity;
import javax.servlet.annotation.ServletSecurity.EmptyRoleSemantic;
import org.junit.Assert;
import org.junit.Test;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
public class TestSecurityConstraint {
private static final String URL_PATTERN = "/test";
private static final String ROLE1 = "R1";
private static final Log DUMMY_LOG = LogFactory.getLog("DUMMY");
private static final SecurityConstraint GET_ONLY;
private static final SecurityConstraint POST_ONLY;
private static final SecurityConstraint GET_OMIT;
private static final SecurityConstraint POST_OMIT;
static {
// Configure the constraints to use in the tests
GET_ONLY = new SecurityConstraint();
GET_ONLY.addAuthRole(ROLE1);
SecurityCollection scGetOnly = new SecurityCollection();
scGetOnly.addMethod("GET");
scGetOnly.addPatternDecoded(URL_PATTERN);
scGetOnly.setName("GET-ONLY");
GET_ONLY.addCollection(scGetOnly);
POST_ONLY = new SecurityConstraint();
POST_ONLY.addAuthRole(ROLE1);
SecurityCollection scPostOnly = new SecurityCollection();
scPostOnly.addMethod("POST");
scPostOnly.addPatternDecoded(URL_PATTERN);
scPostOnly.setName("POST_ONLY");
POST_ONLY.addCollection(scPostOnly);
GET_OMIT = new SecurityConstraint();
GET_OMIT.addAuthRole(ROLE1);
SecurityCollection scGetOmit = new SecurityCollection();
scGetOmit.addOmittedMethod("GET");
scGetOmit.addPatternDecoded(URL_PATTERN);
scGetOmit.setName("GET_OMIT");
GET_OMIT.addCollection(scGetOmit);
POST_OMIT = new SecurityConstraint();
POST_OMIT.addAuthRole(ROLE1);
SecurityCollection scPostOmit = new SecurityCollection();
scPostOmit.addOmittedMethod("POST");
scPostOmit.addPatternDecoded(URL_PATTERN);
scPostOmit.setName("POST_OMIT");
POST_OMIT.addCollection(scPostOmit);
}
/**
* Uses the examples in SRV.13.4 as the basis for these tests
*/
@Test
public void testCreateConstraints() {
ServletSecurityElement element;
SecurityConstraint[] result;
Set<HttpMethodConstraintElement> hmces = new HashSet<>();
// Example 13-1
// @ServletSecurity
element = new ServletSecurityElement();
result = SecurityConstraint.createConstraints(element, URL_PATTERN);
Assert.assertEquals(0, result.length);
// Example 13-2
// @ServletSecurity(
// @HttpConstraint(
// transportGuarantee = TransportGuarantee.CONFIDENTIAL))
element = new ServletSecurityElement(
new HttpConstraintElement(
ServletSecurity.TransportGuarantee.CONFIDENTIAL));
result = SecurityConstraint.createConstraints(element, URL_PATTERN);
Assert.assertEquals(1, result.length);
Assert.assertFalse(result[0].getAuthConstraint());
Assert.assertTrue(result[0].findCollections()[0].findPattern(URL_PATTERN));
Assert.assertEquals(0, result[0].findCollections()[0].findMethods().length);
Assert.assertEquals(ServletSecurity.TransportGuarantee.CONFIDENTIAL.name(),
result[0].getUserConstraint());
// Example 13-3
// @ServletSecurity(@HttpConstraint(EmptyRoleSemantic.DENY))
element = new ServletSecurityElement(
new HttpConstraintElement(EmptyRoleSemantic.DENY));
result = SecurityConstraint.createConstraints(element, URL_PATTERN);
Assert.assertEquals(1, result.length);
Assert.assertTrue(result[0].getAuthConstraint());
Assert.assertTrue(result[0].findCollections()[0].findPattern(URL_PATTERN));
Assert.assertEquals(0, result[0].findCollections()[0].findMethods().length);
Assert.assertEquals(ServletSecurity.TransportGuarantee.NONE.name(),
result[0].getUserConstraint());
// Example 13-4
// @ServletSecurity(@HttpConstraint(rolesAllowed = "R1"))
element = new ServletSecurityElement(new HttpConstraintElement(
ServletSecurity.TransportGuarantee.NONE, ROLE1));
result = SecurityConstraint.createConstraints(element, URL_PATTERN);
Assert.assertEquals(1, result.length);
Assert.assertTrue(result[0].getAuthConstraint());
Assert.assertEquals(1, result[0].findAuthRoles().length);
Assert.assertTrue(result[0].findAuthRole(ROLE1));
Assert.assertTrue(result[0].findCollections()[0].findPattern(URL_PATTERN));
Assert.assertEquals(0, result[0].findCollections()[0].findMethods().length);
Assert.assertEquals(ServletSecurity.TransportGuarantee.NONE.name(),
result[0].getUserConstraint());
// Example 13-5
// @ServletSecurity((httpMethodConstraints = {
// @HttpMethodConstraint(value = "GET", rolesAllowed = "R1"),
// @HttpMethodConstraint(value = "POST", rolesAllowed = "R1",
// transportGuarantee = TransportGuarantee.CONFIDENTIAL)
// })
hmces.clear();
hmces.add(new HttpMethodConstraintElement("GET",
new HttpConstraintElement(
ServletSecurity.TransportGuarantee.NONE, ROLE1)));
hmces.add(new HttpMethodConstraintElement("POST",
new HttpConstraintElement(
ServletSecurity.TransportGuarantee.CONFIDENTIAL,
ROLE1)));
element = new ServletSecurityElement(hmces);
result = SecurityConstraint.createConstraints(element, URL_PATTERN);
Assert.assertEquals(2, result.length);
for (int i = 0; i < 2; i++) {
Assert.assertTrue(result[i].getAuthConstraint());
Assert.assertEquals(1, result[i].findAuthRoles().length);
Assert.assertTrue(result[i].findAuthRole(ROLE1));
Assert.assertTrue(result[i].findCollections()[0].findPattern(URL_PATTERN));
Assert.assertEquals(1, result[i].findCollections()[0].findMethods().length);
String method = result[i].findCollections()[0].findMethods()[0];
if ("GET".equals(method)) {
Assert.assertEquals(ServletSecurity.TransportGuarantee.NONE.name(),
result[i].getUserConstraint());
} else if ("POST".equals(method)) {
Assert.assertEquals(ServletSecurity.TransportGuarantee.CONFIDENTIAL.name(),
result[i].getUserConstraint());
} else {
Assert.fail("Unexpected method :[" + method + "]");
}
}
// Example 13-6
// @ServletSecurity(value = @HttpConstraint(rolesAllowed = "R1"),
// httpMethodConstraints = @HttpMethodConstraint("GET"))
hmces.clear();
hmces.add(new HttpMethodConstraintElement("GET"));
element = new ServletSecurityElement(
new HttpConstraintElement(
ServletSecurity.TransportGuarantee.NONE,
ROLE1),
hmces);
result = SecurityConstraint.createConstraints(element, URL_PATTERN);
Assert.assertEquals(2, result.length);
for (int i = 0; i < 2; i++) {
Assert.assertTrue(result[i].findCollections()[0].findPattern(URL_PATTERN));
if (result[i].findCollections()[0].findMethods().length == 1) {
Assert.assertEquals("GET",
result[i].findCollections()[0].findMethods()[0]);
Assert.assertFalse(result[i].getAuthConstraint());
} else if (result[i].findCollections()[0].findOmittedMethods().length == 1) {
Assert.assertEquals("GET",
result[i].findCollections()[0].findOmittedMethods()[0]);
Assert.assertTrue(result[i].getAuthConstraint());
Assert.assertEquals(1, result[i].findAuthRoles().length);
Assert.assertEquals(ROLE1, result[i].findAuthRoles()[0]);
} else {
Assert.fail("Unexpected number of methods defined");
}
Assert.assertEquals(ServletSecurity.TransportGuarantee.NONE.name(),
result[i].getUserConstraint());
}
// Example 13-7
// @ServletSecurity(value = @HttpConstraint(rolesAllowed = "R1"),
// httpMethodConstraints = @HttpMethodConstraint(value="TRACE",
// emptyRoleSemantic = EmptyRoleSemantic.DENY))
hmces.clear();
hmces.add(new HttpMethodConstraintElement("TRACE",
new HttpConstraintElement(EmptyRoleSemantic.DENY)));
element = new ServletSecurityElement(
new HttpConstraintElement(
ServletSecurity.TransportGuarantee.NONE,
ROLE1),
hmces);
result = SecurityConstraint.createConstraints(element, URL_PATTERN);
Assert.assertEquals(2, result.length);
for (int i = 0; i < 2; i++) {
Assert.assertTrue(result[i].findCollections()[0].findPattern(URL_PATTERN));
if (result[i].findCollections()[0].findMethods().length == 1) {
Assert.assertEquals("TRACE",
result[i].findCollections()[0].findMethods()[0]);
Assert.assertTrue(result[i].getAuthConstraint());
Assert.assertEquals(0, result[i].findAuthRoles().length);
} else if (result[i].findCollections()[0].findOmittedMethods().length == 1) {
Assert.assertEquals("TRACE",
result[i].findCollections()[0].findOmittedMethods()[0]);
Assert.assertTrue(result[i].getAuthConstraint());
Assert.assertEquals(1, result[i].findAuthRoles().length);
Assert.assertEquals(ROLE1, result[i].findAuthRoles()[0]);
} else {
Assert.fail("Unexpected number of methods defined");
}
Assert.assertEquals(ServletSecurity.TransportGuarantee.NONE.name(),
result[i].getUserConstraint());
}
// Example 13-8 is the same as 13-4
// Example 13-9 is the same as 13-7
}
@Test
public void testFindUncoveredHttpMethods01() {
// No new constraints if denyUncoveredHttpMethods is false
SecurityConstraint[] result =
SecurityConstraint.findUncoveredHttpMethods(
new SecurityConstraint[] {GET_ONLY}, false, DUMMY_LOG);
Assert.assertEquals(0, result.length);
}
@Test
public void testFindUncoveredHttpMethods02() {
// No new constraints if denyUncoveredHttpMethods is false
SecurityConstraint[] result =
SecurityConstraint.findUncoveredHttpMethods(
new SecurityConstraint[] {GET_OMIT}, false, DUMMY_LOG);
Assert.assertEquals(0, result.length);
}
@Test
public void testFindUncoveredHttpMethods03() {
// No new constraints if denyUncoveredHttpMethods is false
SecurityConstraint[] result =
SecurityConstraint.findUncoveredHttpMethods(
new SecurityConstraint[] {POST_ONLY}, false, DUMMY_LOG);
Assert.assertEquals(0, result.length);
}
@Test
public void testFindUncoveredHttpMethods04() {
// No new constraints if denyUncoveredHttpMethods is false
SecurityConstraint[] result =
SecurityConstraint.findUncoveredHttpMethods(
new SecurityConstraint[] {POST_OMIT}, false, DUMMY_LOG);
Assert.assertEquals(0, result.length);
}
@Test
public void testFindUncoveredHttpMethods05() {
SecurityConstraint[] result =
SecurityConstraint.findUncoveredHttpMethods(
new SecurityConstraint[] {GET_ONLY}, true, DUMMY_LOG);
Assert.assertEquals(1, result.length);
// Should be a deny constraint
Assert.assertTrue(result[0].getAuthConstraint());
// Should have a single collection
Assert.assertEquals(1, result[0].findCollections().length);
SecurityCollection sc = result[0].findCollections()[0];
// Should list GET as an omitted method
Assert.assertEquals(0, sc.findMethods().length);
Assert.assertEquals(1, sc.findOmittedMethods().length);
Assert.assertEquals("GET", sc.findOmittedMethods()[0]);
}
@Test
public void testFindUncoveredHttpMethods06() {
SecurityConstraint[] result =
SecurityConstraint.findUncoveredHttpMethods(
new SecurityConstraint[] {POST_ONLY}, true, DUMMY_LOG);
Assert.assertEquals(1, result.length);
// Should be a deny constraint
Assert.assertTrue(result[0].getAuthConstraint());
// Should have a single collection
Assert.assertEquals(1, result[0].findCollections().length);
SecurityCollection sc = result[0].findCollections()[0];
// Should list POST as an omitted method
Assert.assertEquals(0, sc.findMethods().length);
Assert.assertEquals(1, sc.findOmittedMethods().length);
Assert.assertEquals("POST", sc.findOmittedMethods()[0]);
}
@Test
public void testFindUncoveredHttpMethods07() {
SecurityConstraint[] result =
SecurityConstraint.findUncoveredHttpMethods(
new SecurityConstraint[] {GET_OMIT}, true, DUMMY_LOG);
Assert.assertEquals(1, result.length);
// Should be a deny constraint
Assert.assertTrue(result[0].getAuthConstraint());
// Should have a single collection
Assert.assertEquals(1, result[0].findCollections().length);
SecurityCollection sc = result[0].findCollections()[0];
// Should list GET as an method
Assert.assertEquals(0, sc.findOmittedMethods().length);
Assert.assertEquals(1, sc.findMethods().length);
Assert.assertEquals("GET", sc.findMethods()[0]);
}
@Test
public void testFindUncoveredHttpMethods08() {
SecurityConstraint[] result =
SecurityConstraint.findUncoveredHttpMethods(
new SecurityConstraint[] {POST_OMIT}, true, DUMMY_LOG);
Assert.assertEquals(1, result.length);
// Should be a deny constraint
Assert.assertTrue(result[0].getAuthConstraint());
// Should have a single collection
Assert.assertEquals(1, result[0].findCollections().length);
SecurityCollection sc = result[0].findCollections()[0];
// Should list POST as an method
Assert.assertEquals(0, sc.findOmittedMethods().length);
Assert.assertEquals(1, sc.findMethods().length);
Assert.assertEquals("POST", sc.findMethods()[0]);
}
@Test
public void testFindUncoveredHttpMethods09() {
SecurityConstraint[] result =
SecurityConstraint.findUncoveredHttpMethods(
new SecurityConstraint[] {GET_ONLY, GET_OMIT}, true,
DUMMY_LOG);
Assert.assertEquals(0, result.length);
}
@Test
public void testFindUncoveredHttpMethods10() {
SecurityConstraint[] result =
SecurityConstraint.findUncoveredHttpMethods(
new SecurityConstraint[] {POST_ONLY, POST_OMIT}, true,
DUMMY_LOG);
Assert.assertEquals(0, result.length);
}
@Test
public void testFindUncoveredHttpMethods11() {
SecurityConstraint[] result =
SecurityConstraint.findUncoveredHttpMethods(
new SecurityConstraint[] {GET_ONLY, POST_ONLY}, true,
DUMMY_LOG);
Assert.assertEquals(1, result.length);
// Should be a deny constraint
Assert.assertTrue(result[0].getAuthConstraint());
// Should have a single collection
Assert.assertEquals(1, result[0].findCollections().length);
SecurityCollection sc = result[0].findCollections()[0];
// Should list GET and POST as omitted methods
Assert.assertEquals(0, sc.findMethods().length);
Assert.assertEquals(2, sc.findOmittedMethods().length);
HashSet<String> omittedMethods = new HashSet<>();
for (String omittedMethod : sc.findOmittedMethods()) {
omittedMethods.add(omittedMethod);
}
Assert.assertTrue(omittedMethods.remove("GET"));
Assert.assertTrue(omittedMethods.remove("POST"));
}
@Test
public void testFindUncoveredHttpMethods12() {
SecurityConstraint[] result =
SecurityConstraint.findUncoveredHttpMethods(
new SecurityConstraint[] {GET_OMIT, POST_OMIT}, true,
DUMMY_LOG);
Assert.assertEquals(0, result.length);
}
@Test
public void testFindUncoveredHttpMethods13() {
SecurityConstraint[] result =
SecurityConstraint.findUncoveredHttpMethods(
new SecurityConstraint[] {GET_ONLY, POST_OMIT}, true,
DUMMY_LOG);
Assert.assertEquals(1, result.length);
// Should be a deny constraint
Assert.assertTrue(result[0].getAuthConstraint());
// Should have a single collection
Assert.assertEquals(1, result[0].findCollections().length);
SecurityCollection sc = result[0].findCollections()[0];
// Should list POST as a method
Assert.assertEquals(1, sc.findMethods().length);
Assert.assertEquals(0, sc.findOmittedMethods().length);
Assert.assertEquals("POST", sc.findMethods()[0]);
}
@Test
public void testFindUncoveredHttpMethods14() {
SecurityConstraint[] result =
SecurityConstraint.findUncoveredHttpMethods(
new SecurityConstraint[] {GET_OMIT, POST_ONLY}, true,
DUMMY_LOG);
Assert.assertEquals(1, result.length);
// Should be a deny constraint
Assert.assertTrue(result[0].getAuthConstraint());
// Should have a single collection
Assert.assertEquals(1, result[0].findCollections().length);
SecurityCollection sc = result[0].findCollections()[0];
// Should list GET as a method
Assert.assertEquals(1, sc.findMethods().length);
Assert.assertEquals(0, sc.findOmittedMethods().length);
Assert.assertEquals("GET", sc.findMethods()[0]);
}
}

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.tomcat.util.descriptor.web;
import org.junit.Assert;
import org.junit.Test;
/**
* Test case for {@link ServletDef}
*/
public class TestServletDef {
@Test(expected = IllegalArgumentException.class)
public void testSetServletNameNull() {
new ServletDef().setServletName(null);
}
@Test(expected = IllegalArgumentException.class)
public void testSetServletNameEmptyString() {
new ServletDef().setServletName("");
}
@Test
public void testSetServletName() {
ServletDef servletDef = new ServletDef();
servletDef.setServletName("test");
Assert.assertEquals("'test' is expected as servlet name",
"test", servletDef.getServletName());
}
}

View File

@@ -0,0 +1,155 @@
/*
* 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.tomcat.util.descriptor.web;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import org.junit.Assert;
import org.junit.Test;
import org.apache.tomcat.util.digester.Digester;
public class TestWebRuleSet {
private Digester fragmentDigester = new Digester();
private WebRuleSet fragmentRuleSet = new WebRuleSet(true);
private Digester webDigester = new Digester();
private WebRuleSet webRuleSet = new WebRuleSet(false);
public TestWebRuleSet() {
fragmentDigester.addRuleSet(fragmentRuleSet);
webDigester.addRuleSet(webRuleSet);
}
@Test
public void testSingleNameInWebFragmentXml() throws Exception {
WebXml webXml = new WebXml();
parse(webXml, "web-fragment-1name.xml", true, true);
Assert.assertEquals("name1", webXml.getName());
}
@Test
public void testMultipleNameInWebFragmentXml() throws Exception {
parse(new WebXml(), "web-fragment-2name.xml", true, false);
}
@Test
public void testSingleOrderingInWebFragmentXml() throws Exception {
WebXml webXml = new WebXml();
parse(webXml, "web-fragment-1ordering.xml", true, true);
Assert.assertEquals(1, webXml.getBeforeOrdering().size());
Assert.assertTrue(webXml.getBeforeOrdering().contains("bar"));
}
@Test
public void testMultipleOrderingInWebFragmentXml() throws Exception {
parse(new WebXml(), "web-fragment-2ordering.xml", true, false);
}
@Test
public void testSingleOrderingInWebXml() throws Exception {
WebXml webXml = new WebXml();
parse(webXml, "web-1ordering.xml", false, true);
Assert.assertEquals(1, webXml.getAbsoluteOrdering().size());
Assert.assertTrue(webXml.getAbsoluteOrdering().contains("bar"));
}
@Test
public void testMultipleOrderingInWebXml() throws Exception {
parse(new WebXml(), "web-2ordering.xml", false, false);
}
@Test
public void testRecycle() throws Exception {
// Name
parse(new WebXml(), "web-fragment-2name.xml", true, false);
parse(new WebXml(), "web-fragment-1name.xml", true, true);
parse(new WebXml(), "web-fragment-2name.xml", true, false);
parse(new WebXml(), "web-fragment-1name.xml", true, true);
// Relative ordering
parse(new WebXml(), "web-fragment-2ordering.xml", true, false);
parse(new WebXml(), "web-fragment-1ordering.xml", true, true);
parse(new WebXml(), "web-fragment-2ordering.xml", true, false);
parse(new WebXml(), "web-fragment-1ordering.xml", true, true);
// Absolute ordering
parse(new WebXml(), "web-2ordering.xml", false, false);
parse(new WebXml(), "web-1ordering.xml", false, true);
parse(new WebXml(), "web-2ordering.xml", false, false);
parse(new WebXml(), "web-1ordering.xml", false, true);
}
@Test
public void testLifecycleMethodsDefinitions() throws Exception {
// post-construct and pre-destroy
parse(new WebXml(), "web-1lifecyclecallback.xml", false, true);
// conflicting post-construct definitions
parse(new WebXml(), "web-2lifecyclecallback.xml", false, false);
}
private synchronized void parse(WebXml webXml, String target,
boolean fragment, boolean expected) {
Digester d;
if (fragment) {
d = fragmentDigester;
fragmentRuleSet.recycle();
} else {
d = webDigester;
webRuleSet.recycle();
}
d.push(webXml);
File f = new File("test/org/apache/catalina/startup/" + target);
boolean result = true;
try (InputStream is = new FileInputStream(f);) {
d.parse(is);
} catch (Exception e) {
if (expected) {
// Didn't expect an exception
e.printStackTrace();
}
result = false;
}
if (expected) {
Assert.assertTrue(result);
} else {
Assert.assertFalse(result);
}
}
}

View File

@@ -0,0 +1,473 @@
/*
* 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.tomcat.util.descriptor.web;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.junit.Assert;
import org.junit.Test;
import org.apache.tomcat.util.descriptor.DigesterFactory;
import org.apache.tomcat.util.descriptor.XmlErrorHandler;
import org.apache.tomcat.util.descriptor.XmlIdentifiers;
import org.apache.tomcat.util.digester.Digester;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/**
* Test case for {@link WebXml}.
*/
public class TestWebXml {
@Test
public void testParseVersion() {
WebXml webxml = new WebXml();
// Defaults
Assert.assertEquals(3, webxml.getMajorVersion());
Assert.assertEquals(1, webxml.getMinorVersion());
// Both get changed
webxml.setVersion("2.5");
Assert.assertEquals(2, webxml.getMajorVersion());
Assert.assertEquals(5, webxml.getMinorVersion());
// unknown input should be ignored
webxml.setVersion("0.0");
Assert.assertEquals(2, webxml.getMajorVersion());
Assert.assertEquals(5, webxml.getMinorVersion());
// null input should be ignored
webxml.setVersion(null);
Assert.assertEquals(2, webxml.getMajorVersion());
Assert.assertEquals(5, webxml.getMinorVersion());
}
@Test
public void testParsePublicIdVersion22() {
WebXml webxml = new WebXml();
webxml.setPublicId(XmlIdentifiers.WEB_22_PUBLIC);
Assert.assertEquals(2, webxml.getMajorVersion());
Assert.assertEquals(2, webxml.getMinorVersion());
Assert.assertEquals("2.2", webxml.getVersion());
}
@Test
public void testParsePublicIdVersion23() {
WebXml webxml = new WebXml();
webxml.setPublicId(XmlIdentifiers.WEB_23_PUBLIC);
Assert.assertEquals(2, webxml.getMajorVersion());
Assert.assertEquals(3, webxml.getMinorVersion());
Assert.assertEquals("2.3", webxml.getVersion());
}
@Test
public void testParseVersion24() {
WebXml webxml = new WebXml();
webxml.setVersion("2.4");
Assert.assertEquals(2, webxml.getMajorVersion());
Assert.assertEquals(4, webxml.getMinorVersion());
Assert.assertEquals("2.4", webxml.getVersion());
}
@Test
public void testParseVersion25() {
WebXml webxml = new WebXml();
webxml.setVersion("2.5");
Assert.assertEquals(2, webxml.getMajorVersion());
Assert.assertEquals(5, webxml.getMinorVersion());
Assert.assertEquals("2.5", webxml.getVersion());
}
@Test
public void testParseVersion30() {
WebXml webxml = new WebXml();
webxml.setVersion("3.0");
Assert.assertEquals(3, webxml.getMajorVersion());
Assert.assertEquals(0, webxml.getMinorVersion());
Assert.assertEquals("3.0", webxml.getVersion());
}
@Test
public void testParseVersion31() {
WebXml webxml = new WebXml();
webxml.setVersion("3.1");
Assert.assertEquals(3, webxml.getMajorVersion());
Assert.assertEquals(1, webxml.getMinorVersion());
Assert.assertEquals("3.1", webxml.getVersion());
}
@Test
public void testValidateVersion22() throws IOException, SAXException {
doTestValidateVersion("2.2");
}
@Test
public void testValidateVersion23() throws IOException, SAXException {
doTestValidateVersion("2.3");
}
@Test
public void testValidateVersion24() throws IOException, SAXException {
doTestValidateVersion("2.4");
}
@Test
public void testValidateVersion25() throws IOException, SAXException {
doTestValidateVersion("2.5");
}
@Test
public void testValidateVersion30() throws IOException, SAXException {
doTestValidateVersion("3.0");
}
@Test
public void testValidateVersion31() throws IOException, SAXException {
doTestValidateVersion("3.1");
}
private void doTestValidateVersion(String version) throws IOException, SAXException {
WebXml webxml = new WebXml();
// Special cases
if ("2.2".equals(version)) {
webxml.setPublicId(XmlIdentifiers.WEB_22_PUBLIC);
} else if ("2.3".equals(version)) {
webxml.setPublicId(XmlIdentifiers.WEB_23_PUBLIC);
} else {
webxml.setVersion(version);
}
// Merged web.xml that is published as MERGED_WEB_XML context attribute
// in the simplest case consists of webapp's web.xml file
// plus the default conf/web.xml one.
Set<WebXml> defaults = new HashSet<>();
defaults.add(getDefaultWebXmlFragment());
webxml.merge(defaults);
Digester digester = DigesterFactory.newDigester(true, true, new WebRuleSet(), true);
XmlErrorHandler handler = new XmlErrorHandler();
digester.setErrorHandler(handler);
InputSource is = new InputSource(new StringReader(webxml.toXml()));
WebXml webxmlResult = new WebXml();
digester.push(webxmlResult);
digester.parse(is);
Assert.assertEquals(0, handler.getErrors().size());
Assert.assertEquals(0, handler.getWarnings().size());
Assert.assertEquals(version, webxml.getVersion());
Assert.assertEquals(version, webxmlResult.getVersion());
}
// A simplified copy of ContextConfig.getDefaultWebXmlFragment().
// Assuming that global web.xml exists, host-specific web.xml does not exist.
private WebXml getDefaultWebXmlFragment() throws IOException, SAXException {
InputSource globalWebXml = new InputSource(new File("conf/web.xml")
.getAbsoluteFile().toURI().toString());
WebXml webXmlDefaultFragment = new WebXml();
webXmlDefaultFragment.setOverridable(true);
webXmlDefaultFragment.setDistributable(true);
webXmlDefaultFragment.setAlwaysAddWelcomeFiles(false);
Digester digester = DigesterFactory.newDigester(true, true, new WebRuleSet(), true);
XmlErrorHandler handler = new XmlErrorHandler();
digester.setErrorHandler(handler);
digester.push(webXmlDefaultFragment);
digester.parse(globalWebXml);
Assert.assertEquals(0, handler.getErrors().size());
Assert.assertEquals(0, handler.getWarnings().size());
webXmlDefaultFragment.setReplaceWelcomeFiles(true);
// Assert that web.xml was parsed and is not empty. Default servlet is known to be there.
Assert.assertNotNull(webXmlDefaultFragment.getServlets().get("default"));
// Manually add some version specific features to ensure that these do
// not cause problems for the merged web.xml
// Filters were added in 2.3 so should be excluded in 2.2
FilterDef filterDef = new FilterDef();
filterDef.setFilterClass("org.apache.tomcat.DummyFilter");
filterDef.setFilterName("Dummy");
webXmlDefaultFragment.addFilter(filterDef);
FilterMap filterMap = new FilterMap();
filterMap.setFilterName("Dummy");
filterMap.addURLPatternDecoded("/*");
webXmlDefaultFragment.addFilterMapping(filterMap);
// Listeners were added in 2.3 so should be excluded in 2.2
webXmlDefaultFragment.addListener("org.apache.tomcat.DummyListener");
// resource-env-ref was added in 2.3 so should be excluded in 2.2
ContextResourceEnvRef resourceEnvRef = new ContextResourceEnvRef();
resourceEnvRef.setName("dummy");
resourceEnvRef.setType("dummy");
webXmlDefaultFragment.addResourceEnvRef(resourceEnvRef);
// ejb-local-ref was added in 2.3 so should be excluded in 2.2
ContextLocalEjb ejbLocalRef = new ContextLocalEjb();
ejbLocalRef.setName("dummy");
ejbLocalRef.setType("Session");
ejbLocalRef.setLocal("dummy");
ejbLocalRef.setHome("dummy");
webXmlDefaultFragment.addEjbLocalRef(ejbLocalRef);
// Servlet/run-as was added in 2.3 so should be excluded in 2.2
ServletDef servletDef = new ServletDef();
servletDef.setServletName("Dummy");
servletDef.setServletClass("org.apache.tomcat.DummyServlet");
servletDef.setRunAs("dummy");
webXmlDefaultFragment.addServlet(servletDef);
webXmlDefaultFragment.addServletMapping("/dummy", "Dummy");
// resource-ref/res-sharing-scope was added in 2.3 so should be excluded
// in 2.2
ContextResource contextResource = new ContextResource();
contextResource.setName("dummy");
contextResource.setType("dummy");
contextResource.setAuth("Container");
contextResource.setScope("Shareable");
webXmlDefaultFragment.addResourceRef(contextResource);
// security-constraint/display-name was added in 2.3 so should be
// excluded in 2.2
SecurityConstraint sc = new SecurityConstraint();
sc.setDisplayName("dummy");
SecurityCollection collection = new SecurityCollection();
collection.setName("dummy");
collection.addPatternDecoded("/*");
collection.addMethod("DELETE");
sc.addCollection(collection);
webXmlDefaultFragment.addSecurityConstraint(sc);
// service-ref was added in 2.4 so should be excluded in 2.3 and earlier
ContextService serviceRef = new ContextService();
serviceRef.setName("dummy");
serviceRef.setInterface("dummy");
webXmlDefaultFragment.addServiceRef(serviceRef);
// message-destination-ref was added in 2.4 so should be excluded in 2.3
// and earlier
MessageDestinationRef mdRef = new MessageDestinationRef();
mdRef.setName("dummy");
mdRef.setType("dummy");
mdRef.setUsage("Consumes");
webXmlDefaultFragment.addMessageDestinationRef(mdRef);
// message-destination was added in 2.4 so should be excluded in 2.3
// and earlier
MessageDestination md = new MessageDestination();
md.setName("dummy");
webXmlDefaultFragment.addMessageDestination(md);
// local-encoding-mapping-list was added in 2.4 so should be excluded in
// 2.3 and earlier
webXmlDefaultFragment.addLocaleEncodingMapping("en", "UTF-8");
// jsp-config was added in Servlet 2.4
webXmlDefaultFragment.addTaglib("dummy", "dummy");
// filter-mapping/dispatcher added in Servlet 2.4
filterMap.setDispatcher("REQUEST");
// listener-[description|display-name|icon] added in Servlet 2.4
// None of these are supported in WebXml
// filter-mapping/dispatcher/ASYNC added in Servlet 3.0
filterMap.setDispatcher("ASYNC");
// error-page with just location allowed in Servlet 3.0+
ErrorPage errorPage = new ErrorPage();
errorPage.setLocation("/dummy");
webXmlDefaultFragment.addErrorPage(errorPage);
// async-supported added to Servlet and Filter in 3.0
filterDef.setAsyncSupported("false");
servletDef.setAsyncSupported("false");
// session-cookie-config added in 3.0
SessionConfig sessionConfig = new SessionConfig();
sessionConfig.setCookieDomain("dummy");
webXmlDefaultFragment.setSessionConfig(sessionConfig);
// http-method-omission added in Servlet 3.0
// Let this trigger a validation error as dropping it silently could
// be a security concern
// multi-part-config added in Servlet 3.0
MultipartDef multiPart = new MultipartDef();
servletDef.setMultipartDef(multiPart);
// deny-uncovered-http-methods added in Servlet 3.1
webXmlDefaultFragment.setDenyUncoveredHttpMethods(true);
return webXmlDefaultFragment;
}
@Test
public void testLifecycleMethodsWebXml() {
WebXml webxml = new WebXml();
webxml.addPostConstructMethods("a", "a");
webxml.addPreDestroyMethods("b", "b");
WebXml fragment = new WebXml();
fragment.addPostConstructMethods("c", "c");
fragment.addPreDestroyMethods("d", "d");
Set<WebXml> fragments = new HashSet<>();
fragments.add(fragment);
webxml.merge(fragments);
Map<String, String> postConstructMethods = webxml.getPostConstructMethods();
Map<String, String> preDestroyMethods = webxml.getPreDestroyMethods();
Assert.assertEquals(1, postConstructMethods.size());
Assert.assertEquals(1, preDestroyMethods.size());
Assert.assertEquals("a", postConstructMethods.get("a"));
Assert.assertEquals("b", preDestroyMethods.get("b"));
}
@Test
public void testLifecycleMethodsWebFragments() {
WebXml webxml = new WebXml();
WebXml fragment1 = new WebXml();
fragment1.addPostConstructMethods("a", "a");
fragment1.addPreDestroyMethods("b", "b");
WebXml fragment2 = new WebXml();
fragment2.addPostConstructMethods("c", "c");
fragment2.addPreDestroyMethods("d", "d");
Set<WebXml> fragments = new HashSet<>();
fragments.add(fragment1);
fragments.add(fragment2);
webxml.merge(fragments);
Map<String, String> postConstructMethods = webxml.getPostConstructMethods();
Map<String, String> preDestroyMethods = webxml.getPreDestroyMethods();
Assert.assertEquals(2, postConstructMethods.size());
Assert.assertEquals(2, preDestroyMethods.size());
Assert.assertEquals("a", postConstructMethods.get("a"));
Assert.assertEquals("c", postConstructMethods.get("c"));
Assert.assertEquals("b", preDestroyMethods.get("b"));
Assert.assertEquals("d", preDestroyMethods.get("d"));
}
@Test
public void testLifecycleMethodsWebFragmentsWithConflicts() {
WebXml webxml = new WebXml();
WebXml fragment1 = new WebXml();
fragment1.addPostConstructMethods("a", "a");
fragment1.addPreDestroyMethods("b", "a");
WebXml fragment2 = new WebXml();
fragment2.addPostConstructMethods("a", "b");
Set<WebXml> fragments = new HashSet<>();
fragments.add(fragment1);
fragments.add(fragment2);
Assert.assertFalse(webxml.merge(fragments));
Assert.assertEquals(0, webxml.getPostConstructMethods().size());
WebXml fragment3 = new WebXml();
fragment3.addPreDestroyMethods("b", "b");
fragments.remove(fragment2);
fragments.add(fragment3);
Assert.assertFalse(webxml.merge(fragments));
Assert.assertEquals(0, webxml.getPreDestroyMethods().size());
}
@Test(expected=IllegalArgumentException.class)
public void testBug54387a() {
// Multiple servlets may not be mapped to the same url-pattern
WebXml webxml = new WebXml();
webxml.addServletMapping("/foo", "a");
webxml.addServletMapping("/foo", "b");
}
@Test(expected=IllegalArgumentException.class)
public void testBug54387b() {
// Multiple servlets may not be mapped to the same url-pattern
WebXml webxml = new WebXml();
WebXml f1 = new WebXml();
WebXml f2 = new WebXml();
HashSet<WebXml> fragments = new HashSet<>();
fragments.add(f1);
fragments.add(f2);
f1.addServletMapping("/foo", "a");
f2.addServletMapping("/foo", "b");
webxml.merge(fragments);
}
@Test
public void testBug54387c() {
// Multiple servlets may not be mapped to the same url-pattern but main
// web.xml takes priority
WebXml webxml = new WebXml();
WebXml f1 = new WebXml();
WebXml f2 = new WebXml();
HashSet<WebXml> fragments = new HashSet<>();
fragments.add(f1);
fragments.add(f2);
f1.addServletMapping("/foo", "a");
f2.addServletMapping("/foo", "b");
webxml.addServletMapping("/foo", "main");
webxml.merge(fragments);
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,63 @@
/*
* 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.tomcat.util.file;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.apache.catalina.webresources.TomcatURLStreamHandlerFactory;
public class TestConfigFileLoader {
@BeforeClass
public static void setup() {
TomcatURLStreamHandlerFactory.getInstance();
System.setProperty("catalina.base", "");
}
@Test
public void test01() throws IOException {
doTest("classpath:org/apache/catalina/mbeans-descriptors.xml");
}
@Test(expected=FileNotFoundException.class)
public void test02() throws IOException {
doTest("classpath:org/apache/catalina/foo");
}
@Test
public void test03() throws IOException {
doTest("test/webresources/dir1");
}
@Test(expected=FileNotFoundException.class)
public void test04() throws IOException {
doTest("test/webresources/unknown");
}
private void doTest(String path) throws IOException {
try (InputStream is = ConfigFileLoader.getInputStream(path)) {
Assert.assertNotNull(is);
}
}
}

Some files were not shown because too many files have changed in this diff Show More