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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.util.validators;
  23. import org.junit.Before;
  24. import org.junit.Test;
  25. import org.junit.runner.RunWith;
  26. import org.mockito.runners.MockitoJUnitRunner;
  27. import static org.junit.Assert.assertEquals;
  28. import static org.junit.Assert.assertFalse;
  29. import static org.junit.Assert.assertTrue;
  30. @RunWith(MockitoJUnitRunner.class)
  31. public class IntegerValidatorTest {
  32. private IntegerValidator instance;
  33. @Before
  34. public void setUp() throws Exception {
  35. instance = new IntegerValidator(5, 10);
  36. }
  37. @Test(expected = IllegalArgumentException.class)
  38. public void testInvalidConstruction() throws Exception {
  39. instance = new IntegerValidator(10, 5);
  40. }
  41. @Test
  42. public void testPlaceholderMinimum() throws Exception {
  43. instance = new IntegerValidator(-1, 10);
  44. assertEquals(Integer.MIN_VALUE, instance.getMin());
  45. }
  46. @Test
  47. public void testPlaceholderMaximum() throws Exception {
  48. instance = new IntegerValidator(5, -1);
  49. assertEquals(Integer.MAX_VALUE, instance.getMax());
  50. }
  51. @Test
  52. public void testGetMax() throws Exception {
  53. assertEquals(5, instance.getMin());
  54. }
  55. @Test
  56. public void testGetMin() throws Exception {
  57. assertEquals(10, instance.getMax());
  58. }
  59. @Test
  60. public void testValidate_Less_Reason() throws Exception {
  61. assertEquals("Must be at least 5", instance.validate(4).getFailureReason());
  62. }
  63. @Test
  64. public void testValidate_LessThanMin() throws Exception {
  65. assertTrue(instance.validate(4).isFailure());
  66. }
  67. @Test
  68. public void testValidate_Min() throws Exception {
  69. assertFalse(instance.validate(5).isFailure());
  70. }
  71. @Test
  72. public void testValidate_Max() throws Exception {
  73. assertFalse(instance.validate(10).isFailure());
  74. }
  75. @Test
  76. public void testValidate_MoreThanMax() throws Exception {
  77. assertTrue(instance.validate(11).isFailure());
  78. }
  79. @Test
  80. public void testValidate_Max_Reason() throws Exception {
  81. assertEquals("Must be at most 10", instance.validate(11).getFailureReason());
  82. }
  83. @Test
  84. public void testValidate_Normal() throws Exception {
  85. assertFalse(instance.validate(6).isFailure());
  86. }
  87. }