|
5 | 5 | import java.util.Map;
|
6 | 6 | import java.util.Set;
|
7 | 7 |
|
| 8 | +import com.datastax.spark.connector.japi.CassandraJavaUtil; |
| 9 | +import org.hamcrest.BaseMatcher; |
| 10 | +import org.hamcrest.Description; |
| 11 | +import org.hamcrest.Matcher; |
8 | 12 | import scala.reflect.api.TypeTags;
|
9 | 13 |
|
10 | 14 | import static com.datastax.spark.connector.japi.CassandraJavaUtil.*;
|
|
31 | 35 | @SuppressWarnings("unchecked")
|
32 | 36 | public class CassandraJavaUtilTest {
|
33 | 37 |
|
| 38 | + /** |
| 39 | + * Scala refelection type tags change the string reprsentation of some types, in scala 2.11 java.lang |
| 40 | + * is included, in scala 2.12 it is removed. To remove this conflict we just always remove the java.lang |
| 41 | + * portion |
| 42 | + */ |
| 43 | + private String removeJavaLang(String target) { |
| 44 | + return target.replaceAll("java.lang.", ""); |
| 45 | + } |
| 46 | + |
| 47 | + private final String STRING = removeJavaLang(String.class.getName()); |
| 48 | + private final String LIST_STRING = |
| 49 | + removeJavaLang(String.format("%s[%s]", List.class.getName(), String.class.getName())); |
| 50 | + private final String MAP_STRING_INT = |
| 51 | + removeJavaLang(String.format("%s[%s,%s]", Map.class.getName(), String.class.getName(), Integer.class.getName())); |
| 52 | + private final String LIST_SET_MAP_STRING_INT = |
| 53 | + removeJavaLang(String.format("%s[%s[%s[%s,%s]]]", List.class.getName(), Set.class.getName(), Map.class.getName(), String.class.getName(), Integer.class.getName())); |
| 54 | + |
34 | 55 | @Test
|
35 | 56 | public void testTypeTag1() throws Exception {
|
36 | 57 | TypeTags.TypeTag<String> tt = typeTag(String.class);
|
37 |
| - assertThat(tt.tpe().toString(), is(String.class.getName())); |
| 58 | + assertThat(removeJavaLang(tt.tpe().toString()), is(STRING)); |
38 | 59 | }
|
39 | 60 |
|
40 | 61 | @Test
|
41 | 62 | public void testTypeTag2() throws Exception {
|
42 | 63 | TypeTags.TypeTag<List> tt1 = typeTag(List.class, String.class);
|
43 |
| - assertThat(tt1.tpe().toString(), is(String.format("%s[%s]", |
44 |
| - List.class.getName(), String.class.getName()))); |
| 64 | + assertThat(removeJavaLang(removeJavaLang(tt1.tpe().toString())), is(LIST_STRING)); |
45 | 65 |
|
46 | 66 | TypeTags.TypeTag<Map> tt2 = typeTag(Map.class, String.class, Integer.class);
|
47 |
| - assertThat(tt2.tpe().toString(), is(String.format("%s[%s,%s]", |
48 |
| - Map.class.getName(), String.class.getName(), Integer.class.getName()))); |
| 67 | + assertThat(removeJavaLang(removeJavaLang(tt2.tpe().toString())), is(MAP_STRING_INT)); |
49 | 68 | }
|
50 | 69 |
|
51 | 70 | @Test
|
52 | 71 | public void testTypeTag3() throws Exception {
|
53 | 72 | TypeTags.TypeTag<List> tt = typeTag(List.class, typeTag(Set.class, typeTag(Map.class, typeTag(String.class), typeTag(Integer.class))));
|
54 |
| - assertThat(tt.tpe().toString(), is(String.format("%s[%s[%s[%s,%s]]]", |
55 |
| - List.class.getName(), Set.class.getName(), Map.class.getName(), String.class.getName(), Integer.class.getName()))); |
| 73 | + assertThat(removeJavaLang(tt.tpe().toString()), is(LIST_SET_MAP_STRING_INT)); |
56 | 74 | }
|
57 | 75 |
|
58 | 76 | @Test
|
59 | 77 | public void testTypeConverter1() throws Exception {
|
60 | 78 | TypeConverter<List<String>> tc = typeConverter(String.class);
|
61 |
| - assertThat(tc.targetTypeName(), is(String.class.getSimpleName())); |
| 79 | + assertThat(removeJavaLang(tc.targetTypeName()), is(STRING)); |
62 | 80 | }
|
63 | 81 |
|
64 | 82 | @Test
|
65 | 83 | public void testTypeConverter2() throws Exception {
|
66 | 84 | TypeConverter<List<String>> tc1 = typeConverter(List.class, String.class);
|
67 |
| - assertThat(tc1.targetTypeName(), is(String.format("%s[%s]", |
68 |
| - List.class.getName(), String.class.getSimpleName()))); |
| 85 | + assertThat(removeJavaLang(tc1.targetTypeName()), is(LIST_STRING)); |
69 | 86 |
|
70 | 87 | TypeConverter<Map<String, Integer>> tc2 = typeConverter(Map.class, String.class, Integer.class);
|
71 |
| - assertThat(tc2.targetTypeName(), is(String.format("%s[%s,%s]", |
72 |
| - Map.class.getName(), String.class.getSimpleName(), Integer.class.getName()))); |
| 88 | + assertThat(removeJavaLang(tc2.targetTypeName()), is(MAP_STRING_INT)); |
73 | 89 |
|
74 | 90 | }
|
75 | 91 |
|
76 | 92 | @Test
|
77 | 93 | public void testTypeConverter3() throws Exception {
|
78 | 94 | TypeConverter<List> tc = typeConverter(List.class, typeTag(Set.class, typeTag(Map.class, typeTag(String.class), typeTag(Integer.class))));
|
79 |
| - assertThat(tc.targetTypeName(), is(String.format("%s[%s[%s[%s,%s]]]", |
80 |
| - List.class.getName(), Set.class.getName(), Map.class.getName(), String.class.getSimpleName(), Integer.class.getName()))); |
| 95 | + assertThat(removeJavaLang(tc.targetTypeName()), is(LIST_SET_MAP_STRING_INT)); |
81 | 96 | }
|
82 | 97 |
|
83 | 98 | @Test
|
84 | 99 | public void testTypeConverter4() throws Exception {
|
85 | 100 | TypeTags.TypeTag<List> tt = typeTag(List.class, typeTag(Set.class, typeTag(Map.class, typeTag(String.class), typeTag(Integer.class))));
|
86 | 101 | TypeConverter<List> tc = typeConverter(tt);
|
87 |
| - assertThat(tc.targetTypeName(), is(String.format("%s[%s[%s[%s,%s]]]", |
88 |
| - List.class.getName(), Set.class.getName(), Map.class.getName(), String.class.getSimpleName(), Integer.class.getName()))); |
| 102 | + assertThat(removeJavaLang(tc.targetTypeName()), is(LIST_SET_MAP_STRING_INT)); |
89 | 103 | }
|
90 | 104 |
|
91 | 105 | @Test
|
|
0 commit comments