Elffuss BraidHub

kikocis/java-driver public

Improve error message when mapper can't determine keyspace.
Changeset349110a54a94d3c04bbde6a06a8cf2aadd0b8c7dbc15244d43d24e9ff27b6312
Authorolim7t <omichallat+github@gmail.com> · 2014-11-07 18:18 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2014-11-07 18:18 +01:00
Parents31b3bab3142a

Browse the files at this changeset

2 files changed

modified driver-mapping/src/main/java/com/datastax/driver/mapping/AnnotationParser.java +12 −0
@@ -61,6 +61,12 @@
6161
6262 if (Strings.isNullOrEmpty(table.keyspace())) {
6363 ksName = mappingManager.getSession().getLoggedKeyspace();
64+ if (Strings.isNullOrEmpty(ksName))
65+ throw new IllegalArgumentException(String.format(
66+ "Error creating mapper for class %s, the @%s annotation declares no default keyspace, and the session is not currently logged to any keyspace",
67+ entityClass.getSimpleName(),
68+ Table.class.getSimpleName()
69+ ));
6470 }
6571
6672 EntityMapper<T> mapper = factory.create(entityClass, ksName, tableName, writeConsistency, readConsistency);
@@ -113,6 +119,12 @@
113119
114120 if (Strings.isNullOrEmpty(udt.keyspace())) {
115121 ksName = mappingManager.getSession().getLoggedKeyspace();
122+ if (Strings.isNullOrEmpty(ksName))
123+ throw new IllegalArgumentException(String.format(
124+ "Error creating UDT mapper for class %s, the @%s annotation declares no default keyspace, and the session is not currently logged to any keyspace",
125+ udtClass.getSimpleName(),
126+ UDT.class.getSimpleName()
127+ ));
116128 }
117129
118130 EntityMapper<T> mapper = factory.create(udtClass, ksName, udtName, null, null);
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperDefaultKeyspaceTest.java +19 −0
@@ -17,6 +17,7 @@
1717
1818 import java.util.*;
1919
20+import com.datastax.driver.core.Session;
2021 import com.datastax.driver.mapping.annotations.*;
2122 import com.google.common.base.Objects;
2223 import org.testng.annotations.Test;
@@ -219,4 +220,22 @@
219220 m.delete(group);
220221 assertNull(m.get(groupId));
221222 }
223+
224+ @Test(groups = "short",
225+ expectedExceptions = IllegalArgumentException.class,
226+ expectedExceptionsMessageRegExp = "Error creating mapper for class Group, the @Table annotation declares no default keyspace, and the session is not currently logged to any keyspace")
227+ public void should_throw_a_meaningful_error_message_when_no_default_table_keyspace_and_session_not_logged() {
228+ Session session2 = cluster.connect();
229+ MappingManager manager = new MappingManager(session2);
230+ manager.mapper(Group.class);
231+ }
232+
233+ @Test(groups = "short",
234+ expectedExceptions = IllegalArgumentException.class,
235+ expectedExceptionsMessageRegExp = "Error creating UDT mapper for class GroupName, the @UDT annotation declares no default keyspace, and the session is not currently logged to any keyspace")
236+ public void should_throw_a_meaningful_error_message_when_no_default_udt_keyspace_and_session_not_logged() {
237+ Session session2 = cluster.connect();
238+ MappingManager manager = new MappingManager(session2);
239+ manager.udtMapper(GroupName.class);
240+ }
222241 }