|
| 1 | +package com.flowingcode.vaadin.addons.twincolgrid; |
| 2 | + |
| 3 | +import org.junit.Assert; |
| 4 | +import org.junit.Test; |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.LinkedHashSet; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Set; |
| 9 | + |
| 10 | +public class TwinColGridListAdapterTest { |
| 11 | + |
| 12 | + @Test |
| 13 | + public void testGetValueReturnsModifiableListAndDoesNotAffectInternalState() { |
| 14 | + // 1. Instantiate a TwinColGrid<String> |
| 15 | + TwinColGrid<String> twinColGrid = new TwinColGrid<>(); |
| 16 | + |
| 17 | + // 2. Instantiate TwinColGridListAdapter<String> with the created TwinColGrid |
| 18 | + TwinColGridListAdapter<String> adapter = new TwinColGridListAdapter<>(twinColGrid); |
| 19 | + |
| 20 | + // 3. Add a few initial items (e.g., "Vixen", "Comet") to the TwinColGrid's selection. |
| 21 | + Set<String> initialSelection = new LinkedHashSet<>(Arrays.asList("Vixen", "Comet")); |
| 22 | + twinColGrid.setValue(initialSelection); |
| 23 | + |
| 24 | + // 4. Call getValue() on the TwinColGridListAdapter instance. |
| 25 | + List<String> listFromAdapter = adapter.getValue(); |
| 26 | + |
| 27 | + // 5. Assert that the returned List<String> is not null and contains the initial items. |
| 28 | + Assert.assertNotNull("The list returned by getValue() should not be null.", listFromAdapter); |
| 29 | + Assert.assertEquals("The list should contain 2 items.", 2, listFromAdapter.size()); |
| 30 | + Assert.assertTrue("The list should contain 'Vixen'.", listFromAdapter.contains("Vixen")); |
| 31 | + Assert.assertTrue("The list should contain 'Comet'.", listFromAdapter.contains("Comet")); |
| 32 | + |
| 33 | + // 6. Attempt to add a new item (e.g., "Cupid") to the list obtained in step 4. |
| 34 | + // Verify that this operation is successful (no exception thrown) and the list now contains "Cupid". |
| 35 | + boolean added = false; |
| 36 | + try { |
| 37 | + added = listFromAdapter.add("Cupid"); |
| 38 | + } catch (UnsupportedOperationException e) { |
| 39 | + Assert.fail("The list should be modifiable, but add operation threw UnsupportedOperationException."); |
| 40 | + } |
| 41 | + Assert.assertTrue("The add operation should return true, indicating the list was modified.", added); |
| 42 | + Assert.assertEquals("The list from adapter should now contain 3 items.", 3, listFromAdapter.size()); |
| 43 | + Assert.assertTrue("The list from adapter should now contain 'Cupid'.", listFromAdapter.contains("Cupid")); |
| 44 | + |
| 45 | + // 7. Call getValue() on the TwinColGridListAdapter again |
| 46 | + List<String> secondListFromAdapter = adapter.getValue(); |
| 47 | + |
| 48 | + // 8. Assert that this second list *only* contains the original items ("Vixen", "Comet") |
| 49 | + // and *does not* contain "Cupid". |
| 50 | + Assert.assertNotNull("The second list returned by getValue() should not be null.", secondListFromAdapter); |
| 51 | + Assert.assertEquals("The second list should still contain 2 items (original state).", 2, secondListFromAdapter.size()); |
| 52 | + Assert.assertTrue("The second list should contain 'Vixen'.", secondListFromAdapter.contains("Vixen")); |
| 53 | + Assert.assertTrue("The second list should contain 'Comet'.", secondListFromAdapter.contains("Comet")); |
| 54 | + Assert.assertFalse("The second list should NOT contain 'Cupid'.", secondListFromAdapter.contains("Cupid")); |
| 55 | + |
| 56 | + // Also, verify the underlying TwinColGrid's value directly |
| 57 | + Set<String> twinColGridValue = twinColGrid.getValue(); |
| 58 | + Assert.assertNotNull("The TwinColGrid's value should not be null.", twinColGridValue); |
| 59 | + Assert.assertEquals("The TwinColGrid's value should contain 2 items.", 2, twinColGridValue.size()); |
| 60 | + Assert.assertTrue("The TwinColGrid's value should contain 'Vixen'.", twinColGridValue.contains("Vixen")); |
| 61 | + Assert.assertTrue("The TwinColGrid's value should contain 'Comet'.", twinColGridValue.contains("Comet")); |
| 62 | + Assert.assertFalse("The TwinColGrid's value should NOT contain 'Cupid'.", twinColGridValue.contains("Cupid")); |
| 63 | + } |
| 64 | +} |
0 commit comments