You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2024/11/05 16:41:08 UTC
(tomcat) branch 10.1.x updated: Add test case to improve code coverage.
This is an automated email from the ASF dual-hosted git repository.
markt pushed a commit to branch 10.1.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/10.1.x by this push:
new 5671014b0d Add test case to improve code coverage.
5671014b0d is described below
commit 5671014b0d57c7a8ccd6f2c7c7602f6aaf7f1625
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Tue Nov 5 16:13:54 2024 +0000
Add test case to improve code coverage.
Test case provided by Emile Plas.
---
.../tomcat/util/digester/TestObjectCreateRule.java | 177 +++++++++++++++++++++
1 file changed, 177 insertions(+)
diff --git a/test/org/apache/tomcat/util/digester/TestObjectCreateRule.java b/test/org/apache/tomcat/util/digester/TestObjectCreateRule.java
new file mode 100644
index 0000000000..588e1c32ff
--- /dev/null
+++ b/test/org/apache/tomcat/util/digester/TestObjectCreateRule.java
@@ -0,0 +1,177 @@
+/*
+ * 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.digester;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import org.xml.sax.Attributes;
+
+public class TestObjectCreateRule {
+
+ private Digester digester;
+ private ObjectCreateRule rule;
+
+ @Before
+ public void setUp() {
+ digester = new Digester();
+ rule = new ObjectCreateRule("java.lang.String");
+ rule.setDigester(digester);
+ }
+
+ @Test
+ public void testBeginObjectIsCreatedAndPushed() throws Exception {
+ Attributes attributes = new MockAttributes();
+
+ rule.begin("", "element", attributes);
+
+ // Check if the object was pushed onto the stack
+ Object topObject = digester.pop();
+ Assert.assertTrue(topObject instanceof String);
+ }
+
+ @Test
+ public void testBeginWithAttributeNameOverride() throws Exception {
+ rule = new ObjectCreateRule("java.lang.Object", "className");
+ rule.setDigester(digester);
+
+ MockAttributes attributes = new MockAttributes();
+ attributes.setAttribute("className", "java.lang.String");
+
+ rule.begin("", "element", attributes);
+
+ // Check if the overridden class object was pushed onto the stack
+ Object topObject = digester.pop();
+ Assert.assertTrue(topObject instanceof String);
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void testBeginNullClassNameThrowsException() throws Exception {
+ rule = new ObjectCreateRule(null);
+ rule.setDigester(digester);
+ rule.begin("", "element", new MockAttributes());
+ }
+
+ @Test
+ public void testEndObjectIsPopped() throws Exception {
+ String instance = "Test Object";
+ digester.push(instance);
+
+ rule.end("", "element");
+
+ // Check if the object was popped from the stack
+ Assert.assertNull(digester.peek());
+ }
+
+ @Test
+ public void testToString() {
+ rule = new ObjectCreateRule("java.lang.String", "className");
+ String result = rule.toString();
+ Assert.assertTrue(result.contains("className=java.lang.String"));
+ Assert.assertTrue(result.contains("attributeName=className"));
+ }
+
+ @Test
+ public void testBeginClassNotFoundException() throws Exception {
+ rule = new ObjectCreateRule("non.existent.Class", "className");
+ rule.setDigester(digester);
+
+ MockAttributes attributes = new MockAttributes();
+ attributes.setAttribute("className", "non.existent.Class");
+
+ try {
+ rule.begin("", "element", attributes);
+ Assert.fail("Expected ClassNotFoundException to be thrown");
+ } catch (ClassNotFoundException e) {
+ Assert.assertTrue(e.getMessage().contains("non.existent.Class"));
+ }
+
+ Assert.assertNull(digester.peek());
+ }
+
+ private class MockAttributes implements Attributes {
+
+ private String attributeName;
+ private String attributeValue;
+
+ public void setAttribute(String name, String value) {
+ this.attributeName = name;
+ this.attributeValue = value;
+ }
+
+ @Override
+ public int getLength() {
+ return attributeName != null ? 1 : 0;
+ }
+
+ @Override
+ public String getURI(int index) {
+ return "";
+ }
+
+ @Override
+ public String getLocalName(int index) {
+ return attributeName;
+ }
+
+ @Override
+ public String getQName(int index) {
+ return attributeName;
+ }
+
+ @Override
+ public String getType(int index) {
+ return "CDATA";
+ }
+
+ @Override
+ public String getValue(int index) {
+ return attributeValue;
+ }
+
+ @Override
+ public int getIndex(String uri, String localName) {
+ return attributeName != null && attributeName.equals(localName) ? 0 : -1;
+ }
+
+ @Override
+ public int getIndex(String qName) {
+ return attributeName != null && attributeName.equals(qName) ? 0 : -1;
+ }
+
+ @Override
+ public String getType(String uri, String localName) {
+ return "CDATA";
+ }
+
+ @Override
+ public String getType(String qName) {
+ return "CDATA";
+ }
+
+ @Override
+ public String getValue(String uri, String localName) {
+ return attributeValue;
+ }
+
+ @Override
+ public String getValue(String qName) {
+ return attributeValue;
+ }
+ }
+}
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org