|
| 1 | +package org.openintegrationengine.engine.plugins.datatypes; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertFalse; |
| 5 | +import static org.junit.Assert.assertNull; |
| 6 | +import static org.junit.Assert.assertSame; |
| 7 | +import static org.junit.Assert.assertTrue; |
| 8 | +import static org.mockito.ArgumentMatchers.any; |
| 9 | +import static org.mockito.Mockito.mock; |
| 10 | +import static org.mockito.Mockito.spy; |
| 11 | +import static org.mockito.Mockito.verify; |
| 12 | + |
| 13 | +import java.io.IOException; |
| 14 | + |
| 15 | +import org.junit.Before; |
| 16 | +import org.junit.Test; |
| 17 | +import org.xml.sax.ContentHandler; |
| 18 | +import org.xml.sax.DTDHandler; |
| 19 | +import org.xml.sax.EntityResolver; |
| 20 | +import org.xml.sax.ErrorHandler; |
| 21 | +import org.xml.sax.InputSource; |
| 22 | +import org.xml.sax.SAXException; |
| 23 | +import org.xml.sax.SAXNotRecognizedException; |
| 24 | +import org.xml.sax.SAXNotSupportedException; |
| 25 | + |
| 26 | +public class AbstractXMLReaderTest { |
| 27 | + |
| 28 | + private AbstractXMLReader reader; |
| 29 | + |
| 30 | + @Before |
| 31 | + public void setUp() { |
| 32 | + // Create an instance of the concrete inner class for testing |
| 33 | + reader = new TestXMLReader(); |
| 34 | + } |
| 35 | + |
| 36 | + // --------------------------------------------------------- |
| 37 | + // Handler Getter/Setter Tests |
| 38 | + // --------------------------------------------------------- |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testContentHandlerAccessors() { |
| 42 | + assertNull("Should be null initially", reader.getContentHandler()); |
| 43 | + |
| 44 | + ContentHandler mockHandler = mock(ContentHandler.class); |
| 45 | + reader.setContentHandler(mockHandler); |
| 46 | + |
| 47 | + assertSame("Should return the set handler", mockHandler, reader.getContentHandler()); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testErrorHandlerAccessors() { |
| 52 | + assertNull("Should be null initially", reader.getErrorHandler()); |
| 53 | + |
| 54 | + ErrorHandler mockHandler = mock(ErrorHandler.class); |
| 55 | + reader.setErrorHandler(mockHandler); |
| 56 | + |
| 57 | + assertSame("Should return the set handler", mockHandler, reader.getErrorHandler()); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testDTDHandlerAccessors() { |
| 62 | + assertNull("Should be null initially", reader.getDTDHandler()); |
| 63 | + |
| 64 | + DTDHandler mockHandler = mock(DTDHandler.class); |
| 65 | + reader.setDTDHandler(mockHandler); |
| 66 | + |
| 67 | + assertSame("Should return the set handler", mockHandler, reader.getDTDHandler()); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testEntityResolverAccessors() { |
| 72 | + assertNull("Should be null initially", reader.getEntityResolver()); |
| 73 | + |
| 74 | + EntityResolver mockResolver = mock(EntityResolver.class); |
| 75 | + reader.setEntityResolver(mockResolver); |
| 76 | + |
| 77 | + assertSame("Should return the set resolver", mockResolver, reader.getEntityResolver()); |
| 78 | + } |
| 79 | + |
| 80 | + // --------------------------------------------------------- |
| 81 | + // Helper Method Tests |
| 82 | + // --------------------------------------------------------- |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testEnsureHandlerSetSuccess() throws SAXException { |
| 86 | + // Setup: Set a handler |
| 87 | + reader.setContentHandler(mock(ContentHandler.class)); |
| 88 | + |
| 89 | + // Execute: Should not throw exception |
| 90 | + reader.ensureHandlerSet(); |
| 91 | + } |
| 92 | + |
| 93 | + @Test(expected = SAXException.class) |
| 94 | + public void testEnsureHandlerSetFailure() throws SAXException { |
| 95 | + // Setup: Ensure handler is null |
| 96 | + reader.setContentHandler(null); |
| 97 | + |
| 98 | + // Execute: Should throw SAXException |
| 99 | + reader.ensureHandlerSet(); |
| 100 | + } |
| 101 | + |
| 102 | + // --------------------------------------------------------- |
| 103 | + // Parse Delegation Tests |
| 104 | + // --------------------------------------------------------- |
| 105 | + |
| 106 | + @Test |
| 107 | + public void testParseStringDelegatesToInputSource() throws IOException, SAXException { |
| 108 | + // Spy on the reader to verify method calls |
| 109 | + AbstractXMLReader spyReader = spy(new TestXMLReader()); |
| 110 | + String testUri = "file:///test.xml"; |
| 111 | + |
| 112 | + spyReader.parse(testUri); |
| 113 | + |
| 114 | + // Verify that parse(String) called parse(InputSource) |
| 115 | + verify(spyReader).parse(any(InputSource.class)); |
| 116 | + } |
| 117 | + |
| 118 | + // --------------------------------------------------------- |
| 119 | + // Feature Flag Tests |
| 120 | + // --------------------------------------------------------- |
| 121 | + |
| 122 | + @Test |
| 123 | + public void testGetFeatureNamespaces() throws Exception { |
| 124 | + assertTrue("Namespaces should always be true", |
| 125 | + reader.getFeature("http://xml.org/sax/features/namespaces")); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void testGetFeatureNamespacePrefixes() throws Exception { |
| 130 | + assertFalse("Namespace prefixes should always be false", |
| 131 | + reader.getFeature("http://xml.org/sax/features/namespace-prefixes")); |
| 132 | + } |
| 133 | + |
| 134 | + @Test(expected = SAXNotRecognizedException.class) |
| 135 | + public void testGetFeatureUnknown() throws Exception { |
| 136 | + reader.getFeature("http://xml.org/sax/features/unknown-feature"); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + public void testSetFeatureNamespacesTrue() throws Exception { |
| 141 | + // Should succeed (no-op) |
| 142 | + reader.setFeature("http://xml.org/sax/features/namespaces", true); |
| 143 | + } |
| 144 | + |
| 145 | + @Test(expected = SAXNotSupportedException.class) |
| 146 | + public void testSetFeatureNamespacesFalse() throws Exception { |
| 147 | + // Should fail - cannot disable namespaces |
| 148 | + reader.setFeature("http://xml.org/sax/features/namespaces", false); |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + public void testSetFeatureNamespacePrefixesFalse() throws Exception { |
| 153 | + // Should succeed (no-op) |
| 154 | + reader.setFeature("http://xml.org/sax/features/namespace-prefixes", false); |
| 155 | + } |
| 156 | + |
| 157 | + @Test(expected = SAXNotSupportedException.class) |
| 158 | + public void testSetFeatureNamespacePrefixesTrue() throws Exception { |
| 159 | + // Should fail - cannot enable namespace prefixes |
| 160 | + reader.setFeature("http://xml.org/sax/features/namespace-prefixes", true); |
| 161 | + } |
| 162 | + |
| 163 | + @Test(expected = SAXNotRecognizedException.class) |
| 164 | + public void testSetFeatureUnknown() throws Exception { |
| 165 | + reader.setFeature("http://xml.org/sax/features/unknown-feature", true); |
| 166 | + } |
| 167 | + |
| 168 | + // --------------------------------------------------------- |
| 169 | + // Property Tests |
| 170 | + // --------------------------------------------------------- |
| 171 | + |
| 172 | + @Test(expected = SAXNotRecognizedException.class) |
| 173 | + public void testGetPropertyUnknown() throws Exception { |
| 174 | + reader.getProperty("http://xml.org/sax/properties/lexical-handler"); |
| 175 | + } |
| 176 | + |
| 177 | + @Test(expected = SAXNotRecognizedException.class) |
| 178 | + public void testSetPropertyUnknown() throws Exception { |
| 179 | + reader.setProperty("http://xml.org/sax/properties/lexical-handler", new Object()); |
| 180 | + } |
| 181 | + |
| 182 | + // --------------------------------------------------------- |
| 183 | + // Concrete Stub Class |
| 184 | + // --------------------------------------------------------- |
| 185 | + |
| 186 | + /** |
| 187 | + * Minimal concrete implementation of AbstractXMLReader for testing purposes. |
| 188 | + */ |
| 189 | + private static class TestXMLReader extends AbstractXMLReader { |
| 190 | + @Override |
| 191 | + public void parse(InputSource input) throws IOException, SAXException { |
| 192 | + // No-op for testing base class features |
| 193 | + } |
| 194 | + } |
| 195 | +} |
0 commit comments