-
Notifications
You must be signed in to change notification settings - Fork 26
/
H2DataTest.java
257 lines (245 loc) · 9.03 KB
/
H2DataTest.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/**
* Copyright (c) 2012-2022, jcabi.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the jcabi.com nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jcabi.dynamo.mock;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.ComparisonOperator;
import com.amazonaws.services.dynamodbv2.model.Condition;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.jcabi.dynamo.AttributeUpdates;
import com.jcabi.dynamo.Attributes;
import com.jcabi.dynamo.Conditions;
import java.io.File;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
/**
* Test case for {@link H2Data}.
* @author Yegor Bugayenko (yegor@tpc2.com)
* @version $Id$
* @since 0.10
*/
public final class H2DataTest {
/**
* H2Data can store and fetch.
* @throws Exception If some problem inside
*/
@Test
public void storesAndReadsAttributes() throws Exception {
final String table = "users";
final String key = "id";
final int number = 43;
final String attr = "desc";
final String value = "some\n\t\u20ac text";
final MkData data = new H2Data().with(
table, new String[] {key}, new String[] {attr}
);
data.put(table, new Attributes().with(key, number).with(attr, value));
MatcherAssert.assertThat(
data.iterate(
table, new Conditions().with(key, Conditions.equalTo(number))
).iterator().next(),
Matchers.hasEntry(
Matchers.equalTo(attr),
Matchers.equalTo(new AttributeValue(value))
)
);
}
/**
* H2Data can store to a file.
* @throws Exception If some problem inside
* @see <a href="https://code.google.com/p/h2database/issues/detail?id=447">
* Google Code: DB file extension customizability</a>
*/
@Test
@Disabled
public void storesToFile(@TempDir final Path temp) throws Exception {
final File file = temp.resolve("foo.txt").toFile();
final String table = "tbl";
final String key = "key1";
final MkData data = new H2Data(file).with(
table, new String[] {key}, new String[0]
);
data.put(table, new Attributes().with(key, "x2"));
MatcherAssert.assertThat(file.exists(), Matchers.is(true));
MatcherAssert.assertThat(file.length(), Matchers.greaterThan(0L));
}
/**
* H2Data can create many tables.
* @throws Exception If some problem inside
*/
@Test
public void createsManyTables() throws Exception {
new H2Data()
.with("firsttable", new String[] {"firstid"}, new String[0])
.with("secondtable", new String[]{"secondid"}, new String[0]);
}
/**
* H2Data can create tables with long names (max length of DynamoDb table
* name is 255 characters).
* @throws Exception In case test fails
*/
@Test
public void createsTablesWithLongNames() throws Exception {
new H2Data()
.with(
//@checkstyle MagicNumberCheck (1 line)
Joiner.on("").join(Collections.nCopies(40, "X")),
new String[]{"k1"}, new String[0]
);
}
@Test
public void supportsTableNamesWithIllegalCharacters() throws Exception {
new H2Data().with(".-.", new String[]{"pk"}, new String[0]);
}
/**
* H2Data supports column names with characters illegal to H2.
* @throws Exception In case test fails
* @todo #28:30min H2Data doesn't support COLUMNS with ".", "-" or digits
* but I don't know for sure should it support these symbols or not.
* It's needed to be confirmed and test should be uncommented
* when H2Data will be supporting mentioned symbols.
*/
@Test
@Disabled
public void supportsColumnNamesWithIllegalCharacters() throws Exception {
final String key = "0-.col.-0";
final String table = "test";
new H2Data().with(
table, new String[] {key}
).put(table, new Attributes().with(key, "value"));
}
@Test
public void deletesRecords() throws Exception {
final String table = "customers";
final String field = "name";
final String man = "Kevin";
final String woman = "Helen";
final H2Data data = new H2Data()
.with(table, new String[]{field});
data.put(
table,
new Attributes().with(field, man)
);
data.put(
table,
new Attributes().with(field, woman)
);
data.delete(table, new Attributes().with(field, man));
final List<Attributes> rest = Lists.newArrayList(
data.iterate(table, new Conditions())
);
MatcherAssert.assertThat(
rest.size(),
Matchers.equalTo(1)
);
MatcherAssert.assertThat(
rest.get(0).get(field).getS(),
Matchers.equalTo(woman)
);
}
/**
* H2Data can update table attributes.
* @throws Exception In case test fails
*/
@Test
public void updatesTableAttributes() throws Exception {
final String table = "tests";
final String key = "tid";
final int number = 43;
final String attr = "descr";
final String value = "Dummy\n\t\u20ac text";
final String updated = "Updated";
final MkData data = new H2Data().with(
table, new String[] {key}, attr
);
data.put(table, new Attributes().with(key, number).with(attr, value));
data.update(
table,
new Attributes().with(key, number),
new AttributeUpdates().with(attr, "something else")
);
data.update(
table,
new Attributes().with(key, number),
new AttributeUpdates().with(attr, updated)
);
final Iterable<Attributes> result = data.iterate(
table, new Conditions().with(key, Conditions.equalTo(number))
);
MatcherAssert.assertThat(
result.iterator().next(),
Matchers.hasEntry(
Matchers.equalTo(attr),
Matchers.equalTo(new AttributeValue(updated))
)
);
MatcherAssert.assertThat(
result,
Matchers.iterableWithSize(1)
);
}
/**
* H2Data can fetch with comparison.
* @throws Exception If some problem inside
*/
@Test
public void fetchesWithComparison() throws Exception {
final String table = "x12";
final String key = "foo1";
final String value = "bar2";
final MkData data = new H2Data().with(table, new String[] {key}, value);
data.put(table, new Attributes().with(key, "101").with(value, 0));
data.put(table, new Attributes().with(key, "102").with(value, 1));
MatcherAssert.assertThat(
data.iterate(table, new Conditions()),
Matchers.iterableWithSize(2)
);
MatcherAssert.assertThat(
data.iterate(
table,
new Conditions().with(
value,
new Condition()
.withAttributeValueList(
new AttributeValue().withN("0")
)
.withComparisonOperator(ComparisonOperator.GT)
)
).iterator().next().get(value).getN(),
Matchers.equalTo("1")
);
}
}