├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── doc └── images │ └── ValidationDocumentFilter.jpg ├── pom.xml └── src ├── main ├── java │ └── tools4j │ │ └── validator │ │ ├── BigDecimalValidator.java │ │ ├── BigIntegerValidator.java │ │ ├── BooleanValidator.java │ │ ├── BusinessRule.java │ │ ├── BusinessRuleListener.java │ │ ├── BusinessRuleTuple.java │ │ ├── BusinessRulesManager.java │ │ ├── ByteValidator.java │ │ ├── DateValidator.java │ │ ├── DefaultBusinessRuleListener.java │ │ ├── DoubleValidator.java │ │ ├── EmailValidator.java │ │ ├── FloatValidator.java │ │ ├── IntegerValidator.java │ │ ├── LongValidator.java │ │ ├── NumberValidator.java │ │ ├── PickListValidator.java │ │ ├── ShortValidator.java │ │ ├── StringValidator.java │ │ ├── TransformationListener.java │ │ ├── ValidationListener.java │ │ ├── Validator.java │ │ ├── ValidatorFactory.java │ │ └── utils │ │ ├── ValidationDocumentFilter.java │ │ ├── ValidationInputVerifier.java │ │ └── ValidationLabel.java └── resources │ └── tools4j │ └── resources │ ├── Validator.properties │ ├── Validator_de.properties │ └── Validator_en.properties └── test └── java └── tools4j └── validator ├── features ├── AllFeaturesTest.java ├── BasicFeature.java ├── BigDecimalFeature.java ├── BigIntegerFeature.java ├── BooleanFeature.java ├── BusinessRulesFeature.java ├── ByteFeature.java ├── DateFeature.java ├── DoubleFeature.java ├── DynamicValidationFeature.java ├── EmailFeature.java ├── FloatFeature.java ├── IntegerFeature.java ├── LongFeature.java ├── NumberFeature.java ├── PickListFeature.java ├── ShortFeature.java └── StringFeature.java └── utils └── features ├── DocumentFilterFeature.java ├── InputVerifierFeature.java └── Testbed.java /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.gitignore.io/api/eclipse,intellij,netbeans 2 | 3 | /target/ 4 | 5 | ### Eclipse ### 6 | *.pydevproject 7 | .metadata 8 | .gradle 9 | bin/ 10 | tmp/ 11 | *.tmp 12 | *.bak 13 | *.swp 14 | *~.nib 15 | local.properties 16 | .settings/ 17 | .loadpath 18 | 19 | # Eclipse Core 20 | .project 21 | 22 | # External tool builders 23 | .externalToolBuilders/ 24 | 25 | # Locally stored "Eclipse launch configurations" 26 | *.launch 27 | 28 | # CDT-specific 29 | .cproject 30 | 31 | # JDT-specific (Eclipse Java Development Tools) 32 | .classpath 33 | 34 | # Java annotation processor (APT) 35 | .factorypath 36 | 37 | # PDT-specific 38 | .buildpath 39 | 40 | # sbteclipse plugin 41 | .target 42 | 43 | # TeXlipse plugin 44 | .texlipse 45 | 46 | 47 | ### Intellij ### 48 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio 49 | 50 | *.iml 51 | 52 | ## Directory-based project format: 53 | .idea/ 54 | # if you remove the above rule, at least ignore the following: 55 | 56 | # User-specific stuff: 57 | # .idea/workspace.xml 58 | # .idea/tasks.xml 59 | # .idea/dictionaries 60 | 61 | # Sensitive or high-churn files: 62 | # .idea/dataSources.ids 63 | # .idea/dataSources.xml 64 | # .idea/sqlDataSources.xml 65 | # .idea/dynamic.xml 66 | # .idea/uiDesigner.xml 67 | 68 | # Gradle: 69 | # .idea/gradle.xml 70 | # .idea/libraries 71 | 72 | # Mongo Explorer plugin: 73 | # .idea/mongoSettings.xml 74 | 75 | ## File-based project format: 76 | *.ipr 77 | *.iws 78 | 79 | ## Plugin-specific files: 80 | 81 | # IntelliJ 82 | /out/ 83 | 84 | # mpeltonen/sbt-idea plugin 85 | .idea_modules/ 86 | 87 | # JIRA plugin 88 | atlassian-ide-plugin.xml 89 | 90 | # Crashlytics plugin (for Android Studio and IntelliJ) 91 | com_crashlytics_export_strings.xml 92 | crashlytics.properties 93 | crashlytics-build.properties 94 | 95 | 96 | ### NetBeans ### 97 | nbproject/private/ 98 | build/ 99 | nbbuild/ 100 | dist/ 101 | nbdist/ 102 | nbactions.xml 103 | nb-configuration.xml 104 | .nb-gradle/ 105 | 106 | ### PMD ### 107 | *.pmd -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | 2 | language: java 3 | jdk: 4 | - oraclejdk8 5 | 6 | after_success: 7 | - mvn clean cobertura:cobertura coveralls:report 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | tools4j-validator - Framework for Validation 2 | Copyright (c) 2014, David A. Bauer 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are met: 7 | 8 | 1. Redistributions of source code must retain the above copyright notice, this 9 | list of conditions and the following disclaimer. 10 | 11 | 2. Redistributions in binary form must reproduce the above copyright notice, 12 | this list of conditions and the following disclaimer in the documentation 13 | and/or other materials provided with the distribution. 14 | 15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/relvaner/tools4j-validator.svg?branch=master)](https://travis-ci.org/relvaner/tools4j-validator) 2 | [![Coverage Status](https://coveralls.io/repos/github/relvaner/tools4j-validator/badge.svg?branch=master)](https://coveralls.io/github/relvaner/tools4j-validator?branch=master) 3 | [Note: Coverage under Windows 85,7%] 4 | 5 | Why another validation framework? 6 | ================================= 7 | 8 | Validation frameworks are often conceptualized for Java Beans. Motivation for this project was, that there is no simple solution in cycle, for validating individual data types of Java Swing applications. This framework is also particularly useful, if it can not be determined a priori, which types of data should be validated (dynamic validation in combination with validation properties in JSON format). It is especially useful when you want to define validation constraints separated from business logic. This can be the case for database data validation, where validation requirements can dynamically change. 9 | 10 | 11 | Introduction 12 | ============ 13 | 14 | The framework is used for checking the data for correctness. Specifically data can be checked for correctness by means of regular expression and the value range or field length for strings. Additionally own validators can be written, that can then define additional verifiable criteria (derived classes of type Validator <T>). Validation is a maximum of three steps (Transformation → Validation → BusinessRule). When passing a string, the string is first transformed to the desired data type. During the transformation process, decimal delimiters will transferred to a point notation (comma → point). Then, the actual validation (pattern, value range, length range) is performed. At the conclusion, rules can be checked for compliance. In Table 1 is a list of the various validators and its constraints to see. 15 | 16 | Overview 17 | ======== 18 | 19 | | Validator | Constraints | 20 | | --------- | ----------- | 21 | | StringValidator | NotNull, Null, NotEmpty, Empty, Pattern, Min, Max, Size | 22 | | EmailValidator | NotNull, Null, NotEmpty, Empty, Pattern, Min, Max, Size | 23 | | PickListValidator | NotNull, Null, NotEmpty, Empty, Pattern, Min, Max, Size, List | 24 | | DateValidator | NotNull, Null, Pattern, Valid, Past, Future | 25 | | BigDecimalValidator | NotNull, Null, Min, Max, Range | 26 | | BigIntegerValidator | NotNull, Null, Min, Max, Range | 27 | | DoubleValidator | NotNull, Null, Min, Max, Range | 28 | | FloatValidator | NotNull, Null, Min, Max, Range | 29 | | LongValidator | NotNull, Null, Min, Max, Range | 30 | | IntegerValidator | NotNull, Null, Min, Max, Range | 31 | | ShortValidator | NotNull, Null, Min, Max, Range | 32 | | ByteValidator | NotNull, Null, Min, Max, Range | 33 | | BooleanValidator | NotNull, Null, AssertFalse, AssertTrue | 34 | 35 | Table 1: Overview of validators and possible constraints. 36 | 37 | Examples 38 | ======== 39 | Example of a StringValidator: 40 | 41 | Validation with a pattern, followed by the validation and an output of an error message and the type of an error message. 42 |
StringValidator v = new StringValidator();
 43 | v.setPattern("[A-Za-z]+");
 44 | 
 45 | v.validate("122");
 46 | 
 47 | System.out.println(v.getViolationMessage());
 48 | 
 49 | The input value "122" is not allowed ("[A-Za-z]+" expected)!
 50 | 
 51 | System.out.println(v.getViolationConstraint());
 52 | 
 53 | Pattern
54 | Example of an IntegerValidator: 55 | 56 | Validation with specifying a minimum, followed by the validation and an output of an error message. 57 |
IntegerValidator v = new IntegerValidator();
 58 | v.setMin(25);
 59 | 
 60 | v.validate(23);
 61 | 
 62 | System.out.println(v.getViolationMessage());
 63 | 
 64 | The input value "23" is out of range (>=25)!
65 | 66 | Language Support for the output of error messages (currently German and English): 67 | 68 |
Validator.setViolationMessageLanguage("de");
69 | 70 | Swing 71 | ===== 72 | 73 | In Swing, text fields can be validated at runtime using the ValidationDocumentFilter class (see Fig. 1). 74 | 75 |
StringValidator v = new StringValidator();
 76 | v.setPattern("[A-Za-z]+");
 77 | 
 78 | TextField tf = new TextField("ABCD");
 79 | AbstractDocument doc = (AbstractDocument) tf.getDocument();
 80 | doc.setDocumentFilter(new ValidationDocumentFilter(window, v));
81 | 82 | ![alt text](https://github.com/relvaner/tools4j-validator/blob/master/doc/images/ValidationDocumentFilter.jpg "ValidationDocumentFilter") 83 | 84 | Fig. 1: Validation of a text field with an error message shown. 85 | 86 | As an alternative, use an InputVerifier (here: ValidationInputVerifier) instead: 87 | 88 |
tf.setInputVerifier(new ValidationInputVerifier(window, v)); // TextField
89 | 90 | --- 91 | 92 | You can redirect the ViolationMessage also to a ValidationLabel (output): 93 | 94 |
doc.setDocumentFilter(new ValidationDocumentFilter(v, tf, output));
95 | 96 | or 97 | 98 |
tf.setInputVerifier(new ValidationInputVerifier(v, output)); // TextField
99 | 100 | Dynamic Validation 101 | ================== 102 | A dynamic validation is made possible by coding the validation properties in JSON format. Definition of validation properties for an integer field in JSON: 103 | 104 |
{
105 |     "type": "Integer",
106 |     "constraints": [
107 |         {"constraint": "NotNull"},
108 |         {"constraint": "Range", "min": 2, "max": 10}
109 |                    ]
110 | }
111 | 112 | Example: 113 | 114 | Given are the constraints and the value which has to be checked. Using the ValidatorFactory, the type of validator is determined. Then the corresponding validator is instantiated and then the validation is performed. 115 | 116 |
String constraints = "{ … }";
117 | String type = ValidatorFactory.parseType(constraints);
118 | 
119 | Validator<?> v = ValidatorFactory.createValidator(type);
120 | v.setConstraints(constraints);
121 | 
122 | v.validateString(value);
123 | 124 | Validation properties in JSON syntax obtained by: 125 | 126 |
v.getConstraintsAsJsonObject().toString();
127 | 128 | Business Rules 129 | ============== 130 | 131 | In the following, the use of business rules will be explained with an example. In this example, the passed date of birth should be checked on a minimum age of 18 years (age of majority). First, a business rule is written to verify the age of majority and added to the Business Rules Manager (see below). 132 | 133 |
BusinessRulesManager manager = new BusinessRulesManager();
134 | String id = manager.addRule("AdultCheck", new BusinessRule() {
135 | 	@Override
136 | 	public boolean checkBusinessRule(Calendar value) {
137 | 		// AdultCheck
138 | 		Calendar now = Calendar.getInstance();
139 | 		now.set(Calendar.YEAR, now.get(Calendar.YEAR)-18);
140 | 				
141 | 		return value.compareTo(now)<=0;
142 | 	}
143 | });
144 | 145 | In the next step the apply of a business rule for the validator will be activated. This is done by adding the corresponding ID of the Business Rule and registering a handler for the business rule (here: DefaultBusinessRuleListener). 146 | 147 |
DateValidator v = new DateValidator();
148 | v.setBusinessRuleID(id);
149 | v.setBusinessRuleListener(new DefaultBusinessRuleListener(manager));
150 | v.setPast(true);
151 | v.setPattern("dd.MM.yyyy");
152 | v.validateString("12.06.2004");
153 | 
154 | System.out.println(v.getViolationMessage());
155 | System.out.println(v.getViolationConstraint());
156 | 
157 | 158 | Error message: 159 | 160 |
The input value is not compliant to rules  ("AdultCheck")!
-------------------------------------------------------------------------------- /doc/images/ValidationDocumentFilter.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/relvaner/tools4j-validator/bceadd50e21ee8fa3fa73902fbde7655e74c8092/doc/images/ValidationDocumentFilter.jpg -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 27 | 28 | 4.0.0 29 | tools4j 30 | tools4j-validator 31 | tools4j-validator 32 | 2.7.5 33 | jar 34 | 35 | Framework for Validation 36 | 2014 37 | 38 | 39 | 40 | relvaner 41 | David A. Bauer 42 | 43 | 44 | 45 | 46 | 47 | 48 | org.apache.maven.plugins 49 | maven-assembly-plugin 50 | 3.1.0 51 | 52 | 53 | bin 54 | 55 | ${project.artifactId}-${project.version} 56 | 57 | META-INF/LICENSE.txt 58 | 59 | 60 | 61 | 62 | make-assembly 63 | package 64 | 65 | single 66 | 67 | 68 | 69 | 70 | 71 | org.eluder.coveralls 72 | coveralls-maven-plugin 73 | 4.3.0 74 | 79 | 80 | 81 | org.codehaus.mojo 82 | cobertura-maven-plugin 83 | 2.7 84 | 85 | xml 86 | 256m 87 | 88 | false 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | javax.json 97 | javax.json-api 98 | 1.1.2 99 | 100 | 101 | org.glassfish 102 | javax.json 103 | 1.1.2 104 | 105 | 106 | org.easytesting 107 | fest-swing 108 | 1.2.1 109 | test 110 | 111 | 112 | junit 113 | junit 114 | 4.12 115 | test 116 | 117 | 118 | 119 | 120 | 1.8 121 | 1.8 122 | UTF-8 123 | 124 | -------------------------------------------------------------------------------- /src/main/java/tools4j/validator/BigDecimalValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator; 28 | 29 | import java.awt.Window; 30 | import java.math.BigDecimal; 31 | 32 | public class BigDecimalValidator extends NumberValidator { 33 | @Override 34 | protected boolean transform(Window window, String value) { 35 | boolean result = true; 36 | 37 | try { 38 | transformedValue = new BigDecimal(value); 39 | } 40 | catch (NumberFormatException e) { 41 | violationConstraint = "Transform"; 42 | violationMessage = String.format(violationMessageResource.getString("STR_TRANSFORM"), value, "BigDecimal"); 43 | 44 | showViolationDialog(window); 45 | 46 | result = false; 47 | } 48 | 49 | return result; 50 | } 51 | 52 | @Override 53 | protected BigDecimal getNumber(BigDecimal value) { 54 | return value; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/tools4j/validator/BigIntegerValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator; 28 | 29 | import java.awt.Window; 30 | import java.math.BigDecimal; 31 | import java.math.BigInteger; 32 | 33 | public class BigIntegerValidator extends NumberValidator { 34 | @Override 35 | protected boolean transform(Window window, String value) { 36 | boolean result = true; 37 | 38 | try { 39 | transformedValue = new BigInteger(value); 40 | } 41 | catch (NumberFormatException e) { 42 | violationConstraint = "Transform"; 43 | violationMessage = String.format(violationMessageResource.getString("STR_TRANSFORM"), value, "BigInteger"); 44 | 45 | showViolationDialog(window); 46 | 47 | result = false; 48 | } 49 | 50 | return result; 51 | } 52 | 53 | @Override 54 | protected BigInteger getNumber(BigDecimal value) { 55 | return value.toBigInteger(); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/tools4j/validator/BooleanValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator; 28 | 29 | import java.awt.Window; 30 | import java.io.StringReader; 31 | 32 | import javax.json.Json; 33 | import javax.json.JsonArrayBuilder; 34 | import javax.json.stream.JsonParser; 35 | import javax.json.stream.JsonParser.Event; 36 | 37 | public class BooleanValidator extends Validator { 38 | protected StringValidator transformValidator; 39 | 40 | protected boolean assertTrue; 41 | protected boolean assertFalse; 42 | 43 | public BooleanValidator() { 44 | transformValidator = new StringValidator(); 45 | transformValidator.setPattern("TRUE|FALSE|1|0"); 46 | } 47 | 48 | public boolean isAssertTrue() { 49 | return assertTrue; 50 | } 51 | 52 | public void setAssertTrue(boolean value) { 53 | assertTrue = value; 54 | } 55 | 56 | public boolean isAssertFalse() { 57 | return assertFalse; 58 | } 59 | 60 | public void setAssertFalse(boolean value) { 61 | assertFalse = value; 62 | } 63 | 64 | @Override 65 | protected boolean transform(Window window, String value) { 66 | boolean result = true; 67 | 68 | value = value.toUpperCase(); 69 | 70 | if (!transformValidator.validate(value)) { 71 | violationConstraint = "Transform"; 72 | violationMessage = String.format(violationMessageResource.getString("STR_TRANSFORM"), value, "Boolean"); 73 | 74 | showViolationDialog(window); 75 | 76 | result = false; 77 | } 78 | else { 79 | switch (value) { 80 | case "TRUE" : transformedValue = true; break; 81 | case "FALSE": transformedValue = false; break; 82 | case "1" : transformedValue = true; break; 83 | case "0" : transformedValue = false; break; 84 | } 85 | } 86 | 87 | return result; 88 | } 89 | 90 | @Override 91 | protected boolean validation(Window window, Boolean value) { 92 | boolean result = true; 93 | 94 | if (assertTrue && !value) { 95 | violationConstraint = "AssertTrue"; 96 | violationMessage = String.format(violationMessageResource.getString("STR_ASSERTTRUE")); 97 | showViolationDialog(window); 98 | 99 | result = false; 100 | } else if (assertFalse && value) { 101 | violationConstraint = "AssertFalse"; 102 | violationMessage = String.format(violationMessageResource.getString("STR_ASSERTFALSE")); 103 | showViolationDialog(window); 104 | 105 | result = false; 106 | } 107 | 108 | return result; 109 | } 110 | 111 | @Override 112 | protected void interpretConstraints(String constraints) { 113 | JsonParser parser = Json.createParser(new StringReader(constraints)); 114 | while (parser.hasNext()) { 115 | Event e = parser.next(); 116 | if (e == Event.KEY_NAME) 117 | if (parser.getString().equalsIgnoreCase("constraint")) { 118 | parser.next(); 119 | if (parser.getString().equalsIgnoreCase("AssertTrue")) { 120 | setAssertTrue(true); 121 | break; 122 | } 123 | else if (parser.getString().equalsIgnoreCase("AssertFalse")) { 124 | setAssertFalse(true); 125 | break; 126 | } 127 | } 128 | } 129 | } 130 | 131 | @Override 132 | public void setConstraints(String constraints) { 133 | assertTrue = false; 134 | assertFalse = false; 135 | 136 | super.setConstraints(constraints); 137 | } 138 | 139 | @Override 140 | protected void buildConstraints(JsonArrayBuilder jsonArrayBuilder) { 141 | if (assertTrue) 142 | jsonArrayBuilder.add(Json.createObjectBuilder() 143 | .add("constraint", "AssertTrue")); 144 | if (assertFalse) 145 | jsonArrayBuilder.add(Json.createObjectBuilder() 146 | .add("constraint", "AssertFalse")); 147 | } 148 | } 149 | -------------------------------------------------------------------------------- /src/main/java/tools4j/validator/BusinessRule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator; 28 | 29 | public interface BusinessRule { 30 | public boolean checkBusinessRule(T value); 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/tools4j/validator/BusinessRuleListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator; 28 | 29 | public interface BusinessRuleListener { 30 | public boolean checkBusinessRule(Validator validator); 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/tools4j/validator/BusinessRuleTuple.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014-2017, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator; 28 | 29 | public class BusinessRuleTuple { 30 | protected final String name; 31 | protected final BusinessRule rule; 32 | 33 | public String getName() { 34 | return name; 35 | } 36 | 37 | public BusinessRule getRule() { 38 | return rule; 39 | } 40 | 41 | public BusinessRuleTuple(String name, BusinessRule rule) { 42 | super(); 43 | this.name = name; 44 | this.rule = rule; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/tools4j/validator/BusinessRulesManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014-2017, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator; 28 | 29 | import java.util.HashMap; 30 | import java.util.Map; 31 | import java.util.UUID; 32 | 33 | public class BusinessRulesManager { 34 | protected Map rules; 35 | protected Map namesOfRules; 36 | 37 | public BusinessRulesManager() { 38 | rules = new HashMap<>(); 39 | namesOfRules = new HashMap<>(); 40 | } 41 | 42 | public Map getRules() { 43 | return rules; 44 | } 45 | 46 | public void setRules(Map rules) { 47 | this.rules = rules; 48 | } 49 | 50 | public String addRule(String name, BusinessRule rule) { 51 | String result = UUID.randomUUID().toString(); 52 | 53 | if (rules.get(result)==null) { 54 | rules.put(result, new BusinessRuleTuple(name, rule)); 55 | if (name!=null) 56 | namesOfRules.put(name, result); 57 | } 58 | else 59 | return null; // uuid duplicate 60 | 61 | return result; 62 | } 63 | 64 | public String addRule(BusinessRule rule) { 65 | return addRule(null, rule); 66 | } 67 | 68 | public String getRuleID(String name) { 69 | return namesOfRules.get(name); 70 | } 71 | 72 | public String getRuleName(String id) { 73 | return rules.get(id).getName(); 74 | } 75 | 76 | @SuppressWarnings("unchecked") 77 | public boolean checkBusinessRule(String id, Object value) { 78 | return ((BusinessRule)rules.get(id).getRule()).checkBusinessRule(value); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/tools4j/validator/ByteValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator; 28 | 29 | import java.awt.Window; 30 | import java.math.BigDecimal; 31 | 32 | public class ByteValidator extends NumberValidator { 33 | @Override 34 | protected boolean transform(Window window, String value) { 35 | boolean result = true; 36 | 37 | try { 38 | transformedValue = Byte.parseByte(value); 39 | } 40 | catch (NumberFormatException e) { 41 | violationConstraint = "Transform"; 42 | violationMessage = String.format(violationMessageResource.getString("STR_TRANSFORM"), value, "Byte"); 43 | 44 | showViolationDialog(window); 45 | 46 | result = false; 47 | } 48 | 49 | return result; 50 | } 51 | 52 | @Override 53 | protected Byte getNumber(BigDecimal value) { 54 | return value.byteValue(); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/tools4j/validator/DateValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator; 28 | 29 | import java.awt.Window; 30 | import java.io.StringReader; 31 | import java.text.ParseException; 32 | import java.text.SimpleDateFormat; 33 | import java.util.Calendar; 34 | import java.util.Date; 35 | 36 | import javax.json.Json; 37 | import javax.json.JsonArrayBuilder; 38 | import javax.json.stream.JsonParser; 39 | import javax.json.stream.JsonParser.Event; 40 | 41 | public class DateValidator extends Validator { 42 | protected String pattern; 43 | 44 | protected boolean valid; 45 | 46 | protected boolean past; 47 | protected boolean future; 48 | 49 | public DateValidator() { 50 | super(); 51 | 52 | pattern = "dd.MM.yy HH:mm"; 53 | } 54 | 55 | public String getPattern() { 56 | return pattern; 57 | } 58 | 59 | public void setPattern(String pattern) { 60 | this.pattern = pattern; 61 | } 62 | 63 | public boolean isValid() { 64 | return valid; 65 | } 66 | 67 | public void setValid(boolean value) { 68 | valid = value; 69 | } 70 | 71 | public boolean isPast() { 72 | return past; 73 | } 74 | 75 | public void setPast(boolean value) { 76 | past = value; 77 | } 78 | 79 | public boolean isFuture() { 80 | return future; 81 | } 82 | 83 | public void setFuture(boolean value) { 84 | future = value; 85 | } 86 | 87 | @Override 88 | protected boolean transform(Window window, String value) { 89 | boolean result = true; 90 | 91 | try { 92 | SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); 93 | if (valid) 94 | simpleDateFormat.setLenient(false); 95 | 96 | Date date = simpleDateFormat.parse(value); 97 | 98 | transformedValue = Calendar.getInstance(); 99 | transformedValue.setTime(date); 100 | } catch (ParseException e) { 101 | if (valid) 102 | violationConstraint = "Pattern|Valid"; 103 | else 104 | violationConstraint = "Pattern"; 105 | violationMessage = String.format(violationMessageResource.getString("STR_DATE"), value, pattern); 106 | 107 | showViolationDialog(window); 108 | 109 | result = false; 110 | } 111 | 112 | return result; 113 | } 114 | 115 | @Override 116 | protected boolean validation(Window window, Calendar value) { 117 | boolean result = true; 118 | 119 | if (valid) { 120 | value.setLenient(false); 121 | try { 122 | value.getTime(); 123 | } 124 | catch(IllegalArgumentException e) { 125 | violationConstraint = "Valid"; 126 | violationMessage = String.format(violationMessageResource.getString("STR_VALID"), e.getMessage()); 127 | 128 | showViolationDialog(window); 129 | 130 | result = false; 131 | } 132 | } 133 | 134 | if (result) { 135 | int c = value.compareTo(Calendar.getInstance()); 136 | if (past && c>0) { 137 | violationConstraint = "Past"; 138 | violationMessage = String.format(violationMessageResource.getString("STR_PAST"), new SimpleDateFormat(pattern).format(value.getTime())); 139 | 140 | showViolationDialog(window); 141 | 142 | result = false; 143 | } else if (future && c<0) { 144 | violationConstraint = "Future"; 145 | violationMessage = String.format(violationMessageResource.getString("STR_FUTURE"), new SimpleDateFormat(pattern).format(value.getTime())); 146 | 147 | showViolationDialog(window); 148 | 149 | result = false; 150 | } 151 | } 152 | 153 | return result; 154 | } 155 | 156 | @Override 157 | protected void interpretConstraints(String constraints) { 158 | JsonParser parser = Json.createParser(new StringReader(constraints)); 159 | while (parser.hasNext()) { 160 | Event e = parser.next(); 161 | if (e == Event.KEY_NAME) 162 | if (parser.getString().equalsIgnoreCase("constraint")) { 163 | parser.next(); 164 | if (parser.getString().equalsIgnoreCase("Pattern")) { 165 | parser.next(); 166 | parser.next(); 167 | setPattern(parser.getString()); 168 | } else if (parser.getString().equalsIgnoreCase("Valid")) { 169 | setValid(true); 170 | } else if (parser.getString().equalsIgnoreCase("Past")) { 171 | setPast(true); 172 | } else if (parser.getString().equalsIgnoreCase("Future")) { 173 | setFuture(true); 174 | } 175 | } 176 | } 177 | parser.close(); 178 | } 179 | 180 | @Override 181 | public void setConstraints(String constraints) { 182 | pattern = null; 183 | valid = false; 184 | past = false; 185 | future = false; 186 | 187 | super.setConstraints(constraints); 188 | } 189 | 190 | @Override 191 | protected void buildConstraints(JsonArrayBuilder jsonArrayBuilder) { 192 | if (pattern!=null) 193 | jsonArrayBuilder.add(Json.createObjectBuilder() 194 | .add("constraint", "Pattern") 195 | .add("regex", pattern)); 196 | 197 | if (valid) 198 | jsonArrayBuilder.add(Json.createObjectBuilder() 199 | .add("constraint", "Valid")); 200 | 201 | if (past) 202 | jsonArrayBuilder.add(Json.createObjectBuilder() 203 | .add("constraint", "Past")); 204 | if (future) 205 | jsonArrayBuilder.add(Json.createObjectBuilder() 206 | .add("constraint", "Future")); 207 | } 208 | } 209 | -------------------------------------------------------------------------------- /src/main/java/tools4j/validator/DefaultBusinessRuleListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator; 28 | 29 | public class DefaultBusinessRuleListener implements BusinessRuleListener { 30 | protected BusinessRulesManager manager; 31 | 32 | public DefaultBusinessRuleListener(BusinessRulesManager manager) { 33 | this.manager = manager; 34 | } 35 | 36 | public String getBusinessRuleName(Validator validator) { 37 | return manager.getRuleName(validator.getBusinessRuleID()); 38 | } 39 | 40 | @Override 41 | public boolean checkBusinessRule(Validator validator) { 42 | return manager.checkBusinessRule(validator.getBusinessRuleID(), validator.getValue()); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/tools4j/validator/DoubleValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator; 28 | 29 | import java.awt.Window; 30 | import java.math.BigDecimal; 31 | 32 | public class DoubleValidator extends NumberValidator { 33 | @Override 34 | protected boolean transform(Window window, String value) { 35 | boolean result = true; 36 | 37 | try { 38 | transformedValue = Double.parseDouble(value.replace(",", ".")); 39 | } 40 | catch (NumberFormatException e) { 41 | violationConstraint = "Transform"; 42 | violationMessage = String.format(violationMessageResource.getString("STR_TRANSFORM"), value, "Double"); 43 | 44 | showViolationDialog(window); 45 | 46 | result = false; 47 | } 48 | 49 | return result; 50 | } 51 | 52 | @Override 53 | protected Double getNumber(BigDecimal value) { 54 | return value.doubleValue(); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/tools4j/validator/EmailValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator; 28 | 29 | public class EmailValidator extends StringValidator { 30 | public EmailValidator() { 31 | pattern = "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}"; // changed, see http://www.regular-expressions.info/email.html 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/tools4j/validator/FloatValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator; 28 | 29 | import java.awt.Window; 30 | import java.math.BigDecimal; 31 | 32 | public class FloatValidator extends NumberValidator { 33 | @Override 34 | protected boolean transform(Window window, String value) { 35 | boolean result = true; 36 | 37 | try { 38 | transformedValue = Float.parseFloat(value); 39 | } 40 | catch (NumberFormatException e) { 41 | violationConstraint = "Transform"; 42 | violationMessage = String.format(violationMessageResource.getString("STR_TRANSFORM"), value, "Float"); 43 | 44 | showViolationDialog(window); 45 | 46 | result = false; 47 | } 48 | 49 | return result; 50 | } 51 | 52 | @Override 53 | protected Float getNumber(BigDecimal value) { 54 | return value.floatValue(); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/tools4j/validator/IntegerValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator; 28 | 29 | import java.awt.Window; 30 | import java.math.BigDecimal; 31 | 32 | public class IntegerValidator extends NumberValidator { 33 | @Override 34 | protected boolean transform(Window window, String value) { 35 | boolean result = true; 36 | 37 | try { 38 | transformedValue = Integer.parseInt(value); 39 | } 40 | catch (NumberFormatException e) { 41 | violationConstraint = "Transform"; 42 | violationMessage = String.format(violationMessageResource.getString("STR_TRANSFORM"), value, "Integer"); 43 | 44 | showViolationDialog(window); 45 | 46 | result = false; 47 | } 48 | 49 | return result; 50 | } 51 | 52 | @Override 53 | protected Integer getNumber(BigDecimal value) { 54 | return value.intValue(); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/tools4j/validator/LongValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator; 28 | 29 | import java.awt.Window; 30 | import java.math.BigDecimal; 31 | 32 | public class LongValidator extends NumberValidator { 33 | @Override 34 | protected boolean transform(Window window, String value) { 35 | boolean result = true; 36 | 37 | try { 38 | transformedValue = Long.parseLong(value); 39 | } 40 | catch (NumberFormatException e) { 41 | violationConstraint = "Transform"; 42 | violationMessage = String.format(violationMessageResource.getString("STR_TRANSFORM"), value, "Long"); 43 | 44 | showViolationDialog(window); 45 | 46 | result = false; 47 | } 48 | 49 | return result; 50 | } 51 | 52 | @Override 53 | protected Long getNumber(BigDecimal value) { 54 | return value.longValue(); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/tools4j/validator/NumberValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator; 28 | 29 | import java.awt.Window; 30 | import java.io.StringReader; 31 | import java.math.BigDecimal; 32 | import java.math.BigInteger; 33 | 34 | import javax.json.Json; 35 | import javax.json.JsonArrayBuilder; 36 | import javax.json.stream.JsonParser; 37 | import javax.json.stream.JsonParser.Event; 38 | 39 | public abstract class NumberValidator> extends Validator { 40 | protected T min; 41 | protected T max; 42 | protected boolean minIncluded; 43 | protected boolean maxIncluded; 44 | 45 | public NumberValidator() { 46 | super(); 47 | 48 | minIncluded = true; 49 | maxIncluded = true; 50 | } 51 | 52 | public T getMin() { 53 | return min; 54 | } 55 | 56 | public void setMin(T min) { 57 | this.min = min; 58 | 59 | minIncluded = true; 60 | } 61 | 62 | public T getMax() { 63 | return max; 64 | } 65 | 66 | public void setMax(T max) { 67 | this.max = max; 68 | 69 | maxIncluded = true; 70 | } 71 | 72 | public boolean isMinIncluded() { 73 | return minIncluded; 74 | } 75 | 76 | public void setMinIncluded(boolean minIncluded) { 77 | this.minIncluded = minIncluded; 78 | } 79 | 80 | public boolean isMaxIncluded() { 81 | return maxIncluded; 82 | } 83 | 84 | public void setMaxIncluded(boolean maxIncluded) { 85 | this.maxIncluded = maxIncluded; 86 | } 87 | 88 | public void setMin(T min, boolean minIncluded) { 89 | setMin(min); 90 | setMinIncluded(minIncluded); 91 | } 92 | 93 | public void setMax(T max, boolean maxIncluded) { 94 | setMax(max); 95 | setMaxIncluded(maxIncluded); 96 | } 97 | 98 | public void setRange(T min, T max) { 99 | setMin(min); 100 | setMax(max); 101 | } 102 | 103 | public void setRange(T min, T max, boolean minIncluded, boolean maxIncluded) { 104 | setMin(min, minIncluded); 105 | setMax(max, maxIncluded); 106 | } 107 | 108 | protected boolean inRange(T value) { 109 | boolean result = true; 110 | 111 | if (min!=null) { 112 | result = value.compareTo(min)>0; 113 | if (minIncluded) result = result || value.compareTo(min)==0; 114 | } 115 | if (max!=null) { 116 | boolean buffer = result; 117 | result = value.compareTo(max)<0; 118 | if (maxIncluded) result = result || value.compareTo(max)==0; 119 | result = buffer && result; 120 | } 121 | 122 | return result; 123 | } 124 | 125 | @Override 126 | protected boolean validation(Window window, T value) { 127 | boolean result = true; 128 | 129 | if (min!=null || max!=null) 130 | if (!inRange(value)) { 131 | String minStr = String.valueOf(min); 132 | String maxStr = String.valueOf(max); 133 | 134 | if (min!=null && max!=null) { 135 | violationConstraint = "Range"; 136 | violationMessage = String.format(violationMessageResource.getString("STR_RANGE"), value, OPENED[minIncluded?1:0]+minStr, maxStr+CLOSED[maxIncluded?1:0]); 137 | } 138 | else if (min==null && max!=null) { 139 | violationConstraint = "Max"; 140 | violationMessage = String.format(violationMessageResource.getString("STR_RANGE2"), value, SMALLER[maxIncluded?(window==null?1:2):0]+maxStr); 141 | } 142 | else if (min!=null && max==null) { 143 | violationConstraint = "Min"; 144 | violationMessage = String.format(violationMessageResource.getString("STR_RANGE2"), value, LARGER[minIncluded?(window==null?1:2):0]+minStr); 145 | } 146 | 147 | showViolationDialog(window); 148 | 149 | result = false; 150 | } 151 | 152 | return result; 153 | } 154 | 155 | @Override 156 | protected void interpretConstraints(String constraints) { 157 | JsonParser parser = Json.createParser(new StringReader(constraints)); 158 | while (parser.hasNext()) { 159 | Event e = parser.next(); 160 | if (e == Event.KEY_NAME) 161 | if (parser.getString().equalsIgnoreCase("constraint")) { 162 | parser.next(); 163 | if (parser.getString().equalsIgnoreCase("Min")) { 164 | parser.next(); 165 | parser.next(); 166 | setMin(getNumber(parser.getBigDecimal())); 167 | e = parser.next(); 168 | if (e != Event.END_OBJECT && parser.getString().equalsIgnoreCase("minIncluded")) { 169 | parser.next(); 170 | setMinIncluded(parser.getInt()==1); 171 | } 172 | } 173 | else if (parser.getString().equalsIgnoreCase("Max")) { 174 | parser.next(); 175 | parser.next(); 176 | setMax(getNumber(parser.getBigDecimal())); 177 | e = parser.next(); 178 | if (e != Event.END_OBJECT && parser.getString().equalsIgnoreCase("maxIncluded")) { 179 | parser.next(); 180 | setMaxIncluded(parser.getInt()==1); 181 | } 182 | } 183 | else if (parser.getString().equalsIgnoreCase("Range")) { 184 | parser.next(); 185 | parser.next(); 186 | setMin(getNumber(parser.getBigDecimal())); 187 | e = parser.next(); 188 | if (e != Event.END_OBJECT && parser.getString().equalsIgnoreCase("minIncluded")) { 189 | parser.next(); 190 | setMinIncluded(parser.getInt()==1); 191 | parser.next(); 192 | } 193 | parser.next(); 194 | setMax(getNumber(parser.getBigDecimal())); 195 | e = parser.next(); 196 | if (e != Event.END_OBJECT && parser.getString().equalsIgnoreCase("maxIncluded")) { 197 | parser.next(); 198 | setMaxIncluded(parser.getInt()==1); 199 | } 200 | } 201 | } 202 | } 203 | parser.close(); 204 | } 205 | 206 | @Override 207 | public void setConstraints(String constraints) { 208 | min = null; 209 | max = null; 210 | 211 | minIncluded = true; 212 | maxIncluded = true; 213 | 214 | super.setConstraints(constraints); 215 | } 216 | 217 | protected abstract T getNumber(BigDecimal value); 218 | 219 | @Override 220 | protected void buildConstraints(JsonArrayBuilder jsonArrayBuilder) { 221 | if (min!=null && max!=null) 222 | jsonArrayBuilder.add(Json.createObjectBuilder() 223 | .add("constraint", "Range") 224 | .add("min", putNumber(min)) 225 | .add("minIncluded", new BigDecimal(minIncluded?1:0)) 226 | .add("max", putNumber(max)) 227 | .add("maxIncluded", new BigDecimal(maxIncluded?1:0))); 228 | else if (min!=null) 229 | jsonArrayBuilder.add(Json.createObjectBuilder() 230 | .add("constraint", "Min") 231 | .add("min", putNumber(min)) 232 | .add("minIncluded", new BigDecimal(minIncluded?1:0))); 233 | else if (max!=null) 234 | jsonArrayBuilder.add(Json.createObjectBuilder() 235 | .add("constraint", "Max") 236 | .add("max", putNumber(max)) 237 | .add("maxIncluded", new BigDecimal(maxIncluded?1:0))); 238 | } 239 | 240 | protected BigDecimal putNumber(T value) { 241 | if (value instanceof BigDecimal) 242 | return (BigDecimal)value; 243 | else if (value instanceof BigInteger) 244 | return new BigDecimal((BigInteger)value); 245 | else if (value instanceof Double) 246 | return new BigDecimal((Double)value); 247 | else if (value instanceof Long) 248 | return new BigDecimal((Long)value); 249 | else if (value instanceof Integer) 250 | return new BigDecimal((Integer)value); 251 | else if (value instanceof Float) 252 | return new BigDecimal((Double)value); 253 | else if (value instanceof Short) 254 | return new BigDecimal((Integer)value); 255 | else if (value instanceof Byte) 256 | return new BigDecimal((Integer)value); 257 | 258 | return null; 259 | } 260 | } 261 | -------------------------------------------------------------------------------- /src/main/java/tools4j/validator/PickListValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator; 28 | 29 | import java.awt.Window; 30 | import java.io.StringReader; 31 | import java.util.HashSet; 32 | import java.util.Set; 33 | 34 | import javax.json.Json; 35 | import javax.json.JsonArrayBuilder; 36 | import javax.json.stream.JsonParser; 37 | import javax.json.stream.JsonParser.Event; 38 | 39 | public class PickListValidator extends StringValidator { 40 | protected Set pickList; 41 | 42 | public Set getPickList() { 43 | return pickList; 44 | } 45 | 46 | public void setPickList(Set pickList) { 47 | this.pickList = pickList; 48 | } 49 | 50 | protected boolean validation(Window window, String value) { 51 | boolean result = super.validation(window, value); 52 | 53 | if (result) 54 | if (pickList!=null) 55 | if (!pickList.contains(value)) { 56 | violationConstraint = "List"; 57 | violationMessage = String.format(violationMessageResource.getString("STR_PICKLIST"), value, pickList.toString()); 58 | showViolationDialog(window); 59 | 60 | result = false; 61 | } 62 | 63 | return result; 64 | } 65 | 66 | @Override 67 | protected void interpretConstraints(String constraints) { 68 | super.interpretConstraints(constraints); 69 | 70 | JsonParser parser = Json.createParser(new StringReader(constraints)); 71 | while (parser.hasNext()) { 72 | Event e = parser.next(); 73 | if (e == Event.KEY_NAME) 74 | if (parser.getString().equalsIgnoreCase("constraint")) { 75 | parser.next(); 76 | if (parser.getString().equalsIgnoreCase("List")) { 77 | parser.next(); 78 | 79 | pickList = new HashSet<>(); 80 | while (parser.hasNext()) { 81 | Event item = parser.next(); 82 | if (item == Event.VALUE_STRING) 83 | pickList.add(parser.getString()); 84 | } 85 | } 86 | } 87 | } 88 | parser.close(); 89 | } 90 | 91 | @Override 92 | public void setConstraints(String constraints) { 93 | pickList = null; 94 | 95 | super.setConstraints(constraints); 96 | } 97 | 98 | @Override 99 | protected void buildConstraints(JsonArrayBuilder jsonArrayBuilder) { 100 | super.buildConstraints(jsonArrayBuilder); 101 | 102 | if (pickList!=null) { 103 | JsonArrayBuilder list = Json.createArrayBuilder(); 104 | for (String s : pickList) 105 | list.add(s); 106 | jsonArrayBuilder.add(Json.createObjectBuilder() 107 | .add("constraint", "List") 108 | .add("list", list)); 109 | } 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /src/main/java/tools4j/validator/ShortValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator; 28 | 29 | import java.awt.Window; 30 | import java.math.BigDecimal; 31 | 32 | public class ShortValidator extends NumberValidator { 33 | @Override 34 | protected boolean transform(Window window, String value) { 35 | boolean result = true; 36 | 37 | try { 38 | transformedValue = Short.parseShort(value); 39 | } 40 | catch (NumberFormatException e) { 41 | violationConstraint = "Transform"; 42 | violationMessage = String.format(violationMessageResource.getString("STR_TRANSFORM"), value, "Short"); 43 | 44 | showViolationDialog(window); 45 | 46 | result = false; 47 | } 48 | 49 | return result; 50 | } 51 | 52 | @Override 53 | protected Short getNumber(BigDecimal value) { 54 | return value.shortValue(); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/tools4j/validator/StringValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator; 28 | 29 | import java.awt.Window; 30 | import java.io.StringReader; 31 | 32 | import javax.json.Json; 33 | import javax.json.JsonArrayBuilder; 34 | import javax.json.stream.JsonParser; 35 | import javax.json.stream.JsonParser.Event; 36 | 37 | public class StringValidator extends Validator { 38 | protected String pattern; 39 | 40 | protected Integer min; 41 | protected Integer max; 42 | 43 | protected boolean notEmpty; // NotEmpyty 44 | protected boolean empty; // Empty 45 | 46 | public String getPattern() { 47 | return pattern; 48 | } 49 | 50 | public void setPattern(String pattern) { 51 | this.pattern = pattern; 52 | } 53 | 54 | public Integer getMin() { 55 | return min; 56 | } 57 | 58 | public void setMin(int min) { 59 | this.min = min; 60 | } 61 | 62 | public Integer getMax() { 63 | return max; 64 | } 65 | 66 | public void setMax(int max) { 67 | this.max = max; 68 | } 69 | 70 | public void setSize(int min, int max) { 71 | setMin(min); 72 | setMax(max); 73 | } 74 | 75 | public boolean inSize(String value) { 76 | boolean result = true; 77 | 78 | if (min!=null) 79 | result = value.length()>=min; 80 | if (max!=null) 81 | result = result && value.length()<=max; 82 | 83 | return result; 84 | } 85 | 86 | public boolean isNotEmpty() { 87 | return notEmpty; 88 | } 89 | 90 | public void setNotEmpty(boolean value) { 91 | notEmpty = value; 92 | } 93 | 94 | public boolean isEmpty() { 95 | return empty; 96 | } 97 | 98 | public void setEmpty(boolean value) { 99 | empty = value; 100 | } 101 | 102 | @Override 103 | protected boolean transform(Window window, String value) { 104 | transformedValue = value; 105 | 106 | return true; 107 | } 108 | 109 | @Override 110 | protected boolean validation(Window window, String value) { 111 | boolean result = true; 112 | 113 | if (pattern!=null) { 114 | result = ((String)value).matches(pattern); 115 | 116 | if (!result) { 117 | violationConstraint = "Pattern"; 118 | violationMessage = String.format(violationMessageResource.getString("STR_PATTERN"), value, pattern); 119 | showViolationDialog(window); 120 | 121 | result = false; 122 | } 123 | } 124 | 125 | if (result) { 126 | if (notEmpty && value.length()==0) { 127 | violationConstraint = "NotEmpty"; 128 | violationMessage = String.format(violationMessageResource.getString("STR_NOTEMPTY")); 129 | showViolationDialog(window); 130 | 131 | result = false; 132 | 133 | } else if (empty && value.length()!=0) { 134 | violationConstraint = "Empty"; 135 | violationMessage = String.format(violationMessageResource.getString("STR_EMPTY"), value); 136 | showViolationDialog(window); 137 | 138 | result = false; 139 | } 140 | } 141 | 142 | if (result) 143 | if (min!=null || max!=null) 144 | if (!inSize(value)) { 145 | String minStr = String.valueOf(min); 146 | String maxStr = String.valueOf(max); 147 | 148 | if (min!=null && max!=null) { 149 | violationConstraint = "Size"; 150 | violationMessage = String.format(violationMessageResource.getString("STR_SIZE"), value, OPENED[1]+minStr, maxStr+CLOSED[1]); 151 | } 152 | else if (min==null && max!=null) { 153 | violationConstraint = "Max"; 154 | violationMessage = String.format(violationMessageResource.getString("STR_SIZE2"), value, SMALLER[window==null?1:2]+maxStr); 155 | } 156 | else if (min!=null && max==null) { 157 | violationConstraint = "Min"; 158 | violationMessage = String.format(violationMessageResource.getString("STR_SIZE2"), value, LARGER[window==null?1:2]+minStr); 159 | } 160 | 161 | showViolationDialog(window); 162 | 163 | result = false; 164 | } 165 | 166 | 167 | return result; 168 | } 169 | 170 | @Override 171 | protected void interpretConstraints(String constraints) { 172 | JsonParser parser = Json.createParser(new StringReader(constraints)); 173 | while (parser.hasNext()) { 174 | Event e = parser.next(); 175 | if (e == Event.KEY_NAME) 176 | if (parser.getString().equalsIgnoreCase("constraint")) { 177 | parser.next(); 178 | if (parser.getString().equalsIgnoreCase("Pattern")) { 179 | parser.next(); 180 | parser.next(); 181 | setPattern(parser.getString()); 182 | } 183 | else if (parser.getString().equalsIgnoreCase("NotEmpty")) { 184 | setNotEmpty(true); 185 | } 186 | else if (parser.getString().equalsIgnoreCase("Empty")) { 187 | setEmpty(true); 188 | } 189 | else if (parser.getString().equalsIgnoreCase("Min")) { 190 | parser.next(); 191 | parser.next(); 192 | setMin(parser.getInt()); 193 | } 194 | else if (parser.getString().equalsIgnoreCase("Max")) { 195 | parser.next(); 196 | parser.next(); 197 | setMax(parser.getInt()); 198 | } 199 | else if (parser.getString().equalsIgnoreCase("Size")) { 200 | parser.next(); 201 | parser.next(); 202 | setMin(parser.getInt()); 203 | parser.next(); 204 | parser.next(); 205 | setMax(parser.getInt()); 206 | } 207 | } 208 | } 209 | parser.close(); 210 | } 211 | 212 | @Override 213 | public void setConstraints(String constraints) { 214 | pattern = null; 215 | notEmpty = false; 216 | empty = false; 217 | min = null; 218 | max = null; 219 | 220 | super.setConstraints(constraints); 221 | } 222 | 223 | @Override 224 | protected void buildConstraints(JsonArrayBuilder jsonArrayBuilder) { 225 | if (pattern!=null) 226 | jsonArrayBuilder.add(Json.createObjectBuilder() 227 | .add("constraint", "Pattern") 228 | .add("regex", pattern)); 229 | 230 | if (notEmpty) 231 | jsonArrayBuilder.add(Json.createObjectBuilder() 232 | .add("constraint", "NotEmpty")); 233 | if (empty) 234 | jsonArrayBuilder.add(Json.createObjectBuilder() 235 | .add("constraint", "Empty")); 236 | 237 | if (min!=null && max!=null) 238 | jsonArrayBuilder.add(Json.createObjectBuilder() 239 | .add("constraint", "Size") 240 | .add("min", min) 241 | .add("max", max)); 242 | else if (min!=null) 243 | jsonArrayBuilder.add(Json.createObjectBuilder() 244 | .add("constraint", "Min") 245 | .add("min", min)); 246 | else if (max!=null) 247 | jsonArrayBuilder.add(Json.createObjectBuilder() 248 | .add("constraint", "Max") 249 | .add("max", max)); 250 | } 251 | } 252 | -------------------------------------------------------------------------------- /src/main/java/tools4j/validator/TransformationListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator; 28 | 29 | public interface TransformationListener { 30 | public void before(Validator validator, String value); 31 | public void after(Validator validator, String value, boolean valid); 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/tools4j/validator/ValidationListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator; 28 | 29 | public interface ValidationListener { 30 | public void before(Validator validator); 31 | public void after(Validator validator, boolean valid); 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/tools4j/validator/Validator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator; 28 | 29 | import java.awt.Window; 30 | import java.io.StringReader; 31 | import java.util.Locale; 32 | import java.util.ResourceBundle; 33 | 34 | import javax.json.Json; 35 | import javax.json.JsonArrayBuilder; 36 | import javax.json.JsonObject; 37 | import javax.json.stream.JsonParser; 38 | import javax.json.stream.JsonParser.Event; 39 | import javax.swing.JOptionPane; 40 | 41 | public abstract class Validator { 42 | private static String violationMessageLanguage = Locale.getDefault().getLanguage(); 43 | protected static ResourceBundle violationMessageResource = ResourceBundle.getBundle("tools4j.resources/Validator", new Locale(violationMessageLanguage)); 44 | 45 | protected static final String[] OPENED = {"(", "["}; 46 | protected static final String[] CLOSED = {")", "]"}; 47 | protected static final String[] SMALLER = {"<", "<=", "\u2264"}; 48 | protected static final String[] LARGER = {">", ">=", "\u2265"}; 49 | 50 | protected String violationMessage; 51 | protected String violationConstraint; 52 | 53 | 54 | protected TransformationListener transformationListener; 55 | protected ValidationListener validationListener; 56 | protected BusinessRuleListener businessRuleListener; 57 | 58 | protected boolean notNull; // NotNull 59 | protected boolean Null; // Null 60 | 61 | protected String businessRuleID; 62 | 63 | protected T transformedValue; 64 | 65 | protected String constraints; 66 | 67 | public static String getViolationMessageLanguage() { 68 | return violationMessageLanguage; 69 | } 70 | 71 | public static void setViolationMessageLanguage(String violationMessageLanguage) { 72 | Validator.violationMessageLanguage = violationMessageLanguage; 73 | 74 | violationMessageResource = ResourceBundle.getBundle("tools4j.resources/Validator", new Locale(violationMessageLanguage)); 75 | } 76 | 77 | public String getViolationMessage() { 78 | return violationMessage; 79 | } 80 | 81 | public String getViolationConstraint() { 82 | return violationConstraint; 83 | } 84 | 85 | public TransformationListener getTransformationListener() { 86 | return transformationListener; 87 | } 88 | 89 | public void setTransformationListener(TransformationListener transformationListener) { 90 | this.transformationListener = transformationListener; 91 | } 92 | 93 | public ValidationListener getValidationListener() { 94 | return validationListener; 95 | } 96 | 97 | public void setValidationListener(ValidationListener validationListener) { 98 | this.validationListener = validationListener; 99 | } 100 | 101 | public BusinessRuleListener getBusinessRuleListener() { 102 | return businessRuleListener; 103 | } 104 | 105 | public void setBusinessRuleListener(BusinessRuleListener businessRuleListener) { 106 | this.businessRuleListener = businessRuleListener; 107 | } 108 | 109 | public boolean isNotNull() { 110 | return notNull; 111 | } 112 | 113 | public void setNotNull(boolean value) { 114 | notNull = value; 115 | } 116 | 117 | public boolean isNull() { 118 | return Null; 119 | } 120 | 121 | public void setNull(boolean value) { 122 | Null = value; 123 | } 124 | 125 | public String getBusinessRuleID() { 126 | return businessRuleID; 127 | } 128 | 129 | public void setBusinessRuleID(String value) { 130 | businessRuleID = value; 131 | } 132 | 133 | protected abstract boolean transform(Window window, String value); 134 | 135 | public boolean validateString(String value) { 136 | return validateString(null, value); 137 | } 138 | 139 | public boolean validateString(Window window, String value) { 140 | boolean result = true; 141 | 142 | if (transformationListener!=null) transformationListener.before(this, value); 143 | 144 | if (value==null) { 145 | if (notNull) { 146 | violationConstraint = "NotNull"; 147 | violationMessage = String.format(violationMessageResource.getString("STR_NOTNULL")); 148 | showViolationDialog(window); 149 | 150 | result = false; 151 | } 152 | } 153 | else { 154 | if (Null) { 155 | violationConstraint = "Null"; 156 | violationMessage = String.format(violationMessageResource.getString("STR_NULL")); 157 | showViolationDialog(window); 158 | 159 | result = false; 160 | } 161 | } 162 | 163 | if (result && value!=null) 164 | result = transform(window, value); 165 | 166 | if (transformationListener!=null) transformationListener.after(this, value, result); 167 | 168 | if (result) 169 | return validate(window, transformedValue); 170 | else 171 | return false; 172 | } 173 | 174 | protected abstract boolean validation(Window window, T value); 175 | 176 | public boolean validate(T value) { 177 | return validate(null, value); 178 | } 179 | 180 | public boolean validate(Window window, T value) { 181 | this.transformedValue = value; 182 | 183 | boolean result = true; 184 | 185 | violationMessage = null; 186 | violationConstraint = null; 187 | 188 | if (validationListener!=null) validationListener.before(this); 189 | 190 | if (value==null) { 191 | if (notNull) { 192 | violationConstraint = "NotNull"; 193 | violationMessage = String.format(violationMessageResource.getString("STR_NOTNULL")); 194 | showViolationDialog(window); 195 | 196 | result = false; 197 | } 198 | } 199 | else { 200 | if (Null) { 201 | violationConstraint = "Null"; 202 | violationMessage = String.format(violationMessageResource.getString("STR_NULL")); 203 | showViolationDialog(window); 204 | 205 | result = false; 206 | } 207 | } 208 | 209 | if (result && value!=null) 210 | result = validation(window, value); 211 | 212 | if (result) 213 | if (businessRuleListener!=null) { 214 | result = businessRuleListener.checkBusinessRule(this); 215 | if (!result) { 216 | violationConstraint = "Rule"; 217 | if (businessRuleID!=null) { 218 | String businessRuleName = null; 219 | if (businessRuleListener instanceof DefaultBusinessRuleListener) 220 | businessRuleName = ((DefaultBusinessRuleListener)businessRuleListener).getBusinessRuleName(this); 221 | violationMessage = String.format(violationMessageResource.getString("STR_RULE2"), businessRuleName!=null ? businessRuleName : businessRuleID); 222 | } 223 | else 224 | violationMessage = String.format(violationMessageResource.getString("STR_RULE")); 225 | showViolationDialog(window); 226 | } 227 | } 228 | 229 | if (validationListener!=null) validationListener.after(this, result); 230 | 231 | return result; 232 | } 233 | 234 | public T getValue() { 235 | return transformedValue; 236 | } 237 | 238 | protected void showViolationDialog(Window window) { 239 | if (window!=null) 240 | JOptionPane.showMessageDialog(window, 241 | violationMessage, 242 | violationMessageResource.getString("STR_TITLE"), 243 | JOptionPane.ERROR_MESSAGE); 244 | } 245 | 246 | public String getConstraints() { 247 | return constraints; 248 | } 249 | 250 | protected abstract void interpretConstraints(String constraints); 251 | 252 | public void setConstraints(String constraints) { 253 | this.constraints = constraints; 254 | 255 | notNull = false; 256 | Null = false; 257 | businessRuleID = null; 258 | 259 | JsonParser parser = Json.createParser(new StringReader(constraints)); 260 | while (parser.hasNext()) { 261 | Event e = parser.next(); 262 | if (e == Event.KEY_NAME) 263 | if (parser.getString().equalsIgnoreCase("constraint")) { 264 | parser.next(); 265 | if (parser.getString().equalsIgnoreCase("NotNull")) { 266 | setNotNull(true); 267 | } else if (parser.getString().equalsIgnoreCase("Null")) { 268 | setNull(true); 269 | } else if (parser.getString().equalsIgnoreCase("Rule")) { 270 | parser.next(); 271 | parser.next(); 272 | setBusinessRuleID(parser.getString()); 273 | } 274 | } 275 | } 276 | parser.close(); 277 | 278 | interpretConstraints(constraints); 279 | } 280 | 281 | public void setConstraints(JsonObject model) { 282 | setConstraints(model.toString()); 283 | } 284 | 285 | protected abstract void buildConstraints(JsonArrayBuilder jsonArrayBuilder); 286 | 287 | public JsonObject getConstraintsAsJsonObject() { 288 | JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder(); 289 | if (isNotNull()) 290 | jsonArrayBuilder.add(Json.createObjectBuilder().add("constraint", "NotNull")); 291 | if (isNull()) 292 | jsonArrayBuilder.add(Json.createObjectBuilder().add("constraint", "Null")); 293 | if (businessRuleID!=null) 294 | jsonArrayBuilder.add(Json.createObjectBuilder() 295 | .add("constraint", "Rule") 296 | .add("id", businessRuleID)); 297 | 298 | buildConstraints(jsonArrayBuilder); 299 | 300 | JsonObject result = Json.createObjectBuilder() 301 | .add("type", getClass().getSimpleName().replace("Validator", "")) 302 | .add("constraints", jsonArrayBuilder).build(); 303 | 304 | return result; 305 | } 306 | } 307 | -------------------------------------------------------------------------------- /src/main/java/tools4j/validator/ValidatorFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator; 28 | 29 | import java.io.StringReader; 30 | 31 | import javax.json.Json; 32 | import javax.json.stream.JsonParser; 33 | import javax.json.stream.JsonParser.Event; 34 | 35 | public class ValidatorFactory { 36 | public static Validator createValidator(String name) { 37 | Validator result = null; 38 | 39 | if (name!=null) 40 | switch (name.toUpperCase().replace("VALIDATOR", "")) { 41 | case "STRING": result = new StringValidator(); break; 42 | case "BOOLEAN": result = new BooleanValidator(); break; 43 | case "BYTE": result = new ByteValidator(); break; 44 | case "SHORT": result = new ShortValidator(); break; 45 | case "INTEGER": result = new IntegerValidator(); break; 46 | case "LONG": result = new LongValidator(); break; 47 | case "FLOAT": result = new FloatValidator(); break; 48 | case "DOUBLE": result = new DoubleValidator(); break; 49 | case "BIGINTEGER": result = new BigIntegerValidator(); break; 50 | case "BIGDECIMAL": result = new BigDecimalValidator(); break; 51 | case "DATE": result = new DateValidator(); break; 52 | case "EMAIL": result = new EmailValidator(); break; 53 | case "PICKLIST": result = new PickListValidator(); break; 54 | } 55 | 56 | return result; 57 | } 58 | 59 | public static String parseType(String json) { 60 | JsonParser parser = Json.createParser(new StringReader(json)); 61 | while (parser.hasNext()) { 62 | Event e = parser.next(); 63 | if (e == Event.KEY_NAME) 64 | if (parser.getString().equalsIgnoreCase("type")) { 65 | parser.next(); 66 | return parser.getString(); 67 | } 68 | } 69 | parser.close(); 70 | 71 | return null; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/main/java/tools4j/validator/utils/ValidationDocumentFilter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator.utils; 28 | 29 | import java.awt.Window; 30 | 31 | import javax.swing.JComponent; 32 | import javax.swing.JLabel; 33 | import javax.swing.text.AttributeSet; 34 | import javax.swing.text.BadLocationException; 35 | import javax.swing.text.DocumentFilter; 36 | 37 | import tools4j.validator.Validator; 38 | 39 | public class ValidationDocumentFilter extends DocumentFilter { 40 | protected Window window; 41 | protected Validator validator; 42 | protected JComponent input; 43 | protected JComponent output; 44 | 45 | public ValidationDocumentFilter(Window window, Validator validator) { 46 | this.window = window; 47 | this.validator = validator; 48 | } 49 | 50 | public ValidationDocumentFilter(Validator validator, JComponent input, JComponent output) { 51 | this.validator = validator; 52 | this.input = input; 53 | this.output = output; 54 | } 55 | 56 | protected void notifyOutput(boolean success) { 57 | if (output instanceof JLabel) 58 | if (!success) 59 | ((JLabel)output).setText((input.getName()!=null ? input.getName()+": " : "")+validator.getViolationMessage()); 60 | else 61 | ((JLabel)output).setText(""); 62 | } 63 | 64 | @Override 65 | public void remove(DocumentFilter.FilterBypass fb, int offset, int length) throws BadLocationException { 66 | String content = fb.getDocument().getText(0, fb.getDocument().getLength()); 67 | String string = content.substring(offset, offset+length); 68 | super.remove(fb, offset, length); 69 | 70 | boolean success; 71 | if (!(success=validator.validateString(window, fb.getDocument().getText(0, fb.getDocument().getLength())))) 72 | super.insertString(fb, offset, string, null); 73 | 74 | if (output!=null) 75 | notifyOutput(success); 76 | } 77 | 78 | @Override 79 | public void insertString(DocumentFilter.FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException { 80 | String content = fb.getDocument().getText(0, fb.getDocument().getLength()); 81 | StringBuffer sb = new StringBuffer(); 82 | if (offset < fb.getDocument().getLength()) { 83 | if (offset>0) sb.append(content.substring(0, offset)); 84 | sb.append(string); 85 | sb.append(content.substring(offset, content.length())); 86 | } 87 | else { 88 | sb.append(content); 89 | sb.append(string); 90 | } 91 | 92 | super.insertString(fb, offset, string, attr); 93 | 94 | boolean success; 95 | if (!(success=validator.validateString(window, sb.toString()))) 96 | super.remove(fb, offset, string.length()); 97 | 98 | if (output!=null) 99 | notifyOutput(success); 100 | } 101 | 102 | @Override 103 | public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException { 104 | insertString(fb, offset, text, attrs); 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /src/main/java/tools4j/validator/utils/ValidationInputVerifier.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator.utils; 28 | 29 | import java.awt.Window; 30 | 31 | import javax.swing.InputVerifier; 32 | import javax.swing.JComboBox; 33 | import javax.swing.JComponent; 34 | import javax.swing.JLabel; 35 | import javax.swing.JTextField; 36 | 37 | import tools4j.validator.Validator; 38 | 39 | public class ValidationInputVerifier extends InputVerifier { 40 | protected Window window; 41 | protected Validator validator; 42 | protected JComponent output; 43 | 44 | public ValidationInputVerifier(Window window, Validator validator) { 45 | this.window = window; 46 | this.validator = validator; 47 | } 48 | 49 | public ValidationInputVerifier(Validator validator, JComponent output) { 50 | this.validator = validator; 51 | this.output = output; 52 | } 53 | 54 | @Override 55 | public boolean verify(JComponent input) { 56 | return ValidationInputVerifier.verify(window, validator, input, output); 57 | } 58 | 59 | protected static boolean verify(Window window, Validator validator, JComponent input, JComponent output) { 60 | boolean result = true; 61 | 62 | if (input instanceof JTextField) 63 | result = validator.validateString(window, ((JTextField)input).getText()); 64 | else if (input instanceof JComboBox) { 65 | result = validator.validateString(window, ((JComboBox)input).getSelectedItem().toString()); 66 | } 67 | 68 | if (output!=null) 69 | if (output instanceof JLabel) 70 | if (!result) 71 | ((JLabel)output).setText((input.getName()!=null ? input.getName()+": " : "")+validator.getViolationMessage()); 72 | else 73 | ((JLabel)output).setText(""); 74 | 75 | return result; 76 | } 77 | 78 | public static boolean verify(Window window, Validator validator, JComponent input) { 79 | return ValidationInputVerifier.verify(window, validator, input, null); 80 | } 81 | 82 | protected static boolean verify(Validator validator, JComponent input, JComponent output) { 83 | return ValidationInputVerifier.verify(null, validator, input, output); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/main/java/tools4j/validator/utils/ValidationLabel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator.utils; 28 | 29 | import java.awt.Color; 30 | 31 | import javax.swing.JLabel; 32 | 33 | public class ValidationLabel extends JLabel { 34 | private static final long serialVersionUID = 8200582943610301540L; 35 | 36 | public ValidationLabel() { 37 | super(); 38 | 39 | this.setForeground(Color.RED); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/resources/tools4j/resources/Validator.properties: -------------------------------------------------------------------------------- 1 | STR_TITLE=Error 2 | STR_NULL=The input value must not be initialized\! 3 | STR_NOTNULL=The input value is not initialized\! 4 | STR_PATTERN=The input value "%s" is not allowed ("%s" expected)\! 5 | STR_NOTEMPTY=The input value must not be empty\! 6 | STR_EMPTY=The input value "%s" must be empty\! 7 | STR_SIZE=The input value "%s" is outside the length range %s; %s\! 8 | STR_SIZE2=The input value "%s" is outside the length range (%s)\! 9 | STR_NUMBER=The input value "%s" is not allowed (Number expected)\! 10 | STR_TRANSFORM=The input value "%s" is not allowed (%s expected)\! 11 | STR_RANGE=The input value "%s" is out of range %s; %s\! 12 | STR_RANGE2=The input value "%s" is out of range (%s)\! 13 | STR_DATE=The input value "%s" is not allowed ("%s" expected)\! 14 | STR_VALID=The input value is not allowed ("%s")\! 15 | STR_PAST=The input value "%s" is not in the past\! 16 | STR_FUTURE=The input value "%s" is not in the future\! 17 | STR_PICKLIST=The input value "%s" is not allowed ("%s" expected)\! 18 | STR_ASSERTTRUE=The input value is not true\! 19 | STR_ASSERTFALSE=The input value is not false\! 20 | STR_RULE=The input value is not compliant to rules\! 21 | STR_RULE2=The input value is not compliant to rules ("%s")\! -------------------------------------------------------------------------------- /src/main/resources/tools4j/resources/Validator_de.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/relvaner/tools4j-validator/bceadd50e21ee8fa3fa73902fbde7655e74c8092/src/main/resources/tools4j/resources/Validator_de.properties -------------------------------------------------------------------------------- /src/main/resources/tools4j/resources/Validator_en.properties: -------------------------------------------------------------------------------- 1 | STR_TITLE=Error 2 | STR_NULL=The input value must not be initialized\! 3 | STR_NOTNULL=The input value is not initialized\! 4 | STR_PATTERN=The input value "%s" is not allowed ("%s" expected)\! 5 | STR_NOTEMPTY=The input value must not be empty\! 6 | STR_EMPTY=The input value "%s" must be empty\! 7 | STR_SIZE=The input value "%s" is outside the length range %s; %s\! 8 | STR_SIZE2=The input value "%s" is outside the length range (%s)\! 9 | STR_NUMBER=The input value "%s" is not allowed (Number expected)\! 10 | STR_TRANSFORM=The input value "%s" is not allowed (%s expected)\! 11 | STR_RANGE=The input value "%s" is out of range %s; %s\! 12 | STR_RANGE2=The input value "%s" is out of range (%s)\! 13 | STR_DATE=The input value "%s" is not allowed ("%s" expected)\! 14 | STR_VALID=The input value is not allowed ("%s")\! 15 | STR_PAST=The input value "%s" is not in the past\! 16 | STR_FUTURE=The input value "%s" is not in the future\! 17 | STR_PICKLIST=The input value "%s" is not allowed ("%s" expected)\! 18 | STR_ASSERTTRUE=The input value is not true\! 19 | STR_ASSERTFALSE=The input value is not false\! 20 | STR_RULE=The input value is not compliant to rules\! 21 | STR_RULE2=The input value is not compliant to rules ("%s")\! -------------------------------------------------------------------------------- /src/test/java/tools4j/validator/features/AllFeaturesTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator.features; 28 | 29 | import org.junit.runner.RunWith; 30 | import org.junit.runners.Suite; 31 | 32 | import tools4j.validator.utils.features.DocumentFilterFeature; 33 | import tools4j.validator.utils.features.InputVerifierFeature; 34 | 35 | @RunWith(Suite.class) 36 | @Suite.SuiteClasses({ 37 | BasicFeature.class, 38 | StringFeature.class, 39 | EmailFeature.class, 40 | PickListFeature.class, 41 | DateFeature.class, 42 | NumberFeature.class, 43 | LongFeature.class, 44 | IntegerFeature.class, 45 | ShortFeature.class, 46 | ByteFeature.class, 47 | FloatFeature.class, 48 | DoubleFeature.class, 49 | BooleanFeature.class, 50 | BigIntegerFeature.class, 51 | BigDecimalFeature.class, 52 | BusinessRulesFeature.class, 53 | DynamicValidationFeature.class, 54 | DocumentFilterFeature.class, 55 | InputVerifierFeature.class 56 | }) 57 | public class AllFeaturesTest { 58 | 59 | } 60 | -------------------------------------------------------------------------------- /src/test/java/tools4j/validator/features/BasicFeature.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator.features; 28 | 29 | import static org.junit.Assert.*; 30 | 31 | import javax.json.JsonArray; 32 | 33 | import org.junit.Before; 34 | import org.junit.Test; 35 | 36 | import tools4j.validator.StringValidator; 37 | 38 | public class BasicFeature { 39 | private StringValidator v1; 40 | private StringValidator v2; 41 | private StringValidator v3; 42 | 43 | @Before 44 | public void before() { 45 | v1 = new StringValidator(); 46 | v2 = new StringValidator(); 47 | v3 = new StringValidator(); 48 | } 49 | 50 | @Test 51 | public void test_Nothing() { 52 | v1.validate(null); 53 | assertTrue(v1.getViolationMessage()==null); 54 | 55 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 56 | v2.validate(null); 57 | assertTrue(v2.getViolationMessage()==null); 58 | assertTrue(v2.getViolationConstraint()==null); 59 | 60 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 61 | v3.validate(null); 62 | assertTrue(v3.getViolationMessage()==null); 63 | assertTrue(v3.getViolationConstraint()==null); 64 | } 65 | 66 | @Test 67 | public void test_resetConstraints() { 68 | v1.setNotNull(true); 69 | v1.setNull(true); 70 | v1.setBusinessRuleID("1"); 71 | 72 | v1.setConstraints("{}"); 73 | assertTrue(((JsonArray)v1.getConstraintsAsJsonObject().get("constraints")).size()==0); 74 | } 75 | 76 | @Test 77 | public void test_NotNull_invalid() { 78 | v1.setNotNull(true); 79 | v1.validate(null); 80 | assertTrue(v1.getViolationMessage()!=null); 81 | assertEquals("NotNull", v1.getViolationConstraint()); 82 | 83 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 84 | v2.validate(null); 85 | assertTrue(v2.getViolationMessage()!=null); 86 | assertEquals("NotNull", v2.getViolationConstraint()); 87 | 88 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 89 | v3.validate(null); 90 | assertTrue(v3.getViolationMessage()!=null); 91 | assertEquals("NotNull", v3.getViolationConstraint()); 92 | } 93 | 94 | @Test 95 | public void test_NotNull_valid() { 96 | v1.setNotNull(false); 97 | v1.validate(null); 98 | assertTrue(v1.getViolationMessage()==null); 99 | assertTrue(v1.getViolationConstraint()==null); 100 | 101 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 102 | v2.validate(null); 103 | assertTrue(v2.getViolationMessage()==null); 104 | assertTrue(v2.getViolationConstraint()==null); 105 | 106 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 107 | v3.validate(null); 108 | assertTrue(v3.getViolationMessage()==null); 109 | assertTrue(v3.getViolationConstraint()==null); 110 | } 111 | 112 | @Test 113 | public void test_Null_invalid() { 114 | v1.setNull(true); 115 | v1.validate(""); 116 | assertTrue(v1.getViolationMessage()!=null); 117 | assertEquals("Null", v1.getViolationConstraint()); 118 | 119 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 120 | v2.validate(""); 121 | assertTrue(v2.getViolationMessage()!=null); 122 | assertEquals("Null", v2.getViolationConstraint()); 123 | 124 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 125 | v3.validate(""); 126 | assertTrue(v3.getViolationMessage()!=null); 127 | assertEquals("Null", v3.getViolationConstraint()); 128 | } 129 | 130 | @Test 131 | public void test_Null_valid() { 132 | v1.setNull(false); 133 | v1.validate(null); 134 | assertTrue(v1.getViolationMessage()==null); 135 | assertTrue(v1.getViolationConstraint()==null); 136 | 137 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 138 | v2.validate(null); 139 | assertTrue(v2.getViolationMessage()==null); 140 | assertTrue(v2.getViolationConstraint()==null); 141 | 142 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 143 | v3.validate(null); 144 | assertTrue(v3.getViolationMessage()==null); 145 | assertTrue(v3.getViolationConstraint()==null); 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /src/test/java/tools4j/validator/features/BigDecimalFeature.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator.features; 28 | 29 | import static org.junit.Assert.*; 30 | 31 | import org.junit.Before; 32 | import org.junit.Test; 33 | 34 | import tools4j.validator.BigDecimalValidator; 35 | 36 | public class BigDecimalFeature { 37 | private BigDecimalValidator v1; 38 | private BigDecimalValidator v2; 39 | private BigDecimalValidator v3; 40 | 41 | @Before 42 | public void before() { 43 | v1 = new BigDecimalValidator(); 44 | v2 = new BigDecimalValidator(); 45 | v3 = new BigDecimalValidator(); 46 | } 47 | 48 | @Test 49 | public void test_Transform_invalid() { 50 | v1.validateString("a"); 51 | assertTrue(v1.getViolationMessage()!=null); 52 | assertEquals("Transform", v1.getViolationConstraint()); 53 | 54 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 55 | v2.validateString("b"); 56 | assertTrue(v2.getViolationMessage()!=null); 57 | assertEquals("Transform", v2.getViolationConstraint()); 58 | 59 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 60 | v3.validateString("b"); 61 | assertTrue(v3.getViolationMessage()!=null); 62 | assertTrue(v3.getViolationConstraint()!=null); 63 | } 64 | 65 | @Test 66 | public void test_Transform_valid() { 67 | v1.validateString("4"); 68 | assertTrue(v1.getViolationMessage()==null); 69 | assertTrue(v1.getViolationConstraint()==null); 70 | 71 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 72 | v2.validateString("1.7976931348623157345345345345E+1030832434"); 73 | assertTrue(v2.getViolationMessage()==null); 74 | assertTrue(v2.getViolationConstraint()==null); 75 | 76 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 77 | v3.validateString("-1.79769313486231573453453453E+1030899975"); 78 | assertTrue(v3.getViolationMessage()==null); 79 | assertTrue(v3.getViolationConstraint()==null); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/test/java/tools4j/validator/features/BigIntegerFeature.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator.features; 28 | 29 | import static org.junit.Assert.*; 30 | 31 | import org.junit.Before; 32 | import org.junit.Test; 33 | 34 | import tools4j.validator.BigIntegerValidator; 35 | 36 | public class BigIntegerFeature { 37 | private BigIntegerValidator v1; 38 | private BigIntegerValidator v2; 39 | private BigIntegerValidator v3; 40 | 41 | @Before 42 | public void before() { 43 | v1 = new BigIntegerValidator(); 44 | v2 = new BigIntegerValidator(); 45 | v3 = new BigIntegerValidator(); 46 | } 47 | 48 | @Test 49 | public void test_Transform_invalid() { 50 | v1.validateString("a"); 51 | assertTrue(v1.getViolationMessage()!=null); 52 | assertEquals("Transform", v1.getViolationConstraint()); 53 | 54 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 55 | v2.validateString("2.34"); 56 | assertTrue(v2.getViolationMessage()!=null); 57 | assertEquals("Transform", v2.getViolationConstraint()); 58 | 59 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 60 | v3.validateString("-2.34"); 61 | assertTrue(v3.getViolationMessage()!=null); 62 | assertEquals("Transform", v3.getViolationConstraint()); 63 | } 64 | 65 | @Test 66 | public void test_Transform_valid() { 67 | v1.validateString("4"); 68 | assertTrue(v1.getViolationMessage()==null); 69 | assertTrue(v1.getViolationConstraint()==null); 70 | 71 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 72 | v2.validateString("-9223372036854775808456345637737"); 73 | assertTrue(v2.getViolationMessage()==null); 74 | assertTrue(v2.getViolationConstraint()==null); 75 | 76 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 77 | v3.validateString("922337203685477580745674574567356"); 78 | assertTrue(v3.getViolationMessage()==null); 79 | assertTrue(v3.getViolationConstraint()==null); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/test/java/tools4j/validator/features/BooleanFeature.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator.features; 28 | 29 | import static org.junit.Assert.*; 30 | 31 | import javax.json.JsonArray; 32 | 33 | import org.junit.Before; 34 | import org.junit.Test; 35 | 36 | import tools4j.validator.BooleanValidator; 37 | 38 | public class BooleanFeature { 39 | private BooleanValidator v1; 40 | private BooleanValidator v2; 41 | private BooleanValidator v3; 42 | 43 | @Before 44 | public void before() { 45 | v1 = new BooleanValidator(); 46 | v2 = new BooleanValidator(); 47 | v3 = new BooleanValidator(); 48 | } 49 | 50 | @Test 51 | public void test_resetConstraints() { 52 | v1.setAssertTrue(true); 53 | v1.setAssertFalse(true); 54 | 55 | v1.setConstraints("{}"); 56 | assertTrue(((JsonArray)v1.getConstraintsAsJsonObject().get("constraints")).size()==0); 57 | } 58 | 59 | @Test 60 | public void test_Transform_invalid() { 61 | v1.validateString("2"); 62 | assertTrue(v1.getViolationMessage()!=null); 63 | assertEquals("Transform", v1.getViolationConstraint()); 64 | 65 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 66 | v2.validateString("ABC"); 67 | assertTrue(v2.getViolationMessage()!=null); 68 | assertEquals("Transform", v2.getViolationConstraint()); 69 | 70 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 71 | v3.validateString("ABC"); 72 | assertTrue(v3.getViolationMessage()!=null); 73 | assertEquals("Transform", v3.getViolationConstraint()); 74 | } 75 | 76 | @Test 77 | public void test_Transform_valid() { 78 | v1.validateString("1"); 79 | assertTrue(v1.getViolationMessage()==null); 80 | assertTrue(v1.getViolationConstraint()==null); 81 | v1.validateString("0"); 82 | assertTrue(v1.getViolationMessage()==null); 83 | assertTrue(v1.getViolationConstraint()==null); 84 | 85 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 86 | v2.validateString("true"); 87 | assertTrue(v2.getViolationMessage()==null); 88 | assertTrue(v2.getViolationConstraint()==null); 89 | 90 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 91 | v3.validateString("false"); 92 | assertTrue(v3.getViolationMessage()==null); 93 | assertTrue(v3.getViolationConstraint()==null); 94 | } 95 | 96 | @Test 97 | public void test_AssertFalse_invalid() { 98 | v1.setAssertFalse(true); 99 | v1.validateString("1"); 100 | assertTrue(v1.getViolationMessage()!=null); 101 | assertEquals("AssertFalse", v1.getViolationConstraint()); 102 | 103 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 104 | v2.validateString("true"); 105 | assertTrue(v2.getViolationMessage()!=null); 106 | assertEquals("AssertFalse", v2.getViolationConstraint()); 107 | 108 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 109 | v3.validateString("true"); 110 | assertTrue(v3.getViolationMessage()!=null); 111 | assertEquals("AssertFalse", v3.getViolationConstraint()); 112 | } 113 | 114 | @Test 115 | public void test_AssertFalse_valid() { 116 | v1.setAssertFalse(false); 117 | v1.validateString("1"); 118 | assertTrue(v1.getViolationMessage()==null); 119 | assertTrue(v1.getViolationConstraint()==null); 120 | v1.setAssertFalse(true); 121 | v1.validateString("0"); 122 | assertTrue(v1.getViolationMessage()==null); 123 | assertTrue(v1.getViolationConstraint()==null); 124 | 125 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 126 | v2.validateString("false"); 127 | assertTrue(v2.getViolationMessage()==null); 128 | assertTrue(v2.getViolationConstraint()==null); 129 | 130 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 131 | v3.validate(false); 132 | assertTrue(v3.getViolationMessage()==null); 133 | assertTrue(v3.getViolationConstraint()==null); 134 | } 135 | 136 | @Test 137 | public void test_AssertTrue_invalid() { 138 | v1.setAssertTrue(true); 139 | v1.validateString("0"); 140 | assertTrue(v1.getViolationMessage()!=null); 141 | assertEquals("AssertTrue", v1.getViolationConstraint()); 142 | 143 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 144 | v2.validateString("false"); 145 | assertTrue(v2.getViolationMessage()!=null); 146 | assertEquals("AssertTrue", v2.getViolationConstraint()); 147 | 148 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 149 | v3.validateString("false"); 150 | assertTrue(v3.getViolationMessage()!=null); 151 | assertEquals("AssertTrue", v3.getViolationConstraint()); 152 | } 153 | 154 | @Test 155 | public void test_AssertTrue_valid() { 156 | v1.setAssertTrue(false); 157 | v1.validateString("0"); 158 | assertTrue(v1.getViolationMessage()==null); 159 | assertTrue(v1.getViolationConstraint()==null); 160 | v1.setAssertTrue(true); 161 | v1.validateString("1"); 162 | assertTrue(v1.getViolationMessage()==null); 163 | assertTrue(v1.getViolationConstraint()==null); 164 | 165 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 166 | v2.validateString("true"); 167 | assertTrue(v2.getViolationMessage()==null); 168 | assertTrue(v2.getViolationConstraint()==null); 169 | 170 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 171 | v3.validate(true); 172 | assertTrue(v3.getViolationMessage()==null); 173 | assertTrue(v3.getViolationConstraint()==null); 174 | } 175 | } 176 | -------------------------------------------------------------------------------- /src/test/java/tools4j/validator/features/BusinessRulesFeature.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator.features; 28 | 29 | import static org.junit.Assert.*; 30 | 31 | import java.util.Calendar; 32 | 33 | import org.junit.Before; 34 | import org.junit.Test; 35 | 36 | import tools4j.validator.BusinessRule; 37 | import tools4j.validator.BusinessRulesManager; 38 | import tools4j.validator.DateValidator; 39 | import tools4j.validator.DefaultBusinessRuleListener; 40 | 41 | public class BusinessRulesFeature { 42 | private DateValidator v1; 43 | private DateValidator v2; 44 | private DateValidator v3; 45 | 46 | @Before 47 | public void before() { 48 | final BusinessRulesManager manager = new BusinessRulesManager(); 49 | manager.addRule("AdultCheck", new BusinessRule() { 50 | @Override 51 | public boolean checkBusinessRule(Calendar value) { 52 | Calendar now = Calendar.getInstance(); 53 | now.set(Calendar.YEAR, now.get(Calendar.YEAR)-18); 54 | 55 | return value.compareTo(now)<=0; 56 | } 57 | }); 58 | 59 | 60 | v1 = new DateValidator(); 61 | v1.setBusinessRuleID(manager.getRuleID("AdultCheck")); 62 | v1.setBusinessRuleListener(new DefaultBusinessRuleListener(manager)); 63 | v1.setPattern("yyyy-mm-dd"); 64 | 65 | v2 = new DateValidator(); 66 | v2.setBusinessRuleListener(new DefaultBusinessRuleListener(manager)); 67 | v3 = new DateValidator(); 68 | v3.setBusinessRuleListener(new DefaultBusinessRuleListener(manager)); 69 | } 70 | 71 | @Test 72 | public void test_Rule_invalid() { 73 | v1.validateString("2192-12-24"); 74 | assertTrue(v1.getViolationMessage()!=null); 75 | assertEquals("Rule", v1.getViolationConstraint()); 76 | 77 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 78 | v2.validateString("2192-12-24"); 79 | assertTrue(v2.getViolationMessage()!=null); 80 | assertEquals("Rule", v2.getViolationConstraint()); 81 | 82 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 83 | v3.validateString("2192-12-24"); 84 | assertTrue(v3.getViolationMessage()!=null); 85 | assertEquals("Rule", v3.getViolationConstraint()); 86 | } 87 | 88 | @Test 89 | public void test_Rule_valid() { 90 | v1.validateString("1971-12-21"); 91 | assertTrue(v1.getViolationMessage()==null); 92 | assertTrue(v1.getViolationConstraint()==null); 93 | 94 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 95 | v2.validateString("1971-12-21"); 96 | assertTrue(v2.getViolationMessage()==null); 97 | assertTrue(v2.getViolationConstraint()==null); 98 | 99 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 100 | v3.validateString("1971-12-21"); 101 | assertTrue(v3.getViolationMessage()==null); 102 | assertTrue(v3.getViolationConstraint()==null); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /src/test/java/tools4j/validator/features/ByteFeature.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator.features; 28 | 29 | import static org.junit.Assert.*; 30 | 31 | import org.junit.Before; 32 | import org.junit.Test; 33 | 34 | import tools4j.validator.ByteValidator; 35 | 36 | public class ByteFeature { 37 | private ByteValidator v1; 38 | private ByteValidator v2; 39 | private ByteValidator v3; 40 | 41 | @Before 42 | public void before() { 43 | v1 = new ByteValidator(); 44 | v2 = new ByteValidator(); 45 | v3 = new ByteValidator(); 46 | } 47 | 48 | @Test 49 | public void test_Transform_invalid() { 50 | v1.validateString("a"); 51 | assertTrue(v1.getViolationMessage()!=null); 52 | assertEquals("Transform", v1.getViolationConstraint()); 53 | 54 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 55 | v2.validateString("2.34"); 56 | assertTrue(v2.getViolationMessage()!=null); 57 | assertEquals("Transform", v2.getViolationConstraint()); 58 | 59 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 60 | v3.validateString("128"); 61 | assertTrue(v3.getViolationMessage()!=null); 62 | assertEquals("Transform", v3.getViolationConstraint()); 63 | 64 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 65 | v3.validateString("-129"); 66 | assertTrue(v3.getViolationMessage()!=null); 67 | assertEquals("Transform", v3.getViolationConstraint()); 68 | } 69 | 70 | @Test 71 | public void test_Transform_valid() { 72 | v1.validateString("4"); 73 | assertTrue(v1.getViolationMessage()==null); 74 | assertTrue(v1.getViolationConstraint()==null); 75 | 76 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 77 | v2.validateString("127"); 78 | assertTrue(v2.getViolationMessage()==null); 79 | assertTrue(v2.getViolationConstraint()==null); 80 | 81 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 82 | v3.validateString("-128"); 83 | assertTrue(v3.getViolationMessage()==null); 84 | assertTrue(v3.getViolationConstraint()==null); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/test/java/tools4j/validator/features/DateFeature.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator.features; 28 | 29 | import static org.junit.Assert.*; 30 | 31 | import java.util.GregorianCalendar; 32 | 33 | import javax.json.JsonArray; 34 | 35 | import org.junit.Before; 36 | import org.junit.Test; 37 | 38 | import tools4j.validator.DateValidator; 39 | 40 | public class DateFeature { 41 | private DateValidator v1; 42 | private DateValidator v2; 43 | private DateValidator v3; 44 | 45 | @Before 46 | public void before() { 47 | v1 = new DateValidator(); 48 | v2 = new DateValidator(); 49 | v3 = new DateValidator(); 50 | } 51 | 52 | @Test 53 | public void test_resetConstraints() { 54 | v1.setPattern(""); 55 | v1.setValid(true); 56 | v1.setPast(true); 57 | v1.setFuture(true); 58 | 59 | v1.setConstraints("{}"); 60 | assertTrue(((JsonArray)v1.getConstraintsAsJsonObject().get("constraints")).size()==0); 61 | } 62 | 63 | @Test 64 | public void test_Pattern_invalid() { 65 | v1.setPattern("yyyy-mm-dd"); 66 | v1.validateString("2014.12-24"); 67 | assertTrue(v1.getViolationMessage()!=null); 68 | assertEquals("Pattern", v1.getViolationConstraint()); 69 | 70 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 71 | v2.validateString("2014.12-24"); 72 | assertTrue(v2.getViolationMessage()!=null); 73 | assertEquals("Pattern", v2.getViolationConstraint()); 74 | 75 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 76 | v3.validateString("2014.12-24"); 77 | assertTrue(v3.getViolationMessage()!=null); 78 | assertEquals("Pattern", v3.getViolationConstraint()); 79 | } 80 | 81 | @Test 82 | public void test_Pattern_valid() { 83 | v1.setPattern("yyyy-mm-dd"); 84 | v1.validateString("2014-12-24"); 85 | assertTrue(v1.getViolationMessage()==null); 86 | assertTrue(v1.getViolationConstraint()==null); 87 | 88 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 89 | v2.validateString("2014-12-24"); 90 | assertTrue(v2.getViolationMessage()==null); 91 | assertTrue(v2.getViolationConstraint()==null); 92 | 93 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 94 | v3.validateString("2014-12-24"); 95 | assertTrue(v3.getViolationMessage()==null); 96 | assertTrue(v3.getViolationConstraint()==null); 97 | } 98 | 99 | @Test 100 | public void test_Valid_invalid() { 101 | v1.setValid(true); 102 | v1.validate(new GregorianCalendar(2014, 12, 32)); 103 | assertTrue(v1.getViolationMessage()!=null); 104 | assertEquals("Valid", v1.getViolationConstraint()); 105 | v1.setPattern("yyyy-mm-dd"); 106 | v1.validateString("2014-12-32"); 107 | assertTrue(v1.getViolationMessage()!=null); 108 | assertEquals("Pattern|Valid", v1.getViolationConstraint()); 109 | 110 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 111 | v2.validate(new GregorianCalendar(2014, 12, 32)); 112 | assertTrue(v2.getViolationMessage()!=null); 113 | assertEquals("Valid", v2.getViolationConstraint()); 114 | 115 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 116 | v3.validate(new GregorianCalendar(2014, 12, 32)); 117 | assertTrue(v3.getViolationMessage()!=null); 118 | assertEquals("Valid", v3.getViolationConstraint()); 119 | } 120 | 121 | @Test 122 | public void test_Valid_valid() { 123 | v1.setValid(false); 124 | v1.setPattern("yyyy-mm-dd"); 125 | v1.validateString("2014-12-32"); 126 | assertTrue(v1.getViolationMessage()==null); 127 | assertTrue(v1.getViolationConstraint()==null); 128 | v1.setValid(true); 129 | v1.validateString("2014-12-24"); 130 | assertTrue(v1.getViolationMessage()==null); 131 | assertTrue(v1.getViolationConstraint()==null); 132 | 133 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 134 | v2.validateString("2014-12-24"); 135 | assertTrue(v2.getViolationMessage()==null); 136 | assertTrue(v2.getViolationConstraint()==null); 137 | 138 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 139 | v3.validateString("2014-12-24"); 140 | assertTrue(v3.getViolationMessage()==null); 141 | assertTrue(v3.getViolationConstraint()==null); 142 | } 143 | 144 | @Test 145 | public void test_Past_invalid() { 146 | v1.setPattern("yyyy-mm-dd"); 147 | v1.setPast(true); 148 | v1.validateString("2194-12-24"); 149 | assertTrue(v1.getViolationMessage()!=null); 150 | assertEquals("Past", v1.getViolationConstraint()); 151 | 152 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 153 | v2.validateString("2194-12-24"); 154 | assertTrue(v2.getViolationMessage()!=null); 155 | assertEquals("Past", v2.getViolationConstraint()); 156 | 157 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 158 | v3.validateString("2194-12-24"); 159 | assertTrue(v3.getViolationMessage()!=null); 160 | assertEquals("Past", v3.getViolationConstraint()); 161 | } 162 | 163 | @Test 164 | public void test_Past_valid() { 165 | v1.setPattern("yyyy-mm-dd"); 166 | v1.setPast(false); 167 | v1.validateString("2194-02-24"); 168 | assertTrue(v1.getViolationMessage()==null); 169 | assertTrue(v1.getViolationConstraint()==null); 170 | v1.setPast(true); 171 | v1.validateString("2014-02-24"); 172 | assertTrue(v1.getViolationMessage()==null); 173 | assertTrue(v1.getViolationConstraint()==null); 174 | 175 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 176 | v2.validateString("2014-02-24"); 177 | assertTrue(v2.getViolationMessage()==null); 178 | assertTrue(v2.getViolationConstraint()==null); 179 | 180 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 181 | v3.validateString("2014-02-24"); 182 | assertTrue(v3.getViolationMessage()==null); 183 | assertTrue(v3.getViolationConstraint()==null); 184 | } 185 | 186 | @Test 187 | public void test_Future_invalid() { 188 | v1.setPattern("yyyy-mm-dd"); 189 | v1.setFuture(true); 190 | v1.validateString("2014-02-24"); 191 | assertTrue(v1.getViolationMessage()!=null); 192 | assertEquals("Future", v1.getViolationConstraint()); 193 | 194 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 195 | v2.validateString("2014-02-24"); 196 | assertTrue(v2.getViolationMessage()!=null); 197 | assertEquals("Future", v2.getViolationConstraint()); 198 | 199 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 200 | v3.validateString("2014-02-24"); 201 | assertTrue(v3.getViolationMessage()!=null); 202 | assertEquals("Future", v3.getViolationConstraint()); 203 | } 204 | 205 | @Test 206 | public void test_Future_valid() { 207 | v1.setPattern("yyyy-mm-dd"); 208 | v1.setFuture(false); 209 | v1.validateString("2014-02-24"); 210 | assertTrue(v1.getViolationMessage()==null); 211 | assertTrue(v1.getViolationConstraint()==null); 212 | v1.setFuture(true); 213 | v1.validateString("2194-02-24"); 214 | assertTrue(v1.getViolationMessage()==null); 215 | assertTrue(v1.getViolationConstraint()==null); 216 | 217 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 218 | v2.validateString("2194-02-24"); 219 | assertTrue(v2.getViolationMessage()==null); 220 | assertTrue(v2.getViolationConstraint()==null); 221 | 222 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 223 | v3.validateString("2194-02-24"); 224 | assertTrue(v3.getViolationMessage()==null); 225 | assertTrue(v3.getViolationConstraint()==null); 226 | } 227 | } 228 | -------------------------------------------------------------------------------- /src/test/java/tools4j/validator/features/DoubleFeature.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator.features; 28 | 29 | import static org.junit.Assert.*; 30 | 31 | import org.junit.Before; 32 | import org.junit.Test; 33 | 34 | import tools4j.validator.DoubleValidator; 35 | 36 | public class DoubleFeature { 37 | private DoubleValidator v1; 38 | private DoubleValidator v2; 39 | private DoubleValidator v3; 40 | 41 | @Before 42 | public void before() { 43 | v1 = new DoubleValidator(); 44 | v2 = new DoubleValidator(); 45 | v3 = new DoubleValidator(); 46 | } 47 | 48 | @Test 49 | public void test_Transform_invalid() { 50 | v1.validateString("a"); 51 | assertTrue(v1.getViolationMessage()!=null); 52 | assertEquals("Transform", v1.getViolationConstraint()); 53 | 54 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 55 | v2.validateString("b"); 56 | assertTrue(v2.getViolationMessage()!=null); 57 | assertEquals("Transform", v2.getViolationConstraint()); 58 | 59 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 60 | v3.validateString("1.7976931348623158E+10308"); 61 | assertEquals(Double.POSITIVE_INFINITY, v3.getValue(), 0.01); 62 | assertTrue(v3.getViolationMessage()==null); 63 | assertTrue(v3.getViolationConstraint()==null); 64 | 65 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 66 | v3.validateString("-1.7976931348623158E+10308"); 67 | assertEquals(Double.NEGATIVE_INFINITY, v3.getValue(), 0.01); 68 | assertTrue(v3.getViolationMessage()==null); 69 | assertTrue(v3.getViolationConstraint()==null); 70 | } 71 | 72 | @Test 73 | public void test_Transform_valid() { 74 | v1.validateString("4"); 75 | assertTrue(v1.getViolationMessage()==null); 76 | assertTrue(v1.getViolationConstraint()==null); 77 | 78 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 79 | v2.validateString("1.7976931348623157E+10308"); 80 | assertTrue(v2.getViolationMessage()==null); 81 | assertTrue(v2.getViolationConstraint()==null); 82 | 83 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 84 | v3.validateString("-1.7976931348623157E+10308"); 85 | assertTrue(v3.getViolationMessage()==null); 86 | assertTrue(v3.getViolationConstraint()==null); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/test/java/tools4j/validator/features/DynamicValidationFeature.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator.features; 28 | 29 | import static org.junit.Assert.*; 30 | 31 | import org.junit.Before; 32 | import org.junit.Test; 33 | 34 | import tools4j.validator.StringValidator; 35 | import tools4j.validator.Validator; 36 | import tools4j.validator.ValidatorFactory; 37 | 38 | public class DynamicValidationFeature { 39 | private Validator v; 40 | 41 | @Before 42 | public void before() { 43 | StringValidator sv = new StringValidator(); 44 | sv.setPattern("[0-9]+"); 45 | 46 | String constraints = sv.getConstraintsAsJsonObject().toString(); 47 | v = ValidatorFactory.createValidator(ValidatorFactory.parseType(constraints)); 48 | v.setConstraints(constraints); 49 | } 50 | 51 | @Test 52 | public void test_Validation_invalid() { 53 | v.validateString(""); 54 | assertTrue(v.getViolationMessage()!=null); 55 | assertEquals("Pattern", v.getViolationConstraint()); 56 | } 57 | 58 | @Test 59 | public void test_Validation_valid() { 60 | v.validateString("12"); 61 | assertTrue(v.getViolationMessage()==null); 62 | assertTrue(v.getViolationConstraint()==null); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/test/java/tools4j/validator/features/EmailFeature.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator.features; 28 | 29 | import static org.junit.Assert.*; 30 | 31 | import org.junit.Before; 32 | import org.junit.Test; 33 | 34 | import tools4j.validator.EmailValidator; 35 | 36 | public class EmailFeature { 37 | private EmailValidator v1; 38 | private EmailValidator v2; 39 | private EmailValidator v3; 40 | 41 | @Before 42 | public void before() { 43 | v1 = new EmailValidator(); 44 | v2 = new EmailValidator(); 45 | v3 = new EmailValidator(); 46 | } 47 | 48 | @Test 49 | public void test_Email_invalid() { 50 | v1.validate("test@web"); 51 | assertTrue(v1.getViolationMessage()!=null); 52 | assertEquals("Pattern", v1.getViolationConstraint()); 53 | 54 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 55 | v2.validate("testweb.de"); 56 | assertTrue(v2.getViolationMessage()!=null); 57 | assertEquals("Pattern", v2.getViolationConstraint()); 58 | 59 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 60 | v3.validate("test.web@de"); 61 | assertTrue(v3.getViolationMessage()!=null); 62 | assertEquals("Pattern", v3.getViolationConstraint()); 63 | } 64 | 65 | @Test 66 | public void test_Email_valid() { 67 | v1.validate("test@web.de"); 68 | assertTrue(v1.getViolationMessage()==null); 69 | assertTrue(v1.getViolationConstraint()==null); 70 | 71 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 72 | v2.validate("test-1@web.de"); 73 | assertTrue(v2.getViolationMessage()==null); 74 | assertTrue(v2.getViolationConstraint()==null); 75 | 76 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 77 | v3.validate("test.1@web.de"); 78 | assertTrue(v3.getViolationMessage()==null); 79 | assertTrue(v3.getViolationConstraint()==null); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/test/java/tools4j/validator/features/FloatFeature.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator.features; 28 | 29 | import static org.junit.Assert.*; 30 | 31 | import org.junit.Before; 32 | import org.junit.Test; 33 | 34 | import tools4j.validator.FloatValidator; 35 | 36 | public class FloatFeature { 37 | private FloatValidator v1; 38 | private FloatValidator v2; 39 | private FloatValidator v3; 40 | 41 | @Before 42 | public void before() { 43 | v1 = new FloatValidator(); 44 | v2 = new FloatValidator(); 45 | v3 = new FloatValidator(); 46 | } 47 | 48 | @Test 49 | public void test_Transform_invalid() { 50 | v1.validateString("a"); 51 | assertTrue(v1.getViolationMessage()!=null); 52 | assertEquals("Transform", v1.getViolationConstraint()); 53 | 54 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 55 | v2.validateString("b"); 56 | assertTrue(v2.getViolationMessage()!=null); 57 | assertEquals("Transform", v2.getViolationConstraint()); 58 | 59 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 60 | v3.validateString("3.4028236E+38"); 61 | assertEquals(Float.POSITIVE_INFINITY, v3.getValue(), 0.01); 62 | assertTrue(v3.getViolationMessage()==null); 63 | assertTrue(v3.getViolationConstraint()==null); 64 | 65 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 66 | v3.validateString("-3.4028236E+38"); 67 | assertEquals(Float.NEGATIVE_INFINITY, v3.getValue(), 0.01); 68 | assertTrue(v3.getViolationMessage()==null); 69 | assertTrue(v3.getViolationConstraint()==null); 70 | } 71 | 72 | @Test 73 | public void test_Transform_valid() { 74 | v1.validateString("4"); 75 | assertTrue(v1.getViolationMessage()==null); 76 | assertTrue(v1.getViolationConstraint()==null); 77 | 78 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 79 | v2.validateString("3.4028235E+38"); 80 | assertTrue(v2.getViolationMessage()==null); 81 | assertTrue(v2.getViolationConstraint()==null); 82 | 83 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 84 | v3.validateString("-3.4028235E+38"); 85 | assertTrue(v3.getViolationMessage()==null); 86 | assertTrue(v3.getViolationConstraint()==null); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/test/java/tools4j/validator/features/IntegerFeature.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator.features; 28 | 29 | import static org.junit.Assert.*; 30 | 31 | import org.junit.Before; 32 | import org.junit.Test; 33 | 34 | import tools4j.validator.IntegerValidator; 35 | 36 | public class IntegerFeature { 37 | private IntegerValidator v1; 38 | private IntegerValidator v2; 39 | private IntegerValidator v3; 40 | 41 | @Before 42 | public void before() { 43 | v1 = new IntegerValidator(); 44 | v2 = new IntegerValidator(); 45 | v3 = new IntegerValidator(); 46 | } 47 | 48 | @Test 49 | public void test_Transform_invalid() { 50 | v1.validateString("a"); 51 | assertTrue(v1.getViolationMessage()!=null); 52 | assertEquals("Transform", v1.getViolationConstraint()); 53 | 54 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 55 | v2.validateString("2.34"); 56 | assertTrue(v2.getViolationMessage()!=null); 57 | assertEquals("Transform", v2.getViolationConstraint()); 58 | 59 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 60 | v3.validateString("-2147483649"); 61 | assertTrue(v3.getViolationMessage()!=null); 62 | assertEquals("Transform", v3.getViolationConstraint()); 63 | 64 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 65 | v3.validateString("2147483648"); 66 | assertTrue(v3.getViolationMessage()!=null); 67 | assertEquals("Transform", v3.getViolationConstraint()); 68 | } 69 | 70 | @Test 71 | public void test_Transform_valid() { 72 | v1.validateString("4"); 73 | assertTrue(v1.getViolationMessage()==null); 74 | assertTrue(v1.getViolationConstraint()==null); 75 | 76 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 77 | v2.validateString("-2147483648"); 78 | assertTrue(v2.getViolationMessage()==null); 79 | assertTrue(v2.getViolationConstraint()==null); 80 | 81 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 82 | v3.validateString("2147483647"); 83 | assertTrue(v3.getViolationMessage()==null); 84 | assertTrue(v3.getViolationConstraint()==null); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/test/java/tools4j/validator/features/LongFeature.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator.features; 28 | 29 | import static org.junit.Assert.*; 30 | 31 | import org.junit.Before; 32 | import org.junit.Test; 33 | 34 | import tools4j.validator.LongValidator; 35 | 36 | public class LongFeature { 37 | private LongValidator v1; 38 | private LongValidator v2; 39 | private LongValidator v3; 40 | 41 | @Before 42 | public void before() { 43 | v1 = new LongValidator(); 44 | v2 = new LongValidator(); 45 | v3 = new LongValidator(); 46 | } 47 | 48 | @Test 49 | public void test_Transform_invalid() { 50 | v1.validateString("a"); 51 | assertTrue(v1.getViolationMessage()!=null); 52 | assertEquals("Transform", v1.getViolationConstraint()); 53 | 54 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 55 | v2.validateString("2.34"); 56 | assertTrue(v2.getViolationMessage()!=null); 57 | assertEquals("Transform", v2.getViolationConstraint()); 58 | 59 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 60 | v3.validateString("-9223372036854775809"); 61 | assertTrue(v3.getViolationMessage()!=null); 62 | assertEquals("Transform", v3.getViolationConstraint()); 63 | 64 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 65 | v3.validateString("9223372036854775808"); 66 | assertTrue(v3.getViolationMessage()!=null); 67 | assertEquals("Transform", v3.getViolationConstraint()); 68 | } 69 | 70 | @Test 71 | public void test_Transform_valid() { 72 | v1.validateString("4"); 73 | assertTrue(v1.getViolationMessage()==null); 74 | assertTrue(v1.getViolationConstraint()==null); 75 | 76 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 77 | v2.validateString("-9223372036854775808"); 78 | assertTrue(v2.getViolationMessage()==null); 79 | assertTrue(v2.getViolationConstraint()==null); 80 | 81 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 82 | v3.validateString("9223372036854775807"); 83 | assertTrue(v3.getViolationMessage()==null); 84 | assertTrue(v3.getViolationConstraint()==null); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/test/java/tools4j/validator/features/NumberFeature.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator.features; 28 | 29 | import static org.junit.Assert.*; 30 | 31 | import javax.json.JsonArray; 32 | 33 | import org.junit.Before; 34 | import org.junit.Test; 35 | 36 | import tools4j.validator.LongValidator; 37 | 38 | public class NumberFeature { 39 | private LongValidator v1; 40 | private LongValidator v2; 41 | private LongValidator v3; 42 | 43 | @Before 44 | public void before() { 45 | v1 = new LongValidator(); 46 | v2 = new LongValidator(); 47 | v3 = new LongValidator(); 48 | } 49 | 50 | @Test 51 | public void test_resetConstraints() { 52 | v1.setMin(1L); 53 | v1.setMax(1L); 54 | 55 | v1.setConstraints("{}"); 56 | assertTrue(((JsonArray)v1.getConstraintsAsJsonObject().get("constraints")).size()==0); 57 | } 58 | 59 | @Test 60 | public void test_Min_invalid() { 61 | v1.setMin((long)4, false); 62 | v1.validate((long)4); 63 | assertTrue(v1.getViolationMessage()!=null); 64 | assertEquals("Min", v1.getViolationConstraint()); 65 | v1.setMin((long)4); 66 | v1.validate((long)3); 67 | assertTrue(v1.getViolationMessage()!=null); 68 | assertEquals("Min", v1.getViolationConstraint()); 69 | 70 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 71 | v2.validate((long)2); 72 | assertTrue(v2.getViolationMessage()!=null); 73 | assertEquals("Min", v2.getViolationConstraint()); 74 | 75 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 76 | v3.validate((long)1); 77 | assertTrue(v3.getViolationMessage()!=null); 78 | assertEquals("Min", v3.getViolationConstraint()); 79 | } 80 | 81 | @Test 82 | public void test_Min_valid() { 83 | v1.setMin((long)4, false); 84 | v1.validateString("5"); 85 | assertTrue(v1.getViolationMessage()==null); 86 | assertTrue(v1.getViolationConstraint()==null); 87 | v1.setMin((long)4); 88 | v1.validateString("4"); 89 | assertTrue(v1.getViolationMessage()==null); 90 | assertTrue(v1.getViolationConstraint()==null); 91 | 92 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 93 | v2.validateString("5"); 94 | assertTrue(v2.getViolationMessage()==null); 95 | assertTrue(v2.getViolationConstraint()==null); 96 | 97 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 98 | v3.validateString("4"); 99 | assertTrue(v3.getViolationMessage()==null); 100 | assertTrue(v3.getViolationConstraint()==null); 101 | } 102 | 103 | @Test 104 | public void test_Max_invalid() { 105 | v1.setMax((long)4, false); 106 | v1.validate((long)4); 107 | assertTrue(v1.getViolationMessage()!=null); 108 | assertEquals("Max", v1.getViolationConstraint()); 109 | v1.setMax((long)4); 110 | v1.validate((long)5); 111 | assertTrue(v1.getViolationMessage()!=null); 112 | assertEquals("Max", v1.getViolationConstraint()); 113 | 114 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 115 | v2.validate((long)6); 116 | assertTrue(v2.getViolationMessage()!=null); 117 | assertEquals("Max", v2.getViolationConstraint()); 118 | 119 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 120 | v3.validate((long)7); 121 | assertTrue(v3.getViolationMessage()!=null); 122 | assertEquals("Max", v3.getViolationConstraint()); 123 | } 124 | 125 | @Test 126 | public void test_Max_valid() { 127 | v1.setMax((long)4, false); 128 | v1.validateString("3"); 129 | assertTrue(v1.getViolationMessage()==null); 130 | assertTrue(v1.getViolationConstraint()==null); 131 | v1.setMax((long)4); 132 | v1.validateString("4"); 133 | assertTrue(v1.getViolationMessage()==null); 134 | assertTrue(v1.getViolationConstraint()==null); 135 | 136 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 137 | v2.validateString("3"); 138 | assertTrue(v2.getViolationMessage()==null); 139 | assertTrue(v2.getViolationConstraint()==null); 140 | 141 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 142 | v3.validateString("2"); 143 | assertTrue(v3.getViolationMessage()==null); 144 | assertTrue(v3.getViolationConstraint()==null); 145 | } 146 | 147 | @Test 148 | public void test_Range_invalid() { 149 | v1.setRange((long)1, (long)4, false, false); 150 | v1.validate((long)4); 151 | assertTrue(v1.getViolationMessage()!=null); 152 | assertEquals("Range", v1.getViolationConstraint()); 153 | v1.validate((long)1); 154 | assertTrue(v1.getViolationMessage()!=null); 155 | assertEquals("Range", v1.getViolationConstraint()); 156 | v1.setRange((long)1, (long)4); 157 | v1.validate((long)5); 158 | assertTrue(v1.getViolationMessage()!=null); 159 | assertEquals("Range", v1.getViolationConstraint()); 160 | v1.validate((long)0); 161 | assertTrue(v1.getViolationMessage()!=null); 162 | assertEquals("Range", v1.getViolationConstraint()); 163 | 164 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 165 | v2.validate((long)6); 166 | assertTrue(v2.getViolationMessage()!=null); 167 | assertEquals("Range", v2.getViolationConstraint()); 168 | 169 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 170 | v3.validate((long)7); 171 | assertTrue(v3.getViolationMessage()!=null); 172 | assertEquals("Range", v3.getViolationConstraint()); 173 | } 174 | 175 | @Test 176 | public void test_Range_valid() { 177 | v1.setRange((long)1, (long)4, false, false); 178 | v1.validateString("3"); 179 | assertTrue(v1.getViolationMessage()==null); 180 | assertTrue(v1.getViolationConstraint()==null); 181 | v1.validateString("2"); 182 | assertTrue(v1.getViolationMessage()==null); 183 | assertTrue(v1.getViolationConstraint()==null); 184 | v1.setRange((long)1, (long)4); 185 | v1.validateString("4"); 186 | assertTrue(v1.getViolationMessage()==null); 187 | assertTrue(v1.getViolationConstraint()==null); 188 | v1.validateString("1"); 189 | assertTrue(v1.getViolationMessage()==null); 190 | assertTrue(v1.getViolationConstraint()==null); 191 | 192 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 193 | v2.validateString("3"); 194 | assertTrue(v2.getViolationMessage()==null); 195 | assertTrue(v2.getViolationConstraint()==null); 196 | 197 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 198 | v3.validateString("2"); 199 | assertTrue(v3.getViolationMessage()==null); 200 | assertTrue(v3.getViolationConstraint()==null); 201 | } 202 | } 203 | -------------------------------------------------------------------------------- /src/test/java/tools4j/validator/features/PickListFeature.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator.features; 28 | 29 | import static org.junit.Assert.*; 30 | 31 | import java.util.HashSet; 32 | import java.util.Set; 33 | 34 | import javax.json.JsonArray; 35 | 36 | import org.junit.Before; 37 | import org.junit.Test; 38 | 39 | import tools4j.validator.PickListValidator; 40 | 41 | public class PickListFeature { 42 | private PickListValidator v1; 43 | private PickListValidator v2; 44 | private PickListValidator v3; 45 | 46 | private Set list; 47 | 48 | @Before 49 | public void before() { 50 | v1 = new PickListValidator(); 51 | v2 = new PickListValidator(); 52 | v3 = new PickListValidator(); 53 | 54 | list = new HashSet<>(); 55 | list.add("1"); 56 | list.add("2"); 57 | list.add("3"); 58 | } 59 | 60 | @Test 61 | public void test_resetConstraints() { 62 | v1.setPickList(new HashSet()); 63 | 64 | v1.setConstraints("{}"); 65 | assertTrue(((JsonArray)v1.getConstraintsAsJsonObject().get("constraints")).size()==0); 66 | } 67 | 68 | @Test 69 | public void test_List_invalid() { 70 | v1.setPickList(list); 71 | v1.validate("0"); 72 | assertTrue(v1.getViolationMessage()!=null); 73 | assertEquals("List", v1.getViolationConstraint()); 74 | 75 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 76 | v2.validate("ABC"); 77 | assertTrue(v2.getViolationMessage()!=null); 78 | assertEquals("List", v2.getViolationConstraint()); 79 | 80 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 81 | v3.validate("4"); 82 | assertTrue(v3.getViolationMessage()!=null); 83 | assertEquals("List", v3.getViolationConstraint()); 84 | } 85 | 86 | @Test 87 | public void test_List_valid() { 88 | v1.setPickList(list); 89 | v1.validate("1"); 90 | assertTrue(v1.getViolationMessage()==null); 91 | assertTrue(v1.getViolationConstraint()==null); 92 | 93 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 94 | v2.validate("2"); 95 | assertTrue(v2.getViolationMessage()==null); 96 | assertTrue(v2.getViolationConstraint()==null); 97 | 98 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 99 | v3.validate("3"); 100 | assertTrue(v3.getViolationMessage()==null); 101 | assertTrue(v3.getViolationConstraint()==null); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /src/test/java/tools4j/validator/features/ShortFeature.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator.features; 28 | 29 | import static org.junit.Assert.*; 30 | 31 | import org.junit.Before; 32 | import org.junit.Test; 33 | 34 | import tools4j.validator.ShortValidator; 35 | 36 | public class ShortFeature { 37 | private ShortValidator v1; 38 | private ShortValidator v2; 39 | private ShortValidator v3; 40 | 41 | @Before 42 | public void before() { 43 | v1 = new ShortValidator(); 44 | v2 = new ShortValidator(); 45 | v3 = new ShortValidator(); 46 | } 47 | 48 | @Test 49 | public void test_Transform_invalid() { 50 | v1.validateString("a"); 51 | assertTrue(v1.getViolationMessage()!=null); 52 | assertEquals("Transform", v1.getViolationConstraint()); 53 | 54 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 55 | v2.validateString("2.34"); 56 | assertTrue(v2.getViolationMessage()!=null); 57 | assertEquals("Transform", v2.getViolationConstraint()); 58 | 59 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 60 | v3.validateString("32768"); 61 | assertTrue(v3.getViolationMessage()!=null); 62 | assertEquals("Transform", v3.getViolationConstraint()); 63 | 64 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 65 | v3.validateString("-32769"); 66 | assertTrue(v3.getViolationMessage()!=null); 67 | assertEquals("Transform", v3.getViolationConstraint()); 68 | } 69 | 70 | @Test 71 | public void test_Transform_valid() { 72 | v1.validateString("4"); 73 | assertTrue(v1.getViolationMessage()==null); 74 | assertTrue(v1.getViolationConstraint()==null); 75 | 76 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 77 | v2.validateString("32767"); 78 | assertTrue(v2.getViolationMessage()==null); 79 | assertTrue(v2.getViolationConstraint()==null); 80 | 81 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 82 | v3.validateString("-32768"); 83 | assertTrue(v3.getViolationMessage()==null); 84 | assertTrue(v3.getViolationConstraint()==null); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/test/java/tools4j/validator/features/StringFeature.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator.features; 28 | 29 | import static org.junit.Assert.*; 30 | 31 | import javax.json.JsonArray; 32 | 33 | import org.junit.Before; 34 | import org.junit.Test; 35 | 36 | import tools4j.validator.StringValidator; 37 | 38 | public class StringFeature { 39 | private StringValidator v1; 40 | private StringValidator v2; 41 | private StringValidator v3; 42 | 43 | @Before 44 | public void before() { 45 | v1 = new StringValidator(); 46 | v2 = new StringValidator(); 47 | v3 = new StringValidator(); 48 | } 49 | 50 | @Test 51 | public void test_resetMessageAndConstraint() { 52 | v1.setPattern("[0-9]*"); 53 | v1.validate("ABC"); 54 | assertTrue(v1.getViolationMessage()!=null); 55 | assertEquals("Pattern", v1.getViolationConstraint()); 56 | v1.validate("123"); 57 | assertNull(v1.getViolationMessage()); 58 | assertNull(v1.getViolationConstraint()); 59 | } 60 | 61 | @Test 62 | public void test_resetConstraints() { 63 | v1.setNotEmpty(true); 64 | v1.setEmpty(true); 65 | v1.setPattern(""); 66 | v1.setMin(1); 67 | v1.setMax(1); 68 | 69 | v1.setConstraints("{}"); 70 | assertTrue(((JsonArray)v1.getConstraintsAsJsonObject().get("constraints")).size()==0); 71 | } 72 | 73 | @Test 74 | public void test_Pattern_invalid() { 75 | v1.setPattern("[0-9]*"); 76 | v1.validate("ABC"); 77 | assertTrue(v1.getViolationMessage()!=null); 78 | assertEquals("Pattern", v1.getViolationConstraint()); 79 | 80 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 81 | v2.validate("ABC"); 82 | assertTrue(v2.getViolationMessage()!=null); 83 | assertEquals("Pattern", v2.getViolationConstraint()); 84 | 85 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 86 | v3.validate("ABC"); 87 | assertTrue(v3.getViolationMessage()!=null); 88 | assertEquals("Pattern", v3.getViolationConstraint()); 89 | } 90 | 91 | @Test 92 | public void test_Pattern_valid() { 93 | v1.setPattern("[A-Z]*"); 94 | v1.validate("ABC"); 95 | assertTrue(v1.getViolationMessage()==null); 96 | assertTrue(v1.getViolationConstraint()==null); 97 | 98 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 99 | v2.validate("ABC"); 100 | assertTrue(v2.getViolationMessage()==null); 101 | assertTrue(v2.getViolationConstraint()==null); 102 | 103 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 104 | v3.validate("ABC"); 105 | assertTrue(v3.getViolationMessage()==null); 106 | assertTrue(v3.getViolationConstraint()==null); 107 | } 108 | 109 | @Test 110 | public void test_NotEmpty_invalid() { 111 | v1.setNotEmpty(true); 112 | v1.validate(""); 113 | assertTrue(v1.getViolationMessage()!=null); 114 | assertEquals("NotEmpty", v1.getViolationConstraint()); 115 | 116 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 117 | v2.validate(""); 118 | assertTrue(v2.getViolationMessage()!=null); 119 | assertEquals("NotEmpty", v2.getViolationConstraint()); 120 | 121 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 122 | v3.validate(""); 123 | assertTrue(v3.getViolationMessage()!=null); 124 | assertEquals("NotEmpty", v3.getViolationConstraint()); 125 | } 126 | 127 | @Test 128 | public void test_NotEmpty_valid() { 129 | v1.setNotEmpty(false); 130 | v1.validate(""); 131 | assertTrue(v1.getViolationMessage()==null); 132 | assertTrue(v1.getViolationConstraint()==null); 133 | 134 | v1.setNotEmpty(true); 135 | v1.validate("ABC"); 136 | assertTrue(v1.getViolationMessage()==null); 137 | assertTrue(v1.getViolationConstraint()==null); 138 | 139 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 140 | v2.validate("ABC"); 141 | assertTrue(v2.getViolationMessage()==null); 142 | assertTrue(v2.getViolationConstraint()==null); 143 | 144 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 145 | v3.validate("ABC"); 146 | assertTrue(v3.getViolationMessage()==null); 147 | assertTrue(v3.getViolationConstraint()==null); 148 | } 149 | 150 | @Test 151 | public void test_Empty_invalid() { 152 | v1.setEmpty(true); 153 | v1.validate("ABC"); 154 | assertTrue(v1.getViolationMessage()!=null); 155 | assertEquals("Empty", v1.getViolationConstraint()); 156 | 157 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 158 | v2.validate("ABC"); 159 | assertTrue(v2.getViolationMessage()!=null); 160 | assertEquals("Empty", v2.getViolationConstraint()); 161 | 162 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 163 | v3.validate("ABC"); 164 | assertTrue(v3.getViolationMessage()!=null); 165 | assertEquals("Empty", v3.getViolationConstraint()); 166 | } 167 | 168 | @Test 169 | public void test_Empty_valid() { 170 | v1.setEmpty(false); 171 | v1.validate("ABC"); 172 | assertTrue(v1.getViolationMessage()==null); 173 | assertTrue(v1.getViolationConstraint()==null); 174 | 175 | v1.setEmpty(true); 176 | v1.validate(""); 177 | assertTrue(v1.getViolationMessage()==null); 178 | assertTrue(v1.getViolationConstraint()==null); 179 | 180 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 181 | v2.validate(""); 182 | assertTrue(v2.getViolationMessage()==null); 183 | assertTrue(v2.getViolationConstraint()==null); 184 | 185 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 186 | v3.validate(""); 187 | assertTrue(v3.getViolationMessage()==null); 188 | assertTrue(v3.getViolationConstraint()==null); 189 | } 190 | 191 | @Test 192 | public void test_Min_invalid() { 193 | v1.setMin(4); 194 | v1.validate("ABC"); 195 | assertTrue(v1.getViolationMessage()!=null); 196 | assertEquals("Min", v1.getViolationConstraint()); 197 | 198 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 199 | v2.validate("ABC"); 200 | assertTrue(v2.getViolationMessage()!=null); 201 | assertEquals("Min", v2.getViolationConstraint()); 202 | 203 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 204 | v3.validate("ABC"); 205 | assertTrue(v3.getViolationMessage()!=null); 206 | assertEquals("Min", v3.getViolationConstraint()); 207 | } 208 | 209 | @Test 210 | public void test_Min_valid() { 211 | v1.setMin(3); 212 | v1.validate("ABC"); 213 | assertTrue(v1.getViolationMessage()==null); 214 | assertTrue(v1.getViolationConstraint()==null); 215 | 216 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 217 | v2.validate("ABC"); 218 | assertTrue(v2.getViolationMessage()==null); 219 | assertTrue(v2.getViolationConstraint()==null); 220 | 221 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 222 | v3.validate("ABC"); 223 | assertTrue(v3.getViolationMessage()==null); 224 | assertTrue(v3.getViolationConstraint()==null); 225 | } 226 | 227 | @Test 228 | public void test_Max_invalid() { 229 | v1.setMax(2); 230 | v1.validate("ABC"); 231 | assertTrue(v1.getViolationMessage()!=null); 232 | assertEquals("Max", v1.getViolationConstraint()); 233 | 234 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 235 | v2.validate("ABC"); 236 | assertTrue(v2.getViolationMessage()!=null); 237 | assertEquals("Max", v2.getViolationConstraint()); 238 | 239 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 240 | v3.validate("ABC"); 241 | assertTrue(v3.getViolationMessage()!=null); 242 | assertEquals("Max", v3.getViolationConstraint()); 243 | } 244 | 245 | @Test 246 | public void test_Max_valid() { 247 | v1.setMax(3); 248 | v1.validate("ABC"); 249 | assertTrue(v1.getViolationMessage()==null); 250 | assertTrue(v1.getViolationConstraint()==null); 251 | 252 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 253 | v2.validate("ABC"); 254 | assertTrue(v2.getViolationMessage()==null); 255 | assertTrue(v2.getViolationConstraint()==null); 256 | 257 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 258 | v3.validate("ABC"); 259 | assertTrue(v3.getViolationMessage()==null); 260 | assertTrue(v3.getViolationConstraint()==null); 261 | } 262 | 263 | @Test 264 | public void test_Size_invalid() { 265 | v1.setSize(1, 2); 266 | v1.validate("ABC"); 267 | assertTrue(v1.getViolationMessage()!=null); 268 | assertEquals("Size", v1.getViolationConstraint()); 269 | v1.setSize(4, 4); 270 | v1.validate("ABC"); 271 | assertTrue(v1.getViolationMessage()!=null); 272 | assertEquals("Size", v1.getViolationConstraint()); 273 | 274 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 275 | v2.validate("ABC"); 276 | assertTrue(v2.getViolationMessage()!=null); 277 | assertEquals("Size", v2.getViolationConstraint()); 278 | 279 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 280 | v3.validate("ABC"); 281 | assertTrue(v3.getViolationMessage()!=null); 282 | assertEquals("Size", v3.getViolationConstraint()); 283 | } 284 | 285 | @Test 286 | public void test_Size_valid() { 287 | v1.setSize(1, 3); 288 | v1.validate("ABC"); 289 | assertTrue(v1.getViolationMessage()==null); 290 | assertTrue(v1.getViolationConstraint()==null); 291 | v1.setSize(2, 5); 292 | v1.validate("ABC"); 293 | assertTrue(v1.getViolationMessage()==null); 294 | assertTrue(v1.getViolationConstraint()==null); 295 | 296 | v2.setConstraints(v1.getConstraintsAsJsonObject()); 297 | v2.validate("ABC"); 298 | assertTrue(v2.getViolationMessage()==null); 299 | assertTrue(v2.getViolationConstraint()==null); 300 | 301 | v3.setConstraints(v1.getConstraintsAsJsonObject().toString()); 302 | v3.validate("ABC"); 303 | assertTrue(v3.getViolationMessage()==null); 304 | assertTrue(v3.getViolationConstraint()==null); 305 | } 306 | } 307 | -------------------------------------------------------------------------------- /src/test/java/tools4j/validator/utils/features/DocumentFilterFeature.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator.utils.features; 28 | 29 | //import static org.junit.Assert.*; 30 | 31 | import static org.junit.Assert.assertTrue; 32 | 33 | import java.awt.event.KeyEvent; 34 | import java.util.Locale; 35 | 36 | import org.fest.swing.fixture.FrameFixture; 37 | import org.junit.After; 38 | import org.junit.Before; 39 | import org.junit.Test; 40 | 41 | public class DocumentFilterFeature { 42 | 43 | protected FrameFixture testbed; 44 | 45 | @Before 46 | public void before() { 47 | Locale.setDefault(Locale.ENGLISH); 48 | 49 | if (System.getProperty("os.name").startsWith("Windows")) 50 | testbed = new FrameFixture(new Testbed()); 51 | } 52 | 53 | @After 54 | public void tearDown() { 55 | if (testbed!=null) 56 | testbed.cleanUp(); 57 | } 58 | 59 | @Test 60 | public void documentFilter_valid() { 61 | if (testbed!=null) { 62 | testbed.textBox("tfDocumentFilter").enterText("65.23"); 63 | testbed.textBox("tfDocumentFilter").requireText("65.23"); 64 | } 65 | } 66 | 67 | @Test 68 | public void documentFilter_invalid() { 69 | if (testbed!=null) { 70 | testbed.textBox("tfDocumentFilter").enterText("65.23a"); 71 | testbed.optionPane().pressKey(KeyEvent.VK_ENTER); 72 | testbed.textBox("tfDocumentFilter").requireText("65.23"); // reset to 65.23 73 | } 74 | } 75 | 76 | @Test 77 | public void documentFilter_Output_valid() { 78 | if (testbed!=null) { 79 | testbed.textBox("tfDocumentFilter_Output").enterText("65.23"); 80 | testbed.textBox("tfDocumentFilter_Output").requireText("65.23"); 81 | } 82 | } 83 | 84 | @Test 85 | public void documentFilter_Output_invalid() { 86 | if (testbed!=null) { 87 | testbed.textBox("tfDocumentFilter_Output").enterText("65.23a"); 88 | testbed.textBox("tfDocumentFilter_Output").requireText("65.23"); // reset to 65.23 89 | assertTrue(testbed.label("lblViolationMessage").text().length()>0); 90 | } 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/test/java/tools4j/validator/utils/features/InputVerifierFeature.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator.utils.features; 28 | 29 | import static org.junit.Assert.*; 30 | 31 | import java.awt.event.KeyEvent; 32 | import java.util.Locale; 33 | 34 | import org.fest.swing.fixture.FrameFixture; 35 | import org.junit.After; 36 | import org.junit.Before; 37 | import org.junit.Test; 38 | 39 | public class InputVerifierFeature { 40 | 41 | protected FrameFixture testbed; 42 | 43 | @Before 44 | public void before() { 45 | Locale.setDefault(Locale.ENGLISH); 46 | 47 | if (System.getProperty("os.name").startsWith("Windows")) 48 | testbed = new FrameFixture(new Testbed()); 49 | } 50 | 51 | @After 52 | public void tearDown() { 53 | if (testbed!=null) 54 | testbed.cleanUp(); 55 | } 56 | 57 | @Test 58 | public void inputVerifier_valid() { 59 | if (testbed!=null) { 60 | testbed.textBox("tfInputVerifier").enterText("ABC"); 61 | testbed.textBox("tfDocumentFilter").focus(); 62 | testbed.textBox("tfDocumentFilter").requireFocused(); 63 | } 64 | } 65 | 66 | @Test 67 | public void inputVerifier_invalid() { 68 | if (testbed!=null) { 69 | testbed.textBox("tfInputVerifier").enterText("ABC4"); 70 | testbed.button("btnInputVerifier").click(); 71 | testbed.optionPane().pressKey(KeyEvent.VK_ENTER); 72 | testbed.textBox("tfInputVerifier").requireFocused(); // doesn't lose focus 73 | } 74 | } 75 | 76 | @Test 77 | public void inputVerifier_Output_valid() { 78 | if (testbed!=null) { 79 | testbed.textBox("tfInputVerifier_Output").enterText("ABC"); 80 | testbed.textBox("tfDocumentFilter_Output").focus(); 81 | testbed.textBox("tfDocumentFilter_Output").requireFocused(); 82 | } 83 | } 84 | 85 | @Test 86 | public void inputVerifier_Output_invalid() { 87 | if (testbed!=null) { 88 | testbed.textBox("tfInputVerifier_Output").enterText("ABC4"); 89 | testbed.button("btnInputVerifier_Output").click(); 90 | testbed.textBox("tfInputVerifier_Output").requireFocused(); // doesn't lose focus 91 | assertTrue(testbed.label("lblViolationMessage").text().length()>0); 92 | } 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/test/java/tools4j/validator/utils/features/Testbed.java: -------------------------------------------------------------------------------- 1 | /* 2 | * tools4j-validator - Framework for Validation 3 | * Copyright (c) 2014, David A. Bauer 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * 1. Redistributions of source code must retain the above copyright notice, this 10 | * list of conditions and the following disclaimer. 11 | * 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | package tools4j.validator.utils.features; 28 | 29 | import java.awt.EventQueue; 30 | 31 | import javax.swing.JFrame; 32 | import javax.swing.JPanel; 33 | import javax.swing.JTextField; 34 | import javax.swing.text.AbstractDocument; 35 | 36 | import tools4j.validator.DoubleValidator; 37 | import tools4j.validator.StringValidator; 38 | import tools4j.validator.utils.ValidationDocumentFilter; 39 | import tools4j.validator.utils.ValidationInputVerifier; 40 | import tools4j.validator.utils.ValidationLabel; 41 | 42 | import javax.swing.JButton; 43 | import javax.swing.JLabel; 44 | import javax.swing.border.EtchedBorder; 45 | import javax.swing.SwingConstants; 46 | 47 | import java.awt.BorderLayout; 48 | import java.awt.event.FocusEvent; 49 | import java.awt.event.FocusListener; 50 | 51 | public class Testbed extends JFrame{ 52 | private static final long serialVersionUID = 1L; 53 | 54 | private JPanel contentPane; 55 | private JTextField tfDocumentFilter; 56 | private JTextField tfInputVerifier; 57 | private JLabel lblViolationMessage; 58 | private JTextField tfDocumentFilter_Output; 59 | private JTextField tfInputVerifier_Output; 60 | 61 | /** 62 | * Launch the application. 63 | */ 64 | public static void main(String[] args) { 65 | EventQueue.invokeLater(new Runnable() { 66 | public void run() { 67 | try { 68 | new Testbed(); 69 | } catch (Exception e) { 70 | e.printStackTrace(); 71 | } 72 | } 73 | }); 74 | } 75 | 76 | /** 77 | * Create the application. 78 | */ 79 | public Testbed() { 80 | setBounds(100, 100, 479, 184); 81 | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 82 | contentPane = new JPanel(); 83 | getContentPane().setLayout(null); 84 | setContentPane(contentPane); 85 | contentPane.setLayout(null); 86 | 87 | tfDocumentFilter = new JTextField(); 88 | tfDocumentFilter.setName("tfDocumentFilter"); 89 | tfDocumentFilter.setBounds(10, 21, 86, 20); 90 | contentPane.add(tfDocumentFilter); 91 | tfDocumentFilter.setColumns(10); 92 | AbstractDocument doc = (AbstractDocument)tfDocumentFilter.getDocument(); 93 | DoubleValidator documentFilterValidator = new DoubleValidator(); 94 | doc.setDocumentFilter(new ValidationDocumentFilter(this, documentFilterValidator)); 95 | 96 | tfInputVerifier = new JTextField(); 97 | tfInputVerifier.setName("tfInputVerifier"); 98 | tfInputVerifier.setBounds(10, 64, 86, 20); 99 | contentPane.add(tfInputVerifier); 100 | tfInputVerifier.setColumns(10); 101 | StringValidator inputVerifierValidator = new StringValidator(); 102 | inputVerifierValidator.setPattern("[\\p{Alpha}]+"); 103 | tfInputVerifier.setInputVerifier(new ValidationInputVerifier(this, inputVerifierValidator)); 104 | 105 | JButton btnInputVerifier = new JButton("Okay"); 106 | btnInputVerifier.setName("btnInputVerifier"); 107 | btnInputVerifier.setBounds(117, 63, 89, 23); 108 | contentPane.add(btnInputVerifier); 109 | 110 | JPanel panel = new JPanel(); 111 | panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null)); 112 | panel.setBounds(10, 104, 431, 30); 113 | contentPane.add(panel); 114 | panel.setLayout(new BorderLayout(0, 0)); 115 | 116 | lblViolationMessage = new ValidationLabel(); 117 | lblViolationMessage.setName("lblViolationMessage"); 118 | lblViolationMessage.setHorizontalAlignment(SwingConstants.LEFT); 119 | panel.add(lblViolationMessage); 120 | 121 | tfDocumentFilter_Output = new JTextField(); 122 | tfDocumentFilter_Output.setName("tfDocumentFilter_Output"); 123 | tfDocumentFilter_Output.setBounds(256, 21, 86, 20); 124 | contentPane.add(tfDocumentFilter_Output); 125 | tfDocumentFilter_Output.setColumns(10); 126 | doc = (AbstractDocument)tfDocumentFilter_Output.getDocument(); 127 | doc.setDocumentFilter(new ValidationDocumentFilter(documentFilterValidator, tfDocumentFilter_Output, lblViolationMessage)); 128 | tfDocumentFilter_Output.addFocusListener(new FocusListener() { 129 | @Override 130 | public void focusGained(FocusEvent e) { 131 | } 132 | 133 | @Override 134 | public void focusLost(FocusEvent e) { 135 | lblViolationMessage.setText(""); 136 | } 137 | }); 138 | 139 | tfInputVerifier_Output = new JTextField(); 140 | tfInputVerifier_Output.setName("tfInputVerifier_Output"); 141 | tfInputVerifier_Output.setBounds(256, 64, 86, 20); 142 | contentPane.add(tfInputVerifier_Output); 143 | tfInputVerifier_Output.setColumns(10); 144 | tfInputVerifier_Output.setInputVerifier(new ValidationInputVerifier(inputVerifierValidator, lblViolationMessage)); 145 | 146 | JButton btnInputVerifier_Output = new JButton("Okay"); 147 | btnInputVerifier_Output.setName("btnInputVerifier_Output"); 148 | btnInputVerifier_Output.setBounds(352, 63, 89, 23); 149 | contentPane.add(btnInputVerifier_Output); 150 | 151 | setVisible(true); 152 | } 153 | } 154 | --------------------------------------------------------------------------------