Fixed untestable exception and added tests

This commit is contained in:
Willem 2016-05-13 12:30:47 +02:00
parent 2d383ff50e
commit 729963967a
5 changed files with 54 additions and 14 deletions

View file

@ -62,11 +62,7 @@ public class TPQConfigBuilder<T> {
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();
}

View file

@ -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.
*
@ -44,18 +47,32 @@ public class ModelXMLMarshaller extends ModelXMLInfoSet {
/**
* Creates a query set xml marshaller.
* @throws JAXBException When context could not be build.
* @param jaxbContextSupplier The jaxb context supplier.
*/
public ModelXMLMarshaller() throws JAXBException {
this(JAXBContext.newInstance(TPQuerySet.class));
public ModelXMLMarshaller(Supplier<JAXBContext> 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.
* @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);
}
/**

View file

@ -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();
}
}

View file

@ -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;
}
}

View file

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<query-set name="test" language="SQL">
<this-will-throw-jaxb-error/>
</query-set>