@Test public void testAddAllAtIndexFails() throws Exception { EncodedColumnQualiferCellsList list = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); populateList(list); try { list.addAll(0, new ArrayList<Cell>()); } catch (UnsupportedOperationException expected) { } }
@Override public Tuple next() throws SQLException { // XXX: No access here to the region instance to enclose this with startRegionOperation / // stopRegionOperation synchronized (scanner) { try { // TODO: size List<Cell> results = useQualifierAsIndex ? new EncodedColumnQualiferCellsList(minMaxQualifiers.getFirst(), minMaxQualifiers.getSecond(), encodingScheme) : new ArrayList<Cell>(); // Results are potentially returned even when the return value of s.next is false // since this is an indication of whether or not there are more values after the // ones returned boolean hasMore = scanner.nextRaw(results); if (!hasMore && results.isEmpty()) { return null; } // We instantiate a new tuple because in all cases currently we hang on to it // (i.e. to compute and hold onto the TopN). Tuple tuple = useQualifierAsIndex ? new PositionBasedMultiKeyValueTuple() : new MultiKeyValueTuple(); tuple.setKeyValues(results); return tuple; } catch (IOException e) { throw ServerUtil.parseServerException(e); } } }
@Test public void testAddAll() throws Exception { EncodedColumnQualiferCellsList list1 = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); populateList(list1); EncodedColumnQualiferCellsList list2 = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); populateList(list2); /* * Note that we don't care about equality of the element being added with the element already * present at the index. */ assertTrue(list1.addAll(list2)); }
@Test public void testSetNull() throws Exception { EncodedColumnQualiferCellsList list = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); try { list.add(null); fail("Adding null elements to the list is not allowed"); } catch (NullPointerException expected) { } }
@Test public void testToArrayWithoutParam() throws Exception { EncodedColumnQualiferCellsList list = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); Cell[] cells = new Cell[7]; populateListAndArray(list, cells); Object[] array = list.toArray(); assertTrue(Arrays.equals(cells, array)); }
@Test public void testToArrayWithParam() throws Exception { EncodedColumnQualiferCellsList list = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); Cell[] cells = new Cell[7]; populateListAndArray(list, cells); Cell[] array = list.toArray(new Cell[0]); assertTrue(Arrays.equals(cells, array)); }
@Test public void testGetIndex() throws Exception { EncodedColumnQualiferCellsList list = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); Cell[] cells = new Cell[7]; populateListAndArray(list, cells); for (int i = 0; i < cells.length; i++) { assertEquals(cells[i], list.get(i)); } }
@Test public void testLastIndexOf() throws Exception { EncodedColumnQualiferCellsList list = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); Cell[] cells = new Cell[7]; populateListAndArray(list, cells); for (int i = 0; i < cells.length; i++) { assertEquals(i, list.lastIndexOf(cells[i])); } }
@Test public void testIndexOf() throws Exception { EncodedColumnQualiferCellsList list = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); Cell[] cells = new Cell[7]; populateListAndArray(list, cells); for (int i = 0; i < cells.length; i++) { assertEquals(i, list.indexOf(cells[i])); } }
@Test public void testRemoveAll() throws Exception { EncodedColumnQualiferCellsList list1 = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); populateList(list1); ArrayList<Cell> list2 = new ArrayList<>(); populateList(list2); assertTrue(list1.removeAll(list2)); assertTrue(list1.isEmpty()); assertFalse(list2.isEmpty()); }
@Test public void testFailFastListIterator() throws Exception { EncodedColumnQualiferCellsList list = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); populateList(list); ListIterator<Cell> itr = list.listIterator(); itr.next(); list.add(KeyValueUtil.createFirstOnRow(row, cf, FOUR_BYTE_QUALIFIERS.encode(0))); try { itr.next(); fail("ConcurrentModificationException should have been thrown as the list was modified without using iterator"); } catch (ConcurrentModificationException expected) { } list = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); populateList(list); itr = list.listIterator(); itr.next(); itr.next(); itr.remove(); itr.next(); list.remove(KeyValueUtil.createFirstOnRow(row, cf, FOUR_BYTE_QUALIFIERS.encode(0))); try { itr.next(); fail("ConcurrentModificationException should have been thrown as the list was modified without using iterator"); } catch (ConcurrentModificationException expected) { } }
@Test public void testSize() { EncodedColumnQualiferCellsList list = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); assertEquals(0, list.size()); populateList(list); assertEquals(7, list.size()); int originalSize = list.size(); Iterator itr = list.iterator(); while (itr.hasNext()) { itr.next(); itr.remove(); assertEquals(--originalSize, list.size()); } }
@Test public void testIsEmpty() throws Exception { EncodedColumnQualiferCellsList list = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); assertTrue(list.isEmpty()); populateList(list); assertFalse(list.isEmpty()); Iterator itr = list.iterator(); while (itr.hasNext()) { itr.next(); itr.remove(); if (itr.hasNext()) { assertFalse(list.isEmpty()); } } assertTrue(list.isEmpty()); }
@Test public void testListIterator() throws Exception { EncodedColumnQualiferCellsList list = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); Cell[] cells = new Cell[7]; int i = 0;
@Test public void testFailFastIterator() throws Exception { EncodedColumnQualiferCellsList list = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); populateList(list); int i = 0; Iterator<Cell> itr = list.iterator(); while (itr.hasNext()) { i++; try { itr.next(); list.add(KeyValueUtil.createFirstOnRow(row, cf, FOUR_BYTE_QUALIFIERS.encode(0))); if (i == 2) { fail("ConcurrentModificationException should have been thrown as the list is being modified while being iterated through"); } } catch (ConcurrentModificationException expected) { assertEquals("Exception should have been thrown when getting the second element", 2, i); break; } } }
@Test public void testClear() throws Exception { EncodedColumnQualiferCellsList list = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); populateList(list); list.clear(); assertTrue(list.isEmpty()); assertEquals(0, list.size()); }
@Test public void testContains() throws Exception { EncodedColumnQualiferCellsList list = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); Cell[] cells = new Cell[7]; populateListAndArray(list, cells); for (Cell c : cells) { assertTrue(list.contains(c)); } assertFalse(list.contains(KeyValueUtil.createFirstOnRow(row, cf, FOUR_BYTE_QUALIFIERS.encode(13)))); }
@Test public void testRetainAll() throws Exception { EncodedColumnQualiferCellsList list1 = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); populateList(list1); EncodedColumnQualiferCellsList list2 = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); populateList(list2); // retainAll won't be modifying the list1 since they both have the same elements equality wise assertFalse(list1.retainAll(list2)); list2.remove(KeyValueUtil.createFirstOnRow(row, cf, FOUR_BYTE_QUALIFIERS.encode(12))); assertTrue(list1.retainAll(list2)); assertEquals(list1.size(), list2.size()); for (Cell c : list1) { assertTrue(list2.contains(c)); } }
@Test public void testContainsAll() throws Exception { EncodedColumnQualiferCellsList list1 = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); populateList(list1); EncodedColumnQualiferCellsList list2 = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); populateList(list2); assertTrue(list1.containsAll(list2)); list2.remove(KeyValueUtil.createFirstOnRow(row, cf, FOUR_BYTE_QUALIFIERS.encode(11))); assertTrue(list1.containsAll(list2)); assertFalse(list2.containsAll(list1)); list2.add(KeyValueUtil.createFirstOnRow(row, cf, FOUR_BYTE_QUALIFIERS.encode(13))); assertFalse(list1.containsAll(list2)); assertFalse(list2.containsAll(list1)); List<Cell> arrayList = new ArrayList<>(); populateList(arrayList); assertTrue(list1.containsAll(arrayList)); }
@Test public void testRemove() throws Exception { EncodedColumnQualiferCellsList list = new EncodedColumnQualiferCellsList(11, 16, FOUR_BYTE_QUALIFIERS); Cell[] cells = new Cell[7]; populateListAndArray(list, cells); assertTrue(list.remove(cells[0])); assertEquals(6, list.size()); assertTrue(list.remove(cells[6])); assertEquals(5, list.size()); assertTrue(list.remove(cells[3])); assertEquals(4, list.size()); assertFalse(list.remove(KeyValueUtil.createFirstOnRow(row, cf, FOUR_BYTE_QUALIFIERS.encode(13)))); assertEquals(4, list.size()); }