-
Notifications
You must be signed in to change notification settings - Fork 452
/
CollectionOfValuesTest.java
190 lines (151 loc) · 5.77 KB
/
CollectionOfValuesTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package org.mongodb.morphia.mapping;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import org.bson.types.ObjectId;
import org.junit.Assert;
import org.junit.Test;
import org.mongodb.morphia.TestBase;
import org.mongodb.morphia.annotations.Embedded;
import org.mongodb.morphia.annotations.Entity;
import org.mongodb.morphia.annotations.Id;
import org.mongodb.morphia.dao.BasicDAO;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import static java.util.Arrays.asList;
/**
* @author Uwe Schaefer, (us@thomas-daily.de)
*/
public class CollectionOfValuesTest extends TestBase {
@Test
public void testCity() {
getMorphia().map(City.class);
City city = new City();
city.name = "My city";
city.array = new byte[]{4, 5};
for (byte i = 0; i < 2; i++) {
for (byte j = 0; j < 2; j++) {
city.cells[i][j] = (i * 100 + j);
}
}
getDs().save(city);
City loaded = getDs().get(city);
Assert.assertEquals(city.name, loaded.name);
compare(city.array, loaded.array);
for (int i = 0; i < city.cells.length; i++) {
compare(city.cells[i], loaded.cells[i]);
}
}
@Test
@SuppressWarnings("unchecked")
public void testCreateEntityWithBasicDBList() throws Exception {
BasicDAO<TestEntity, ObjectId> dao;
dao = new BasicDAO<TestEntity, ObjectId>(TestEntity.class, getDs());
TestEntity entity = new TestEntity();
List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
data.add(new BasicDBObject("type", "text")
.append("data", new BasicDBObject("text", "sometext")));
entity.setData(data);
dao.save(entity);
final TestEntity fetched = dao.get(entity.getId());
Assert.assertEquals(entity, fetched);
}
@Test
public void testListOfListMapping() throws Exception {
getMorphia().map(ContainsListOfList.class);
getDs().delete(getDs().find(ContainsListOfList.class));
final ContainsListOfList entity = new ContainsListOfList();
entity.strings = asList(Arrays.asList("element1", "element2"), Arrays.asList("element3"));
entity.integers = asList(Arrays.asList(1, 2), Arrays.asList(3));
getDs().save(entity);
final ContainsListOfList loaded = getDs().get(entity);
Assert.assertNotNull(loaded.strings);
Assert.assertEquals(entity.strings, loaded.strings);
Assert.assertEquals(entity.strings.get(0), loaded.strings.get(0));
Assert.assertEquals(entity.strings.get(0).get(0), loaded.strings.get(0).get(0));
Assert.assertNotNull(loaded.integers);
Assert.assertEquals(entity.integers, loaded.integers);
Assert.assertEquals(entity.integers.get(0), loaded.integers.get(0));
Assert.assertEquals(entity.integers.get(0).get(0), loaded.integers.get(0).get(0));
Assert.assertNotNull(loaded.id);
}
@Test
public void testTwoDimensionalArrayMapping() throws Exception {
getMorphia().map(ContainsTwoDimensionalArray.class);
final ContainsTwoDimensionalArray entity = new ContainsTwoDimensionalArray();
entity.oneDimArray = "Joseph".getBytes();
entity.twoDimArray = new byte[][]{"Joseph".getBytes(), "uwe".getBytes()};
getDs().save(entity);
final ContainsTwoDimensionalArray loaded = getDs().get(ContainsTwoDimensionalArray.class, entity.id);
Assert.assertNotNull(loaded.id);
Assert.assertNotNull(loaded.oneDimArray);
Assert.assertNotNull(loaded.twoDimArray);
compare(entity.oneDimArray, loaded.oneDimArray);
compare(entity.twoDimArray[0], loaded.twoDimArray[0]);
compare(entity.twoDimArray[1], loaded.twoDimArray[1]);
}
private void compare(final byte[] left, final byte[] right) {
Assert.assertArrayEquals(left, right);
}
private void compare(final int[] left, final int[] right) {
Assert.assertArrayEquals(left, right);
}
@Entity("CreateEntityWithDBListIT-TestEntity")
public static class TestEntity {
@Id
private ObjectId id;
private BasicDBList data;
public BasicDBList getData() {
return data;
}
public void setData(final List<?> data) {
this.data = new BasicDBList();
this.data.addAll(data);
}
public ObjectId getId() {
return id;
}
@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (data != null ? data.hashCode() : 0);
return result;
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final TestEntity that = (TestEntity) o;
if (id != null ? !id.equals(that.id) : that.id != null) {
return false;
}
return !(data != null ? !data.equals(that.data) : that.data != null);
}
}
public static class ContainsListOfList {
@Id
private ObjectId id;
private List<List<String>> strings;
private List<List<Integer>> integers;
}
public static class ContainsTwoDimensionalArray {
@Id
private ObjectId id;
private byte[] oneDimArray;
private byte[][] twoDimArray;
}
@Entity(noClassnameStored = true)
public static class City {
@Id
private ObjectId id;
private String name;
@Embedded
private byte[] array;
private int[][] cells = new int[2][2];
}
}