| 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 | */ |
| 16 | package com.datastax.driver.mapping; |
| 17 | |
| 18 | import com.datastax.driver.core.CCMTestsSupport; |
| 19 | import com.datastax.driver.mapping.annotations.Enumerated; |
| 20 | import com.datastax.driver.mapping.annotations.PartitionKey; |
| 21 | import com.datastax.driver.mapping.annotations.Table; |
| 22 | import org.testng.annotations.Test; |
| 23 | |
| 24 | import static org.assertj.core.api.Assertions.assertThat; |
| 25 | import static org.junit.Assert.assertNull; |
| 26 | |
| 27 | /** |
| 28 | * Covers handling of enum fields by the mapper. |
| 29 | */ |
| 30 | @SuppressWarnings("unused") |
| 31 | public class MapperEnumTest extends CCMTestsSupport { |
| 32 | |
| 33 | @Override |
| 34 | public void onTestContextInitialized() { |
| 35 | execute("CREATE TABLE asOrdinal (k int primary key, v int)", |
| 36 | "CREATE TABLE asString (k int primary key, v text)", |
| 37 | "CREATE TABLE asPk (k int primary key, v int)"); |
| 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 | /** |
| 65 | * Ensures that an entity that has an Enum as part of its primary key |
| 66 | * can be successfully stored and retrieved. |
| 67 | * |
| 68 | * @jira_ticket JAVA-831 |
| 69 | */ |
| 70 | @Test(groups = "short") |
| 71 | public void should_handle_enum_as_primary_key() { |
| 72 | Mapper<AsPk> mapper = new MappingManager(session()).mapper(AsPk.class); |
| 73 | |
| 74 | mapper.save(new AsPk(Enum.FOO, 42)); |
| 75 | AsPk o = mapper.get(Enum.FOO); |
| 76 | |
| 77 | assertThat(o.getV()).isEqualTo(42); |
| 78 | mapper.delete(Enum.FOO); |
| 79 | |
| 80 | assertThat(mapper.get(Enum.FOO)).isNull(); |
| 81 | } |
| 82 | |
| 83 | @Table(name = "asOrdinal") |
| 84 | public static class AsOrdinal { |
| 85 | @PartitionKey |
| 86 | private int k; |
| 87 | |
| 88 | @Enumerated(EnumType.ORDINAL) |
| 89 | private Enum v; |
| 90 | |
| 91 | public AsOrdinal(Integer k, Enum v) { |
| 92 | this.k = k; |
| 93 | this.v = v; |
| 94 | } |
| 95 | |
| 96 | public AsOrdinal() { |
| 97 | } |
| 98 | |
| 99 | public int getK() { |
| 100 | return k; |
| 101 | } |
| 102 | |
| 103 | public void setK(int k) { |
| 104 | this.k = k; |
| 105 | } |
| 106 | |
| 107 | public Enum getV() { |
| 108 | return v; |
| 109 | } |
| 110 | |
| 111 | public void setV(Enum v) { |
| 112 | this.v = v; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | @Table(name = "asString") |
| 117 | public static class AsString { |
| 118 | @PartitionKey |
| 119 | private int k; |
| 120 | |
| 121 | @Enumerated(EnumType.STRING) |
| 122 | private Enum v; |
| 123 | |
| 124 | public AsString(Integer k, Enum v) { |
| 125 | this.k = k; |
| 126 | this.v = v; |
| 127 | } |
| 128 | |
| 129 | public AsString() { |
| 130 | } |
| 131 | |
| 132 | public int getK() { |
| 133 | return k; |
| 134 | } |
| 135 | |
| 136 | public void setK(int k) { |
| 137 | this.k = k; |
| 138 | } |
| 139 | |
| 140 | public Enum getV() { |
| 141 | return v; |
| 142 | } |
| 143 | |
| 144 | public void setV(Enum v) { |
| 145 | this.v = v; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | @Table(name = "asPk") |
| 150 | public static class AsPk { |
| 151 | |
| 152 | @PartitionKey |
| 153 | @Enumerated(EnumType.ORDINAL) |
| 154 | private Enum k; |
| 155 | |
| 156 | private int v; |
| 157 | |
| 158 | public AsPk(Enum k, int v) { |
| 159 | this.k = k; |
| 160 | this.v = v; |
| 161 | } |
| 162 | |
| 163 | public AsPk() { |
| 164 | } |
| 165 | |
| 166 | public Enum getK() { |
| 167 | return k; |
| 168 | } |
| 169 | |
| 170 | public void setK(Enum k) { |
| 171 | this.k = k; |
| 172 | } |
| 173 | |
| 174 | public int getV() { |
| 175 | return v; |
| 176 | } |
| 177 | |
| 178 | public void setV(int v) { |
| 179 | this.v = v; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | } |