Elffuss BraidHub

kikocis/java-driver public

77caed65d1a2 · 3.1 KiBRawPermalink
1/*
2 * Copyright (C) 2012-2015 DataStax Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.datastax.driver.mapping;
17
18import com.datastax.driver.core.CCMBridge;
19import com.datastax.driver.mapping.annotations.Enumerated;
20import com.datastax.driver.mapping.annotations.PartitionKey;
21import com.datastax.driver.mapping.annotations.Table;
22import com.google.common.collect.Lists;
23import org.testng.annotations.Test;
24
25import java.util.Collection;
26
27import static org.junit.Assert.assertNull;
28
29/**
30 * Covers handling of enum fields by the mapper.
31 */
32public class MapperEnumTest extends CCMBridge.PerClassSingleNodeCluster {
33
34 @Override
35 protected Collection<String> getTableDefinitions() {
36 return Lists.newArrayList("CREATE TABLE asOrdinal (k int primary key, v int)",
37 "CREATE TABLE asString (k int primary key, v text)");
38 }
39
40 public enum Enum {
41 FOO, BAR
42 }
43
44 @Test(groups = "short")
45 public void should_handle_null_enum_as_ordinal() {
46 Mapper<AsOrdinal> mapper = new MappingManager(session).mapper(AsOrdinal.class);
47
48 mapper.save(new AsOrdinal(1, null));
49 AsOrdinal o = mapper.get(1);
50
51 assertNull(o.getV());
52 }
53
54 @Test(groups = "short")
55 public void should_handle_null_enum_as_string() {
56 Mapper<AsString> mapper = new MappingManager(session).mapper(AsString.class);
57
58 mapper.save(new AsString(1, null));
59 AsString o = mapper.get(1);
60
61 assertNull(o.getV());
62 }
63
64 @Table(name = "asOrdinal")
65 public static class AsOrdinal {
66 @PartitionKey
67 private int k;
68
69 @Enumerated(EnumType.ORDINAL)
70 private Enum v;
71
72 public AsOrdinal(Integer k, Enum v) {
73 this.k = k;
74 this.v = v;
75 }
76
77 public AsOrdinal() {
78 }
79
80 public int getK() {
81 return k;
82 }
83
84 public void setK(int k) {
85 this.k = k;
86 }
87
88 public Enum getV() {
89 return v;
90 }
91
92 public void setV(Enum v) {
93 this.v = v;
94 }
95 }
96
97 @Table(name = "asString")
98 public static class AsString {
99 @PartitionKey
100 private int k;
101
102 @Enumerated(EnumType.STRING)
103 private Enum v;
104
105 public AsString(Integer k, Enum v) {
106 this.k = k;
107 this.v = v;
108 }
109
110 public AsString() {
111 }
112
113 public int getK() {
114 return k;
115 }
116
117 public void setK(int k) {
118 this.k = k;
119 }
120
121 public Enum getV() {
122 return v;
123 }
124
125 public void setV(Enum v) {
126 this.v = v;
127 }
128 }
129}