Skip to content

Commit bf0392d

Browse files
committed
add AbstractXMLReaderTest
Signed-off-by: Tony Germano <tony@germano.name>
1 parent cfe08df commit bf0392d

File tree

1 file changed

+235
-0
lines changed

1 file changed

+235
-0
lines changed
Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
// SPDX-License-Identifier: MPL-2.0
2+
// SPDX-FileCopyrightText: 2025 Tony Germano
3+
4+
package org.openintegrationengine.engine.plugins.datatypes;
5+
6+
import static org.junit.Assert.assertFalse;
7+
import static org.junit.Assert.assertNull;
8+
import static org.junit.Assert.assertSame;
9+
import static org.junit.Assert.assertTrue;
10+
import static org.mockito.ArgumentMatchers.any;
11+
import static org.mockito.ArgumentMatchers.eq;
12+
import static org.mockito.Mockito.inOrder;
13+
import static org.mockito.Mockito.mock;
14+
import static org.mockito.Mockito.spy;
15+
import static org.mockito.Mockito.verify;
16+
17+
import java.io.IOException;
18+
19+
import org.junit.Before;
20+
import org.junit.Test;
21+
import org.mockito.InOrder;
22+
import org.xml.sax.ContentHandler;
23+
import org.xml.sax.DTDHandler;
24+
import org.xml.sax.EntityResolver;
25+
import org.xml.sax.ErrorHandler;
26+
import org.xml.sax.InputSource;
27+
import org.xml.sax.SAXException;
28+
import org.xml.sax.SAXNotRecognizedException;
29+
import org.xml.sax.SAXNotSupportedException;
30+
import org.xml.sax.helpers.AttributesImpl;
31+
32+
public class AbstractXMLReaderTest {
33+
34+
private AbstractXMLReader reader;
35+
36+
@Before
37+
public void setUp() {
38+
reader = new TestXMLReader();
39+
}
40+
41+
// ---------------------------------------------------------
42+
// Parsing Logic Tests
43+
// ---------------------------------------------------------
44+
45+
@Test
46+
public void testParseEmitsSaxEvents() throws Exception {
47+
// Mock the ContentHandler to verify events
48+
ContentHandler handler = mock(ContentHandler.class);
49+
reader.setContentHandler(handler);
50+
51+
// Execute parse
52+
reader.parse(new InputSource());
53+
54+
// Verify the exact sequence of SAX events was fired
55+
InOrder inOrder = inOrder(handler);
56+
inOrder.verify(handler).startDocument();
57+
inOrder.verify(handler).startElement(eq(""), eq("test"), eq("test"), any(AttributesImpl.class));
58+
inOrder.verify(handler).characters(eq("value".toCharArray()), eq(0), eq(5));
59+
inOrder.verify(handler).endElement(eq(""), eq("test"), eq("test"));
60+
inOrder.verify(handler).endDocument();
61+
}
62+
63+
@Test
64+
public void testParseStringDelegatesToInputSource() throws IOException, SAXException {
65+
// Spy on the reader to verify method calls
66+
AbstractXMLReader spyReader = spy(new TestXMLReader());
67+
String testUri = "file:///test.xml";
68+
69+
// Set a dummy handler so ensuringHandlerSet doesn't fail
70+
spyReader.setContentHandler(mock(ContentHandler.class));
71+
72+
spyReader.parse(testUri);
73+
74+
// Verify that parse(String) called parse(InputSource)
75+
verify(spyReader).parse(any(InputSource.class));
76+
}
77+
78+
// ---------------------------------------------------------
79+
// Handler Getter/Setter Tests
80+
// ---------------------------------------------------------
81+
82+
@Test
83+
public void testContentHandlerAccessors() {
84+
assertNull("Should be null initially", reader.getContentHandler());
85+
86+
ContentHandler mockHandler = mock(ContentHandler.class);
87+
reader.setContentHandler(mockHandler);
88+
89+
assertSame("Should return the set handler", mockHandler, reader.getContentHandler());
90+
}
91+
92+
@Test
93+
public void testErrorHandlerAccessors() {
94+
assertNull("Should be null initially", reader.getErrorHandler());
95+
96+
ErrorHandler mockHandler = mock(ErrorHandler.class);
97+
reader.setErrorHandler(mockHandler);
98+
99+
assertSame("Should return the set handler", mockHandler, reader.getErrorHandler());
100+
}
101+
102+
@Test
103+
public void testDTDHandlerAccessors() {
104+
assertNull("Should be null initially", reader.getDTDHandler());
105+
106+
DTDHandler mockHandler = mock(DTDHandler.class);
107+
reader.setDTDHandler(mockHandler);
108+
109+
assertSame("Should return the set handler", mockHandler, reader.getDTDHandler());
110+
}
111+
112+
@Test
113+
public void testEntityResolverAccessors() {
114+
assertNull("Should be null initially", reader.getEntityResolver());
115+
116+
EntityResolver mockResolver = mock(EntityResolver.class);
117+
reader.setEntityResolver(mockResolver);
118+
119+
assertSame("Should return the set resolver", mockResolver, reader.getEntityResolver());
120+
}
121+
122+
// ---------------------------------------------------------
123+
// Helper Method Tests
124+
// ---------------------------------------------------------
125+
126+
@Test
127+
public void testEnsureHandlerSetSuccess() throws SAXException {
128+
// Setup: Set a handler
129+
reader.setContentHandler(mock(ContentHandler.class));
130+
131+
// Execute: Should not throw exception
132+
reader.ensureHandlerSet();
133+
}
134+
135+
@Test(expected = SAXException.class)
136+
public void testEnsureHandlerSetFailure() throws SAXException {
137+
// Setup: Ensure handler is null
138+
reader.setContentHandler(null);
139+
140+
// Execute: Should throw SAXException
141+
reader.ensureHandlerSet();
142+
}
143+
144+
// ---------------------------------------------------------
145+
// Feature Flag Tests
146+
// ---------------------------------------------------------
147+
148+
@Test
149+
public void testGetFeatureNamespaces() throws Exception {
150+
assertTrue("Namespaces should always be true",
151+
reader.getFeature("http://xml.org/sax/features/namespaces"));
152+
}
153+
154+
@Test
155+
public void testGetFeatureNamespacePrefixes() throws Exception {
156+
assertFalse("Namespace prefixes should always be false",
157+
reader.getFeature("http://xml.org/sax/features/namespace-prefixes"));
158+
}
159+
160+
@Test(expected = SAXNotRecognizedException.class)
161+
public void testGetFeatureUnknown() throws Exception {
162+
reader.getFeature("http://xml.org/sax/features/unknown-feature");
163+
}
164+
165+
@Test
166+
public void testSetFeatureNamespacesTrue() throws Exception {
167+
// Should succeed (no-op)
168+
reader.setFeature("http://xml.org/sax/features/namespaces", true);
169+
}
170+
171+
@Test(expected = SAXNotSupportedException.class)
172+
public void testSetFeatureNamespacesFalse() throws Exception {
173+
// Should fail - cannot disable namespaces
174+
reader.setFeature("http://xml.org/sax/features/namespaces", false);
175+
}
176+
177+
@Test
178+
public void testSetFeatureNamespacePrefixesFalse() throws Exception {
179+
// Should succeed (no-op)
180+
reader.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
181+
}
182+
183+
@Test(expected = SAXNotSupportedException.class)
184+
public void testSetFeatureNamespacePrefixesTrue() throws Exception {
185+
// Should fail - cannot enable namespace prefixes
186+
reader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
187+
}
188+
189+
@Test(expected = SAXNotRecognizedException.class)
190+
public void testSetFeatureUnknown() throws Exception {
191+
reader.setFeature("http://xml.org/sax/features/unknown-feature", true);
192+
}
193+
194+
// ---------------------------------------------------------
195+
// Property Tests
196+
// ---------------------------------------------------------
197+
198+
@Test(expected = SAXNotRecognizedException.class)
199+
public void testGetPropertyUnknown() throws Exception {
200+
reader.getProperty("http://xml.org/sax/properties/lexical-handler");
201+
}
202+
203+
@Test(expected = SAXNotRecognizedException.class)
204+
public void testSetPropertyUnknown() throws Exception {
205+
reader.setProperty("http://xml.org/sax/properties/lexical-handler", new Object());
206+
}
207+
208+
// ---------------------------------------------------------
209+
// Concrete Implementation for Testing
210+
// ---------------------------------------------------------
211+
212+
/**
213+
* A concrete implementation that simulates parsing a simple XML document:
214+
* <test>value</test>
215+
*/
216+
private static class TestXMLReader extends AbstractXMLReader {
217+
@Override
218+
public void parse(InputSource input) throws IOException, SAXException {
219+
// Verify handler is present
220+
ensureHandlerSet();
221+
222+
// Simulate parsing <test>value</test>
223+
contentHandler.startDocument();
224+
225+
AttributesImpl atts = new AttributesImpl();
226+
contentHandler.startElement("", "test", "test", atts);
227+
228+
String text = "value";
229+
contentHandler.characters(text.toCharArray(), 0, text.length());
230+
231+
contentHandler.endElement("", "test", "test");
232+
contentHandler.endDocument();
233+
}
234+
}
235+
}

0 commit comments

Comments
 (0)