From 577d1b76b0d77baaa9f3d48409e3d8020c74b3ee Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sat, 30 May 2020 18:33:25 +0200 Subject: [PATCH] Continued: - added validation of parameter: all methods and constructors with access level protected, package and public must check if parameters are valid, e.g. not null where no null is wanted/expected. - removed jcore-logger-lib.jar as no logging is possible here MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- nbproject/project.properties | 3 -- .../validator/BaseObjectValidator.java | 28 ++++++++++-------- .../validator/bool/BaseBooleanValidator.java | 23 ++++++++++----- .../validator/date/BaseDateValidator.java | 21 ++++++++++---- .../validator/number/BaseNumberValidator.java | 20 ++++++++++--- .../validator/string/BaseStringValidator.java | 29 ++++++++++--------- 6 files changed, 79 insertions(+), 45 deletions(-) diff --git a/nbproject/project.properties b/nbproject/project.properties index 9da5665..643d00e 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -30,13 +30,11 @@ dist.jar=${dist.dir}/jcoreee.jar dist.javadoc.dir=${dist.dir}/javadoc endorsed.classpath= excludes= -file.reference.jcore-logger-lib.jar=lib/jcore-logger-lib.jar includes=** jar.archive.disabled=${jnlp.enabled} jar.compress=false jar.index=${jnlp.enabled} javac.classpath=\ - ${file.reference.jcore-logger-lib.jar}:\ ${libs.javaee-api-7.0.classpath} # Space-separated list of extra javac options javac.compilerargs=-Xlint:unchecked -Xlint:deprecation @@ -90,6 +88,5 @@ run.test.classpath=\ ${javac.test.classpath}:\ ${build.test.classes.dir} source.encoding=UTF-8 -source.reference.jcore-logger-lib.jar=../jcore-logger-lib/src/ src.dir=src test.src.dir=test diff --git a/src/org/mxchange/jcoreee/validator/BaseObjectValidator.java b/src/org/mxchange/jcoreee/validator/BaseObjectValidator.java index e1ea37f..fe24f27 100644 --- a/src/org/mxchange/jcoreee/validator/BaseObjectValidator.java +++ b/src/org/mxchange/jcoreee/validator/BaseObjectValidator.java @@ -55,9 +55,21 @@ public abstract class BaseObjectValidator implements Validator, Serializ *

* @throws ValidatorException If something more horrible went wrong */ - protected void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields, Boolean allowNull) throws ValidatorException { - // Trace message - //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("preValidate: context={0},component={1},value={2},requiredFields={3} - CALLED!", context, component, value, Arrays.toString(requiredFields))); //NOI18N + protected void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields, final Boolean allowNull) throws ValidatorException { + // Are all parameter set? + if (null == context) { + // Throw NPE + throw new NullPointerException("Parameter context is null"); //NOI18N + } else if (null == component) { + // Throw NPE again + throw new NullPointerException("Parameter component is null"); //NOI18N + } else if (null == requiredFields) { + // Throw it again + throw new NullPointerException("Parameter requiredFields is null"); //NOI18N + } else if (null == allowNull) { + // Throw it once more + throw new NullPointerException("Parameter allowNull is null"); //NOI18N + } // Init message and key String requiredMessage = null; @@ -67,9 +79,6 @@ public abstract class BaseObjectValidator implements Validator, Serializ // Check component's id against required fields and find a match for (final String field : requiredFields) { - // Get logger - //this.getLogger().logDebug(MessageFormat.format("preValidate: field={0},clientId={1}", field, clientId)); //NOI18N - // Is it the same? if (clientId.endsWith(field)) { // Is it null? @@ -83,15 +92,10 @@ public abstract class BaseObjectValidator implements Validator, Serializ } } - // Debug message - //* NOISY-DEBUG: */ this.getLogger().logDebug(MessageFormat.format("preValidate: requiredMessage={0}", requiredMessage)); //NOI18N // Is it not null? if (null != requiredMessage) { - throw new ValidatorException(new FacesMessage(MessageFormat.format("Value {0} for clientId={1} is not valid/unexpected.", value, clientId))); + throw new ValidatorException(new FacesMessage(MessageFormat.format("Value {0} for clientId={1} is not valid/unexpected.", value, clientId))); //NOI18N } - - // Trace message - //* NOISY-DEBUG: */ System.out.println("preValidate: EXIT!"); //NOI18N } } diff --git a/src/org/mxchange/jcoreee/validator/bool/BaseBooleanValidator.java b/src/org/mxchange/jcoreee/validator/bool/BaseBooleanValidator.java index 045f867..dd7e5af 100644 --- a/src/org/mxchange/jcoreee/validator/bool/BaseBooleanValidator.java +++ b/src/org/mxchange/jcoreee/validator/bool/BaseBooleanValidator.java @@ -39,22 +39,31 @@ public abstract class BaseBooleanValidator extends BaseObjectValidator { private static final long serialVersionUID = 42_378_178_715_910_689L; @Override - public void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields, Boolean allowNull) throws ValidatorException { - // Trace message - //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("preValidate: context={0},component={1},value={2},requiredFields={3} - CALLED!", context, component, value, Arrays.toString(requiredFields))); //NOI18N + public void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields, final Boolean allowNull) throws ValidatorException { + // Are all parameter set? + if (null == context) { + // Throw NPE + throw new NullPointerException("Parameter context is null"); //NOI18N + } else if (null == component) { + // Throw NPE again + throw new NullPointerException("Parameter component is null"); //NOI18N + } else if (null == requiredFields) { + // Throw it again + throw new NullPointerException("Parameter requiredFields is null"); //NOI18N + } else if (null == allowNull) { + // Throw it once more + throw new NullPointerException("Parameter allowNull is null"); //NOI18N + } // Pre-validate super.preValidate(context, component, value, requiredFields, allowNull); // Get client id and init message + key - String clientId = component.getClientId(); + final String clientId = component.getClientId(); String requiredMessage = null; // So far all fine, no check if the field is fine for (final String field : requiredFields) { - // Debug message - //this.getLogger().logDebug(MessageFormat.format("preValidate: field={0},clientId={1}", field, clientId)); //NOI18N - // Is it the same? if (clientId.endsWith(field)) { // Compare value's type diff --git a/src/org/mxchange/jcoreee/validator/date/BaseDateValidator.java b/src/org/mxchange/jcoreee/validator/date/BaseDateValidator.java index 9d5b1d6..b808279 100644 --- a/src/org/mxchange/jcoreee/validator/date/BaseDateValidator.java +++ b/src/org/mxchange/jcoreee/validator/date/BaseDateValidator.java @@ -38,21 +38,30 @@ public abstract class BaseDateValidator extends BaseObjectValidator { @Override public void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields, Boolean allowNull) throws ValidatorException { - // Trace message - //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("preValidate: context={0},component={1},value={2},requiredFields={3} - CALLED!", context, component, value, Arrays.toString(requiredFields))); //NOI18N + // Are all parameter set? + if (null == context) { + // Throw NPE + throw new NullPointerException("Parameter context is null"); //NOI18N + } else if (null == component) { + // Throw NPE again + throw new NullPointerException("Parameter component is null"); //NOI18N + } else if (null == requiredFields) { + // Throw it again + throw new NullPointerException("Parameter requiredFields is null"); //NOI18N + } else if (null == allowNull) { + // Throw it once more + throw new NullPointerException("Parameter allowNull is null"); //NOI18N + } // Pre-validate super.preValidate(context, component, value, requiredFields, allowNull); // Get client id and init message + key - String clientId = component.getClientId(); + final String clientId = component.getClientId(); String requiredMessage = null; // So far all fine, no check if the field is fine for (final String field : requiredFields) { - // Debug message - //this.getLogger().logDebug(MessageFormat.format("preValidate: field={0},clientId={1}", field, clientId)); //NOI18N - // Is it the same? if (clientId.endsWith(field)) { // Compare value's type diff --git a/src/org/mxchange/jcoreee/validator/number/BaseNumberValidator.java b/src/org/mxchange/jcoreee/validator/number/BaseNumberValidator.java index 8d212b5..0bd5aac 100644 --- a/src/org/mxchange/jcoreee/validator/number/BaseNumberValidator.java +++ b/src/org/mxchange/jcoreee/validator/number/BaseNumberValidator.java @@ -36,15 +36,27 @@ public abstract class BaseNumberValidator extends BaseObjectValidator { private static final long serialVersionUID = 25_481_878_590_589_321L; @Override - public void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields, Boolean allowNull) throws ValidatorException { - // Trace message - //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("preValidate: context={0},component={1},value={2},requiredFields={3},allowNull={4} - CALLED!", context, component, value, Arrays.toString(requiredFields, allowNull))); //NOI18N + public void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields, final Boolean allowNull) throws ValidatorException { + // Are all parameter set? + if (null == context) { + // Throw NPE + throw new NullPointerException("Parameter context is null"); //NOI18N + } else if (null == component) { + // Throw NPE again + throw new NullPointerException("Parameter component is null"); //NOI18N + } else if (null == requiredFields) { + // Throw it again + throw new NullPointerException("Parameter requiredFields is null"); //NOI18N + } else if (null == allowNull) { + // Throw it once more + throw new NullPointerException("Parameter allowNull is null"); //NOI18N + } // Pre-validate super.preValidate(context, component, value, requiredFields, allowNull); // Get client id and init message + key - String clientId = component.getClientId(); + final String clientId = component.getClientId(); String requiredMessage = null; // So far all fine, no check if the field is fine diff --git a/src/org/mxchange/jcoreee/validator/string/BaseStringValidator.java b/src/org/mxchange/jcoreee/validator/string/BaseStringValidator.java index acec6d5..58f9c6b 100644 --- a/src/org/mxchange/jcoreee/validator/string/BaseStringValidator.java +++ b/src/org/mxchange/jcoreee/validator/string/BaseStringValidator.java @@ -37,22 +37,31 @@ public abstract class BaseStringValidator extends BaseObjectValidator { private static final long serialVersionUID = 15_484_578_781_760_287L; @Override - protected void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields, Boolean allowNull) throws ValidatorException { - // Trace message - //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("preValidate: context={0},component={1},value={2},fields={3} - CALLED!", context, component, value, Arrays.toString(requiredFields))); //NOI18N + protected void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields, final Boolean allowNull) throws ValidatorException { + // Are all parameter set? + if (null == context) { + // Throw NPE + throw new NullPointerException("Parameter context is null"); //NOI18N + } else if (null == component) { + // Throw NPE again + throw new NullPointerException("Parameter component is null"); //NOI18N + } else if (null == requiredFields) { + // Throw it again + throw new NullPointerException("Parameter requiredFields is null"); //NOI18N + } else if (null == allowNull) { + // Throw it once more + throw new NullPointerException("Parameter allowNull is null"); //NOI18N + } // Pre-validate (example: on null) super.preValidate(context, component, value, requiredFields, allowNull); // Get client id and init message + key - String clientId = component.getClientId(); + final String clientId = component.getClientId(); String requiredMessage = null; // So far all fine, no check if the field is fine for (final String field : requiredFields) { - // Debug message - //this.getLogger().logDebug(MessageFormat.format("preValidate: field={0},clientId={1}", field, clientId)); //NOI18N - // Is it the same? if (clientId.endsWith(field)) { // Compare value's type @@ -75,16 +84,10 @@ public abstract class BaseStringValidator extends BaseObjectValidator { } } - // Debug message - //this.getLogger().logDebug(MessageFormat.format("preValidate: requiredMessage={0}", requiredMessage)); //NOI18N - // Is it not null? if (null != requiredMessage) { // Then there was something wrong with it throw new ValidatorException(new FacesMessage(requiredMessage)); } - - // Trace message - //* NOISY-DEBUG: */ System.out.println("preValidate: EXIT!"); //NOI18N } } -- 2.39.5