From ecf1499ae6006ca8985b3132bda4ff5fe032c1d5 Mon Sep 17 00:00:00 2001 From: Andrea Aime Date: Sun, 18 Sep 2016 15:42:29 +0200 Subject: [PATCH] Removing powermock dependency --- modules/library/xml/pom.xml | 13 - .../org/geotools/xml/DocumentFactory.java | 7 +- .../org/geotools/xml/XMLHandlerHints.java | 3 + .../org/geotools/xml/DocumentFactoryTest.java | 335 +++++++++--------- pom.xml | 12 - 5 files changed, 177 insertions(+), 193 deletions(-) diff --git a/modules/library/xml/pom.xml b/modules/library/xml/pom.xml index a0fd262543f..5485237a864 100644 --- a/modules/library/xml/pom.xml +++ b/modules/library/xml/pom.xml @@ -135,19 +135,6 @@ 1.9.5 test - - org.powermock - powermock-module-junit4 - 1.5.6 - test - - - org.powermock - powermock-api-mockito - 1.5.6 - test - - diff --git a/modules/library/xml/src/main/java/org/geotools/xml/DocumentFactory.java b/modules/library/xml/src/main/java/org/geotools/xml/DocumentFactory.java index 7b6ded8522e..2cf2618842e 100644 --- a/modules/library/xml/src/main/java/org/geotools/xml/DocumentFactory.java +++ b/modules/library/xml/src/main/java/org/geotools/xml/DocumentFactory.java @@ -165,7 +165,12 @@ public static Object getInstance(InputStream is, Map hints, Level * Convenience method to create an instance of a SAXParser if it is null. */ private static SAXParser getParser(Map hints) throws SAXException { - SAXParserFactory spf = SAXParserFactory.newInstance(); + SAXParserFactory spf = null; + if(hints != null && hints.containsKey(XMLHandlerHints.SAX_PARSER_FACTORY)) { + spf = (SAXParserFactory) hints.get(XMLHandlerHints.SAX_PARSER_FACTORY); + } else { + spf = SAXParserFactory.newInstance(); + } spf.setNamespaceAware(true); spf.setValidating(false); try { diff --git a/modules/library/xml/src/main/java/org/geotools/xml/XMLHandlerHints.java b/modules/library/xml/src/main/java/org/geotools/xml/XMLHandlerHints.java index e40610d8774..f381ca95c6b 100644 --- a/modules/library/xml/src/main/java/org/geotools/xml/XMLHandlerHints.java +++ b/modules/library/xml/src/main/java/org/geotools/xml/XMLHandlerHints.java @@ -46,6 +46,9 @@ public class XMLHandlerHints implements Map { public static final String FILTER_COMPLIANCE_STRICTNESS = "org.geotools.xml.filter.FILTER_COMPLIANCE_STRICTNESS"; /** Supplied {@link EntityResolver} for Schema and/or DTD validation */ public final static String ENTITY_RESOLVER ="org.xml.sax.EntityResolver"; + /** Supplied {@link SaxParserFactory} */ + public final static String SAX_PARSER_FACTORY ="javax.xml.parsers.SAXParserFactory"; + /** * The value so that the parser will encode all Geotools filters with no modifications. */ diff --git a/modules/library/xml/src/test/java/org/geotools/xml/DocumentFactoryTest.java b/modules/library/xml/src/test/java/org/geotools/xml/DocumentFactoryTest.java index 38eba80b85d..51b07caba99 100644 --- a/modules/library/xml/src/test/java/org/geotools/xml/DocumentFactoryTest.java +++ b/modules/library/xml/src/test/java/org/geotools/xml/DocumentFactoryTest.java @@ -1,31 +1,3 @@ -package org.geotools.xml; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -import org.xml.sax.SAXException; -import org.xml.sax.SAXNotRecognizedException; -import org.xml.sax.SAXNotSupportedException; - -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.parsers.SAXParser; -import javax.xml.parsers.SAXParserFactory; -import java.io.IOException; -import java.io.InputStream; -import java.net.URI; -import java.util.HashMap; -import java.util.Map; -import java.util.logging.Level; - -import static org.mockito.Mockito.*; - /** * GeoTools - The Open Source Java GIS Toolkit * http://geotools.org @@ -41,164 +13,193 @@ * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - * - * @author Aaron Waddell */ -@RunWith(PowerMockRunner.class) -@PrepareForTest({ DocumentFactory.class, SAXParserFactory.class }) -public class DocumentFactoryTest { - private final String DISALLOW_DOCTYPE_DECLAIRATION ="http://apache.org/xml/features/disallow-doctype-decl"; - private final String LOAD_EXTERNAL_DTD = "http://apache.org/xml/features/nonvalidating/load-external-dtd"; - private final String EXTERNAL_GENERAL_ENTITIES_FEATURE = - "http://xml.org/sax/features/external-general-entities"; - - private final String EXTERNAL_PARAMETER_ENTITIES_FEATURE = - "http://xml.org/sax/features/external-parameter-entities"; - - private URI uri; - - private Map hints; - - @Mock - private SAXParserFactory mockSaxParserFactory; - - @Mock - private SAXParser mockSaxParser; - - @Mock - InputStream inputStream; - - @Before - public void before() throws Exception { - uri = new URI("http://geotools.org"); - hints = new HashMap(); - - MockitoAnnotations.initMocks(this); - - PowerMockito.mockStatic(SAXParserFactory.class); - PowerMockito.when(SAXParserFactory.newInstance()).thenReturn(mockSaxParserFactory); - - when(mockSaxParserFactory.newSAXParser()).thenReturn(mockSaxParser); - - Answer startDocumentAnswer = new Answer() { - @Override - public Void answer(InvocationOnMock invocationOnMock) throws Throwable { - XMLSAXHandler xmlSaxHandler = (XMLSAXHandler) invocationOnMock - .getArguments()[1]; - xmlSaxHandler.startDocument(); - return null; - } - }; - doAnswer(startDocumentAnswer).when(mockSaxParser) - .parse(anyString(), any(XMLSAXHandler.class)); - doAnswer(startDocumentAnswer).when(mockSaxParser) - .parse(any(InputStream.class), any(XMLSAXHandler.class)); - } - @Test - public void testGetInstanceFromURIWithSpecifiedLevelButNoPDisableExternalEntities() - throws Exception { - DocumentFactory.getInstance(uri, hints, Level.WARNING); - verifyDisableExternalEntities(false); - } +package org.geotools.xml; - @Test - public void testGetInstanceFromURIWithSpecifiedLevelAndDisableExternalEntitiesTrue() - throws Exception { - hints.put(DocumentFactory.DISABLE_EXTERNAL_ENTITIES, Boolean.TRUE); - DocumentFactory.getInstance(uri, hints, Level.WARNING); +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; - verifyDisableExternalEntities(true); - } +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.util.HashMap; +import java.util.Map; +import java.util.logging.Level; - @Test - public void testGetInstanceFromURIWithSpecifiedLevelAndDisableExternalEntitiesFalse() - throws Exception { - hints.put(DocumentFactory.DISABLE_EXTERNAL_ENTITIES, Boolean.FALSE); - DocumentFactory.getInstance(uri, hints, Level.WARNING); +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; - verifyDisableExternalEntities(false); - } +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.xml.sax.SAXException; +import org.xml.sax.SAXNotRecognizedException; +import org.xml.sax.SAXNotSupportedException; - @Test(expected = SAXException.class) - public void testGetInstanceFromURIWithSpecifiedLevelParseFailureIOException() - throws Exception { - doThrow(new IOException("Failure")).when(mockSaxParser) - .parse(anyString(), any(XMLSAXHandler.class)); - DocumentFactory.getInstance(uri, hints, Level.WARNING); - } +/** + * Test for {@link DocumentFactory} + * + * @author Aaron Waddell + */ +public class DocumentFactoryTest { + private final String DISALLOW_DOCTYPE_DECLAIRATION = "http://apache.org/xml/features/disallow-doctype-decl"; - @Test(expected = SAXException.class) - public void testGetInstanceFromURIWithSpecifiedLevelParseFailureSAXException() - throws Exception { - doThrow(new SAXException("Failure")).when(mockSaxParser) - .parse(anyString(), any(XMLSAXHandler.class)); - DocumentFactory.getInstance(uri, hints, Level.WARNING); - } + private final String LOAD_EXTERNAL_DTD = "http://apache.org/xml/features/nonvalidating/load-external-dtd"; - @Test(expected = SAXException.class) - public void testGetInstanceFromURIWithSpecifiedLevelNewSAXParserFailure() - throws Exception { - when(mockSaxParserFactory.newSAXParser()) - .thenThrow(new ParserConfigurationException("Failure")); - DocumentFactory.getInstance(uri, hints, Level.WARNING); - } + private final String EXTERNAL_GENERAL_ENTITIES_FEATURE = "http://xml.org/sax/features/external-general-entities"; - @Test - public void testGetInstanceFromInputStreamWithSpecifiedLevelNoDisableExternalEntities() - throws Exception { - DocumentFactory.getInstance(inputStream, hints, Level.WARNING); + private final String EXTERNAL_PARAMETER_ENTITIES_FEATURE = "http://xml.org/sax/features/external-parameter-entities"; - verifyDisableExternalEntities(false); - } + private URI uri; - @Test - public void testGetInstanceFromInputStreamWithSpecifiedLevelAndDisableExternalEntitiesTrue() - throws Exception { - hints.put(DocumentFactory.DISABLE_EXTERNAL_ENTITIES, Boolean.TRUE); - DocumentFactory.getInstance(inputStream, hints, Level.WARNING); + private Map hints; - verifyDisableExternalEntities(true); - } + @Mock + private SAXParserFactory mockSaxParserFactory; - @Test - public void testGetInstanceFromInputStreamWithSpecifiedLevelAndPDisableExternalEntitiesFalse() - throws Exception { - hints.put(DocumentFactory.DISABLE_EXTERNAL_ENTITIES, Boolean.FALSE); - DocumentFactory.getInstance(inputStream, hints, Level.WARNING); + @Mock + private SAXParser mockSaxParser; - verifyDisableExternalEntities(false); - } + @Mock + InputStream inputStream; - @Test(expected = SAXException.class) - public void testGetInstanceFromInputStreamWithSpecifiedLevelParseFailureIOException() - throws Exception { - doThrow(new IOException("Parse failure")).when(mockSaxParser) - .parse(any(InputStream.class), any(XMLSAXHandler.class)); - DocumentFactory.getInstance(inputStream, hints, Level.WARNING); - } + @Before + public void before() throws Exception { + uri = new URI("http://geotools.org"); + hints = new HashMap(); - @Test(expected = SAXException.class) - public void testGetInstanceFromInputStreamWithSpecifiedLevelParseFailureSAXException() - throws Exception { - doThrow(new SAXException("Parse failure")).when(mockSaxParser) - .parse(any(InputStream.class), any(XMLSAXHandler.class)); - DocumentFactory.getInstance(inputStream, hints, Level.WARNING); - } + MockitoAnnotations.initMocks(this); + hints.put(XMLHandlerHints.SAX_PARSER_FACTORY, mockSaxParserFactory); + when(mockSaxParserFactory.newSAXParser()).thenReturn(mockSaxParser); - void verifyDisableExternalEntities(boolean disabled) throws SAXNotRecognizedException, SAXNotSupportedException, ParserConfigurationException{ - if(disabled){ - verify(mockSaxParserFactory).setFeature(DISALLOW_DOCTYPE_DECLAIRATION, true); - verify(mockSaxParserFactory).setFeature(LOAD_EXTERNAL_DTD, false); - verify(mockSaxParserFactory).setFeature(EXTERNAL_GENERAL_ENTITIES_FEATURE, false); - verify(mockSaxParserFactory).setFeature(EXTERNAL_PARAMETER_ENTITIES_FEATURE, false); - } - else { - verify(mockSaxParserFactory, never()).setFeature(DISALLOW_DOCTYPE_DECLAIRATION, true); - verify(mockSaxParserFactory, never()).setFeature(LOAD_EXTERNAL_DTD, false); - verify(mockSaxParserFactory, never()).setFeature(EXTERNAL_GENERAL_ENTITIES_FEATURE, false); - verify(mockSaxParserFactory, never()).setFeature(EXTERNAL_PARAMETER_ENTITIES_FEATURE, false); + Answer startDocumentAnswer = new Answer() { + @Override + public Void answer(InvocationOnMock invocationOnMock) throws Throwable { + XMLSAXHandler xmlSaxHandler = (XMLSAXHandler) invocationOnMock.getArguments()[1]; + xmlSaxHandler.startDocument(); + return null; } + }; + doAnswer(startDocumentAnswer).when(mockSaxParser).parse(anyString(), + any(XMLSAXHandler.class)); + doAnswer(startDocumentAnswer).when(mockSaxParser).parse(any(InputStream.class), + any(XMLSAXHandler.class)); + } + + @Test + public void testGetInstanceFromURIWithSpecifiedLevelButNoPDisableExternalEntities() + throws Exception { + DocumentFactory.getInstance(uri, hints, Level.WARNING); + verifyDisableExternalEntities(false); + } + + @Test + public void testGetInstanceFromURIWithSpecifiedLevelAndDisableExternalEntitiesTrue() + throws Exception { + hints.put(DocumentFactory.DISABLE_EXTERNAL_ENTITIES, Boolean.TRUE); + DocumentFactory.getInstance(uri, hints, Level.WARNING); + + verifyDisableExternalEntities(true); + } + + @Test + public void testGetInstanceFromURIWithSpecifiedLevelAndDisableExternalEntitiesFalse() + throws Exception { + hints.put(DocumentFactory.DISABLE_EXTERNAL_ENTITIES, Boolean.FALSE); + DocumentFactory.getInstance(uri, hints, Level.WARNING); + + verifyDisableExternalEntities(false); + } + + @Test(expected = SAXException.class) + public void testGetInstanceFromURIWithSpecifiedLevelParseFailureIOException() throws Exception { + doThrow(new IOException("Failure")).when(mockSaxParser).parse(anyString(), + any(XMLSAXHandler.class)); + DocumentFactory.getInstance(uri, hints, Level.WARNING); + } + + @Test(expected = SAXException.class) + public void testGetInstanceFromURIWithSpecifiedLevelParseFailureSAXException() + throws Exception { + doThrow(new SAXException("Failure")).when(mockSaxParser).parse(anyString(), + any(XMLSAXHandler.class)); + DocumentFactory.getInstance(uri, hints, Level.WARNING); + } + + @Test(expected = SAXException.class) + public void testGetInstanceFromURIWithSpecifiedLevelNewSAXParserFailure() throws Exception { + when(mockSaxParserFactory.newSAXParser()) + .thenThrow(new ParserConfigurationException("Failure")); + DocumentFactory.getInstance(uri, hints, Level.WARNING); + } + + @Test + public void testGetInstanceFromInputStreamWithSpecifiedLevelNoDisableExternalEntities() + throws Exception { + DocumentFactory.getInstance(inputStream, hints, Level.WARNING); + + verifyDisableExternalEntities(false); + } + + @Test + public void testGetInstanceFromInputStreamWithSpecifiedLevelAndDisableExternalEntitiesTrue() + throws Exception { + hints.put(DocumentFactory.DISABLE_EXTERNAL_ENTITIES, Boolean.TRUE); + DocumentFactory.getInstance(inputStream, hints, Level.WARNING); + + verifyDisableExternalEntities(true); + } + + @Test + public void testGetInstanceFromInputStreamWithSpecifiedLevelAndPDisableExternalEntitiesFalse() + throws Exception { + hints.put(DocumentFactory.DISABLE_EXTERNAL_ENTITIES, Boolean.FALSE); + DocumentFactory.getInstance(inputStream, hints, Level.WARNING); + + verifyDisableExternalEntities(false); + } + + @Test(expected = SAXException.class) + public void testGetInstanceFromInputStreamWithSpecifiedLevelParseFailureIOException() + throws Exception { + doThrow(new IOException("Parse failure")).when(mockSaxParser).parse(any(InputStream.class), + any(XMLSAXHandler.class)); + DocumentFactory.getInstance(inputStream, hints, Level.WARNING); + } + + @Test(expected = SAXException.class) + public void testGetInstanceFromInputStreamWithSpecifiedLevelParseFailureSAXException() + throws Exception { + doThrow(new SAXException("Parse failure")).when(mockSaxParser).parse(any(InputStream.class), + any(XMLSAXHandler.class)); + DocumentFactory.getInstance(inputStream, hints, Level.WARNING); + } + + void verifyDisableExternalEntities(boolean disabled) throws SAXNotRecognizedException, + SAXNotSupportedException, ParserConfigurationException { + if (disabled) { + verify(mockSaxParserFactory).setFeature(DISALLOW_DOCTYPE_DECLAIRATION, true); + verify(mockSaxParserFactory).setFeature(LOAD_EXTERNAL_DTD, false); + verify(mockSaxParserFactory).setFeature(EXTERNAL_GENERAL_ENTITIES_FEATURE, false); + verify(mockSaxParserFactory).setFeature(EXTERNAL_PARAMETER_ENTITIES_FEATURE, false); + } else { + verify(mockSaxParserFactory, never()).setFeature(DISALLOW_DOCTYPE_DECLAIRATION, true); + verify(mockSaxParserFactory, never()).setFeature(LOAD_EXTERNAL_DTD, false); + verify(mockSaxParserFactory, never()).setFeature(EXTERNAL_GENERAL_ENTITIES_FEATURE, + false); + verify(mockSaxParserFactory, never()).setFeature(EXTERNAL_PARAMETER_ENTITIES_FEATURE, + false); } + } } diff --git a/pom.xml b/pom.xml index db51e94b21e..fe4f52577c9 100644 --- a/pom.xml +++ b/pom.xml @@ -1083,18 +1083,6 @@ mockito-core 1.8.5 - - org.powermock - powermock-module-junit4 - 1.4.9 - test - - - org.powermock - powermock-api-mockito - 1.4.9 - test -