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.

VetoableListSelectionModelTest.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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.addons.ui_swing.components.vetoable;
  23. import java.beans.PropertyChangeEvent;
  24. import java.beans.PropertyVetoException;
  25. import java.beans.VetoableChangeListener;
  26. import javax.swing.event.ListSelectionEvent;
  27. import javax.swing.event.ListSelectionListener;
  28. import org.junit.Test;
  29. import org.junit.runner.RunWith;
  30. import org.mockito.Mock;
  31. import org.mockito.runners.MockitoJUnitRunner;
  32. import static javax.swing.ListSelectionModel.SINGLE_SELECTION;
  33. import static org.junit.Assert.assertEquals;
  34. import static org.junit.Assert.assertFalse;
  35. import static org.junit.Assert.assertTrue;
  36. import static org.mockito.Matchers.any;
  37. import static org.mockito.Mockito.doThrow;
  38. import static org.mockito.Mockito.never;
  39. import static org.mockito.Mockito.verify;
  40. @RunWith(MockitoJUnitRunner.class)
  41. public class VetoableListSelectionModelTest {
  42. @Mock private VetoableChangeListener vetoListener;
  43. @Mock private ListSelectionListener selectionListener;
  44. @Test
  45. public void testInitialState() {
  46. final VetoableListSelectionModel instance = new VetoableListSelectionModel();
  47. assertEquals(-1, instance.getLeadSelectionIndex());
  48. assertEquals(SINGLE_SELECTION, instance.getSelectionMode());
  49. assertFalse(instance.getValueIsAdjusting());
  50. }
  51. @Test
  52. public void testNonVetoed() {
  53. final VetoableListSelectionModel instance = new VetoableListSelectionModel();
  54. instance.setLeadSelectionIndex(5);
  55. assertEquals(5, instance.getLeadSelectionIndex());
  56. }
  57. @Test
  58. public void testVetoedCalled() throws PropertyVetoException {
  59. final VetoableListSelectionModel instance = new VetoableListSelectionModel();
  60. instance.addVetoableSelectionListener(vetoListener);
  61. instance.setLeadSelectionIndex(5);
  62. assertEquals(5, instance.getLeadSelectionIndex());
  63. verify(vetoListener).vetoableChange(any(PropertyChangeEvent.class));
  64. }
  65. @Test
  66. public void testVetoedCalledAndVetoed() throws PropertyVetoException {
  67. doThrow(new PropertyVetoException(null, null))
  68. .when(vetoListener).vetoableChange(any(PropertyChangeEvent.class));
  69. final VetoableListSelectionModel instance = new VetoableListSelectionModel();
  70. instance.addVetoableSelectionListener(vetoListener);
  71. instance.setLeadSelectionIndex(5);
  72. assertEquals(-1, instance.getLeadSelectionIndex());
  73. }
  74. @Test
  75. public void testListenerCalledWithoutVetoListener() {
  76. final VetoableListSelectionModel instance = new VetoableListSelectionModel();
  77. instance.addListSelectionListener(selectionListener);
  78. instance.setLeadSelectionIndex(5);
  79. assertEquals(5, instance.getLeadSelectionIndex());
  80. verify(selectionListener).valueChanged(any(ListSelectionEvent.class));
  81. }
  82. @Test
  83. public void testListenerCalledWithVetoListenerNoVeto() throws PropertyVetoException {
  84. final VetoableListSelectionModel instance = new VetoableListSelectionModel();
  85. instance.addListSelectionListener(selectionListener);
  86. instance.addVetoableSelectionListener(vetoListener);
  87. instance.setLeadSelectionIndex(5);
  88. assertEquals(5, instance.getLeadSelectionIndex());
  89. verify(vetoListener).vetoableChange(any(PropertyChangeEvent.class));
  90. verify(selectionListener).valueChanged(any(ListSelectionEvent.class));
  91. }
  92. @Test
  93. public void testListenerCalledWithVetoListenerWithVeto() throws PropertyVetoException {
  94. doThrow(new PropertyVetoException(null, null))
  95. .when(vetoListener).vetoableChange(any(PropertyChangeEvent.class));
  96. final VetoableListSelectionModel instance = new VetoableListSelectionModel();
  97. instance.addListSelectionListener(selectionListener);
  98. instance.addVetoableSelectionListener(vetoListener);
  99. instance.setLeadSelectionIndex(5);
  100. assertEquals(-1, instance.getLeadSelectionIndex());
  101. verify(vetoListener).vetoableChange(any(PropertyChangeEvent.class));
  102. verify(selectionListener, never()).valueChanged(any(ListSelectionEvent.class));
  103. }
  104. @Test
  105. public void testClearSelection() {
  106. final VetoableListSelectionModel instance = new VetoableListSelectionModel();
  107. instance.setLeadSelectionIndex(5);
  108. assertEquals(5, instance.getLeadSelectionIndex());
  109. instance.clearSelection();
  110. assertEquals(-1, instance.getLeadSelectionIndex());
  111. }
  112. @Test
  113. public void testIsSelectionEmpty() {
  114. final VetoableListSelectionModel instance = new VetoableListSelectionModel();
  115. assertTrue(instance.isSelectionEmpty());
  116. instance.setLeadSelectionIndex(5);
  117. assertFalse(instance.isSelectionEmpty());
  118. }
  119. @Test
  120. public void testIsSelectedIndex() {
  121. final VetoableListSelectionModel instance = new VetoableListSelectionModel();
  122. assertTrue(instance.isSelectedIndex(-1));
  123. assertFalse(instance.isSelectedIndex(5));
  124. instance.setLeadSelectionIndex(5);
  125. assertTrue(instance.isSelectedIndex(5));
  126. assertFalse(instance.isSelectedIndex(-1));
  127. }
  128. }