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.

GenericTableModelTest.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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;
  23. import com.google.common.base.MoreObjects;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import java.util.Objects;
  27. import javax.swing.event.TableModelEvent;
  28. import javax.swing.event.TableModelListener;
  29. import org.junit.Before;
  30. import org.junit.Test;
  31. import org.junit.runner.RunWith;
  32. import org.mockito.ArgumentCaptor;
  33. import org.mockito.Captor;
  34. import org.mockito.Mock;
  35. import org.mockito.runners.MockitoJUnitRunner;
  36. import static org.junit.Assert.assertEquals;
  37. import static org.junit.Assert.assertFalse;
  38. import static org.junit.Assert.assertSame;
  39. import static org.junit.Assert.assertTrue;
  40. import static org.mockito.Mockito.verify;
  41. @RunWith(MockitoJUnitRunner.class)
  42. public class GenericTableModelTest {
  43. @Mock private TableModelListener listener;
  44. @Captor private ArgumentCaptor<TableModelEvent> tableModelEvent;
  45. private List<TestObject> values;
  46. private TestObject newObject;
  47. private GenericTableModel<TestObject> instance;
  48. private String[] headers;
  49. @Before
  50. public void setUp() throws Exception {
  51. values = new ArrayList<>();
  52. values.add(new TestObject("one", new NotAString("value-one")));
  53. values.add(new TestObject("two", new NotAString("value-two")));
  54. values.add(new TestObject("three", new NotAString("value-three")));
  55. values.add(new TestObject("four", new NotAString("value-four")));
  56. values.add(new TestObject("five", new NotAString("value-five")));
  57. newObject = new TestObject("RAR", new NotAString("RAR"));
  58. headers = new String[]{"getName", "getValue"};
  59. instance = new GenericTableModel<>(TestObject.class, headers);
  60. values.forEach(instance::addValue);
  61. instance.addTableModelListener(listener);
  62. }
  63. @Test
  64. public void testGetRowCount() throws Exception {
  65. assertEquals(values.size(), instance.getRowCount());
  66. }
  67. /// TODO: Row out of bounds
  68. @Test
  69. public void testGetColumnCount() throws Exception {
  70. assertEquals(headers.length, instance.getColumnCount());
  71. }
  72. // TODO: Column out of bounds
  73. @Test
  74. public void testGetColumnName() throws Exception {
  75. for (int i = 0; i < headers.length; i++) {
  76. assertEquals(headers[i], instance.getColumnName(i));
  77. }
  78. }
  79. // TODO: Get out of bounds column names
  80. @Test
  81. public void testGetColumnClass() throws Exception {
  82. assertSame(String.class, instance.getColumnClass(0));
  83. assertSame(NotAString.class, instance.getColumnClass(1));
  84. }
  85. // TODO: Get column out of bounds
  86. @Test
  87. public void testGetValueAt() throws Exception {
  88. for (int i = 0; i < values.size(); i++) {
  89. assertEquals(values.get(i).getName(), instance.getValueAt(i, 0));
  90. assertEquals(values.get(i).getValue(), instance.getValueAt(i, 1));
  91. }
  92. }
  93. // TODO: Get row and column out of bounds
  94. @Test
  95. public void testIsCellEditable() throws Exception {
  96. assertFalse(instance.isCellEditable(0, 0));
  97. assertFalse(instance.isCellEditable(0, 1));
  98. assertFalse(instance.isCellEditable(1, 0));
  99. assertFalse(instance.isCellEditable(1, 1));
  100. }
  101. // TODO: Test non default isEditable
  102. @Test
  103. public void testGetValue() throws Exception {
  104. for (int i = 0; i < values.size(); i++) {
  105. assertEquals(values.get(i), instance.getValue(i));
  106. }
  107. }
  108. // TODO: Test out of bounds
  109. @Test
  110. public void testGetIndex() throws Exception {
  111. for (int i = 0; i < values.size(); i++) {
  112. assertEquals(i, instance.getIndex(values.get(i)));
  113. }
  114. assertEquals(-1, instance.getIndex(new TestObject("", new NotAString(""))));
  115. assertEquals(-1, instance.getIndex(null));
  116. }
  117. @Test
  118. public void testSetHeaderNames() throws Exception {
  119. for (int i = 0; i < headers.length; i++) {
  120. assertEquals(headers[i], instance.getColumnName(i));
  121. }
  122. final String[] setHeaders = {"name", "value"};
  123. instance.setHeaderNames(setHeaders);
  124. for (int i = 0; i < setHeaders.length; i++) {
  125. assertEquals(setHeaders[i], instance.getColumnName(i));
  126. }
  127. // TODO: Check listener fired appropriately
  128. }
  129. // TODO: Set longer and shorter than
  130. @Test
  131. public void testSetValueAt() throws Exception {
  132. assertEquals(values.get(0).getName(), instance.getValueAt(0, 0));
  133. instance.setValueAt(newObject, 0, 0);
  134. assertEquals(values.get(0).getName(), instance.getValueAt(0, 0));
  135. verify(listener).tableChanged(tableModelEvent.capture());
  136. assertEquals(0, tableModelEvent.getValue().getType());
  137. assertEquals(0, tableModelEvent.getValue().getFirstRow());
  138. assertEquals(0, tableModelEvent.getValue().getLastRow());
  139. assertEquals(-1, tableModelEvent.getValue().getColumn());
  140. }
  141. // TODO: Test with non default edit function
  142. // TODO: Test with incorrect value type
  143. @Test
  144. public void testReplaceValueAt() throws Exception {
  145. assertEquals(values.get(0), instance.getValue(0));
  146. instance.replaceValueAt(newObject, 0);
  147. assertEquals(newObject, instance.getValue(0));
  148. // TODO: Check listener fired appropriately
  149. }
  150. @Test
  151. public void testRemoveValue() throws Exception {
  152. assertEquals(values.size(), instance.getRowCount());
  153. assertTrue(instance.getIndex(values.get(0)) != -1);
  154. instance.removeValue(values.get(0));
  155. assertEquals(values.size() -1, instance.getRowCount());
  156. assertEquals(-1, instance.getIndex(values.get(0)));
  157. // TODO: Check listener fired appropriately
  158. }
  159. // TODO: Test invalid value
  160. @Test
  161. public void testRemoveAll() throws Exception {
  162. assertEquals(values.size(), instance.getRowCount());
  163. instance.removeAll();
  164. // TODO: Check listener fired appropriately
  165. }
  166. @Test
  167. public void testAddValue() throws Exception {
  168. assertEquals(values.size(), instance.getRowCount());
  169. assertEquals(-1, instance.getIndex(newObject));
  170. instance.addValue(newObject);
  171. assertEquals(values.size() + 1, instance.getRowCount());
  172. assertEquals(values.size(), instance.getIndex(newObject));
  173. // TODO: Check listener fired appropriately
  174. }
  175. @Test
  176. public void testElements() throws Exception {
  177. assertEquals(values, new ArrayList<>(instance.elements()));
  178. }
  179. private static class TestObject {
  180. private String name;
  181. private NotAString value;
  182. public TestObject(final String name, final NotAString value) {
  183. this.name = name;
  184. this.value = value;
  185. }
  186. public String getName() {
  187. return name;
  188. }
  189. public void setName(final String name) {
  190. this.name = name;
  191. }
  192. public NotAString getValue() {
  193. return value;
  194. }
  195. public void setValue(final NotAString value) {
  196. this.value = value;
  197. }
  198. @Override
  199. public String toString() {
  200. return MoreObjects.toStringHelper(this)
  201. .add("Name", getName())
  202. .add("Value", getValue())
  203. .toString();
  204. }
  205. @Override
  206. public boolean equals(final Object object) {
  207. return object instanceof TestObject &&
  208. Objects.equals(getName(), ((TestObject) object).getName())
  209. && Objects.equals(getValue(), ((TestObject) object).getValue());
  210. }
  211. @Override
  212. public int hashCode() {
  213. return Objects.hash(getName(), getValue());
  214. }
  215. }
  216. private static class NotAString {
  217. private final String value;
  218. public NotAString(final String value) {
  219. this.value = value;
  220. }
  221. public String get() {
  222. return value;
  223. }
  224. @Override
  225. public String toString() {
  226. return MoreObjects.toStringHelper(this)
  227. .add("Value", get())
  228. .toString();
  229. }
  230. @Override
  231. public boolean equals(final Object object) {
  232. return object instanceof NotAString
  233. && Objects.equals(get(), ((NotAString) object).get());
  234. }
  235. @Override
  236. public int hashCode() {
  237. return Objects.hash(get());
  238. }
  239. }
  240. }