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