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

View File

@@ -0,0 +1,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);
}
}

View File

@@ -0,0 +1,696 @@
/*
* 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.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
/**
* Test case for {@link WebXml} fragment ordering.
*/
public class TestWebXmlOrdering {
private WebXml app;
private WebXml a;
private WebXml b;
private WebXml c;
private WebXml d;
private WebXml e;
private WebXml f;
private Map<String,WebXml> fragments;
private int posA;
private int posB;
private int posC;
private int posD;
private int posE;
private int posF;
@Before
public void setUp() {
app = new WebXml();
a = new WebXml();
a.setName("a");
b = new WebXml();
b.setName("b");
c = new WebXml();
c.setName("c");
d = new WebXml();
d.setName("d");
e = new WebXml();
e.setName("e");
f = new WebXml();
f.setName("f");
// Control the input order
fragments = new LinkedHashMap<>();
fragments.put("a",a);
fragments.put("b",b);
fragments.put("c",c);
fragments.put("d",d);
fragments.put("e",e);
fragments.put("f",f);
}
@Test
public void testOrderWebFragmentsAbsolute() {
app.addAbsoluteOrdering("c");
app.addAbsoluteOrdering("a");
app.addAbsoluteOrdering("b");
app.addAbsoluteOrdering("e");
app.addAbsoluteOrdering("d");
Set<WebXml> ordered = WebXml.orderWebFragments(app, fragments, null);
Iterator<WebXml> iter = ordered.iterator();
Assert.assertEquals(c,iter.next());
Assert.assertEquals(a,iter.next());
Assert.assertEquals(b,iter.next());
Assert.assertEquals(e,iter.next());
Assert.assertEquals(d,iter.next());
Assert.assertFalse(iter.hasNext());
}
@Test
public void testOrderWebFragmentsAbsolutePartial() {
app.addAbsoluteOrdering("c");
app.addAbsoluteOrdering("a");
Set<WebXml> ordered = WebXml.orderWebFragments(app, fragments, null);
Iterator<WebXml> iter = ordered.iterator();
Assert.assertEquals(c,iter.next());
Assert.assertEquals(a,iter.next());
Assert.assertFalse(iter.hasNext());
}
@Test
public void testOrderWebFragmentsAbsoluteOthersStart() {
app.addAbsoluteOrdering(WebXml.ORDER_OTHERS);
app.addAbsoluteOrdering("b");
app.addAbsoluteOrdering("d");
Set<WebXml> others = new HashSet<>();
others.add(a);
others.add(c);
others.add(e);
others.add(f);
Set<WebXml> ordered = WebXml.orderWebFragments(app, fragments, null);
Iterator<WebXml> iter = ordered.iterator();
while (others.size() > 0) {
WebXml o = iter.next();
Assert.assertTrue(others.contains(o));
others.remove(o);
}
Assert.assertEquals(b,iter.next());
Assert.assertEquals(d,iter.next());
Assert.assertFalse(iter.hasNext());
}
@Test
public void testOrderWebFragmentsAbsoluteOthersMiddle() {
app.addAbsoluteOrdering("b");
app.addAbsoluteOrdering(WebXml.ORDER_OTHERS);
app.addAbsoluteOrdering("d");
Set<WebXml> others = new HashSet<>();
others.add(a);
others.add(c);
others.add(e);
others.add(f);
Set<WebXml> ordered = WebXml.orderWebFragments(app, fragments, null);
Iterator<WebXml> iter = ordered.iterator();
Assert.assertEquals(b,iter.next());
while (others.size() > 0) {
WebXml o = iter.next();
Assert.assertTrue(others.contains(o));
others.remove(o);
}
Assert.assertEquals(d,iter.next());
Assert.assertFalse(iter.hasNext());
}
@Test
public void testWebFragmentsAbsoluteWrongFragmentName() {
app.addAbsoluteOrdering("a");
app.addAbsoluteOrdering("z");
Set<WebXml> ordered = WebXml.orderWebFragments(app, fragments, null);
Assert.assertEquals(1,ordered.size());
Assert.assertEquals(fragments.get("a"),ordered.toArray()[0]);
}
@Test
public void testOrderWebFragmentsAbsoluteOthersEnd() {
app.addAbsoluteOrdering("b");
app.addAbsoluteOrdering("d");
app.addAbsoluteOrdering(WebXml.ORDER_OTHERS);
Set<WebXml> others = new HashSet<>();
others.add(a);
others.add(c);
others.add(e);
others.add(f);
Set<WebXml> ordered = WebXml.orderWebFragments(app, fragments, null);
Iterator<WebXml> iter = ordered.iterator();
Assert.assertEquals(b,iter.next());
Assert.assertEquals(d,iter.next());
while (others.size() > 0) {
WebXml o = iter.next();
Assert.assertTrue(others.contains(o));
others.remove(o);
}
Assert.assertFalse(iter.hasNext());
}
private void doRelativeOrderingTest(RelativeOrderingTestRunner runner) {
// Confirm we have all 720 possible input orders
// Set<String> orders = new HashSet<>();
// Test all possible input orders since some bugs were discovered that
// depended on input order
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 5; j++) {
for (int k = 0; k < 4; k++) {
for (int l = 0; l < 3; l++) {
for (int m = 0; m < 2; m++) {
setUp();
runner.init();
ArrayList<WebXml> source = new ArrayList<>();
source.addAll(fragments.values());
Map<String,WebXml> input =
new LinkedHashMap<>();
WebXml one = source.remove(i);
input.put(one.getName(), one);
WebXml two = source.remove(j);
input.put(two.getName(), two);
WebXml three = source.remove(k);
input.put(three.getName(), three);
WebXml four = source.remove(l);
input.put(four.getName(), four);
WebXml five = source.remove(m);
input.put(five.getName(), five);
WebXml six = source.remove(0);
input.put(six.getName(), six);
/*
String order = one.getName() + two.getName() +
three.getName() + four.getName() +
five.getName() + six.getName();
orders.add(order);
*/
Set<WebXml> ordered =
WebXml.orderWebFragments(app, input, null);
populatePositions(ordered);
runner.validate(getOrder(ordered));
}
}
}
}
}
// System.out.println(orders.size());
}
private String getOrder(Set<WebXml> ordered) {
StringBuilder sb = new StringBuilder(ordered.size());
for (WebXml webXml : ordered) {
sb.append(webXml.getName());
}
return sb.toString();
}
private void populatePositions(Set<WebXml> ordered) {
List<WebXml> indexed = new ArrayList<>();
indexed.addAll(ordered);
posA = indexed.indexOf(a);
posB = indexed.indexOf(b);
posC = indexed.indexOf(c);
posD = indexed.indexOf(d);
posE = indexed.indexOf(e);
posF = indexed.indexOf(f);
}
@Test
public void testOrderWebFragmentsRelative1() {
// First example from servlet spec
doRelativeOrderingTest(new RelativeTestRunner1());
}
@Test
public void testOrderWebFragmentsRelative2() {
// Second example - use fragment a for no-id fragment
doRelativeOrderingTest(new RelativeTestRunner2());
}
@Test
public void testOrderWebFragmentsRelative3() {
// Third example from spec with e & f added
doRelativeOrderingTest(new RelativeTestRunner3());
}
@Test
public void testOrderWebFragmentsRelative4Bug54068() {
// Simple sequence that failed for some inputs
doRelativeOrderingTest(new RelativeTestRunner4());
}
@Test
public void testOrderWebFragmentsRelative5Bug54068() {
// Simple sequence that failed for some inputs
doRelativeOrderingTest(new RelativeTestRunner5());
}
@Test
public void testOrderWebFragmentsRelative6Bug54068() {
// Simple sequence that failed for some inputs
doRelativeOrderingTest(new RelativeTestRunner6());
}
@Test
public void testOrderWebFragmentsRelative7() {
// Reference loop (but not circular dependencies)
doRelativeOrderingTest(new RelativeTestRunner7());
}
@Test
public void testOrderWebFragmentsRelative8() {
// More complex, trying to break the algorithm
doRelativeOrderingTest(new RelativeTestRunner8());
}
@Test
public void testOrderWebFragmentsRelative9() {
// Variation on bug 54068
doRelativeOrderingTest(new RelativeTestRunner9());
}
@Test
public void testOrderWebFragmentsRelative10() {
// Variation on bug 54068
doRelativeOrderingTest(new RelativeTestRunner10());
}
@Test
public void testOrderWebFragmentsRelative11() {
// Test references to non-existent fragments
doRelativeOrderingTest(new RelativeTestRunner11());
}
@Test(expected=IllegalArgumentException.class)
public void testOrderWebFragmentsrelativeCircular1() {
a.addBeforeOrdering("b");
b.addBeforeOrdering("a");
WebXml.orderWebFragments(app, fragments, null);
}
@Test(expected=IllegalArgumentException.class)
public void testOrderWebFragmentsrelativeCircular2() {
a.addBeforeOrderingOthers();
b.addAfterOrderingOthers();
c.addBeforeOrdering("a");
c.addAfterOrdering("b");
WebXml.orderWebFragments(app, fragments, null);
}
private interface RelativeOrderingTestRunner {
void init();
void validate(String order);
}
private class RelativeTestRunner1 implements RelativeOrderingTestRunner {
@Override
public void init() {
a.addAfterOrderingOthers();
a.addAfterOrdering("c");
b.addBeforeOrderingOthers();
c.addAfterOrderingOthers();
f.addBeforeOrderingOthers();
f.addBeforeOrdering("b");
}
@Override
public void validate(String order) {
// There is some duplication in the tests below - it is easier to
// check the tests are complete this way.
//a.addAfterOrderingOthers();
Assert.assertTrue(order, posA > posB);
Assert.assertTrue(order, posA > posC);
Assert.assertTrue(order, posA > posD);
Assert.assertTrue(order, posA > posE);
Assert.assertTrue(order, posA > posF);
// a.addAfterOrdering("c");
Assert.assertTrue(order, posA > posC);
// b.addBeforeOrderingOthers();
Assert.assertTrue(order, posB < posC);
// c.addAfterOrderingOthers();
Assert.assertTrue(order, posC > posB);
Assert.assertTrue(order, posC > posD);
Assert.assertTrue(order, posC > posE);
Assert.assertTrue(order, posC > posF);
// f.addBeforeOrderingOthers();
Assert.assertTrue(order, posF < posA);
Assert.assertTrue(order, posF < posB);
Assert.assertTrue(order, posF < posC);
Assert.assertTrue(order, posF < posD);
Assert.assertTrue(order, posF < posE);
// f.addBeforeOrdering("b");
Assert.assertTrue(order, posF < posB);
}
}
private class RelativeTestRunner2 implements RelativeOrderingTestRunner {
@Override
public void init() {
a.addAfterOrderingOthers();
a.addBeforeOrdering("c");
b.addBeforeOrderingOthers();
d.addAfterOrderingOthers();
e.addBeforeOrderingOthers();
}
@Override
public void validate(String order) {
// There is some duplication in the tests below - it is easier to
// check the tests are complete this way.
// a.addAfterOrderingOthers();
Assert.assertTrue(order, posA > posB);
Assert.assertTrue(order, posA > posE);
Assert.assertTrue(order, posA > posF);
// a.addBeforeOrdering("c");
Assert.assertTrue(order, posC > posA);
Assert.assertTrue(order, posC > posB);
Assert.assertTrue(order, posC > posE);
Assert.assertTrue(order, posC > posF);
// b.addBeforeOrderingOthers();
Assert.assertTrue(order, posB < posA);
Assert.assertTrue(order, posB < posC);
Assert.assertTrue(order, posB < posD);
Assert.assertTrue(order, posB < posF);
// d.addAfterOrderingOthers();
Assert.assertTrue(order, posD > posB);
Assert.assertTrue(order, posD > posE);
Assert.assertTrue(order, posD > posF);
// e.addBeforeOrderingOthers();
Assert.assertTrue(order, posE < posA);
Assert.assertTrue(order, posE < posC);
Assert.assertTrue(order, posE < posD);
Assert.assertTrue(order, posE < posF);
}
}
private class RelativeTestRunner3 implements RelativeOrderingTestRunner {
@Override
public void init() {
a.addAfterOrdering("b");
c.addBeforeOrderingOthers();
}
@Override
public void validate(String order) {
// There is some duplication in the tests below - it is easier to
// check the tests are complete this way.
// a.addAfterOrdering("b");
Assert.assertTrue(order, posA > posB);
// c.addBeforeOrderingOthers();
Assert.assertTrue(order, posC < posA);
Assert.assertTrue(order, posC < posB);
Assert.assertTrue(order, posC < posD);
Assert.assertTrue(order, posC < posE);
Assert.assertTrue(order, posC < posF);
}
}
private class RelativeTestRunner4 implements RelativeOrderingTestRunner {
@Override
public void init() {
b.addAfterOrdering("a");
c.addAfterOrdering("b");
}
@Override
public void validate(String order) {
// There is some duplication in the tests below - it is easier to
// check the tests are complete this way.
// b.addAfterOrdering("a");
Assert.assertTrue(order, posB > posA);
// c.addAfterOrdering("b");
Assert.assertTrue(order, posC > posB);
}
}
private class RelativeTestRunner5 implements RelativeOrderingTestRunner {
@Override
public void init() {
b.addBeforeOrdering("a");
c.addBeforeOrdering("b");
}
@Override
public void validate(String order) {
// There is some duplication in the tests below - it is easier to
// check the tests are complete this way.
// b.addBeforeOrdering("a");
Assert.assertTrue(order, posB < posA);
// c.addBeforeOrdering("b");
Assert.assertTrue(order, posC < posB);
}
}
private class RelativeTestRunner6 implements RelativeOrderingTestRunner {
@Override
public void init() {
b.addBeforeOrdering("a");
b.addAfterOrdering("c");
}
@Override
public void validate(String order) {
// There is some duplication in the tests below - it is easier to
// check the tests are complete this way.
// b.addBeforeOrdering("a");
Assert.assertTrue(order, posB < posA);
//b.addAfterOrdering("c");
Assert.assertTrue(order, posB > posC);
}
}
private class RelativeTestRunner7 implements RelativeOrderingTestRunner {
@Override
public void init() {
b.addBeforeOrdering("a");
c.addBeforeOrdering("b");
a.addAfterOrdering("c");
}
@Override
public void validate(String order) {
// There is some duplication in the tests below - it is easier to
// check the tests are complete this way.
// b.addBeforeOrdering("a");
Assert.assertTrue(order, posB < posA);
// c.addBeforeOrdering("b");
Assert.assertTrue(order, posC < posB);
// a.addAfterOrdering("c");
Assert.assertTrue(order, posA > posC);
}
}
private class RelativeTestRunner8 implements RelativeOrderingTestRunner {
@Override
public void init() {
a.addBeforeOrderingOthers();
a.addBeforeOrdering("b");
b.addBeforeOrderingOthers();
c.addAfterOrdering("b");
d.addAfterOrdering("c");
e.addAfterOrderingOthers();
f.addAfterOrderingOthers();
f.addAfterOrdering("e");
}
@Override
public void validate(String order) {
// There is some duplication in the tests below - it is easier to
// check the tests are complete this way.
// a.addBeforeOrderingOthers();
Assert.assertTrue(order, posA < posB);
Assert.assertTrue(order, posA < posC);
Assert.assertTrue(order, posA < posD);
Assert.assertTrue(order, posA < posE);
Assert.assertTrue(order, posA < posF);
// a.addBeforeOrdering("b");
Assert.assertTrue(order, posA < posB);
// b.addBeforeOrderingOthers();
Assert.assertTrue(order, posB < posC);
Assert.assertTrue(order, posB < posD);
Assert.assertTrue(order, posB < posE);
Assert.assertTrue(order, posB < posF);
// c.addAfterOrdering("b");
Assert.assertTrue(order, posC > posB);
// d.addAfterOrdering("c");
Assert.assertTrue(order, posD > posC);
// e.addAfterOrderingOthers();
Assert.assertTrue(order, posE > posA);
Assert.assertTrue(order, posE > posB);
Assert.assertTrue(order, posE > posC);
Assert.assertTrue(order, posE > posD);
// f.addAfterOrderingOthers();
Assert.assertTrue(order, posF > posA);
Assert.assertTrue(order, posF > posB);
Assert.assertTrue(order, posF > posC);
Assert.assertTrue(order, posF > posD);
Assert.assertTrue(order, posF > posE);
// f.addAfterOrdering("e");
Assert.assertTrue(order, posF > posE);
}
}
private class RelativeTestRunner9 implements RelativeOrderingTestRunner {
@Override
public void init() {
a.addBeforeOrderingOthers();
b.addBeforeOrdering("a");
c.addBeforeOrdering("b");
}
@Override
public void validate(String order) {
// There is some duplication in the tests below - it is easier to
// check the tests are complete this way.
// a.addBeforeOrderingOthers();
Assert.assertTrue(order, posA < posD);
Assert.assertTrue(order, posA < posE);
Assert.assertTrue(order, posA < posF);
// b.addBeforeOrdering("a");
Assert.assertTrue(order, posB < posA);
// c.addBeforeOrdering("b");
Assert.assertTrue(order, posC < posB);
}
}
private class RelativeTestRunner10 implements RelativeOrderingTestRunner {
@Override
public void init() {
a.addAfterOrderingOthers();
b.addAfterOrdering("a");
c.addAfterOrdering("b");
}
@Override
public void validate(String order) {
// There is some duplication in the tests below - it is easier to
// check the tests are complete this way.
// a.addAfterOrderingOthers();
Assert.assertTrue(order, posA > posD);
Assert.assertTrue(order, posA > posE);
Assert.assertTrue(order, posA > posF);
// b.addAfterOrdering("a");
Assert.assertTrue(order, posB > posA);
// c.addAfterOrdering("b");
Assert.assertTrue(order, posC > posB);
}
}
private class RelativeTestRunner11 implements RelativeOrderingTestRunner {
@Override
public void init() {
a.addAfterOrdering("b");
b.addAfterOrdering("z");
b.addBeforeOrdering("y");
}
@Override
public void validate(String order) {
// There is some duplication in the tests below - it is easier to
// check the tests are complete this way.
// a.addAfterOrdering("b");
Assert.assertTrue(order, posA > posB);
}
}
}