Repository: incubator-drill Updated Branches: refs/heads/master eeb716006 -> c4e1c58f3
DRILL-1640: DrillColumnMetaDataList does not implement List methods Project: http://git-wip-us.apache.org/repos/asf/incubator-drill/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-drill/commit/c4e1c58f Tree: http://git-wip-us.apache.org/repos/asf/incubator-drill/tree/c4e1c58f Diff: http://git-wip-us.apache.org/repos/asf/incubator-drill/diff/c4e1c58f Branch: refs/heads/master Commit: c4e1c58f3e418f7c13c7201aac6210ab76fe0e70 Parents: eeb7160 Author: Matt Burgess <mburg...@pentaho.com> Authored: Thu Nov 6 12:30:11 2014 -0500 Committer: Aditya Kishore <a...@apache.org> Committed: Thu Nov 6 11:29:12 2014 -0800 ---------------------------------------------------------------------- .../drill/jdbc/DrillColumnMetaDataList.java | 67 +++++- .../drill/jdbc/DrillColumnMetaDataListTest.java | 224 +++++++++++++++++++ 2 files changed, 285 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/c4e1c58f/exec/jdbc/src/main/java/org/apache/drill/jdbc/DrillColumnMetaDataList.java ---------------------------------------------------------------------- diff --git a/exec/jdbc/src/main/java/org/apache/drill/jdbc/DrillColumnMetaDataList.java b/exec/jdbc/src/main/java/org/apache/drill/jdbc/DrillColumnMetaDataList.java index 61afbfb..e75be70 100644 --- a/exec/jdbc/src/main/java/org/apache/drill/jdbc/DrillColumnMetaDataList.java +++ b/exec/jdbc/src/main/java/org/apache/drill/jdbc/DrillColumnMetaDataList.java @@ -18,6 +18,11 @@ package org.apache.drill.jdbc; import java.sql.ResultSetMetaData; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; import net.hydromatic.avatica.ColumnMetaData; import net.hydromatic.avatica.ColumnMetaData.AvaticaType; @@ -32,21 +37,21 @@ import org.apache.drill.exec.record.MaterializedField; public class DrillColumnMetaDataList extends BasicList<ColumnMetaData>{ static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(DrillColumnMetaDataList.class); - private ColumnMetaData[] columns = new ColumnMetaData[0]; + private List<ColumnMetaData> columns = new ArrayList<ColumnMetaData>(); @Override public int size() { - return columns.length; + return (columns.size()); } @Override public ColumnMetaData get(int index) { - return columns[index]; + return columns.get(index); } public void updateColumnMetaData(String catalogName, String schemaName, String tableName, BatchSchema schema){ - columns = new ColumnMetaData[schema.getFieldCount()]; + columns = new ArrayList<ColumnMetaData>(schema.getFieldCount()); for(int i = 0; i < schema.getFieldCount(); i++){ MaterializedField f = schema.getColumn(i); MajorType t = f.getType(); @@ -71,8 +76,8 @@ public class DrillColumnMetaDataList extends BasicList<ColumnMetaData>{ false, // writable false, // definitely writable "none" // column class name - ); - columns[i] =col; + ); + columns.add(col); } } @@ -159,4 +164,54 @@ public class DrillColumnMetaDataList extends BasicList<ColumnMetaData>{ return "?"; } + + @Override + public boolean contains(Object o) { + return columns.contains(o); + } + + @Override + public Iterator<ColumnMetaData> iterator() { + return columns.iterator(); + } + + @Override + public Object[] toArray() { + return columns.toArray(); + } + + @Override + public <T> T[] toArray(T[] a) { + return columns.toArray(a); + } + + @Override + public boolean containsAll(Collection<?> c) { + return columns.containsAll(c); + } + + @Override + public int indexOf(Object o) { + return columns.indexOf(o); + } + + @Override + public int lastIndexOf(Object o) { + return columns.lastIndexOf(o); + } + + @Override + public ListIterator<ColumnMetaData> listIterator() { + return columns.listIterator(); + } + + @Override + public ListIterator<ColumnMetaData> listIterator(int index) { + return columns.listIterator(index); + } + + @Override + public List<ColumnMetaData> subList(int fromIndex, int toIndex) { + return columns.subList(fromIndex, toIndex); + } } http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/c4e1c58f/exec/jdbc/src/test/java/org/apache/drill/jdbc/DrillColumnMetaDataListTest.java ---------------------------------------------------------------------- diff --git a/exec/jdbc/src/test/java/org/apache/drill/jdbc/DrillColumnMetaDataListTest.java b/exec/jdbc/src/test/java/org/apache/drill/jdbc/DrillColumnMetaDataListTest.java new file mode 100644 index 0000000..a6f2325 --- /dev/null +++ b/exec/jdbc/src/test/java/org/apache/drill/jdbc/DrillColumnMetaDataListTest.java @@ -0,0 +1,224 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.drill.jdbc; + +import net.hydromatic.avatica.ColumnMetaData; +import org.apache.drill.common.expression.SchemaPath; +import org.apache.drill.exec.record.BatchSchema; +import org.apache.drill.exec.record.MaterializedField; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +import java.util.Iterator; + +import static org.apache.drill.common.types.TypeProtos.MajorType; +import static org.apache.drill.common.types.TypeProtos.MinorType; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class DrillColumnMetaDataListTest { + + private DrillColumnMetaDataList emptyList; + + private DrillColumnMetaDataList oneElementList; + + private DrillColumnMetaDataList twoElementList; + + private ColumnMetaData exampleIntColumn = new ColumnMetaData( + 0, false, false, false, false, 0, true, 10, "intLabel", "intColName", "schemaName", + 0, 1, ",myTable", "myCategory", new ColumnMetaData.ScalarType( 1, "myIntType", ColumnMetaData.Rep.INTEGER ), + true, false, false, Integer.class.getName() ); + + private ColumnMetaData exampleStringColumn = new ColumnMetaData( + 0, false, false, false, false, 0, true, 10, "stringLabel", "stringColName", "schemaName", + 0, 1, ",myTable", "myCategory", new ColumnMetaData.ScalarType( 1, "myStringType", ColumnMetaData.Rep.STRING ), + false, true, true, String.class.getName() ); + + @Before + public void setUp() throws Exception { + emptyList = new DrillColumnMetaDataList(); + + // Create mock columns + final MaterializedField exampleIntField = mock(MaterializedField.class); + MajorType exampleIntType = MajorType.newBuilder().setMinorType(MinorType.INT).build(); + when(exampleIntField.getAsSchemaPath()).thenReturn(SchemaPath.getSimplePath("/path/to/testInt")); + when(exampleIntField.getType()).thenReturn(exampleIntType); + + final MaterializedField exampleStringField = mock(MaterializedField.class); + MajorType exampleStringType = MajorType.newBuilder().setMinorType(MinorType.VARCHAR).build(); + when(exampleStringField.getAsSchemaPath()).thenReturn(SchemaPath.getSimplePath("/path/to/testString")); + when(exampleStringField.getType()).thenReturn(exampleStringType); + + oneElementList = new DrillColumnMetaDataList(); + BatchSchema oneElementSchema = mock(BatchSchema.class); + when(oneElementSchema.getFieldCount()).thenReturn(1); + doAnswer( + new Answer<MaterializedField>() { + + @Override + public MaterializedField answer(InvocationOnMock invocationOnMock) throws Throwable { + Integer index = (Integer) invocationOnMock.getArguments()[0]; + if (index == 0) { + return exampleIntField; + } + return null; + } + } + ).when(oneElementSchema).getColumn(Mockito.anyInt()); + oneElementList.updateColumnMetaData("testCatalog", "testSchema", "testTable", oneElementSchema); + + twoElementList = new DrillColumnMetaDataList(); + BatchSchema twoElementSchema = mock(BatchSchema.class); + when(twoElementSchema.getFieldCount()).thenReturn(2); + doAnswer( + new Answer<MaterializedField>() { + + @Override + public MaterializedField answer(InvocationOnMock invocationOnMock) throws Throwable { + Integer index = (Integer) invocationOnMock.getArguments()[0]; + if (index == 0) { + return exampleIntField; + } else if (index == 1) { + return exampleStringField; + } + return null; + } + } + ).when(twoElementSchema).getColumn(Mockito.anyInt()); + twoElementList.updateColumnMetaData("testCatalog", "testSchema", "testTable", twoElementSchema); + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void testSize() throws Exception { + assertEquals("Default constructor should give empty list", 0, emptyList.size()); + assertEquals(1, oneElementList.size()); + assertEquals(2, twoElementList.size()); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testGetFromEmptyList() throws Exception { + emptyList.get(0); + } + + @Test + public void testGetFromNonEmptyList() throws Exception { + assertEquals(oneElementList.get(0).columnName, "/path/to/testInt"); + assertEquals(twoElementList.get(0).columnName, "/path/to/testInt"); + assertEquals(twoElementList.get(1).columnName, "/path/to/testString"); + } + + @Test + public void testUpdateColumnMetaData() throws Exception { + + } + + @Test + public void testIsEmpty() throws Exception { + assertTrue("Default constructor should give empty list", emptyList.isEmpty()); + assertFalse("One-element List should not be empty", oneElementList.isEmpty()); + assertFalse("Two-element List should not be empty", twoElementList.isEmpty()); + } + + @Test + public void testContains() throws Exception { + + assertFalse(emptyList.contains(exampleIntColumn)); + assertFalse(emptyList.contains(exampleStringColumn)); + + assertTrue(oneElementList.contains(oneElementList.get(0))); + assertFalse(oneElementList.contains(exampleStringColumn)); + + assertTrue(twoElementList.contains(twoElementList.get(0))); + assertTrue(twoElementList.contains(twoElementList.get(1))); + assertFalse(twoElementList.contains(exampleStringColumn)); + } + + @Test + public void testIterator() throws Exception { + assertFalse(emptyList.iterator().hasNext()); + + Iterator<ColumnMetaData> iterator1 = oneElementList.iterator(); + assertNotNull(iterator1); + assertTrue(iterator1.hasNext()); + assertEquals(iterator1.next(), oneElementList.get(0)); + assertFalse(iterator1.hasNext()); + + Iterator<ColumnMetaData> iterator2 = twoElementList.iterator(); + assertNotNull(iterator2); + assertTrue(iterator2.hasNext()); + assertEquals(iterator2.next(), twoElementList.get(0)); + assertTrue(iterator2.hasNext()); + assertEquals(iterator2.next(), twoElementList.get(1)); + assertFalse(iterator2.hasNext()); + } + + @Test + public void testToArray() throws Exception { + assertEquals(0, emptyList.toArray().length); + assertEquals(1, oneElementList.toArray().length); + assertEquals(2, twoElementList.toArray().length); + } + + @Test + public void testToArrayWithParam() throws Exception { + ColumnMetaData[] colArray0 = emptyList.toArray(new ColumnMetaData[] {}); + assertEquals(0, colArray0.length); + ColumnMetaData[] colArray1 = oneElementList.toArray(new ColumnMetaData[] {}); + assertEquals(1, colArray1.length); + ColumnMetaData[] colArray2 = twoElementList.toArray(new ColumnMetaData[] {}); + assertEquals(2, colArray2.length); + } + + @Test + public void testIndexOf() throws Exception { + assertEquals(-1, emptyList.indexOf(exampleIntColumn)); + assertEquals(-1, oneElementList.indexOf(exampleIntColumn)); + assertEquals(-1, twoElementList.indexOf(exampleIntColumn)); + + assertEquals(0, oneElementList.indexOf(oneElementList.get(0))); + assertEquals(0, twoElementList.indexOf(twoElementList.get(0))); + + assertEquals(1, twoElementList.indexOf(twoElementList.get(1))); + } + + @Test + public void testLastIndexOf() throws Exception { + assertEquals(-1, emptyList.lastIndexOf(exampleIntColumn)); + assertEquals(-1, oneElementList.lastIndexOf(exampleIntColumn)); + assertEquals(-1, twoElementList.lastIndexOf(exampleIntColumn)); + + assertEquals(0, oneElementList.lastIndexOf(oneElementList.get(0))); + assertEquals(0, twoElementList.lastIndexOf(twoElementList.get(0))); + + assertEquals(1, twoElementList.lastIndexOf(twoElementList.get(1))); + } +}