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.

IntegerValidatorTest.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.Before;
  19. import org.junit.Test;
  20. import org.junit.runner.RunWith;
  21. import org.mockito.runners.MockitoJUnitRunner;
  22. import static org.junit.Assert.assertEquals;
  23. import static org.junit.Assert.assertFalse;
  24. import static org.junit.Assert.assertTrue;
  25. @RunWith(MockitoJUnitRunner.class)
  26. public class IntegerValidatorTest {
  27. private IntegerValidator instance;
  28. @Before
  29. public void setUp() throws Exception {
  30. instance = new IntegerValidator(5, 10);
  31. }
  32. @Test(expected = IllegalArgumentException.class)
  33. public void testInvalidConstruction() throws Exception {
  34. instance = new IntegerValidator(10, 5);
  35. }
  36. @Test
  37. public void testPlaceholderMinimum() throws Exception {
  38. instance = new IntegerValidator(-1, 10);
  39. assertEquals(Integer.MIN_VALUE, instance.getMin());
  40. }
  41. @Test
  42. public void testPlaceholderMaximum() throws Exception {
  43. instance = new IntegerValidator(5, -1);
  44. assertEquals(Integer.MAX_VALUE, instance.getMax());
  45. }
  46. @Test
  47. public void testGetMax() throws Exception {
  48. assertEquals(5, instance.getMin());
  49. }
  50. @Test
  51. public void testGetMin() throws Exception {
  52. assertEquals(10, instance.getMax());
  53. }
  54. @Test
  55. public void testValidate_Less_Reason() throws Exception {
  56. assertEquals("Must be at least 5", instance.validate(4).getFailureReason());
  57. }
  58. @Test
  59. public void testValidate_LessThanMin() throws Exception {
  60. assertTrue(instance.validate(4).isFailure());
  61. }
  62. @Test
  63. public void testValidate_Min() throws Exception {
  64. assertFalse(instance.validate(5).isFailure());
  65. }
  66. @Test
  67. public void testValidate_Max() throws Exception {
  68. assertFalse(instance.validate(10).isFailure());
  69. }
  70. @Test
  71. public void testValidate_MoreThanMax() throws Exception {
  72. assertTrue(instance.validate(11).isFailure());
  73. }
  74. @Test
  75. public void testValidate_Max_Reason() throws Exception {
  76. assertEquals("Must be at most 10", instance.validate(11).getFailureReason());
  77. }
  78. @Test
  79. public void testValidate_Normal() throws Exception {
  80. assertFalse(instance.validate(6).isFailure());
  81. }
  82. }