Skip to content

Commit

Permalink
DRILL-3199 Implemented GenericAccessor.isNull(), added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Burgess authored and parthchandra committed Jun 29, 2015
1 parent c199860 commit dbd8925
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public GenericAccessor(ValueVector v) {

@Override
public boolean isNull(int index) {
throw new UnsupportedOperationException();
return v.getAccessor().isNull(index);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/**
* 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.exec.vector.accessor;

import org.apache.drill.exec.proto.UserBitShared;
import org.apache.drill.exec.vector.ValueVector;
import org.junit.Before;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class GenericAccessorTest {

public static final Object NON_NULL_VALUE = "Non-null value";

private GenericAccessor genericAccessor;
private ValueVector valueVector;
private ValueVector.Accessor accessor;
private UserBitShared.SerializedField metadata;

@Before
public void setUp() throws Exception {
// Set up a mock accessor that has two columns, one non-null and one null
accessor = mock(ValueVector.Accessor.class);
when(accessor.getObject(anyInt())).thenAnswer(new Answer<Object>() {

@Override
public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
Object[] args = invocationOnMock.getArguments();
Integer index = (Integer) args[0];
if(index == 0) {
return NON_NULL_VALUE;
}
if(index == 1) {
return null;
}
throw new IndexOutOfBoundsException("Index out of bounds");
}
});
when(accessor.isNull(anyInt())).thenAnswer(new Answer<Object>() {

@Override
public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
Object[] args = invocationOnMock.getArguments();
Integer index = (Integer) args[0];
if(index == 0) {
return false;
}
return true;
}
});

metadata = UserBitShared.SerializedField.getDefaultInstance();
valueVector = mock(ValueVector.class);
when(valueVector.getAccessor()).thenReturn(accessor);
when(valueVector.getMetadata()).thenReturn(metadata);

genericAccessor = new GenericAccessor(valueVector);
}

@Test
public void testIsNull() throws Exception {
assertFalse(genericAccessor.isNull(0));
assertTrue(genericAccessor.isNull(1));
}

@Test
public void testGetObject() throws Exception {
assertEquals(NON_NULL_VALUE, genericAccessor.getObject(0));
assertNull(genericAccessor.getObject(1));
}

@Test(expected=IndexOutOfBoundsException.class)
public void testGetObject_indexOutOfBounds() throws Exception {
genericAccessor.getObject(2);
}

@Test
public void testGetType() throws Exception {
assertEquals(UserBitShared.SerializedField.getDefaultInstance().getMajorType(), genericAccessor.getType());
}
}

0 comments on commit dbd8925

Please sign in to comment.