You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NumericalValidatorTest.java 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.util.validators;
  18. import org.junit.Test;
  19. import static org.junit.Assert.*;
  20. public class NumericalValidatorTest {
  21. @Test(expected=IllegalArgumentException.class)
  22. public void testConstructorThrowsExceptionForInvalidArguments() {
  23. new NumericalValidator(2, 1);
  24. }
  25. @Test
  26. public void testGetMax() {
  27. assertEquals(10, new NumericalValidator(1, 10).getMax());
  28. assertEquals(Integer.MAX_VALUE, new NumericalValidator(1, -1).getMax());
  29. }
  30. @Test
  31. public void testGetMin() {
  32. assertEquals(1, new NumericalValidator(1, 10).getMin());
  33. assertEquals(Integer.MIN_VALUE, new NumericalValidator(-1, 10).getMin());
  34. }
  35. @Test
  36. public void testNAN() {
  37. final ValidationResponse vr = new NumericalValidator(3, 5).validate("foo");
  38. assertTrue(vr.isFailure());
  39. assertTrue(vr.getFailureReason().contains("number"));
  40. }
  41. @Test
  42. public void testMin() {
  43. final Validator<String> nv1 = new NumericalValidator(-1, -1);
  44. final Validator<String> nv2 = new NumericalValidator(-5, -1);
  45. assertFalse(nv1.validate("-5").isFailure());
  46. assertFalse(nv2.validate("-5").isFailure());
  47. assertFalse(nv2.validate("-4").isFailure());
  48. assertFalse(nv2.validate("10").isFailure());
  49. assertTrue(nv2.validate("-6").isFailure());
  50. }
  51. @Test
  52. public void testMax() {
  53. final Validator<String> nv1 = new NumericalValidator(-1, -1);
  54. final Validator<String> nv2 = new NumericalValidator(-1, 10);
  55. assertFalse(nv1.validate("-5").isFailure());
  56. assertFalse(nv1.validate("50").isFailure());
  57. assertFalse(nv2.validate("-5").isFailure());
  58. assertFalse(nv2.validate("-4").isFailure());
  59. assertFalse(nv2.validate("10").isFailure());
  60. assertTrue(nv2.validate("11").isFailure());
  61. }
  62. }