DATACASS-166: Throw IllegalArgumentException if a column is requested that isn't in the Cassandra table and merged pull request
This commit is contained in:
committed by
Matthew Adams
parent
ae607283f9
commit
a362d64d98
1
.gitignore
vendored
1
.gitignore
vendored
@@ -12,3 +12,4 @@ build
|
||||
.project
|
||||
.settings
|
||||
.cassandra
|
||||
.idea
|
||||
|
||||
@@ -34,7 +34,8 @@ public class ColumnReader {
|
||||
* Returns the row's column value.
|
||||
*/
|
||||
public Object get(String name) {
|
||||
return get(columns.getIndexOf(name));
|
||||
int indexOf = getColumnIndex(name);
|
||||
return get(indexOf);
|
||||
}
|
||||
|
||||
public Object get(int i) {
|
||||
@@ -110,7 +111,7 @@ public class ColumnReader {
|
||||
* @throws ClassCastException if the value cannot be converted to the requested type.
|
||||
*/
|
||||
public <T> T get(CqlIdentifier name, Class<T> requestedType) {
|
||||
return get(columns.getIndexOf(name.toCql()), requestedType);
|
||||
return get(getColumnIndex(name.toCql()), requestedType);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -139,4 +140,12 @@ public class ColumnReader {
|
||||
return (T) o;
|
||||
}
|
||||
|
||||
private int getColumnIndex(String name) {
|
||||
int indexOf = columns.getIndexOf(name);
|
||||
if (indexOf == -1) {
|
||||
throw new IllegalArgumentException("Column does not exist in Cassandra table: " + name);
|
||||
}
|
||||
return indexOf;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
package org.springframework.data.cassandra.test.unit.convert;
|
||||
|
||||
import com.datastax.driver.core.ColumnDefinitions;
|
||||
import com.datastax.driver.core.Row;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.cassandra.core.cql.CqlIdentifier;
|
||||
import org.springframework.data.cassandra.convert.ColumnReader;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class ColumnReaderTest {
|
||||
|
||||
public static final String NON_EXISTENT_COLUMN = "column_name";
|
||||
|
||||
@Mock
|
||||
private Row row;
|
||||
|
||||
@Mock
|
||||
private ColumnDefinitions columnDefinitions;
|
||||
|
||||
private ColumnReader underTest;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
given(row.getColumnDefinitions()).willReturn(columnDefinitions);
|
||||
underTest = new ColumnReader(row);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void throwsIllegalArgumentExceptionIfColumnDoesNotExistByName() throws Exception {
|
||||
given(columnDefinitions.contains(NON_EXISTENT_COLUMN)).willReturn(false);
|
||||
given(columnDefinitions.getIndexOf(NON_EXISTENT_COLUMN)).willReturn(-1);
|
||||
|
||||
try {
|
||||
underTest.get(NON_EXISTENT_COLUMN);
|
||||
fail("Expected illegal argument exception");
|
||||
} catch (IllegalArgumentException e) {
|
||||
assertEquals("Column does not exist in Cassandra table: " + NON_EXISTENT_COLUMN, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void throwsIllegalArgumentExceptionIfColumnDoesNotExistByCqlIdentifier() throws Exception {
|
||||
given(columnDefinitions.contains(NON_EXISTENT_COLUMN)).willReturn(false);
|
||||
given(columnDefinitions.getIndexOf(NON_EXISTENT_COLUMN)).willReturn(-1);
|
||||
|
||||
try {
|
||||
underTest.get(new CqlIdentifier(NON_EXISTENT_COLUMN));
|
||||
fail("Expected illegal argument exception");
|
||||
} catch (IllegalArgumentException e) {
|
||||
assertEquals("Column does not exist in Cassandra table: " + NON_EXISTENT_COLUMN, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void throwsIllegalArgumentExceptionIfColumnDoesNotExistByCqlIdentifierAndType() throws Exception {
|
||||
given(columnDefinitions.contains(NON_EXISTENT_COLUMN)).willReturn(false);
|
||||
given(columnDefinitions.getIndexOf(NON_EXISTENT_COLUMN)).willReturn(-1);
|
||||
|
||||
try {
|
||||
underTest.get(new CqlIdentifier(NON_EXISTENT_COLUMN), String.class);
|
||||
fail("Expected illegal argument exception");
|
||||
} catch (IllegalArgumentException e) {
|
||||
assertEquals("Column does not exist in Cassandra table: " + NON_EXISTENT_COLUMN, e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user