From 1e69054eb7c7ecd84a709ff7a576d4ca93b090d4 Mon Sep 17 00:00:00 2001 From: Willem Date: Mon, 27 Jul 2015 21:17:00 +0200 Subject: [PATCH] converted to Validate object --- .../store/executor/jdbc/TPQExecutorJdbc.java | 7 ++++--- .../store/executor/jpa/TPQExecutorJpaTest.java | 1 - .../tpquery/config/builder/TPQConfigBuilder.java | 13 +++++++------ .../tpquery/model/TPQuerySetXMLMarshaller.java | 12 ++++++------ .../statement/TPQStatementMarshaller.java | 5 ++--- .../tpquery/store/TPQueryStoreStatement.java | 16 +++++++--------- .../store/executor/AbstractTPQExecutor.java | 14 +++++--------- .../tpquery/store/manager/TPQStoreManager.java | 3 --- .../store/manager/TPQStoreManagerEntry.java | 7 +++---- .../TPQStoreManagerEntryParameterMetaData.java | 8 +++----- .../store/manager/TPQStoreManagerEntryView.java | 6 +++--- .../manager/TPQStoreManagerStatementContext.java | 15 +++++++-------- .../store/manager/TPQStoreManagerTest.java | 4 +--- .../store/proxy/TPQueryProxyFactoryTest.java | 13 +++++++------ 14 files changed, 55 insertions(+), 69 deletions(-) diff --git a/tpquery-executor-jdbc/src/main/java/net/forwardfire/tpquery/store/executor/jdbc/TPQExecutorJdbc.java b/tpquery-executor-jdbc/src/main/java/net/forwardfire/tpquery/store/executor/jdbc/TPQExecutorJdbc.java index f059c5f..6aa0ba2 100644 --- a/tpquery-executor-jdbc/src/main/java/net/forwardfire/tpquery/store/executor/jdbc/TPQExecutorJdbc.java +++ b/tpquery-executor-jdbc/src/main/java/net/forwardfire/tpquery/store/executor/jdbc/TPQExecutorJdbc.java @@ -26,7 +26,8 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Map; -import java.util.Objects; + +import org.apache.commons.lang3.Validate; import net.forwardfire.tpquery.TPQFactory; import net.forwardfire.tpquery.store.executor.AbstractTPQExecutor; @@ -77,7 +78,7 @@ public class TPQExecutorJdbc extends AbstractTPQExecutor { * @throws JAXBException If the jaxb marshaller throws an error. */ public TPQConfigBuilder(TPQConfig storeConfig,Supplier resultBuilder) throws JAXBException { - Objects.requireNonNull(storeConfig,"Can't build null config."); - Objects.requireNonNull(resultBuilder,"Can't build with null builder."); + Validate.notNull(storeConfig,"Can't build null config."); + Validate.notNull(resultBuilder,"Can't build with null builder."); this.storeConfig = storeConfig; this.resultBuilder = resultBuilder; this.xmlDriver = new TPQuerySetXMLMarshaller(); @@ -89,7 +90,7 @@ public class TPQConfigBuilder { } protected TPQConfigBuilder readQuerySet(InputStream input,String systemId) throws JAXBException { - Objects.requireNonNull(input, "Can't read null InputStream"); + Validate.notNull(input, "Can't read null InputStream"); return addQuerySet(systemId,xmlDriver.unmarshal(input)); } @@ -100,7 +101,7 @@ public class TPQConfigBuilder { * @throws JAXBException When marshall error. */ public TPQConfigBuilder readQuerySet(String resource) throws JAXBException { - Objects.requireNonNull(resource, "Can't read null resource"); + Validate.notNull(resource, "Can't read null resource"); return readQuerySet(getClass().getClassLoader().getResourceAsStream(resource),resource); } @@ -111,7 +112,7 @@ public class TPQConfigBuilder { * @throws JAXBException When marshall error. */ public TPQConfigBuilder readQuerySet(File file) throws JAXBException { - Objects.requireNonNull(file, "Can't read null File"); + Validate.notNull(file, "Can't read null File"); try { return readQuerySet(new FileInputStream(file),file.getAbsolutePath()); } catch (FileNotFoundException e) { diff --git a/tpquery-store/src/main/java/net/forwardfire/tpquery/model/TPQuerySetXMLMarshaller.java b/tpquery-store/src/main/java/net/forwardfire/tpquery/model/TPQuerySetXMLMarshaller.java index 7f0ca83..664906e 100644 --- a/tpquery-store/src/main/java/net/forwardfire/tpquery/model/TPQuerySetXMLMarshaller.java +++ b/tpquery-store/src/main/java/net/forwardfire/tpquery/model/TPQuerySetXMLMarshaller.java @@ -24,13 +24,14 @@ package net.forwardfire.tpquery.model; import java.io.InputStream; import java.io.OutputStream; -import java.util.Objects; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; +import org.apache.commons.lang3.Validate; + /** * Jaxb marshaller of the query sets. * @@ -54,8 +55,7 @@ public class TPQuerySetXMLMarshaller extends AbstractXMLMarshaller { * @param querySetContext The jaxb query set context. */ public TPQuerySetXMLMarshaller(JAXBContext querySetContext) { - Objects.requireNonNull(querySetContext,"querySetContext is null."); - this.querySetContext = querySetContext; + this.querySetContext = Validate.notNull(querySetContext,"querySetContext is null."); } /** @@ -65,8 +65,8 @@ public class TPQuerySetXMLMarshaller extends AbstractXMLMarshaller { * @throws JAXBException When error. */ public void marshal(TPQuerySet querySet, OutputStream output) throws JAXBException { - Objects.requireNonNull(querySet,"querySet is null."); - Objects.requireNonNull(output,"OutputStream is null."); + Validate.notNull(querySet,"querySet is null."); + Validate.notNull(output,"OutputStream is null."); Marshaller marshaller = querySetContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); @@ -81,7 +81,7 @@ public class TPQuerySetXMLMarshaller extends AbstractXMLMarshaller { * @throws JAXBException When error. */ public TPQuerySet unmarshal(InputStream input) throws JAXBException { - Objects.requireNonNull(input,"InputStream is null."); + Validate.notNull(input,"InputStream is null."); Unmarshaller unmarshaller = querySetContext.createUnmarshaller(); return (TPQuerySet) unmarshaller.unmarshal(input); diff --git a/tpquery-store/src/main/java/net/forwardfire/tpquery/statement/TPQStatementMarshaller.java b/tpquery-store/src/main/java/net/forwardfire/tpquery/statement/TPQStatementMarshaller.java index e1f4e92..46fc364 100644 --- a/tpquery-store/src/main/java/net/forwardfire/tpquery/statement/TPQStatementMarshaller.java +++ b/tpquery-store/src/main/java/net/forwardfire/tpquery/statement/TPQStatementMarshaller.java @@ -24,7 +24,6 @@ package net.forwardfire.tpquery.statement; import java.util.ArrayList; import java.util.List; -import java.util.Objects; import org.apache.commons.lang3.Validate; @@ -46,7 +45,7 @@ public class TPQStatementMarshaller { * @return The statements as text. */ public String marshal(List parts) { - Objects.requireNonNull(parts,"Can't print null list."); + Validate.notNull(parts,"Can't print null list."); StringBuilder buf = new StringBuilder(DEFAULT_PRINT_SIZE); for (TPQStatement part : parts) { if (part instanceof TPQStatementPartParameter) { @@ -71,7 +70,7 @@ public class TPQStatementMarshaller { * @return The list with statement parts. */ public List unmarshal(String statementText) { - Objects.requireNonNull(statementText,"Can't parse null text."); + Validate.notNull(statementText,"Can't parse null text."); List result = new ArrayList<>(); parseLoop(result,statementText); return result; diff --git a/tpquery-store/src/main/java/net/forwardfire/tpquery/store/TPQueryStoreStatement.java b/tpquery-store/src/main/java/net/forwardfire/tpquery/store/TPQueryStoreStatement.java index 044421f..7a1c740 100644 --- a/tpquery-store/src/main/java/net/forwardfire/tpquery/store/TPQueryStoreStatement.java +++ b/tpquery-store/src/main/java/net/forwardfire/tpquery/store/TPQueryStoreStatement.java @@ -23,6 +23,7 @@ package net.forwardfire.tpquery.store; import java.io.IOException; + import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.ObjectStreamException; @@ -32,7 +33,8 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Objects; + +import org.apache.commons.lang3.Validate; /** * Holds the written statements and meta info. @@ -60,15 +62,11 @@ public class TPQueryStoreStatement implements Serializable { * @param valueMapping The value mapping of the statement. */ public TPQueryStoreStatement(String name,String statement,String language,Integer timeout,Map queryHints,List valueMapping) { - Objects.requireNonNull(name,"name is null"); - Objects.requireNonNull(statement,"statement is null"); - Objects.requireNonNull(language,"language is null"); - Objects.requireNonNull(queryHints,"properties is null"); - this.name=name; - this.statement=statement; + this.name=Validate.notBlank(name,"name is null"); + this.statement=Validate.notBlank(statement,"statement is null"); this.timeout=timeout; // null is no timeout - this.language=language; - this.queryHints=Collections.unmodifiableMap(queryHints); + this.language=Validate.notBlank(language,"language is null"); + this.queryHints=Collections.unmodifiableMap(Validate.notNull(queryHints,"queryHints is null")); this.valueMapping=Collections.unmodifiableList(valueMapping); } diff --git a/tpquery-store/src/main/java/net/forwardfire/tpquery/store/executor/AbstractTPQExecutor.java b/tpquery-store/src/main/java/net/forwardfire/tpquery/store/executor/AbstractTPQExecutor.java index 43fb653..5f57f4d 100644 --- a/tpquery-store/src/main/java/net/forwardfire/tpquery/store/executor/AbstractTPQExecutor.java +++ b/tpquery-store/src/main/java/net/forwardfire/tpquery/store/executor/AbstractTPQExecutor.java @@ -25,9 +25,9 @@ package net.forwardfire.tpquery.store.executor; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Objects; import java.util.function.BiFunction; +import org.apache.commons.lang3.Validate; /** * The abstract query executor make implementing an executor easier. @@ -48,15 +48,12 @@ public abstract class AbstractTPQExecutor { * @param context The executor context. */ public AbstractTPQExecutor(TPQExecutorContext context) { - Objects.requireNonNull(context,"Can't execute with null context."); - this.context = context; - backendTypes = new HashMap<>(); + this.context = Validate.notNull(context,"Can't execute with null context."); + this.backendTypes = new HashMap<>(); } protected void registrateStatementCreator(String language,BiFunction statementCreator) { - Objects.requireNonNull(language,"Can't registrate null language"); - Objects.requireNonNull(statementCreator,"Can't registrate null StatementCreator"); - backendTypes.put(language, statementCreator); + backendTypes.put(Validate.notNull(language,"Can't registrate null language"), Validate.notNull(statementCreator,"Can't registrate null StatementCreator")); } /** @@ -115,8 +112,7 @@ public abstract class AbstractTPQExecutor { protected abstract void prepareTimeout(S statement,Integer timeout); protected S prepare(C connection,TPQExecutorStatement query) { - BiFunction statementCreator = backendTypes.get(query.getLanguage()); - Objects.requireNonNull(statementCreator,"Unsupported language type: "+query.getLanguage()); + BiFunction statementCreator = Validate.notNull(backendTypes.get(query.getLanguage()),"Unsupported language type: {}",query.getLanguage()); S result = statementCreator.apply(connection, query.getStatement()); prepareParameters(result, query); Integer timeout = query.getTimeout(); diff --git a/tpquery-store/src/main/java/net/forwardfire/tpquery/store/manager/TPQStoreManager.java b/tpquery-store/src/main/java/net/forwardfire/tpquery/store/manager/TPQStoreManager.java index b20a9fa..870b080 100644 --- a/tpquery-store/src/main/java/net/forwardfire/tpquery/store/manager/TPQStoreManager.java +++ b/tpquery-store/src/main/java/net/forwardfire/tpquery/store/manager/TPQStoreManager.java @@ -22,8 +22,6 @@ */ package net.forwardfire.tpquery.store.manager; -import java.util.Objects; - import javax.script.CompiledScript; import net.forwardfire.tpquery.TPQManager; @@ -57,7 +55,6 @@ public class TPQStoreManager implements TPQManager { } private TPQStoreManager(TPQStoreManagerConfig config) { - Objects.requireNonNull(config); this.queryStore = new TPQStoreManagerEntryView(config); TPQStatementContext statementContext = new TPQStoreManagerStatementContext(config); this.executorContext = new TPQStoreManagerExecutorContext(config,statementContext); diff --git a/tpquery-store/src/main/java/net/forwardfire/tpquery/store/manager/TPQStoreManagerEntry.java b/tpquery-store/src/main/java/net/forwardfire/tpquery/store/manager/TPQStoreManagerEntry.java index 87570c8..391eabb 100644 --- a/tpquery-store/src/main/java/net/forwardfire/tpquery/store/manager/TPQStoreManagerEntry.java +++ b/tpquery-store/src/main/java/net/forwardfire/tpquery/store/manager/TPQStoreManagerEntry.java @@ -30,6 +30,7 @@ import java.util.Objects; import javax.script.CompiledScript; import org.apache.commons.lang3.ClassUtils; +import org.apache.commons.lang3.Validate; import net.forwardfire.tpquery.config.TPQConfigParameterValueHolder; import net.forwardfire.tpquery.model.TPQuery; @@ -64,10 +65,8 @@ public final class TPQStoreManagerEntry { * @param systemId The systemId of the query. */ public TPQStoreManagerEntry(TPQuery query,String systemId) { - Objects.requireNonNull(query); - Objects.requireNonNull(systemId); - this.query=query; - this.systemId=systemId; + this.query=Validate.notNull(query); + this.systemId=Validate.notBlank(systemId); this.queryHints=new HashMap<>(); this.parameterDefaultCompiledScripts=new HashMap<>(); this.parameterDefaultValues=new HashMap<>(); diff --git a/tpquery-store/src/main/java/net/forwardfire/tpquery/store/manager/TPQStoreManagerEntryParameterMetaData.java b/tpquery-store/src/main/java/net/forwardfire/tpquery/store/manager/TPQStoreManagerEntryParameterMetaData.java index 05ef28a..0f2c478 100644 --- a/tpquery-store/src/main/java/net/forwardfire/tpquery/store/manager/TPQStoreManagerEntryParameterMetaData.java +++ b/tpquery-store/src/main/java/net/forwardfire/tpquery/store/manager/TPQStoreManagerEntryParameterMetaData.java @@ -22,7 +22,7 @@ */ package net.forwardfire.tpquery.store.manager; -import java.util.Objects; +import org.apache.commons.lang3.Validate; import net.forwardfire.tpquery.store.TPQueryParameterMetaData; @@ -47,10 +47,8 @@ public class TPQStoreManagerEntryParameterMetaData implements TPQueryParameterMe * @param required The required info the of parameter. */ public TPQStoreManagerEntryParameterMetaData(String name,Class valueType,boolean nullable,boolean required) { - Objects.requireNonNull(name); - Objects.requireNonNull(valueType); - this.name=name; - this.valueType=valueType; + this.name=Validate.notBlank(name); + this.valueType=Validate.notNull(valueType); this.nullable=nullable; this.required=required; } diff --git a/tpquery-store/src/main/java/net/forwardfire/tpquery/store/manager/TPQStoreManagerEntryView.java b/tpquery-store/src/main/java/net/forwardfire/tpquery/store/manager/TPQStoreManagerEntryView.java index 0541f2a..402cc99 100644 --- a/tpquery-store/src/main/java/net/forwardfire/tpquery/store/manager/TPQStoreManagerEntryView.java +++ b/tpquery-store/src/main/java/net/forwardfire/tpquery/store/manager/TPQStoreManagerEntryView.java @@ -24,7 +24,8 @@ package net.forwardfire.tpquery.store.manager; import java.util.List; import java.util.Map; -import java.util.Objects; + +import org.apache.commons.lang3.Validate; import net.forwardfire.tpquery.store.TPQueryParameterMetaData; import net.forwardfire.tpquery.store.TPQueryStore; @@ -44,8 +45,7 @@ public final class TPQStoreManagerEntryView implements TPQueryStore { * @param config The config from which the data comes. */ public TPQStoreManagerEntryView(TPQStoreManagerConfig config) { - Objects.requireNonNull(config); - this.config = config; + this.config = Validate.notNull(config); } @Override diff --git a/tpquery-store/src/main/java/net/forwardfire/tpquery/store/manager/TPQStoreManagerStatementContext.java b/tpquery-store/src/main/java/net/forwardfire/tpquery/store/manager/TPQStoreManagerStatementContext.java index a418d1f..fe3d608 100644 --- a/tpquery-store/src/main/java/net/forwardfire/tpquery/store/manager/TPQStoreManagerStatementContext.java +++ b/tpquery-store/src/main/java/net/forwardfire/tpquery/store/manager/TPQStoreManagerStatementContext.java @@ -24,7 +24,6 @@ package net.forwardfire.tpquery.store.manager; import java.util.HashMap; import java.util.Map; -import java.util.Objects; import org.apache.commons.lang3.Validate; @@ -56,27 +55,27 @@ public class TPQStoreManagerStatementContext implements TPQStatementContext { @Override public TPQuery getQuery(String name) { - Validate.notNull(name,"Can't search null name "); + Validate.notBlank(name,"Can't search null name "); TPQStoreManagerEntry result = Validate.notNull(config.getEntries().get(name),"Could not find query: %s,",name); return Validate.notNull(result.getQuery()); } @Override public TPQStatementParameter getParameterType(TPQueryParameter param) { - Objects.requireNonNull(param,"Can' search null parameter "); - return Objects.requireNonNull(config.getStatementParameters().get(param.getType()),"Could not find parameter type: "+param.getType()+" of: "+param.getName()); + Validate.notNull(param,"Can' search null parameter "); + return Validate.notNull(config.getStatementParameters().get(param.getType()),"Could not find parameter type: "+param.getType()+" of: "+param.getName()); } @Override public TPQStatementLanguage getStatementLanguage(TPQuery query) { - Objects.requireNonNull(query,"Can' search null query "); - return Objects.requireNonNull(config.getStatementLanguages().get(query.getLanguage()),"Could not find language type: "+query.getLanguage()+" of: "+query.getName()); + Validate.notNull(query,"Can' search null query "); + return Validate.notNull(config.getStatementLanguages().get(query.getLanguage()),"Could not find language type: "+query.getLanguage()+" of: "+query.getName()); } @Override public TPQueryParameter getQueryParameterByName(TPQuery query, String name) { - Objects.requireNonNull(query,"Can' search null query "); - Objects.requireNonNull(name,"Can' search null name "); + Validate.notNull(query,"Can' search null query "); + Validate.notBlank(name,"Can' search null name "); TPQueryParameter result = null; for (TPQueryParameter param:query.getQueryParameters()) { // note: slow path as only used in init, else mod data model. if (name.equals(param.getName())) { diff --git a/tpquery-store/src/test/java/net/forwardfire/tpquery/store/manager/TPQStoreManagerTest.java b/tpquery-store/src/test/java/net/forwardfire/tpquery/store/manager/TPQStoreManagerTest.java index 523835e..d1a8a23 100644 --- a/tpquery-store/src/test/java/net/forwardfire/tpquery/store/manager/TPQStoreManagerTest.java +++ b/tpquery-store/src/test/java/net/forwardfire/tpquery/store/manager/TPQStoreManagerTest.java @@ -1,7 +1,6 @@ package net.forwardfire.tpquery.store.manager; import java.util.HashMap; -import java.util.Map; import net.forwardfire.tpquery.TPQManager; import net.forwardfire.tpquery.TPQFactory; @@ -16,7 +15,7 @@ public class TPQStoreManagerTest { public void testParameterValueScriptError() throws Exception { TPQConfig config = TPQFactory.createConfig(); config.addParameterValueScript("throw 'error'"); - TPQManager store = TPQFactory + TPQFactory .createManagerBuilder(config) .createQuerySet("junit", "jar:junit:mem") .setLanguage(TPQFactory.StatementLanguage.SQL) @@ -28,7 +27,6 @@ public class TPQStoreManagerTest { .build() .build() .build(); - //store.getQueryExecutorContext().prepareQuery("junit.test", new HashMap()); } @Test(expected=TPQueryStoreScriptEngineException.class) diff --git a/tpquery-store/src/test/java/net/forwardfire/tpquery/store/proxy/TPQueryProxyFactoryTest.java b/tpquery-store/src/test/java/net/forwardfire/tpquery/store/proxy/TPQueryProxyFactoryTest.java index 686e428..0c07817 100644 --- a/tpquery-store/src/test/java/net/forwardfire/tpquery/store/proxy/TPQueryProxyFactoryTest.java +++ b/tpquery-store/src/test/java/net/forwardfire/tpquery/store/proxy/TPQueryProxyFactoryTest.java @@ -2,6 +2,7 @@ package net.forwardfire.tpquery.store.proxy; import static org.junit.Assert.assertNotNull; +import java.util.ArrayList; import java.util.List; import net.forwardfire.tpquery.TPQFactory; @@ -40,11 +41,11 @@ public class TPQueryProxyFactoryTest { .buildTree() .build() .build(); - //TPQExecutorJpa exe = new TPQExecutorJpa(store.getQueryExecutorContext()); + assertNotNull(store); TPQueryProxyFactory proxy = new TPQueryProxyFactory(); - proxy.registrateResultHandler(Object.class, (queryName,parameters) -> null); - proxy.registrateResultHandler(List.class, (queryName,parameters) -> null); + proxy.registrateResultHandler(Object.class, (queryName,parameters) -> new TestEntity()); + proxy.registrateResultHandler(List.class, (queryName,parameters) -> new ArrayList()); proxy.registrateResultHandler(void.class, (queryName,parameters) -> null); proxy.registrateResultHandler(int.class, (queryName,parameters) -> null); proxy.registrateResultHandler(Integer.class, (queryName,parameters) -> null); //exe.executeUpdate(em, queryName, parameters) @@ -53,15 +54,15 @@ public class TPQueryProxyFactoryTest { TestEntityFooBarDataService foobarService = proxy.newProxyInstance(TestEntityFooBarDataService.class); List d = dataService.testSelect(); - //assertNotNull(d); + assertNotNull(d); d = dataService.testSelectMultiple(1,"abc1"); d = dataService.testSelectMultiple(2,"abc2"); d = dataService.testSelectMultiple(3,"abc3"); - //assertNotNull(d); + assertNotNull(d); d = foobarService.testFoo(); - //assertNotNull(d); + assertNotNull(d); } @TPQueryProxy(prefix="junit.")