From 91535f6f71e639abc70589da6c5bef5f1b983c7e Mon Sep 17 00:00:00 2001 From: Vladislav Medvedev Date: Sun, 3 Jan 2016 15:28:41 +0700 Subject: [PATCH 1/3] Add unit tests for jodd vtor module --- CODE_COVERAGE.md | 12 + jodd-vtor/build.gradle | 1 + .../java/jodd/vtor/ValidationContext.java | 6 + .../src/test/java/jodd/vtor/ManualTest.java | 1 + .../src/test/java/jodd/vtor/TestUtils.java | 36 +++ .../vtor/ValidationConstraintContextTest.java | 60 +++++ .../java/jodd/vtor/ValidationContextTest.java | 122 +++++++++ .../test/java/jodd/vtor/ViolationTest.java | 99 +++++++ .../java/jodd/vtor/VtorExceptionTest.java | 65 +++++ .../java/jodd/vtor/VtorMatchProfilesTest.java | 95 +++++++ ...rMatchProfilesWithResetedProfilesTest.java | 81 ++++++ .../src/test/java/jodd/vtor/VtorTest.java | 249 ++++++++++++++++++ .../test/java/jodd/vtor/VtorTestSupport.java | 43 +++ .../constraint/AssertFalseConstraintTest.java | 56 ++++ .../constraint/AssertTrueConstraintTest.java | 43 +++ .../constraint/AssertValidConstraintTest.java | 72 +++++ .../vtor/constraint/ConstraintTestBase.java | 37 +++ .../EqualToDeclaredFieldConstraintTest.java | 112 ++++++++ .../EqualToFieldConstraintTest.java | 122 +++++++++ .../HasSubstringConstraintTest.java | 111 ++++++++ .../vtor/constraint/LengthConstraintTest.java | 87 ++++++ .../vtor/constraint/MaxConstraintTest.java | 75 ++++++ .../constraint/MaxLengthConstraintTest.java | 79 ++++++ .../vtor/constraint/MinConstraintTest.java | 75 ++++++ .../constraint/MinLengthConstraintTest.java | 78 ++++++ .../NotBlankConstraintTest.java} | 125 ++++----- .../constraint/NotNullConstraintTest.java | 44 ++++ .../vtor/constraint/RangeConstraintTest.java | 85 ++++++ .../vtor/constraint/SizeConstraintTest.java | 113 ++++++++ .../constraint/TimeAfterConstraintTest.java | 84 ++++++ .../constraint/TimeBeforeConstraintTest.java | 85 ++++++ .../WildcardMatchConstraintTest.java | 77 ++++++ .../WildcardPathMatchConstraintTest.java | 78 ++++++ 33 files changed, 2434 insertions(+), 74 deletions(-) create mode 100644 CODE_COVERAGE.md create mode 100644 jodd-vtor/src/test/java/jodd/vtor/TestUtils.java create mode 100644 jodd-vtor/src/test/java/jodd/vtor/ValidationConstraintContextTest.java create mode 100644 jodd-vtor/src/test/java/jodd/vtor/ValidationContextTest.java create mode 100644 jodd-vtor/src/test/java/jodd/vtor/ViolationTest.java create mode 100644 jodd-vtor/src/test/java/jodd/vtor/VtorExceptionTest.java create mode 100644 jodd-vtor/src/test/java/jodd/vtor/VtorMatchProfilesTest.java create mode 100644 jodd-vtor/src/test/java/jodd/vtor/VtorMatchProfilesWithResetedProfilesTest.java create mode 100644 jodd-vtor/src/test/java/jodd/vtor/VtorTest.java create mode 100644 jodd-vtor/src/test/java/jodd/vtor/VtorTestSupport.java create mode 100644 jodd-vtor/src/test/java/jodd/vtor/constraint/AssertFalseConstraintTest.java create mode 100644 jodd-vtor/src/test/java/jodd/vtor/constraint/AssertTrueConstraintTest.java create mode 100644 jodd-vtor/src/test/java/jodd/vtor/constraint/AssertValidConstraintTest.java create mode 100644 jodd-vtor/src/test/java/jodd/vtor/constraint/ConstraintTestBase.java create mode 100644 jodd-vtor/src/test/java/jodd/vtor/constraint/EqualToDeclaredFieldConstraintTest.java create mode 100644 jodd-vtor/src/test/java/jodd/vtor/constraint/EqualToFieldConstraintTest.java create mode 100644 jodd-vtor/src/test/java/jodd/vtor/constraint/HasSubstringConstraintTest.java create mode 100644 jodd-vtor/src/test/java/jodd/vtor/constraint/LengthConstraintTest.java create mode 100644 jodd-vtor/src/test/java/jodd/vtor/constraint/MaxConstraintTest.java create mode 100644 jodd-vtor/src/test/java/jodd/vtor/constraint/MaxLengthConstraintTest.java create mode 100644 jodd-vtor/src/test/java/jodd/vtor/constraint/MinConstraintTest.java create mode 100644 jodd-vtor/src/test/java/jodd/vtor/constraint/MinLengthConstraintTest.java rename jodd-vtor/src/test/java/jodd/vtor/{ConstraintTest.java => constraint/NotBlankConstraintTest.java} (52%) create mode 100644 jodd-vtor/src/test/java/jodd/vtor/constraint/NotNullConstraintTest.java create mode 100644 jodd-vtor/src/test/java/jodd/vtor/constraint/RangeConstraintTest.java create mode 100644 jodd-vtor/src/test/java/jodd/vtor/constraint/SizeConstraintTest.java create mode 100644 jodd-vtor/src/test/java/jodd/vtor/constraint/TimeAfterConstraintTest.java create mode 100644 jodd-vtor/src/test/java/jodd/vtor/constraint/TimeBeforeConstraintTest.java create mode 100644 jodd-vtor/src/test/java/jodd/vtor/constraint/WildcardMatchConstraintTest.java create mode 100644 jodd-vtor/src/test/java/jodd/vtor/constraint/WildcardPathMatchConstraintTest.java diff --git a/CODE_COVERAGE.md b/CODE_COVERAGE.md new file mode 100644 index 000000000..dc49db411 --- /dev/null +++ b/CODE_COVERAGE.md @@ -0,0 +1,12 @@ +# Code Coverage Report generation + +To generate the code coverage report, execute the following command: +> Windows: gradlew jacocoReport +> Linux/Unix/ OSX: ./gradlew jacocoReport + +This will generate code coverage report for all the modules. In order to view the report for a module, open the following file in your browser. +> build/reports/coverage/MODULE_NAME/index.html + +For example: +* build/reports/coverage/jodd-decora/index.html +* build/reports/coverage/jodd-core/index.html \ No newline at end of file diff --git a/jodd-vtor/build.gradle b/jodd-vtor/build.gradle index 72eee3157..bd85df0f9 100644 --- a/jodd-vtor/build.gradle +++ b/jodd-vtor/build.gradle @@ -3,6 +3,7 @@ ext.moduleName = 'Jodd VTor' ext.moduleDescription = 'Jodd VTor is focused validation framework for any Java object.' dependencies { + compile 'org.mockito:mockito-core:1.9.5' compile project(':jodd-core') compile project(':jodd-bean') diff --git a/jodd-vtor/src/main/java/jodd/vtor/ValidationContext.java b/jodd-vtor/src/main/java/jodd/vtor/ValidationContext.java index c10111e68..5f1893a64 100644 --- a/jodd-vtor/src/main/java/jodd/vtor/ValidationContext.java +++ b/jodd-vtor/src/main/java/jodd/vtor/ValidationContext.java @@ -194,4 +194,10 @@ protected void copyDefaultCheckProperties(Check destCheck, Annotation annotation destCheck.setMessage(message); } + /** + * Clears the cache map + */ + protected void clearCache() { + cache.clear(); + } } diff --git a/jodd-vtor/src/test/java/jodd/vtor/ManualTest.java b/jodd-vtor/src/test/java/jodd/vtor/ManualTest.java index d685de22b..02c514f07 100644 --- a/jodd-vtor/src/test/java/jodd/vtor/ManualTest.java +++ b/jodd-vtor/src/test/java/jodd/vtor/ManualTest.java @@ -56,6 +56,7 @@ public void testManualConfig() { assertEquals("string", v.getName()); assertEquals("1", v.getInvalidValue()); assertEquals("string", v.getCheck().getName()); + assertNull(v.getCheck().getMessage()); assertEquals(MinLengthConstraint.class, v.getConstraint().getClass()); // new context that contains previous diff --git a/jodd-vtor/src/test/java/jodd/vtor/TestUtils.java b/jodd-vtor/src/test/java/jodd/vtor/TestUtils.java new file mode 100644 index 000000000..233c984d8 --- /dev/null +++ b/jodd-vtor/src/test/java/jodd/vtor/TestUtils.java @@ -0,0 +1,36 @@ +// Copyright (c) 2003-present, Jodd Team (http://jodd.org) +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +package jodd.vtor; + +public class TestUtils { + public static String stringWithLength(int length) { + StringBuilder stringBuilder = new StringBuilder(); + for (int i = 0; i < length; i++) { + stringBuilder.append((char) (i + 65)); + } + return stringBuilder.toString(); + } +} diff --git a/jodd-vtor/src/test/java/jodd/vtor/ValidationConstraintContextTest.java b/jodd-vtor/src/test/java/jodd/vtor/ValidationConstraintContextTest.java new file mode 100644 index 000000000..526f8ea11 --- /dev/null +++ b/jodd-vtor/src/test/java/jodd/vtor/ValidationConstraintContextTest.java @@ -0,0 +1,60 @@ +// Copyright (c) 2003-present, Jodd Team (http://jodd.org) +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +package jodd.vtor; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +public class ValidationConstraintContextTest { + + @Test + public void testConstructor() throws Exception { + Vtor vtor = mock(Vtor.class); + Object target = new Object(); + ValidationConstraintContext context = new ValidationConstraintContext(vtor, target, "niceConstraint"); + assertEquals(context.getName(), "niceConstraint"); + assertEquals(context.getTarget(), target); + assertEquals(context.getValidator(), vtor); + } + + @Test + public void testValidateWithin() throws Exception { + Vtor vtor = mock(Vtor.class); + Object target = new Object(); + ValidationConstraintContext context = new ValidationConstraintContext(vtor, target, "niceConstraint"); + + ValidationContext vctx = mock(ValidationContext.class); + Object value = new Object(); + //when + context.validateWithin(vctx, value); + //then + verify(vtor).validate(eq(vctx), eq(value), eq("niceConstraint")); + } +} \ No newline at end of file diff --git a/jodd-vtor/src/test/java/jodd/vtor/ValidationContextTest.java b/jodd-vtor/src/test/java/jodd/vtor/ValidationContextTest.java new file mode 100644 index 000000000..c8469d979 --- /dev/null +++ b/jodd-vtor/src/test/java/jodd/vtor/ValidationContextTest.java @@ -0,0 +1,122 @@ +// Copyright (c) 2003-present, Jodd Team (http://jodd.org) +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +package jodd.vtor; + +import jodd.introspector.PropertyDescriptor; +import jodd.vtor.constraint.Max; +import jodd.vtor.constraint.Min; +import org.junit.Test; + +import java.lang.annotation.*; +import java.util.ArrayList; +import java.util.List; + +import static junit.framework.TestCase.fail; +import static org.junit.Assert.*; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.*; + +public class ValidationContextTest { + + @Test + public void testResolveFor() throws Exception { + //when + //Class ForCheck1 contains two fields. field1 has two constraints. Second field2 has a non constraint annotation + ValidationContext context = ValidationContext.resolveFor(ClassForCheck1.class); + + //then + assertEquals("context must return a map with one element when resolve an object which has one field with constraint annotations", context.map.size(), 1); + assertNotNull("context must return a map with key field1 when resolve an object which has field with name field1 and constraint annotations", context.map.get("field1")); + assertNull("context must not return a map with key field2 when resolve an object which has a field with name field2 without constraint annotations", context.map.get("field2")); + assertEquals("context must return a map with two checks when resolve an object which has a field with two constraint annotations", context.map.get("field1").size(), 2); + } + + @Test(expected = VtorException.class) + public void testAddClassThrowVtorException() throws Exception { + ValidationContext context = new ValidationContext() { + @Override + protected V newConstraint(Class constraint, Class targetType) throws Exception { + throw new RuntimeException("terrible error"); + } + }; + context.addClassChecks(ClassForCheck1.class); + fail("when newConstraint throws some Exception then method should throws VtorException"); + } + + @Test + public void testCollectPropertyAnnotationChecks(){ + ValidationContext context = spy(new ValidationContext()); + PropertyDescriptor propertyDescriptor = mock(PropertyDescriptor.class); + List list = new ArrayList<>(); + context.collectPropertyAnnotationChecks(list, propertyDescriptor); + verify(context, never()).collectAnnotationChecks(any(List.class), any(Class.class),any(String.class), any(Annotation[].class)); + } + + @Test + public void testAddClassChecksWithCache() throws Exception { + ValidationContext context = spy(new ValidationContext()); + try { + context.addClassChecks(ClassForCheck1.class); + context.addClassChecks(ClassForCheck1.class); + } finally { + context.clearCache(); + } + + //collectProperty must be invoked only for first call of addClassChecks. Two calls for two annotations + verify(context, times(2)).collectPropertyAnnotationChecks(any(List.class), any(PropertyDescriptor.class)); + } + + private static class ClassForCheck1 { + @Max(20) + @Min(10) + private String field1; + + @NotConstraint + private String field2; + + public String getField1() { + return field1; + } + + public void setField1(String field1) { + this.field1 = field1; + } + + public String getField2() { + return field2; + } + + public void setField2(String field2) { + this.field2 = field2; + } + } + + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.FIELD) + public @interface NotConstraint { + } + +} \ No newline at end of file diff --git a/jodd-vtor/src/test/java/jodd/vtor/ViolationTest.java b/jodd-vtor/src/test/java/jodd/vtor/ViolationTest.java new file mode 100644 index 000000000..f2a249fd3 --- /dev/null +++ b/jodd-vtor/src/test/java/jodd/vtor/ViolationTest.java @@ -0,0 +1,99 @@ +// Copyright (c) 2003-present, Jodd Team (http://jodd.org) +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +package jodd.vtor; + +import org.junit.Test; + +import java.lang.annotation.Annotation; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.mock; + +public class ViolationTest { + + @Test + public void testConstructor1() throws Exception { + //given + Object validatedObject = new Object(); + Object invalidValue = new Object(); + + //when + Violation violation = new Violation("niceViolation", validatedObject, invalidValue); + + //then + assertEquals("validatedObject must be equal to a validatedObject which was given to constructor", violation.getValidatedObject(), validatedObject); + assertEquals("invalidValue must be equal to a invalidValue which was given to constructor", violation.getInvalidValue(), invalidValue); + assertNull("violation must be null", violation.getCheck()); + assertNull("constraint must be null", violation.getConstraint()); + } + + @Test + public void testConstructor2() throws Exception { + //given + Object validatedObject = new Object(); + Object invalidValue = new Object(); + ValidationConstraint constr = mock(ValidationConstraint.class); + Check niceCheck = new Check("niceCheck", constr); + + //when + Violation violation = new Violation("niceViolation", validatedObject, invalidValue, niceCheck); + + //then + assertEquals("name must be equal to a name of violation which was given to constructor", violation.getName(), "niceViolation"); + assertEquals("ValidatedObject must be equal to a ValidatedObject which was given to constructor", violation.getValidatedObject(), validatedObject); + assertEquals("InvalidValue must be equal to a InvalidValue which was given to constructor", violation.getInvalidValue(), invalidValue); + assertEquals("Check must be equal to a Check which was given to constructor", violation.getCheck(), niceCheck); + assertEquals("Constraint must be equal to a Constraint which was given to constructor", violation.getConstraint(), constr); + } + + + @Test + public void testToString() throws Exception { + //given + Object validatedObject = new Object(); + Object invalidValue = new Object(); + Violation violation = new Violation("niceViolation", validatedObject, invalidValue, new Check("niceCheck", new TestValidationConstraint())); + + //when + String toString = violation.toString(); + + //then + assertEquals(toString, "Violation{niceViolation:jodd.vtor.ViolationTest$TestValidationConstraint}"); + } + + private static class TestValidationConstraint implements ValidationConstraint { + @Override + public void configure(Annotation annotation) { + + } + + @Override + public boolean isValid(ValidationConstraintContext vcc, Object value) { + return false; + } + } +} \ No newline at end of file diff --git a/jodd-vtor/src/test/java/jodd/vtor/VtorExceptionTest.java b/jodd-vtor/src/test/java/jodd/vtor/VtorExceptionTest.java new file mode 100644 index 000000000..747310ee9 --- /dev/null +++ b/jodd-vtor/src/test/java/jodd/vtor/VtorExceptionTest.java @@ -0,0 +1,65 @@ +// Copyright (c) 2003-present, Jodd Team (http://jodd.org) +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +package jodd.vtor; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class VtorExceptionTest { + + @Test + public void testConstructor1() { + //given + RuntimeException cause = new RuntimeException("test"); + //when + VtorException vtorException = new VtorException(cause); + //then + assertEquals("created instance must have the same cause as was given to its constructor", vtorException.getCause(), cause); + } + + @Test + public void testConstructor2() { + //given + String message = "test"; + //when + VtorException vtorException = new VtorException(message); + //then + assertEquals("created instance must have the same message as was given to its constructor", vtorException.getMessage(), message); + } + + @Test + public void testConstructor3() { + //given + String message = "test"; + RuntimeException cause = new RuntimeException(); + //when + VtorException vtorException = new VtorException(message, cause); + //then + assertEquals("created instance must return message with cause details when create instance with message and some cause", vtorException.getMessage(), message+"; <--- java.lang.RuntimeException"); + assertEquals("created instance must have the same cause as was given to its constructor", vtorException.getCause(), cause); + } +} \ No newline at end of file diff --git a/jodd-vtor/src/test/java/jodd/vtor/VtorMatchProfilesTest.java b/jodd-vtor/src/test/java/jodd/vtor/VtorMatchProfilesTest.java new file mode 100644 index 000000000..4f243b2eb --- /dev/null +++ b/jodd-vtor/src/test/java/jodd/vtor/VtorMatchProfilesTest.java @@ -0,0 +1,95 @@ +// Copyright (c) 2003-present, Jodd Team (http://jodd.org) +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +package jodd.vtor; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import static junit.framework.TestCase.assertFalse; +import static org.junit.Assert.assertTrue; + +public class VtorMatchProfilesTest { + private Vtor vtor; + + @Before + public void setup() { + vtor = new Vtor(); + } + + @Test + public void testAllProfiles() { + assertTrue("result must be true when match a list with one element which is *", vtor.matchProfiles(new String[]{Vtor.ALL_PROFILES})); + assertFalse("result must be false when match a list with many elements", vtor.matchProfiles(new String[]{Vtor.ALL_PROFILES, "someProfile"})); + } + + @Test + public void testMatchProfilesAgainstSomeProfile() { + //when + vtor.useProfile("testProfile1"); + + //then + assertFalse("result must be false when match null value", + vtor.matchProfiles(null)); + + assertFalse("result must be false when match list of profiles without any assigned profiles", + vtor.matchProfiles(new String[]{"testProfile2", "testProfile3"})); + + assertTrue("result must be true when match list of profiles with one assigned profile", + vtor.matchProfiles(new String[]{"testProfile1", "testProfile2", "testProfile3"})); + + assertTrue("result must be true when match list of profiles with one assigned profile", + vtor.matchProfiles(new String[]{"testProfile1", "testProfile2", "testProfile3"})); + + assertTrue("result must be true when match list of profiles with one assigned profile", + vtor.matchProfiles(new String[]{"testProfile1", "testProfile2", "testProfile3"})); + + assertTrue("result must be true when match list of profiles with one assigned profile", + vtor.matchProfiles(new String[]{"testProfile1", "testProfile2", "testProfile3"})); + + assertTrue("result must be true when match unordered list of profiles with one assigned profile", + vtor.matchProfiles(new String[]{"testProfile2", "testProfile1", "testProfile3"})); + + assertFalse("result must be false when match a list of profiles with one wrong mandatory profile", + vtor.matchProfiles(new String[]{"+testProfile2", "testProfile1", "testProfile3"})); + + assertFalse("result must be false when match a list of profiles with one assigned profile which was marked as optional", + vtor.matchProfiles(new String[]{"testProfile2", "-testProfile1", "testProfile3"})); + } + + @Test + public void testDefaultProfile() { + //when + vtor.useProfile(Vtor.DEFAULT_PROFILE); + + //then + assertTrue("result must be true when match null value", vtor.matchProfiles(null)); + assertTrue("result must be true when match a list with empty profile", vtor.matchProfiles(new String[]{"testProfile2", "", "testProfile3"})); + assertFalse("result must be false when match a list without empty profile", vtor.matchProfiles(new String[]{"testProfile2", "testProfile3"})); + } + +} + diff --git a/jodd-vtor/src/test/java/jodd/vtor/VtorMatchProfilesWithResetedProfilesTest.java b/jodd-vtor/src/test/java/jodd/vtor/VtorMatchProfilesWithResetedProfilesTest.java new file mode 100644 index 000000000..a3aebc5b7 --- /dev/null +++ b/jodd-vtor/src/test/java/jodd/vtor/VtorMatchProfilesWithResetedProfilesTest.java @@ -0,0 +1,81 @@ +// Copyright (c) 2003-present, Jodd Team (http://jodd.org) +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +package jodd.vtor; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class VtorMatchProfilesWithResetedProfilesTest { + private Vtor vtor; + + @Before + public void setup() { + //given + vtor = new Vtor(); + vtor.resetProfiles(); + } + + @Test + public void testValidateAllProfilesByDefaultIsTrue() { + vtor.setValidateAllProfilesByDefault(true); + assertTrue("any profile must match when property ValidateAllProfilesByDefault is active", + vtor.matchProfiles(new String[]{"testProfile"})); + } + + @Test + public void testNullProfiles() { + assertTrue("result mast be true when match null value instead of profiles", + vtor.matchProfiles(null)); + } + + @Test + public void testEmptyListOfProfiles() { + assertTrue("result mast be true when match an empty list of profiles", + vtor.matchProfiles(new String[]{})); + } + + @Test + public void testOneProfileIsEmpty() { + assertTrue("result mast be true when match a list with an empty profile", + vtor.matchProfiles(new String[]{"", "testProfile"})); + } + + @Test + public void testOneProfileIsDefault() { + assertTrue("result mast be true when match a list with a default profile", + vtor.matchProfiles(new String[]{Vtor.DEFAULT_PROFILE, "testProfile"})); + } + + @Test + public void testNonSpecialProfiles(){ + assertFalse("result must be false when match non special profiles", + vtor.matchProfiles(new String[]{"testProfile1", "testProfile2"})); + } + +} diff --git a/jodd-vtor/src/test/java/jodd/vtor/VtorTest.java b/jodd-vtor/src/test/java/jodd/vtor/VtorTest.java new file mode 100644 index 000000000..86d3b01e2 --- /dev/null +++ b/jodd-vtor/src/test/java/jodd/vtor/VtorTest.java @@ -0,0 +1,249 @@ +// Copyright (c) 2003-present, Jodd Team (http://jodd.org) +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +package jodd.vtor; + +import org.junit.Test; + +import java.util.*; + +import static org.junit.Assert.*; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.*; + +public class VtorTest extends VtorTestSupport { + + @Test + public void testCreate() throws Exception { + Vtor vtor = Vtor.create(); + assertNotNull(vtor); + } + + @Test + public void testSetSeverity() throws Exception { + Vtor vtor = new Vtor(); + vtor.setSeverity(1); + assertEquals(vtor.severity, 1); + } + + @Test + public void testValidateAllProfilesByDefault() throws Exception { + Vtor vtor = new Vtor(); + vtor.setValidateAllProfilesByDefault(true); + assertTrue(vtor.isValidateAllProfilesByDefault()); + } + + @Test + public void testHasViolations() throws Exception { + Vtor vtor = new Vtor(); + assertFalse("method must return false when violations list is empty", vtor.hasViolations()); + + vtor.addViolation(mock(Violation.class)); + assertTrue("method must return true when add some violation", vtor.hasViolations()); + } + + @Test + public void testAddViolation_withNullValue() throws Exception { + Vtor vtor = new Vtor(); + vtor.addViolation(null); + assertNull("list of violations must be null when add only null violation", vtor.getViolations()); + } + + @Test + public void testAddViolation_withTwoDifferentValues() throws Exception { + //given + Vtor vtor = new Vtor(); + Violation violation1 = mock(Violation.class); + vtor.addViolation(violation1); + Violation violation2 = mock(Violation.class); + vtor.addViolation(violation2); + assertEquals("size of list with violations must be 2 when add two violation", vtor.getViolations().size(), 2); + assertEquals("first violation must be equal to first added violation", vtor.getViolations().get(0), violation1); + assertEquals("second violation must be equal to second added violation", vtor.getViolations().get(1), violation2); + + //when + vtor.addViolation(null); + + //then + assertEquals("list of violations must not be changed when add a null violation", vtor.getViolations().size(), 2); + } + + @Test + public void testUseProfile_withNullValue() throws Exception { + Vtor vtor = new Vtor(); + vtor.useProfile(null); + assertNull("list of enabled profiles must be null when add only null profile", vtor.enabledProfiles); + } + + @Test + public void testUseProfile_withTwoDifferentValues() throws Exception { + //given + Vtor vtor = new Vtor(); + vtor.useProfile("testProfile"); + assertEquals("size of list with profiles must be 1 when add one profile", vtor.enabledProfiles.size(), 1); + assertEquals("first element of enabled profiles must be testProfile when use testProfile", vtor.enabledProfiles.iterator().next(), "testProfile"); + + //when + vtor.useProfile(null); + + //then + assertEquals("size of list with profiles must not be changed when use null profile", vtor.enabledProfiles.size(), 1); + } + + @Test + public void testUseProfiles_withNullValue() throws Exception { + Vtor vtor = new Vtor(); + vtor.useProfiles(null); + assertNull("list of enabled profiles must be null when add only null profile", vtor.enabledProfiles); + } + + @Test + public void testUseProfiles_withTwoDifferentValues() throws Exception { + //given + Vtor vtor = new Vtor(); + vtor.useProfiles("testProfile1", "testProfile2"); + assertEquals("size of list with profiles must be 2 when add two profile", vtor.enabledProfiles.size(), 2); + Iterator resultIterator = vtor.enabledProfiles.iterator(); + assertEquals("first element must be equal to first added profile", resultIterator.next(), "testProfile1"); + assertEquals("second element must be equal to second added profile", resultIterator.next(), "testProfile2"); + + //when + vtor.useProfile(null); + + //then + assertEquals("size of list with profiles must not be changed when add null profile", vtor.enabledProfiles.size(), 2); + } + + private Map> createValidateFldHasCheckerTestConstraints(ValidationConstraint testCheck1Constraint) { + Map> constraints = new HashMap<>(); + List checks = new ArrayList<>(); + checks.add(new Check("testCheck1", testCheck1Constraint)); + constraints.put("testField", checks); + return constraints; + } + + @Test + public void testValidateFldHasChecker_isValidFalse() throws Exception { + //given + ValidationConstraint testCheck1Constraint = mock(ValidationConstraint.class); + //ValidationConstraint.isValid always returns false + stub(testCheck1Constraint.isValid(any(ValidationConstraintContext.class), any())).toReturn(false); + Map> constraints = createValidateFldHasCheckerTestConstraints(testCheck1Constraint); + + //when validate an object with field testField + List violations = new Vtor().validate(mockValidationContext(constraints), new ValidateTestObject("testValue"), "testField"); + + //then + //isValid for ValidationConstraint mast be invoked + verify(testCheck1Constraint).isValid(any(ValidationConstraintContext.class), eq("testValue")); + assertEquals("result must contain one violation when constraint check returns false", violations.size(), 1); + } + + @Test + public void testValidateFldHasChecker_isValidTrue() throws Exception { + //given + ValidationConstraint testCheck1Constraint = mock(ValidationConstraint.class); + //ValidationConstraint.isValid always returns false + stub(testCheck1Constraint.isValid(any(ValidationConstraintContext.class), any())).toReturn(true); + Map> constraints = createValidateFldHasCheckerTestConstraints(testCheck1Constraint); + + //when validate an object with field testField + List validate = new Vtor().validate(mockValidationContext(constraints), new ValidateTestObject("testValue"), "testField"); + + //then + //isValid for ValidationConstraint mast be invoked + verify(testCheck1Constraint).isValid(any(ValidationConstraintContext.class), eq("testValue")); + assertNull("result must not contain any violation when constraint check returns true", validate); + } + + @Test + public void testValidateCheckForDifferentProfile() throws Exception { + //given a list of constraints with different profiles, one check has same profile as Vtor + Vtor vtor = new Vtor(); + vtor.useProfile("profil1"); + Map> constraints = new HashMap<>(); + ValidationConstraint testCheck1Constraint = mock(ValidationConstraint.class); + ValidationConstraint testCheck2Constraint = mock(ValidationConstraint.class); + Check ch1 = createCheckWithProfile("check1", "profil1", testCheck1Constraint); + Check ch2 = createCheckWithProfile("check2", "profil2", testCheck2Constraint); + List checks = new ArrayList<>(); + checks.add(ch1); + checks.add(ch2); + constraints.put("testField", checks); + + //when + List violations = vtor.validate(mockValidationContext(constraints), new ValidateTestObject("testValue"), "testField"); + + //then + assertEquals("result must contain one violation", violations.size(), 1); + assertEquals("result must contain one violation with check for profile1", violations.get(0).getCheck().getProfiles()[0], "profil1"); + verify(testCheck1Constraint).isValid(any(ValidationConstraintContext.class), eq("testValue")); + verify(testCheck2Constraint, never()).isValid(any(ValidationConstraintContext.class), eq("testValue")); + } + + @Test + public void testValidateCheckSeverity() throws Exception { + //given + Vtor vtor = new Vtor(); + vtor.setSeverity(10); + + Map> constraints = new HashMap<>(); + ValidationConstraint testCheck1Constraint = mock(ValidationConstraint.class); + ValidationConstraint testCheck2Constraint = mock(ValidationConstraint.class); + + Check ch1 = new Check("check1", testCheck1Constraint); + ch1.setSeverity(15); + Check ch2 = new Check("check2", testCheck2Constraint); + ch2.setSeverity(5); + List checks = new ArrayList<>(); + checks.add(ch1); + checks.add(ch2); + constraints.put("testField", checks); + + //when + List violations = vtor.validate(mockValidationContext(constraints), new ValidateTestObject("testValue"), "testField"); + + //then + assertEquals("list of violations must have size 1 when validate two checks with severity 5 and 15", violations.size(), 1); + assertEquals("list of violations must contain check1 with severity 15 when validate two checks with severity 5 and 15", violations.get(0).getCheck(), ch1); + verify(testCheck1Constraint).isValid(any(ValidationConstraintContext.class), eq("testValue")); + verify(testCheck2Constraint, never()).isValid(any(ValidationConstraintContext.class), eq("testValue")); + } + + private static class ValidateTestObject { + private final String testField; + + public ValidateTestObject(String testField) { + this.testField = testField; + } + + public String getTestField() { + return testField; + } + } + + +} \ No newline at end of file diff --git a/jodd-vtor/src/test/java/jodd/vtor/VtorTestSupport.java b/jodd-vtor/src/test/java/jodd/vtor/VtorTestSupport.java new file mode 100644 index 000000000..f0f445b75 --- /dev/null +++ b/jodd-vtor/src/test/java/jodd/vtor/VtorTestSupport.java @@ -0,0 +1,43 @@ +// Copyright (c) 2003-present, Jodd Team (http://jodd.org) +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +package jodd.vtor; + +import java.util.List; +import java.util.Map; + +public class VtorTestSupport { + protected Check createCheckWithProfile(String name, String profile, ValidationConstraint constraint) { + Check check = new Check(name, constraint); + check.setProfiles(profile); + return check; + } + + protected ValidationContext mockValidationContext(Map> constraints) { + ValidationContext res = new ValidationContext(); + res.map.putAll(constraints); + return res; + } +} diff --git a/jodd-vtor/src/test/java/jodd/vtor/constraint/AssertFalseConstraintTest.java b/jodd-vtor/src/test/java/jodd/vtor/constraint/AssertFalseConstraintTest.java new file mode 100644 index 000000000..8211e6017 --- /dev/null +++ b/jodd-vtor/src/test/java/jodd/vtor/constraint/AssertFalseConstraintTest.java @@ -0,0 +1,56 @@ +// Copyright (c) 2003-present, Jodd Team (http://jodd.org) +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +package jodd.vtor.constraint; + +import jodd.vtor.ValidationConstraintContext; +import org.junit.Test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class AssertFalseConstraintTest extends ConstraintTestBase { + + @Test + public void testAssertFalse() { + AssertFalseConstraint assertFalseConstraint = new AssertFalseConstraint(); + //this is an empty method nothing can be verified + assertFalseConstraint.configure(null); + ValidationConstraintContext vc = mockContext(); + assertTrue(assertFalseConstraint.isValid(mockContext(), "false")); + assertTrue(assertFalseConstraint.isValid(vc, null)); + assertTrue(assertFalseConstraint.isValid(vc, Boolean.FALSE)); + } + + @Test + public void testAssertTrue() { + AssertFalseConstraint assertFalseConstraint = new AssertFalseConstraint(); + //this is an empty method nothing can be verified + assertFalseConstraint.configure(null); + ValidationConstraintContext vc = mockContext(); + assertFalse(assertFalseConstraint.isValid(mockContext(), "true")); + assertFalse(assertFalseConstraint.isValid(vc, Boolean.TRUE)); + } +} \ No newline at end of file diff --git a/jodd-vtor/src/test/java/jodd/vtor/constraint/AssertTrueConstraintTest.java b/jodd-vtor/src/test/java/jodd/vtor/constraint/AssertTrueConstraintTest.java new file mode 100644 index 000000000..73ef7e4bc --- /dev/null +++ b/jodd-vtor/src/test/java/jodd/vtor/constraint/AssertTrueConstraintTest.java @@ -0,0 +1,43 @@ +// Copyright (c) 2003-present, Jodd Team (http://jodd.org) +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +package jodd.vtor.constraint; + +import org.junit.Test; + +import static org.junit.Assert.assertTrue; + +public class AssertTrueConstraintTest extends ConstraintTestBase { + + @Test + public void testAssertTrue() { + AssertTrueConstraint assertTrueConstraint = new AssertTrueConstraint(); + //this is an empty method nothing can be verified + assertTrueConstraint.configure(null); + assertTrue(assertTrueConstraint.isValid(mockContext(), "on")); + assertTrue(assertTrueConstraint.isValid(mockContext(), null)); + assertTrue(assertTrueConstraint.isValid(mockContext(), Boolean.TRUE)); + } +} \ No newline at end of file diff --git a/jodd-vtor/src/test/java/jodd/vtor/constraint/AssertValidConstraintTest.java b/jodd-vtor/src/test/java/jodd/vtor/constraint/AssertValidConstraintTest.java new file mode 100644 index 000000000..578311a7c --- /dev/null +++ b/jodd-vtor/src/test/java/jodd/vtor/constraint/AssertValidConstraintTest.java @@ -0,0 +1,72 @@ +// Copyright (c) 2003-present, Jodd Team (http://jodd.org) +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +package jodd.vtor.constraint; + +import jodd.vtor.ValidationConstraintContext; +import jodd.vtor.ValidationContext; +import org.junit.Test; + +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.eq; +import static org.mockito.Matchers.isNull; +import static org.mockito.Mockito.*; + +public class AssertValidConstraintTest extends ConstraintTestBase { + + @Test + public void testIsValid_withNullValue() { + //given + ValidationContext targetValidationContext = mock(ValidationContext.class); + AssertValidConstraint assertValidConstraint = new AssertValidConstraint(targetValidationContext); + ValidationConstraintContext vcc = mockContext(); + + //when + boolean valid = assertValidConstraint.isValid(vcc, null); + + //then + assertTrue("result must be true when validate null value", valid); + verify(vcc, never()).validateWithin(eq(targetValidationContext), isNull()); + } + + @Test + public void testIsValid() { + //given + ValidationContext targetValidationContext = mock(ValidationContext.class); + AssertValidConstraint assertValidConstraint = new AssertValidConstraint(targetValidationContext); + ValidationConstraintContext vcc = mockContext(); + Object someValue = new Object(); + //this method is empty so nothing to check + assertValidConstraint.configure(null); + + //when validate some value + boolean valid = assertValidConstraint.isValid(vcc, someValue); + + //then validateWithin must be called for validated value + assertTrue("result must be true when validate not value", valid); + //validateWithin must be called for validated value + verify(vcc).validateWithin(eq(targetValidationContext), eq(someValue)); + } +} \ No newline at end of file diff --git a/jodd-vtor/src/test/java/jodd/vtor/constraint/ConstraintTestBase.java b/jodd-vtor/src/test/java/jodd/vtor/constraint/ConstraintTestBase.java new file mode 100644 index 000000000..fc8b39e22 --- /dev/null +++ b/jodd-vtor/src/test/java/jodd/vtor/constraint/ConstraintTestBase.java @@ -0,0 +1,37 @@ +// Copyright (c) 2003-present, Jodd Team (http://jodd.org) +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +package jodd.vtor.constraint; + +import jodd.vtor.ValidationConstraintContext; + +import static org.mockito.Mockito.mock; + +public class ConstraintTestBase { + + protected ValidationConstraintContext mockContext() { + return mock(ValidationConstraintContext.class); + } +} diff --git a/jodd-vtor/src/test/java/jodd/vtor/constraint/EqualToDeclaredFieldConstraintTest.java b/jodd-vtor/src/test/java/jodd/vtor/constraint/EqualToDeclaredFieldConstraintTest.java new file mode 100644 index 000000000..931638d3a --- /dev/null +++ b/jodd-vtor/src/test/java/jodd/vtor/constraint/EqualToDeclaredFieldConstraintTest.java @@ -0,0 +1,112 @@ +// Copyright (c) 2003-present, Jodd Team (http://jodd.org) +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +package jodd.vtor.constraint; + +import jodd.vtor.ValidationConstraintContext; +import jodd.vtor.VtorException; +import org.junit.Test; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.stub; + +public class EqualToDeclaredFieldConstraintTest extends ConstraintTestBase { + + @Test + public void testValidate_withNullValue() { + assertTrue("result must be true when validate null value", + EqualToDeclaredFieldConstraint.validate(new Object(), null, "someField")); + } + + @Test + public void testConstructor() { + EqualToDeclaredFieldConstraint equalToDeclaredFieldConstraint = new EqualToDeclaredFieldConstraint(); + assertNull(equalToDeclaredFieldConstraint.getFieldName()); + String fieldName = "testField"; + equalToDeclaredFieldConstraint = new EqualToDeclaredFieldConstraint(fieldName); + assertEquals("field name must be the same as was given to constructor", equalToDeclaredFieldConstraint.getFieldName(), fieldName); + } + + @Test + public void testSetFieldName() { + EqualToDeclaredFieldConstraint equalToDeclaredFieldConstraint = new EqualToDeclaredFieldConstraint(); + String fieldName = "someField"; + equalToDeclaredFieldConstraint.setFieldName(fieldName); + assertEquals("value must be the same as was given to set method", equalToDeclaredFieldConstraint.getFieldName(), fieldName); + } + + @Test + public void testConfigure() { + EqualToDeclaredFieldConstraint equalToDeclaredFieldConstraint = new EqualToDeclaredFieldConstraint(); + //set a field name through an annotation + EqualToDeclaredField fldAnnotation = mock(EqualToDeclaredField.class); + String field = "anotherField"; + stub(fldAnnotation.value()).toReturn(field); + + equalToDeclaredFieldConstraint.configure(fldAnnotation); + assertEquals("field name must be the same as was set to annotation when configure", + equalToDeclaredFieldConstraint.getFieldName(), field); + } + + @Test + public void testIsValid_forEqualValues() { + EqualToDeclaredFieldConstraint equalToDeclaredFieldConstraint = new EqualToDeclaredFieldConstraint("testField"); + ValidationConstraintContext cvv = mockContext(); + stub(cvv.getTarget()).toReturn(new TestValue("someValue")); + + assertTrue("result must be true when field and value are equals", equalToDeclaredFieldConstraint.isValid(cvv, "someValue")); + } + + @Test + public void testIsValid_forDifferentValues() { + EqualToDeclaredFieldConstraint equalToDeclaredFieldConstraint = new EqualToDeclaredFieldConstraint("testField"); + ValidationConstraintContext cvv = mockContext(); + stub(cvv.getTarget()).toReturn(new TestValue("someValue")); + + assertFalse("result must be false when validated field and value are different", equalToDeclaredFieldConstraint.isValid(cvv, "wrongValue")); + } + + @Test(expected = VtorException.class) + public void testValidate_FieldNotFound() { + TestValue testVal = new TestValue("someValue"); + EqualToDeclaredFieldConstraint.validate(testVal, "someValue", "wrongField"); + fail("EqualToDeclaredFieldConstraint should throw VtorException when receive nonexistent field name"); + } + + @Test + public void testValidate_FieldValueIsNull() { + TestValue testVal = new TestValue(null); + assertFalse("result must be false when field value is null", EqualToDeclaredFieldConstraint.validate(testVal, "someValue", "testField")); + } + + public static class TestValue { + private String testField; + + public TestValue(String testField) { + this.testField = testField; + } + } +} \ No newline at end of file diff --git a/jodd-vtor/src/test/java/jodd/vtor/constraint/EqualToFieldConstraintTest.java b/jodd-vtor/src/test/java/jodd/vtor/constraint/EqualToFieldConstraintTest.java new file mode 100644 index 000000000..fe55a4c5a --- /dev/null +++ b/jodd-vtor/src/test/java/jodd/vtor/constraint/EqualToFieldConstraintTest.java @@ -0,0 +1,122 @@ +// Copyright (c) 2003-present, Jodd Team (http://jodd.org) +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +package jodd.vtor.constraint; + +import jodd.vtor.ValidationConstraintContext; +import jodd.vtor.VtorException; +import org.junit.Test; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.stub; + +public class EqualToFieldConstraintTest extends ConstraintTestBase { + @Test + public void testValidate_withNullValue() { + assertTrue("result must be true when validate null value", + EqualToFieldConstraint.validate(new Object(), null, "someField")); + } + + @Test + public void testConstructor1() { + EqualToFieldConstraint equalToFieldConstraint = new EqualToFieldConstraint(); + assertNull(equalToFieldConstraint.getFieldName()); + } + + @Test + public void testConstructor2() { + String fieldName = "testField"; + EqualToFieldConstraint equalToFieldConstraint = new EqualToFieldConstraint(fieldName); + assertEquals("field name must be the same as was given to constructor", equalToFieldConstraint.getFieldName(), fieldName); + } + + @Test + public void testSetFieldName() { + EqualToFieldConstraint equalToFieldConstraint = new EqualToFieldConstraint(); + String fieldName = "someField"; + equalToFieldConstraint.setFieldName(fieldName); + assertEquals("value must be the same as was given to set method", equalToFieldConstraint.getFieldName(), fieldName); + } + + @Test + public void testConfigure() { + EqualToFieldConstraint equalToFieldConstraint = new EqualToFieldConstraint(); + //set a field name through an annotation + EqualToField fldAnnotation = mock(EqualToField.class); + String fieldName = "anotherField"; + stub(fldAnnotation.value()).toReturn(fieldName); + + equalToFieldConstraint.configure(fldAnnotation); + assertEquals("field name must be the same as was set to annotation when configure", + equalToFieldConstraint.getFieldName(), fieldName); + } + + @Test + public void testIsValid_forEqualValues() { + EqualToFieldConstraint equalToDeclaredFieldConstraint = new EqualToFieldConstraint("testField"); + ValidationConstraintContext cvv = mockContext(); + stub(cvv.getTarget()).toReturn(new TestBean("someValue")); + + assertTrue("result must be true when field and value are equals", equalToDeclaredFieldConstraint.isValid(cvv, "someValue")); + } + + @Test + public void testIsValid_forDifferentValues() { + EqualToFieldConstraint equalToDeclaredFieldConstraint = new EqualToFieldConstraint("testField"); + ValidationConstraintContext cvv = mockContext(); + stub(cvv.getTarget()).toReturn(new TestBean("someValue")); + assertFalse("result must be false when validated field and value are different", equalToDeclaredFieldConstraint.isValid(cvv, "wrongValue")); + } + + @Test(expected = VtorException.class) + public void testValidate_FieldNotFound() { + TestBean testVal = new TestBean("someValue"); + EqualToFieldConstraint.validate(testVal, "someValue", "wrongField"); + fail("EqualToFieldConstraint should throw VtorException when receive nonexistent field name"); + } + + @Test + public void testValidate_FieldValueIsNull() { + TestBean testVal = new TestBean(null); + assertFalse("result must be false when field value is null", EqualToFieldConstraint.validate(testVal, "someValue", "testField")); + } + + public static class TestBean { + private String testField; + + public TestBean(String testField) { + this.testField = testField; + } + + public String getTestField() { + return testField; + } + + public void setTestField(String testField) { + this.testField = testField; + } + } +} \ No newline at end of file diff --git a/jodd-vtor/src/test/java/jodd/vtor/constraint/HasSubstringConstraintTest.java b/jodd-vtor/src/test/java/jodd/vtor/constraint/HasSubstringConstraintTest.java new file mode 100644 index 000000000..5d793a436 --- /dev/null +++ b/jodd-vtor/src/test/java/jodd/vtor/constraint/HasSubstringConstraintTest.java @@ -0,0 +1,111 @@ +// Copyright (c) 2003-present, Jodd Team (http://jodd.org) +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +package jodd.vtor.constraint; + +import org.junit.Test; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.stub; + +public class HasSubstringConstraintTest extends ConstraintTestBase { + + @Test + public void testConstructor1() { + String someStr = "someStr"; + HasSubstringConstraint hasSubstringConstraint = new HasSubstringConstraint(someStr, true); + assertEquals("string must be the same as was given to constructor", hasSubstringConstraint.getSubstring(), someStr); + assertTrue(hasSubstringConstraint.isIgnoreCase()); + } + + @Test + public void testConstructor2() { + HasSubstringConstraint hasSubstringConstraint = new HasSubstringConstraint(); + assertNull(hasSubstringConstraint.getSubstring()); + assertFalse(hasSubstringConstraint.isIgnoreCase()); + } + + @Test + public void testConfigure() { + HasSubstringConstraint hasSubstringConstraint = new HasSubstringConstraint(); + HasSubstring annotation = mock(HasSubstring.class); + String substring = "testString"; + boolean ignoreCase = true; + stub(annotation.value()).toReturn(substring); + stub(annotation.ignoreCase()).toReturn(ignoreCase); + + hasSubstringConstraint.configure(annotation); + assertEquals("substring must be the same as was set to annotation when configure", + hasSubstringConstraint.getSubstring(), substring); + + assertEquals("ignoreCase must be the same as was set to annotation when configure", + hasSubstringConstraint.isIgnoreCase(), ignoreCase); + + } + + @Test + public void testSetSubstring() { + HasSubstringConstraint hasSubstringConstraint = new HasSubstringConstraint(); + String someStr = "someStr"; + hasSubstringConstraint.setSubstring(someStr); + assertEquals("string must be the same as was given to set method", hasSubstringConstraint.getSubstring(), someStr); + } + + @Test + public void testSetIgnoreCase() { + HasSubstringConstraint hasSubstringConstraint = new HasSubstringConstraint(); + hasSubstringConstraint.setIgnoreCase(true); + assertTrue("IgnoreCase must be the same as was given to set method", hasSubstringConstraint.isIgnoreCase()); + } + + @Test + public void testValidate_WithNullValue() { + HasSubstringConstraint hasSubstringConstraint = new HasSubstringConstraint(); + assertTrue("result must be true when validate a null value", hasSubstringConstraint.isValid(mockContext(), null)); + } + + @Test + public void testIgnoreCase_False() { + HasSubstringConstraint hasSubstringConstraint = new HasSubstringConstraint(); + hasSubstringConstraint.setSubstring("al"); + hasSubstringConstraint.setIgnoreCase(false); + + assertTrue("result mast be true when validate low case string", hasSubstringConstraint.isValid(mockContext(), "value")); + assertFalse("result mast be false when validate upper case string", hasSubstringConstraint.isValid(mockContext(), "VALUE")); + assertFalse("result must be false when validate string without substring", hasSubstringConstraint.isValid(mockContext(), "FOO")); + } + + @Test + public void testIgnoreCase_True() { + HasSubstringConstraint hasSubstringConstraint = new HasSubstringConstraint(); + hasSubstringConstraint.setSubstring("al"); + hasSubstringConstraint.setIgnoreCase(true); + + assertTrue("result mast be true when validate low case string", hasSubstringConstraint.isValid(mockContext(), "value")); + assertTrue("result mast be true when validate upper case string", hasSubstringConstraint.isValid(mockContext(), "VALUE")); + assertFalse("result must be false when validate string without substring", hasSubstringConstraint.isValid(mockContext(), "FOO")); + } +} \ No newline at end of file diff --git a/jodd-vtor/src/test/java/jodd/vtor/constraint/LengthConstraintTest.java b/jodd-vtor/src/test/java/jodd/vtor/constraint/LengthConstraintTest.java new file mode 100644 index 000000000..b09eb4a12 --- /dev/null +++ b/jodd-vtor/src/test/java/jodd/vtor/constraint/LengthConstraintTest.java @@ -0,0 +1,87 @@ +// Copyright (c) 2003-present, Jodd Team (http://jodd.org) +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +package jodd.vtor.constraint; + +import jodd.vtor.TestUtils; +import org.junit.Test; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.stub; + +public class LengthConstraintTest extends ConstraintTestBase { + @Test + public void testValidate_WithNullValue() { + assertTrue("result must be true when validate a null value", LengthConstraint.validate(null, 1, 2)); + } + + @Test + public void testConstructor1() { + LengthConstraint lengthConstraint = new LengthConstraint(); + assertEquals("value must be default", lengthConstraint.getMin(), 0); + assertEquals("value must be default", lengthConstraint.getMax(), 0); + } + + @Test + public void testConstructor2() { + LengthConstraint lengthConstraint = new LengthConstraint(5, 10); + assertEquals("min value must be the same as was given to constructor", lengthConstraint.getMin(), 5); + assertEquals("max value must be the same as was given to constructor", lengthConstraint.getMax(), 10); + } + + @Test + public void testSetMinMax() { + LengthConstraint lengthConstraint = new LengthConstraint(); + lengthConstraint.setMin(5); + lengthConstraint.setMax(10); + assertEquals("min value must be the same as was given to set method", lengthConstraint.getMin(), 5); + assertEquals("max value must be the same as was given to set method", lengthConstraint.getMax(), 10); + } + + @Test + public void testConfigure() { + LengthConstraint lengthConstraint = new LengthConstraint(); + Length annotation = mock(Length.class); + stub(annotation.min()).toReturn(5); + stub(annotation.max()).toReturn(10); + + lengthConstraint.configure(annotation); + assertEquals("min value must be the same as was set to annotation when configure", lengthConstraint.getMin(), 5); + assertEquals("max value must be the same as was set to annotation when configure", lengthConstraint.getMax(), 10); + } + + @Test + public void testLengthConstraint() { + LengthConstraint lengthConstraint = new LengthConstraint(4, 6); + assertFalse("result must be false when validate string with length 7 ", lengthConstraint.isValid(mockContext(), TestUtils.stringWithLength(7))); + assertFalse("result must be false when validate string with length 3", lengthConstraint.isValid(mockContext(), TestUtils.stringWithLength(3))); + assertTrue("result must be true when validate string with length 4", lengthConstraint.isValid(mockContext(), TestUtils.stringWithLength(4))); + assertTrue("result must be true when validate string with length 6", lengthConstraint.isValid(mockContext(), TestUtils.stringWithLength(6))); + assertTrue("result must be true when validate string with length 5", lengthConstraint.isValid(mockContext(), TestUtils.stringWithLength(5))); + } + + +} \ No newline at end of file diff --git a/jodd-vtor/src/test/java/jodd/vtor/constraint/MaxConstraintTest.java b/jodd-vtor/src/test/java/jodd/vtor/constraint/MaxConstraintTest.java new file mode 100644 index 000000000..5257b6eee --- /dev/null +++ b/jodd-vtor/src/test/java/jodd/vtor/constraint/MaxConstraintTest.java @@ -0,0 +1,75 @@ +// Copyright (c) 2003-present, Jodd Team (http://jodd.org) +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +package jodd.vtor.constraint; + +import org.junit.Test; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.stub; + +public class MaxConstraintTest extends ConstraintTestBase { + + @Test + public void testConstructor1() { + MaxConstraint maxConstraint = new MaxConstraint(); + assertEquals("value must be default", maxConstraint.getMax(), 0.0, 0.01); + } + + @Test + public void testConstructor2() { + MaxConstraint maxConstraint = new MaxConstraint(0.1); + assertEquals("max value must be the same as was given to constructor", maxConstraint.getMax(), 0.1, 0.01); + } + + @Test + public void testSetMax() { + MaxConstraint maxConstraint = new MaxConstraint(); + maxConstraint.setMax(0.1); + assertEquals("max value must be the same as was given to set method", maxConstraint.getMax(), 0.1, 0.01); + } + + @Test + public void testConfigure() { + MaxConstraint maxConstraint = new MaxConstraint(); + Max annotation = mock(Max.class); + stub(annotation.value()).toReturn(0.1); + + maxConstraint.configure(annotation); + assertEquals("max value must be the same as was set to annotation when configure", maxConstraint.getMax(), 0.1, 0.01); } + + + @Test + public void testValidate_WithNullValue() { + assertTrue("result must be true when validate a null value", MaxConstraint.validate(null, 12.1)); + } + + @Test + public void testIsValid() { + assertTrue("result must be true when validated value is less than max", new MaxConstraint(12.5).isValid(mockContext(), "12.1")); + assertFalse("result must be true when validated value is grater than max", new MaxConstraint(12.5).isValid(mockContext(), "12.6")); + } +} \ No newline at end of file diff --git a/jodd-vtor/src/test/java/jodd/vtor/constraint/MaxLengthConstraintTest.java b/jodd-vtor/src/test/java/jodd/vtor/constraint/MaxLengthConstraintTest.java new file mode 100644 index 000000000..2c50a406f --- /dev/null +++ b/jodd-vtor/src/test/java/jodd/vtor/constraint/MaxLengthConstraintTest.java @@ -0,0 +1,79 @@ +// Copyright (c) 2003-present, Jodd Team (http://jodd.org) +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +package jodd.vtor.constraint; + +import jodd.vtor.TestUtils; +import org.junit.Test; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.stub; + +public class MaxLengthConstraintTest extends ConstraintTestBase { + + @Test + public void testConstructor1() { + MaxLengthConstraint maxLengthConstraint = new MaxLengthConstraint(); + assertEquals("value must be default", maxLengthConstraint.getMax(), 0); + } + + @Test + public void testConstructor2() { + MaxLengthConstraint maxLengthConstraint = new MaxLengthConstraint(10); + assertEquals("max value must be the same as was given to constructor", maxLengthConstraint.getMax(), 10); + } + + @Test + public void testSetMax() { + MaxLengthConstraint maxLengthConstraint = new MaxLengthConstraint(); + int maxValue = 100; + maxLengthConstraint.setMax(maxValue); + assertEquals("method must return the same value as was given to set method", maxLengthConstraint.getMax(), maxValue); + } + + @Test + public void testConfigure() { + MaxLengthConstraint maxLengthConstraint = new MaxLengthConstraint(); + MaxLength annotation = mock(MaxLength.class); + int maxValue = 100; + stub(annotation.value()).toReturn(maxValue); + + maxLengthConstraint.configure(annotation); + assertEquals("method must return the same value as was set to annotation when configure", maxLengthConstraint.getMax(), maxValue); + } + + @Test + public void testValidate_WithValIsNull() { + assertTrue("result must be true when validate null value", MaxLengthConstraint.validate(null, 1)); + } + + @Test + public void testMaxLengthConstraint() { + MaxLengthConstraint maxLengthConstraint = new MaxLengthConstraint(3); + assertTrue("result must be true when validate a string with size less than maxValue", maxLengthConstraint.isValid(mockContext(), TestUtils.stringWithLength(3))); + assertFalse("result must be false when validate a string with size greater than maxValue", maxLengthConstraint.isValid(mockContext(), TestUtils.stringWithLength(4))); + } +} \ No newline at end of file diff --git a/jodd-vtor/src/test/java/jodd/vtor/constraint/MinConstraintTest.java b/jodd-vtor/src/test/java/jodd/vtor/constraint/MinConstraintTest.java new file mode 100644 index 000000000..0f02bdd8b --- /dev/null +++ b/jodd-vtor/src/test/java/jodd/vtor/constraint/MinConstraintTest.java @@ -0,0 +1,75 @@ +// Copyright (c) 2003-present, Jodd Team (http://jodd.org) +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +package jodd.vtor.constraint; + +import org.junit.Test; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.stub; + +public class MinConstraintTest extends ConstraintTestBase { + @Test + public void testConstructor1() { + MinConstraint minConstraint = new MinConstraint(); + assertEquals("value must be default", minConstraint.getMin(), 0.0, 0.01); + } + + @Test + public void testConstructor2() { + MinConstraint minConstraint = new MinConstraint(10.0); + assertEquals("max value must be the same as was given to constructor", minConstraint.getMin(), 10.0, 0.01); + } + + @Test + public void testSetMin() { + MinConstraint minConstraint = new MinConstraint(); + minConstraint.setMin(10); + assertEquals("method must return the same value as was given to set method", minConstraint.getMin(), 10.0, 0.01); + } + + @Test + public void testConfigure() { + MinConstraint minConstraint = new MinConstraint(); + Min annotation = mock(Min.class); + stub(annotation.value()).toReturn(10.0); + + minConstraint.configure(annotation); + assertEquals("method must return the same value as was set to annotation when configure", minConstraint.getMin(), 10.0, 0.01); + } + + @Test + public void testIsValid() { + MinConstraint minConstraint = new MinConstraint(12.5); + assertTrue("result must be true when validate a value which is greater than minValue", minConstraint.isValid(mockContext(), "12.6")); + assertFalse("result must be false when validate a value which is less than minValue", minConstraint.isValid(mockContext(), "12.1")); + } + + @Test + public void testValidate_WithValIsNull() { + assertTrue("result must be true when validate null value", MinConstraint.validate(null, 12.5)); + } +} \ No newline at end of file diff --git a/jodd-vtor/src/test/java/jodd/vtor/constraint/MinLengthConstraintTest.java b/jodd-vtor/src/test/java/jodd/vtor/constraint/MinLengthConstraintTest.java new file mode 100644 index 000000000..208948007 --- /dev/null +++ b/jodd-vtor/src/test/java/jodd/vtor/constraint/MinLengthConstraintTest.java @@ -0,0 +1,78 @@ +// Copyright (c) 2003-present, Jodd Team (http://jodd.org) +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +package jodd.vtor.constraint; + +import jodd.vtor.TestUtils; +import org.junit.Test; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.stub; + +public class MinLengthConstraintTest extends ConstraintTestBase { + @Test + public void testConstructor1() { + MinLengthConstraint minLengthConstraint = new MinLengthConstraint(); + assertEquals("value must be default", minLengthConstraint.getMin(), 0); + } + + @Test + public void testConstructor2() { + MinLengthConstraint minLengthConstraint = new MinLengthConstraint(10); + assertEquals("value must be the same as was given to constructor", minLengthConstraint.getMin(), 10); + } + + @Test + public void testSetMin() { + MinLengthConstraint minLengthConstraint = new MinLengthConstraint(); + minLengthConstraint.setMin(10); + assertEquals("min value must be the same as was given to set method", minLengthConstraint.getMin(), 10); + } + + @Test + public void testConfigure() { + MinLengthConstraint minLengthConstraint = new MinLengthConstraint(); + MinLength annotation = mock(MinLength.class); + stub(annotation.value()).toReturn(10); + + minLengthConstraint.configure(annotation); + assertEquals("min value must be the same as was set to annotation when configure", minLengthConstraint.getMin(), 10); + } + + @Test + public void testValidate_WithNullValue() { + assertTrue("result must be true when validate a null value", MinLengthConstraint.validate(null, 1)); + } + + @Test + public void testIsValid() { + int min = 3; + MinLengthConstraint minLengthConstraint = new MinLengthConstraint(min); + assertTrue("result must be true when validate string with length greater than min", minLengthConstraint.isValid(mockContext(), TestUtils.stringWithLength(min))); + assertTrue("result must be true when validate string with length equal to min", minLengthConstraint.isValid(mockContext(), TestUtils.stringWithLength(min))); + assertFalse("result must be false when validate string with length less than min", minLengthConstraint.isValid(mockContext(), TestUtils.stringWithLength(min - 1))); + } +} \ No newline at end of file diff --git a/jodd-vtor/src/test/java/jodd/vtor/ConstraintTest.java b/jodd-vtor/src/test/java/jodd/vtor/constraint/NotBlankConstraintTest.java similarity index 52% rename from jodd-vtor/src/test/java/jodd/vtor/ConstraintTest.java rename to jodd-vtor/src/test/java/jodd/vtor/constraint/NotBlankConstraintTest.java index 2314d4635..f739fb334 100644 --- a/jodd-vtor/src/test/java/jodd/vtor/ConstraintTest.java +++ b/jodd-vtor/src/test/java/jodd/vtor/constraint/NotBlankConstraintTest.java @@ -1,74 +1,51 @@ -// Copyright (c) 2003-present, Jodd Team (http://jodd.org) -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are met: -// -// 1. Redistributions of source code must retain the above copyright notice, -// this list of conditions and the following disclaimer. -// -// 2. Redistributions in binary form must reproduce the above copyright -// notice, this list of conditions and the following disclaimer in the -// documentation and/or other materials provided with the distribution. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE -// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR -// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF -// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN -// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) -// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE -// POSSIBILITY OF SUCH DAMAGE. - -package jodd.vtor; - -import jodd.datetime.JDateTime; -import jodd.vtor.constraint.*; -import org.junit.Test; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -public class ConstraintTest { - - @Test - public void testAssertFalse() { - assertTrue(AssertFalseConstraint.validate("false")); - assertTrue(AssertFalseConstraint.validate(null)); - assertTrue(AssertFalseConstraint.validate(Boolean.FALSE)); - } - - @Test - public void testAssertTrue() { - assertTrue(AssertTrueConstraint.validate("on")); - assertTrue(AssertTrueConstraint.validate(null)); - assertTrue(AssertTrueConstraint.validate(Boolean.TRUE)); - } - - @Test - public void testHasSubstring() { - assertTrue(HasSubstringConstraint.validate("value", "al", false)); - assertTrue(HasSubstringConstraint.validate("value", "al", true)); - } - - @Test - public void testMaxConstraint() { - assertTrue(MaxConstraint.validate("12.1", 12.5)); - assertFalse(MaxConstraint.validate("12.6", 12.5)); - } - - @Test - public void testMinConstraint() { - assertTrue(MinConstraint.validate("12.6", 12.5)); - assertFalse(MinConstraint.validate("12.1", 12.5)); - } - - @Test - public void testTimeAfter() { - assertTrue(TimeAfterConstraint.validate("2011-05-01 10:11:12.345", new JDateTime("2011-05-01 10:11:12.344"))); - assertFalse(TimeAfterConstraint.validate("2011-05-01 10:11:12.345", new JDateTime("2011-05-01 10:11:12.345"))); - } -} +// Copyright (c) 2003-present, Jodd Team (http://jodd.org) +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +package jodd.vtor.constraint; + +import org.junit.Test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class NotBlankConstraintTest extends ConstraintTestBase { + + @Test + public void testValidate_WithNullValue() { + assertTrue("result must be true when validate a null value", NotBlankConstraint.validate(null)); + } + + @Test + public void testIsValid() { + NotBlankConstraint notBlankConstraint = new NotBlankConstraint(); + //this is an empty method nothing can be verified + notBlankConstraint.configure(null); + + assertTrue("result must be true when validate not empty string", notBlankConstraint.isValid(mockContext(), "abc")); + assertTrue("result must be true when validate string with space at the beginning", notBlankConstraint.isValid(mockContext(), " abc")); + assertFalse("result must be false when validate string with space", notBlankConstraint.isValid(mockContext(), " ")); + assertFalse("result must be false when validate empty", notBlankConstraint.isValid(mockContext(), "")); + } +} \ No newline at end of file diff --git a/jodd-vtor/src/test/java/jodd/vtor/constraint/NotNullConstraintTest.java b/jodd-vtor/src/test/java/jodd/vtor/constraint/NotNullConstraintTest.java new file mode 100644 index 000000000..0360600c7 --- /dev/null +++ b/jodd-vtor/src/test/java/jodd/vtor/constraint/NotNullConstraintTest.java @@ -0,0 +1,44 @@ +// Copyright (c) 2003-present, Jodd Team (http://jodd.org) +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +package jodd.vtor.constraint; + +import org.junit.Test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class NotNullConstraintTest extends ConstraintTestBase { + + @Test + public void testIsValid() { + NotNullConstraint notNullConstraint = new NotNullConstraint(); + //this is an empty method nothing can be verified + notNullConstraint.configure(null); + + assertTrue("result must be true when validate not null value", notNullConstraint.isValid(mockContext(), new Object())); + assertFalse("result must be false when validate null value", notNullConstraint.isValid(mockContext(), null)); + } +} \ No newline at end of file diff --git a/jodd-vtor/src/test/java/jodd/vtor/constraint/RangeConstraintTest.java b/jodd-vtor/src/test/java/jodd/vtor/constraint/RangeConstraintTest.java new file mode 100644 index 000000000..10d129e70 --- /dev/null +++ b/jodd-vtor/src/test/java/jodd/vtor/constraint/RangeConstraintTest.java @@ -0,0 +1,85 @@ +// Copyright (c) 2003-present, Jodd Team (http://jodd.org) +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +package jodd.vtor.constraint; + +import org.junit.Test; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.stub; + +public class RangeConstraintTest extends ConstraintTestBase { + + @Test + public void testConstructor1() { + RangeConstraint rangeConstraint = new RangeConstraint(); + assertEquals("value must be default", rangeConstraint.getMin(), 0.0, 0.01); + assertEquals("value must be default", rangeConstraint.getMax(), 0.0, 0.01); + } + + @Test + public void testConstructor2() { + RangeConstraint rangeConstraint = new RangeConstraint(1.1, 10.1); + assertEquals("min value must be the same as was given to constructor", rangeConstraint.getMin(), 1.1, 0.01); + assertEquals("max value must be the same as was given to constructor", rangeConstraint.getMax(), 10.1, 0.01); + } + + + @Test + public void testSetMinMax() { + RangeConstraint rangeConstraint = new RangeConstraint(); + rangeConstraint.setMin(1.1); + rangeConstraint.setMax(10.1); + assertEquals("method must return the same value as was given to set method", rangeConstraint.getMin(), 1.1, 0.01); + assertEquals("method must return the same value as was given to set method", rangeConstraint.getMax(), 10.1, 0.01); + } + + @Test + public void testConfigure() { + RangeConstraint rangeConstraint = new RangeConstraint(); + Range annotation = mock(Range.class); + stub(annotation.min()).toReturn(1.1); + stub(annotation.max()).toReturn(10.1); + + rangeConstraint.configure(annotation); + assertEquals("method must return the same value as was set to annotation when configure", rangeConstraint.getMin(), 1.1, 0.01); + assertEquals("method must return the same value as was set to annotation when configure", rangeConstraint.getMax(), 10.1, 0.01); + } + + @Test + public void testValidate_WithValIsNull() { + assertTrue("result must be true when validate null value", RangeConstraint.validate(null, 1, 2)); + } + + @Test + public void testIsValid() { + assertFalse("result must be false when validate value less than min", new RangeConstraint(1.1, 2.0).isValid(mockContext(), "1.0")); + assertFalse("result must be false when validate value grater than max", new RangeConstraint(1.1, 3.0).isValid(mockContext(), "3.1")); + assertTrue("result must be true when validate value grater than min and less than max", new RangeConstraint(2.0, 3.0).isValid(mockContext(), "2.8")); + assertTrue("result must be true when validate value equal to min", new RangeConstraint(2.1, 3.0).isValid(mockContext(), "2.1")); + assertTrue("result must be true when validate value equal to max", new RangeConstraint(1.0, 2.1).isValid(mockContext(), "2.1")); + } +} \ No newline at end of file diff --git a/jodd-vtor/src/test/java/jodd/vtor/constraint/SizeConstraintTest.java b/jodd-vtor/src/test/java/jodd/vtor/constraint/SizeConstraintTest.java new file mode 100644 index 000000000..0d3758acb --- /dev/null +++ b/jodd-vtor/src/test/java/jodd/vtor/constraint/SizeConstraintTest.java @@ -0,0 +1,113 @@ +// Copyright (c) 2003-present, Jodd Team (http://jodd.org) +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +package jodd.vtor.constraint; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.stub; + +public class SizeConstraintTest extends ConstraintTestBase { + + @Test + public void testConstructor1() { + SizeConstraint sizeConstraint = new SizeConstraint(); + assertEquals("min value must be default", sizeConstraint.getMin(), 0); + assertEquals("max value must be default", sizeConstraint.getMax(), 0); + } + + @Test + public void testConstructor2() { + SizeConstraint sizeConstraint = new SizeConstraint(10, 20); + assertEquals("min value must be the same as was given to constructor", sizeConstraint.getMin(), 10); + assertEquals("max value must be the same as was given to constructor", sizeConstraint.getMax(), 20); + } + + @Test + public void testSetMinMax() { + SizeConstraint sizeConstraint = new SizeConstraint(); + sizeConstraint.setMin(10); + sizeConstraint.setMax(20); + assertEquals("method must return the same value as was given to set method", sizeConstraint.getMin(), 10); + assertEquals("method must return the same value as was given to set method", sizeConstraint.getMax(), 20); + } + + @Test + public void testConfigure() { + SizeConstraint sizeConstraint = new SizeConstraint(); + Size annotation = mock(Size.class); + stub(annotation.min()).toReturn(10); + stub(annotation.max()).toReturn(20); + + sizeConstraint.configure(annotation); + assertEquals("method must return the same value as was set to annotation when configure", sizeConstraint.getMin(), 10); + assertEquals("method must return the same value as was set to annotation when configure", sizeConstraint.getMax(), 20); + } + + @Test + public void testValidate_WithValIsNull() { + assertTrue("result must be true when validate null value", SizeConstraint.validate(null, 1, 2)); + } + + private void sizeConstraintCheck(Object val) { + assertFalse("result must be false when validate value less than min", new SizeConstraint(4, 5).isValid(mockContext(), val)); + assertFalse("result must be false when validate value grater than max", new SizeConstraint(0, 1).isValid(mockContext(), val)); + assertTrue("result must be true when validate value grater than min and less than max", new SizeConstraint(1, 3).isValid(mockContext(), val)); + assertTrue("result must be true when validated value equal to min and max", new SizeConstraint(1, 2).isValid(mockContext(), val)); + } + + @Test + public void testIsValid_ForCollection() { + List val = new ArrayList<>(); + val.add("1"); + val.add("2"); + sizeConstraintCheck(val); + } + + @Test + public void testIsValid_ForMap() { + Map val = new HashMap<>(); + val.put("1", "one"); + val.put("2", "two"); + sizeConstraintCheck(val); + } + + @Test + public void testIsValid_ForArray() { + sizeConstraintCheck(new String[]{"one", "two"}); + } + + @Test + public void testValidate_ForUnknownClass() { + assertFalse("result must be false when validate something different than map, array or collection", SizeConstraint.validate(new Object(), 0, 1)); + } +} \ No newline at end of file diff --git a/jodd-vtor/src/test/java/jodd/vtor/constraint/TimeAfterConstraintTest.java b/jodd-vtor/src/test/java/jodd/vtor/constraint/TimeAfterConstraintTest.java new file mode 100644 index 000000000..f58e16499 --- /dev/null +++ b/jodd-vtor/src/test/java/jodd/vtor/constraint/TimeAfterConstraintTest.java @@ -0,0 +1,84 @@ +// Copyright (c) 2003-present, Jodd Team (http://jodd.org) +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +package jodd.vtor.constraint; + +import jodd.datetime.JDateTime; +import jodd.datetime.JDateTimeDefault; +import org.junit.Test; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.stub; + +public class TimeAfterConstraintTest extends ConstraintTestBase { + + @Test + public void testConstructor1() { + TimeAfterConstraint timeAfterConstraint = new TimeAfterConstraint(); + assertNull("time value must be null by default", timeAfterConstraint.getTime()); + } + + @Test + public void testConstructor2() { + JDateTime time = new JDateTime(); + TimeAfterConstraint timeAfterConstraint = new TimeAfterConstraint(time); + assertEquals("time must be the same as was given to constructor", timeAfterConstraint.getTime(), time); + } + + @Test + public void testSetTime() { + TimeAfterConstraint timeAfterConstraint = new TimeAfterConstraint(); + JDateTime time = new JDateTime(); + timeAfterConstraint.setTime(time); + assertEquals("method must return the same time as was given to set method", timeAfterConstraint.getTime(), time); + } + + @Test + public void testConfigure() { + TimeAfterConstraint timeAfterConstraint = new TimeAfterConstraint(); + JDateTime time = new JDateTime(); + TimeAfter annotation = mock(TimeAfter.class); + stub(annotation.value()).toReturn(JDateTimeDefault.formatter.convert(time, JDateTimeDefault.format)); + + timeAfterConstraint.configure(annotation); + assertEquals("method must return the same time as was set to annotation when configure", timeAfterConstraint.getTime(), time); + } + + @Test + public void testValidate_WithValIsNull() { + assertTrue("result must be true when validate null value", TimeAfterConstraint.validate(null, new JDateTime("2011-05-01 10:11:12.344"))); + } + + @Test + public void testIsValid() { + JDateTime time = new JDateTime("2011-05-01 10:11:12.344"); + TimeAfterConstraint constraint = new TimeAfterConstraint(time.clone()); + + assertFalse("result must be true when validate time which is equal to constraint time", constraint.isValid(mockContext(), time.clone())); + assertFalse("result must be false when validate time which is less than constraint time", constraint.isValid(mockContext(), time.clone().subMinute(1))); + assertTrue("result must be true when validate time which is greater than constraint time", constraint.isValid(mockContext(), time.clone().addMinute(1))); + } +} \ No newline at end of file diff --git a/jodd-vtor/src/test/java/jodd/vtor/constraint/TimeBeforeConstraintTest.java b/jodd-vtor/src/test/java/jodd/vtor/constraint/TimeBeforeConstraintTest.java new file mode 100644 index 000000000..96d51461c --- /dev/null +++ b/jodd-vtor/src/test/java/jodd/vtor/constraint/TimeBeforeConstraintTest.java @@ -0,0 +1,85 @@ +// Copyright (c) 2003-present, Jodd Team (http://jodd.org) +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +package jodd.vtor.constraint; + +import jodd.datetime.JDateTime; +import jodd.datetime.JDateTimeDefault; +import org.junit.Test; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.stub; + +public class TimeBeforeConstraintTest extends ConstraintTestBase { + + @Test + public void testConstructor1() { + TimeBeforeConstraint timeBeforeConstraint = new TimeBeforeConstraint(); + assertNull("time value must be null by default", timeBeforeConstraint.getTime()); + } + + @Test + public void testConstructor2() { + JDateTime time = new JDateTime(); + TimeBeforeConstraint timeBeforeConstraint = new TimeBeforeConstraint(time); + assertEquals("time must be the same as was given to constructor", timeBeforeConstraint.getTime(), time); + } + + @Test + public void testSetTime() { + TimeBeforeConstraint timeBeforeConstraint = new TimeBeforeConstraint(); + JDateTime time = new JDateTime(); + timeBeforeConstraint.setTime(time); + assertEquals("method must return the same time as was given to set method", timeBeforeConstraint.getTime(), time); + } + + @Test + public void testConfigure() { + TimeBeforeConstraint timeBeforeConstraint = new TimeBeforeConstraint(); + JDateTime time = new JDateTime(); + timeBeforeConstraint.setTime(time); + TimeBefore annotation = mock(TimeBefore.class); + stub(annotation.value()).toReturn(JDateTimeDefault.formatter.convert(time, JDateTimeDefault.format)); + + timeBeforeConstraint.configure(annotation); + assertEquals("method must return the same time as was set to annotation when configure", timeBeforeConstraint.getTime(), time); + } + + @Test + public void testValidate_WithValIsNull() { + assertTrue("result must be true when validate null value", TimeBeforeConstraint.validate(null, new JDateTime("2011-05-01 10:11:12.344"))); + } + + @Test + public void testIsValid() { + JDateTime time = new JDateTime("2011-05-01 10:11:12.344"); + TimeBeforeConstraint constraint = new TimeBeforeConstraint(time.clone()); + + assertFalse("result must be true when validate time which is equal to constraint time", constraint.isValid(mockContext(), time.clone())); + assertTrue("result must be false when validate time which is less than constraint time", constraint.isValid(mockContext(), time.clone().subMinute(1))); + assertFalse("result must be true when validate time which is greater than constraint time", constraint.isValid(mockContext(), time.clone().addMinute(1))); + } +} \ No newline at end of file diff --git a/jodd-vtor/src/test/java/jodd/vtor/constraint/WildcardMatchConstraintTest.java b/jodd-vtor/src/test/java/jodd/vtor/constraint/WildcardMatchConstraintTest.java new file mode 100644 index 000000000..f54dd2a85 --- /dev/null +++ b/jodd-vtor/src/test/java/jodd/vtor/constraint/WildcardMatchConstraintTest.java @@ -0,0 +1,77 @@ +// Copyright (c) 2003-present, Jodd Team (http://jodd.org) +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +package jodd.vtor.constraint; + +import org.junit.Test; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.stub; + +public class WildcardMatchConstraintTest extends ConstraintTestBase { + + @Test + public void testConstructor1() { + WildcardMatchConstraint wildcardMatchConstraint = new WildcardMatchConstraint(); + assertNull("pattern value must be null by default", wildcardMatchConstraint.getPattern()); + } + + @Test + public void testConstructor2() { + String pattern = "foo"; + WildcardMatchConstraint wildcardMatchConstraint = new WildcardMatchConstraint(pattern); + assertEquals("pattern must be the same as was given to constructor", wildcardMatchConstraint.getPattern(), pattern); + } + + @Test + public void testSetPattern() { + WildcardMatchConstraint wildcardMatchConstraint = new WildcardMatchConstraint(); + String pattern = "foo"; + wildcardMatchConstraint.setPattern(pattern); + assertEquals("method must return the same pattern as was given to set method", wildcardMatchConstraint.getPattern(), pattern); + } + + @Test + public void testConfigure() { + WildcardMatchConstraint wildcardMatchConstraint = new WildcardMatchConstraint(); + WildcardMatch annotation = mock(WildcardMatch.class); + String pattern = "foo"; + stub(annotation.value()).toReturn(pattern); + wildcardMatchConstraint.configure(annotation); + assertEquals("method must return the same pattern as was set to annotation when configure", wildcardMatchConstraint.getPattern(), pattern); + } + + @Test + public void testValidate_WithValIsNull() { + assertTrue("result must be true when validate null value", WildcardMatchConstraint.validate(null, "*")); + } + + @Test + public void testIsValid() { + assertTrue(new WildcardMatchConstraint("a?c").isValid(mockContext(), "abc")); + assertFalse(new WildcardMatchConstraint("axc").isValid(mockContext(), "abc")); + } +} \ No newline at end of file diff --git a/jodd-vtor/src/test/java/jodd/vtor/constraint/WildcardPathMatchConstraintTest.java b/jodd-vtor/src/test/java/jodd/vtor/constraint/WildcardPathMatchConstraintTest.java new file mode 100644 index 000000000..1aecea84e --- /dev/null +++ b/jodd-vtor/src/test/java/jodd/vtor/constraint/WildcardPathMatchConstraintTest.java @@ -0,0 +1,78 @@ +// Copyright (c) 2003-present, Jodd Team (http://jodd.org) +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. + +package jodd.vtor.constraint; + +import org.junit.Test; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.stub; + +public class WildcardPathMatchConstraintTest extends ConstraintTestBase { + + @Test + public void testConstructor1() { + WildcardPathMatchConstraint wildcardPathMatchConstraint = new WildcardPathMatchConstraint(); + assertNull(wildcardPathMatchConstraint.getPattern()); + } + + @Test + public void testConstructor2() { + WildcardPathMatchConstraint wildcardPathMatchConstraint = new WildcardPathMatchConstraint("foo"); + assertEquals(wildcardPathMatchConstraint.getPattern(), "foo"); + } + + @Test + public void testSetPattern() { + WildcardPathMatchConstraint wildcardPathMatchConstraint = new WildcardPathMatchConstraint(); + String pattern = "foo"; + wildcardPathMatchConstraint.setPattern(pattern); + + assertEquals("method must return the same pattern as was given to set method", wildcardPathMatchConstraint.getPattern(), pattern); + } + + @Test + public void testConfigure() { + WildcardPathMatchConstraint wildcardPathMatchConstraint = new WildcardPathMatchConstraint(); + WildcardPathMatch annotation = mock(WildcardPathMatch.class); + String pattern = "foo"; + stub(annotation.value()).toReturn(pattern); + + wildcardPathMatchConstraint.configure(annotation); + assertEquals("method must return the same pattern as was set to annotation when configure", wildcardPathMatchConstraint.getPattern(), pattern); + } + + @Test + public void testValidate_WithValIsNull() { + assertTrue(WildcardPathMatchConstraint.validate(null, "*")); + } + + @Test + public void testIsValid() { + assertTrue(new WildcardPathMatchConstraint("/dir/**").isValid(mockContext(), "/dir/abc")); + assertFalse(new WildcardPathMatchConstraint("/dir/abz").isValid(mockContext(), "/dir/abc")); + } +} \ No newline at end of file From 7afb21f3861dab7769ba219cd6f09c42aed33091 Mon Sep 17 00:00:00 2001 From: Vladislav Medvedev Date: Sun, 3 Jan 2016 18:22:12 +0700 Subject: [PATCH 2/3] =?UTF-8?q?Fixed=20a=20failing=20test=20=E2=80=9CtestU?= =?UTF-8?q?seProfiles=5FwithTwoDifferentValues=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jodd-vtor/src/test/java/jodd/vtor/VtorTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/jodd-vtor/src/test/java/jodd/vtor/VtorTest.java b/jodd-vtor/src/test/java/jodd/vtor/VtorTest.java index 86d3b01e2..38ab09bda 100644 --- a/jodd-vtor/src/test/java/jodd/vtor/VtorTest.java +++ b/jodd-vtor/src/test/java/jodd/vtor/VtorTest.java @@ -104,7 +104,7 @@ public void testUseProfile_withTwoDifferentValues() throws Exception { Vtor vtor = new Vtor(); vtor.useProfile("testProfile"); assertEquals("size of list with profiles must be 1 when add one profile", vtor.enabledProfiles.size(), 1); - assertEquals("first element of enabled profiles must be testProfile when use testProfile", vtor.enabledProfiles.iterator().next(), "testProfile"); + assertTrue("first element of enabled profiles must be testProfile when use testProfile", new ArrayList(vtor.enabledProfiles).contains("testProfile")); //when vtor.useProfile(null); @@ -126,9 +126,9 @@ public void testUseProfiles_withTwoDifferentValues() throws Exception { Vtor vtor = new Vtor(); vtor.useProfiles("testProfile1", "testProfile2"); assertEquals("size of list with profiles must be 2 when add two profile", vtor.enabledProfiles.size(), 2); - Iterator resultIterator = vtor.enabledProfiles.iterator(); - assertEquals("first element must be equal to first added profile", resultIterator.next(), "testProfile1"); - assertEquals("second element must be equal to second added profile", resultIterator.next(), "testProfile2"); + ArrayList enabledProfileList = new ArrayList(vtor.enabledProfiles); + assertTrue("first element must be equal to first added profile", enabledProfileList.contains("testProfile1")); + assertTrue("second element must be equal to second added profile", enabledProfileList.contains("testProfile2")); //when vtor.useProfile(null); From d9a2d0b5da61837136b3398d701c734e73e02aa2 Mon Sep 17 00:00:00 2001 From: Vladislav Medvedev Date: Sun, 3 Jan 2016 19:16:20 +0700 Subject: [PATCH 3/3] =?UTF-8?q?Fixed=20a=20failing=20test=20=E2=80=9CtestU?= =?UTF-8?q?seProfiles=5FwithNullValue=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jodd-vtor/src/test/java/jodd/vtor/VtorTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jodd-vtor/src/test/java/jodd/vtor/VtorTest.java b/jodd-vtor/src/test/java/jodd/vtor/VtorTest.java index 24d1a4522..48f673654 100644 --- a/jodd-vtor/src/test/java/jodd/vtor/VtorTest.java +++ b/jodd-vtor/src/test/java/jodd/vtor/VtorTest.java @@ -128,7 +128,7 @@ public void testUseProfile_withTwoDifferentValues() throws Exception { @Test public void testUseProfiles_withNullValue() throws Exception { Vtor vtor = new Vtor(); - vtor.useProfiles((String) null); + vtor.useProfiles(null); assertNull("list of enabled profiles must be null when add only null profile", vtor.enabledProfiles); }