Elffuss BraidHub

kikocis/java-driver public

b1d245a67292 · 3.0 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.CCMTestsSupport;
19import com.datastax.driver.mapping.annotations.Enumerated;
20import com.datastax.driver.mapping.annotations.PartitionKey;
21import com.datastax.driver.mapping.annotations.Table;
22import org.testng.annotations.Test;
23
24import static org.junit.Assert.assertNull;
25
26/**
27 * Covers handling of enum fields by the mapper.
28 */
29@SuppressWarnings("unused")
30public class MapperEnumTest extends CCMTestsSupport {
31
32 @Override
33 public void onTestContextInitialized() {
34 execute("CREATE TABLE asOrdinal (k int primary key, v int)",
35 "CREATE TABLE asString (k int primary key, v text)");
36 }
37
38 public enum Enum {
39 FOO, BAR
40 }
41
42 @Test(groups = "short")
43 public void should_handle_null_enum_as_ordinal() {
44 Mapper<AsOrdinal> mapper = new MappingManager(session()).mapper(AsOrdinal.class);
45
46 mapper.save(new AsOrdinal(1, null));
47 AsOrdinal o = mapper.get(1);
48
49 assertNull(o.getV());
50 }
51
52 @Test(groups = "short")
53 public void should_handle_null_enum_as_string() {
54 Mapper<AsString> mapper = new MappingManager(session()).mapper(AsString.class);
55
56 mapper.save(new AsString(1, null));
57 AsString o = mapper.get(1);
58
59 assertNull(o.getV());
60 }
61
62 @Table(name = "asOrdinal")
63 public static class AsOrdinal {
64 @PartitionKey
65 private int k;
66
67 @Enumerated(EnumType.ORDINAL)
68 private Enum v;
69
70 public AsOrdinal(Integer k, Enum v) {
71 this.k = k;
72 this.v = v;
73 }
74
75 public AsOrdinal() {
76 }
77
78 public int getK() {
79 return k;
80 }
81
82 public void setK(int k) {
83 this.k = k;
84 }
85
86 public Enum getV() {
87 return v;
88 }
89
90 public void setV(Enum v) {
91 this.v = v;
92 }
93 }
94
95 @Table(name = "asString")
96 public static class AsString {
97 @PartitionKey
98 private int k;
99
100 @Enumerated(EnumType.STRING)
101 private Enum v;
102
103 public AsString(Integer k, Enum v) {
104 this.k = k;
105 this.v = v;
106 }
107
108 public AsString() {
109 }
110
111 public int getK() {
112 return k;
113 }
114
115 public void setK(int k) {
116 this.k = k;
117 }
118
119 public Enum getV() {
120 return v;
121 }
122
123 public void setV(Enum v) {
124 this.v = v;
125 }
126 }
127}