From 729963967aeff70ba6ee49e5fb707d8b9cc13bab Mon Sep 17 00:00:00 2001 From: Willem Date: Fri, 13 May 2016 12:30:47 +0200 Subject: [PATCH] Fixed untestable exception and added tests --- .../config/builder/TPQConfigBuilder.java | 6 +--- .../tpquery/model/ModelXMLMarshaller.java | 35 ++++++++++++++----- .../config/TPQueryStoreConfigBuilderTest.java | 7 ++++ .../tpquery/model/ModelXMLMarshallerTest.java | 15 ++++++++ .../tpquery/test-query-invalid.xml | 5 +++ 5 files changed, 54 insertions(+), 14 deletions(-) create mode 100644 ff-tpquery-store/src/test/resources/net/forwardfire/tpquery/test-query-invalid.xml diff --git a/ff-tpquery-store/src/main/java/net/forwardfire/tpquery/config/builder/TPQConfigBuilder.java b/ff-tpquery-store/src/main/java/net/forwardfire/tpquery/config/builder/TPQConfigBuilder.java index 70bb527..6d5f16f 100644 --- a/ff-tpquery-store/src/main/java/net/forwardfire/tpquery/config/builder/TPQConfigBuilder.java +++ b/ff-tpquery-store/src/main/java/net/forwardfire/tpquery/config/builder/TPQConfigBuilder.java @@ -62,11 +62,7 @@ public class TPQConfigBuilder { Validate.notNull(resultBuilder,"Can't build with null builder."); this.storeConfig = storeConfig; this.resultBuilder = resultBuilder; - try { - this.xmlDriver = new ModelXMLMarshaller(); - } catch (JAXBException e) { - throw new TPQConfigException(e); - } + this.xmlDriver = new ModelXMLMarshaller(); this.statementMarshaller = new TPQStatementMarshaller(); } diff --git a/ff-tpquery-store/src/main/java/net/forwardfire/tpquery/model/ModelXMLMarshaller.java b/ff-tpquery-store/src/main/java/net/forwardfire/tpquery/model/ModelXMLMarshaller.java index 455995b..c4f6207 100644 --- a/ff-tpquery-store/src/main/java/net/forwardfire/tpquery/model/ModelXMLMarshaller.java +++ b/ff-tpquery-store/src/main/java/net/forwardfire/tpquery/model/ModelXMLMarshaller.java @@ -24,6 +24,7 @@ package net.forwardfire.tpquery.model; import java.io.InputStream; import java.io.OutputStream; +import java.util.function.Supplier; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; @@ -32,6 +33,8 @@ import javax.xml.bind.Unmarshaller; import org.apache.commons.lang3.Validate; +import net.forwardfire.tpquery.config.TPQConfigException; + /** * Jaxb model marshaller of the query sets. * @@ -41,21 +44,35 @@ import org.apache.commons.lang3.Validate; public class ModelXMLMarshaller extends ModelXMLInfoSet { private final JAXBContext jaxbContext; - + /** - * Creates a query set xml marshaller. - * @throws JAXBException When context could not be build. + * Creates a query set xml marshaller. + * @param jaxbContextSupplier The jaxb context supplier. */ - public ModelXMLMarshaller() throws JAXBException { - this(JAXBContext.newInstance(TPQuerySet.class)); + public ModelXMLMarshaller(Supplier jaxbContextSupplier) { + this.jaxbContext = Validate.notNull(Validate.notNull(jaxbContextSupplier,"jaxbContextSupplier is null.").get(),"jaxbContext from supplier is null."); } /** - * Creates a query set xml marshaller. - * @param jaxbContext The jaxb query set context. + * Creates a query set xml marshaller. + * @param classes The root jaxb classes to create the context for. */ - public ModelXMLMarshaller(JAXBContext jaxbContext) { - this.jaxbContext = Validate.notNull(jaxbContext,"jaxbContext is null."); + protected ModelXMLMarshaller(Class ...classes) { + // protected + lamda stuff so it can be fully tested + this(() -> { + try { + return JAXBContext.newInstance(classes); + } catch (JAXBException e) { + throw new TPQConfigException(e); + } + }); + } + + /** + * Creates a query set xml marshaller. + */ + public ModelXMLMarshaller() { + this(TPQuerySet.class); } /** diff --git a/ff-tpquery-store/src/test/java/net/forwardfire/tpquery/config/TPQueryStoreConfigBuilderTest.java b/ff-tpquery-store/src/test/java/net/forwardfire/tpquery/config/TPQueryStoreConfigBuilderTest.java index e63330a..061275d 100644 --- a/ff-tpquery-store/src/test/java/net/forwardfire/tpquery/config/TPQueryStoreConfigBuilderTest.java +++ b/ff-tpquery-store/src/test/java/net/forwardfire/tpquery/config/TPQueryStoreConfigBuilderTest.java @@ -134,4 +134,11 @@ public class TPQueryStoreConfigBuilderTest { .readQuerySet(new File("./src/test/resources/net/forwardfire/tpquery/test-query.not-found")) .build(); } + + @Test(expected=TPQConfigException.class) + public void testReadQuerySetInvalidContext() throws Exception { + TPQFactory.createManagerBuilder() + .readQuerySet(new File("./src/test/resources/net/forwardfire/tpquery/test-query.not-found")) + .build(); + } } diff --git a/ff-tpquery-store/src/test/java/net/forwardfire/tpquery/model/ModelXMLMarshallerTest.java b/ff-tpquery-store/src/test/java/net/forwardfire/tpquery/model/ModelXMLMarshallerTest.java index 443dd10..6972e42 100644 --- a/ff-tpquery-store/src/test/java/net/forwardfire/tpquery/model/ModelXMLMarshallerTest.java +++ b/ff-tpquery-store/src/test/java/net/forwardfire/tpquery/model/ModelXMLMarshallerTest.java @@ -9,10 +9,14 @@ import java.io.ByteArrayOutputStream; import java.util.HashMap; import java.util.Map; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; + import org.junit.Test; import net.forwardfire.tpquery.TPQFactory; import net.forwardfire.tpquery.TPQManager; +import net.forwardfire.tpquery.config.TPQConfigException; import net.forwardfire.tpquery.model.TPQuery; import net.forwardfire.tpquery.model.TPQueryParameter; import net.forwardfire.tpquery.model.TPQuerySet; @@ -113,4 +117,15 @@ public class ModelXMLMarshallerTest { assertEquals("junit",q.getQueryHints().keySet().iterator().next()); assertEquals("false",q.getQueryHints().get("junit")); } + + @Test(expected=TPQConfigException.class) + public void testContextException() throws Exception { + new ModelXMLMarshaller(JaxbContextError.class); + } + + private static class JaxbContextError { + @XmlElementWrapper + @XmlElement + private String test; + } } diff --git a/ff-tpquery-store/src/test/resources/net/forwardfire/tpquery/test-query-invalid.xml b/ff-tpquery-store/src/test/resources/net/forwardfire/tpquery/test-query-invalid.xml new file mode 100644 index 0000000..574ce4f --- /dev/null +++ b/ff-tpquery-store/src/test/resources/net/forwardfire/tpquery/test-query-invalid.xml @@ -0,0 +1,5 @@ + + + + +