Skip to content

Commit 1b3f94d

Browse files
committed
Change CursorIterableTest to use Parameterized
1 parent ef0c852 commit 1b3f94d

2 files changed

Lines changed: 116 additions & 90 deletions

File tree

src/test/java/org/lmdbjava/CursorIterableTest.java

Lines changed: 115 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import static org.lmdbjava.TestUtils.DB_1;
4646
import static org.lmdbjava.TestUtils.DB_2;
4747
import static org.lmdbjava.TestUtils.DB_3;
48+
import static org.lmdbjava.TestUtils.DB_4;
4849
import static org.lmdbjava.TestUtils.POSIX_MODE;
4950
import static org.lmdbjava.TestUtils.bb;
5051

@@ -59,25 +60,83 @@
5960
import java.util.LinkedList;
6061
import java.util.List;
6162
import java.util.NoSuchElementException;
63+
import java.util.function.Function;
6264
import org.hamcrest.Matchers;
6365
import org.junit.After;
6466
import org.junit.Before;
6567
import org.junit.Rule;
6668
import org.junit.Test;
6769
import org.junit.rules.TemporaryFolder;
70+
import org.junit.runner.RunWith;
71+
import org.junit.runners.Parameterized;
6872
import org.lmdbjava.CursorIterable.KeyVal;
6973

70-
/** Test {@link CursorIterable}. */
74+
/**
75+
* Test {@link CursorIterable}.
76+
*/
77+
@RunWith(Parameterized.class)
7178
public final class CursorIterableTest {
7279

73-
@Rule public final TemporaryFolder tmp = new TemporaryFolder();
74-
private Dbi<ByteBuffer> dbJavaComparator;
75-
private Dbi<ByteBuffer> dbLmdbComparator;
76-
private Dbi<ByteBuffer> dbCallbackComparator;
77-
private List<Dbi<ByteBuffer>> dbs = new ArrayList<>();
80+
private static final DbiFlagSet dbiFlagSet = MDB_CREATE;
81+
private static final BufferProxy<ByteBuffer> bufferProxy = ByteBufferProxy.PROXY_OPTIMAL;
82+
83+
@Rule
84+
public final TemporaryFolder tmp = new TemporaryFolder();
85+
7886
private Env<ByteBuffer> env;
7987
private Deque<Integer> list;
8088

89+
/** Injected by {@link #data()} with appropriate runner. */
90+
@Parameterized.Parameter
91+
public DbiFactory dbiFactory;
92+
93+
@Before
94+
public void before() throws IOException {
95+
final File path = tmp.newFile();
96+
final BufferProxy<ByteBuffer> bufferProxy = ByteBufferProxy.PROXY_OPTIMAL;
97+
env =
98+
create(bufferProxy)
99+
.setMapSize(KIBIBYTES.toBytes(256))
100+
.setMaxReaders(1)
101+
.setMaxDbs(3)
102+
.open(path, POSIX_MODE, MDB_NOSUBDIR);
103+
104+
populateTestDataList();
105+
}
106+
107+
@Parameterized.Parameters(name = "{index}: dbi: {0}")
108+
public static Object[] data() {
109+
final DbiFactory defaultComparator = new DbiFactory("defaultComparator", env ->
110+
env.buildDbi()
111+
.withDbName(DB_1)
112+
.withDefaultComparator()
113+
.withDbiFlags(dbiFlagSet)
114+
.open());
115+
final DbiFactory nativeComparator = new DbiFactory("nativeComparator", env ->
116+
env.buildDbi()
117+
.withDbName(DB_2)
118+
.withNativeComparator()
119+
.withDbiFlags(dbiFlagSet)
120+
.open());
121+
final DbiFactory callbackComparator = new DbiFactory("callbackComparator", env ->
122+
env.buildDbi()
123+
.withDbName(DB_3)
124+
.withCallbackComparator(bufferProxy.getComparator(dbiFlagSet))
125+
.withDbiFlags(dbiFlagSet)
126+
.open());
127+
final DbiFactory iteratorComparator = new DbiFactory("iteratorComparator", env ->
128+
env.buildDbi()
129+
.withDbName(DB_4)
130+
.withIteratorComparator(bufferProxy.getComparator(dbiFlagSet))
131+
.withDbiFlags(dbiFlagSet)
132+
.open());
133+
return new Object[] {
134+
defaultComparator,
135+
nativeComparator,
136+
callbackComparator,
137+
iteratorComparator};
138+
}
139+
81140
@After
82141
public void after() {
83142
env.close();
@@ -118,49 +177,8 @@ public void atMostTest() {
118177
verify(atMost(bb(6)), 2, 4, 6);
119178
}
120179

121-
@Before
122-
public void before() throws IOException {
123-
final File path = tmp.newFile();
124-
final BufferProxy<ByteBuffer> bufferProxy = ByteBufferProxy.PROXY_OPTIMAL;
125-
env =
126-
create(bufferProxy)
127-
.setMapSize(KIBIBYTES.toBytes(256))
128-
.setMaxReaders(1)
129-
.setMaxDbs(3)
130-
.open(path, POSIX_MODE, MDB_NOSUBDIR);
131180

132-
final DbiFlagSet dbiFlagSet = MDB_CREATE;
133-
// Use a java comparator for start/stop keys only
134-
dbJavaComparator = env.buildDbi()
135-
.withDbName(DB_1)
136-
.withDefaultComparator()
137-
.withDbiFlags(dbiFlagSet)
138-
.open();
139-
// Use LMDB comparator for start/stop keys
140-
dbLmdbComparator = env.buildDbi()
141-
.withDbName(DB_2)
142-
.withNativeComparator()
143-
.withDbiFlags(dbiFlagSet)
144-
.open();
145-
// Use a java comparator for start/stop keys and as a callback comparaotr
146-
dbCallbackComparator = env.buildDbi()
147-
.withDbName(DB_3)
148-
.withCallbackComparator(bufferProxy.getComparator(dbiFlagSet))
149-
.withDbiFlags(dbiFlagSet)
150-
.open();
151-
152-
populateList();
153-
154-
populateDatabase(dbJavaComparator);
155-
populateDatabase(dbLmdbComparator);
156-
populateDatabase(dbCallbackComparator);
157-
158-
dbs.add(dbJavaComparator);
159-
dbs.add(dbLmdbComparator);
160-
dbs.add(dbCallbackComparator);
161-
}
162-
163-
private void populateList() {
181+
private void populateTestDataList() {
164182
list = new LinkedList<>();
165183
list.addAll(asList(2, 3, 4, 5, 6, 7, 8, 9));
166184
}
@@ -203,14 +221,6 @@ public void closedTest() {
203221
verify(closed(bb(1), bb(7)), 2, 4, 6);
204222
}
205223

206-
public void closedTest1() {
207-
verify(dbLmdbComparator, closed(bb(3), bb(7)), 4, 6);
208-
}
209-
210-
public void closedTest2() {
211-
verify(dbJavaComparator, closed(bb(3), bb(7)), 4, 6);
212-
}
213-
214224
@Test
215225
public void greaterThanBackwardTest() {
216226
verify(greaterThanBackward(bb(6)), 4, 2);
@@ -226,40 +236,36 @@ public void greaterThanTest() {
226236

227237
@Test(expected = IllegalStateException.class)
228238
public void iterableOnlyReturnedOnce() {
229-
for (final Dbi<ByteBuffer> db : dbs) {
230-
try (Txn<ByteBuffer> txn = env.txnRead();
231-
CursorIterable<ByteBuffer> c = db.iterate(txn)) {
239+
final Dbi<ByteBuffer> db = getDb();
240+
try (Txn<ByteBuffer> txn = env.txnRead();
241+
CursorIterable<ByteBuffer> c = db.iterate(txn)) {
232242
c.iterator(); // ok
233243
c.iterator(); // fails
234244
}
235-
}
236245
}
237246

238247
@Test
239248
public void iterate() {
240-
for (final Dbi<ByteBuffer> db : dbs) {
241-
populateList();
249+
final Dbi<ByteBuffer> db = getDb();
242250
try (Txn<ByteBuffer> txn = env.txnRead();
243-
CursorIterable<ByteBuffer> c = db.iterate(txn)) {
251+
CursorIterable<ByteBuffer> c = db.iterate(txn)) {
244252

245253
int cnt = 0;
246254
for (final KeyVal<ByteBuffer> kv : c) {
247255
assertThat(kv.key().getInt(), is(list.pollFirst()));
248256
assertThat(kv.val().getInt(), is(list.pollFirst()));
249257
}
250258
}
251-
}
252259
}
253260

254261
@Test(expected = IllegalStateException.class)
255262
public void iteratorOnlyReturnedOnce() {
256-
for (final Dbi<ByteBuffer> db : dbs) {
263+
final Dbi<ByteBuffer> db = getDb();
257264
try (Txn<ByteBuffer> txn = env.txnRead();
258-
CursorIterable<ByteBuffer> c = db.iterate(txn)) {
265+
CursorIterable<ByteBuffer> c = db.iterate(txn)) {
259266
c.iterator(); // ok
260267
c.iterator(); // fails
261268
}
262-
}
263269
}
264270

265271
@Test
@@ -276,10 +282,10 @@ public void lessThanTest() {
276282

277283
@Test(expected = NoSuchElementException.class)
278284
public void nextThrowsNoSuchElementExceptionIfNoMoreElements() {
279-
for (final Dbi<ByteBuffer> db : dbs) {
280-
populateList();
285+
final Dbi<ByteBuffer> db = getDb();
286+
populateTestDataList();
281287
try (Txn<ByteBuffer> txn = env.txnRead();
282-
CursorIterable<ByteBuffer> c = db.iterate(txn)) {
288+
CursorIterable<ByteBuffer> c = db.iterate(txn)) {
283289
final Iterator<KeyVal<ByteBuffer>> i = c.iterator();
284290
while (i.hasNext()) {
285291
final KeyVal<ByteBuffer> kv = i.next();
@@ -289,7 +295,6 @@ public void nextThrowsNoSuchElementExceptionIfNoMoreElements() {
289295
assertThat(i.hasNext(), is(false));
290296
i.next();
291297
}
292-
}
293298
}
294299

295300
@Test
@@ -341,8 +346,8 @@ public void openTest() {
341346

342347
@Test
343348
public void removeOddElements() {
344-
for (final Dbi<ByteBuffer> db : dbs) {
345-
verify(db, all(), 2, 4, 6, 8);
349+
final Dbi<ByteBuffer> db = getDb();
350+
verify(db, all(), 2, 4, 6, 8);
346351
int idx = -1;
347352
try (Txn<ByteBuffer> txn = env.txnWrite()) {
348353
try (CursorIterable<ByteBuffer> ci = db.iterate(txn)) {
@@ -358,12 +363,11 @@ public void removeOddElements() {
358363
txn.commit();
359364
}
360365
verify(db, all(), 4, 8);
361-
}
362366
}
363367

364368
@Test(expected = Env.AlreadyClosedException.class)
365369
public void nextWithClosedEnvTest() {
366-
for (final Dbi<ByteBuffer> db : dbs) {
370+
final Dbi<ByteBuffer> db = getDb();
367371
try (Txn<ByteBuffer> txn = env.txnRead()) {
368372
try (CursorIterable<ByteBuffer> ci = db.iterate(txn, KeyRange.all())) {
369373
final Iterator<KeyVal<ByteBuffer>> c = ci.iterator();
@@ -372,12 +376,11 @@ public void nextWithClosedEnvTest() {
372376
c.next();
373377
}
374378
}
375-
}
376379
}
377380

378381
@Test(expected = Env.AlreadyClosedException.class)
379382
public void removeWithClosedEnvTest() {
380-
for (final Dbi<ByteBuffer> db : dbs) {
383+
final Dbi<ByteBuffer> db = getDb();
381384
try (Txn<ByteBuffer> txn = env.txnWrite()) {
382385
try (CursorIterable<ByteBuffer> ci = db.iterate(txn, KeyRange.all())) {
383386
final Iterator<KeyVal<ByteBuffer>> c = ci.iterator();
@@ -389,12 +392,11 @@ public void removeWithClosedEnvTest() {
389392
c.remove();
390393
}
391394
}
392-
}
393395
}
394396

395397
@Test(expected = Env.AlreadyClosedException.class)
396398
public void hasNextWithClosedEnvTest() {
397-
for (final Dbi<ByteBuffer> db : dbs) {
399+
final Dbi<ByteBuffer> db = getDb();
398400
try (Txn<ByteBuffer> txn = env.txnRead()) {
399401
try (CursorIterable<ByteBuffer> ci = db.iterate(txn, KeyRange.all())) {
400402
final Iterator<KeyVal<ByteBuffer>> c = ci.iterator();
@@ -403,21 +405,20 @@ public void hasNextWithClosedEnvTest() {
403405
c.hasNext();
404406
}
405407
}
406-
}
407408
}
408409

409410
@Test(expected = Env.AlreadyClosedException.class)
410411
public void forEachRemainingWithClosedEnvTest() {
411-
for (final Dbi<ByteBuffer> db : dbs) {
412+
final Dbi<ByteBuffer> db = getDb();
412413
try (Txn<ByteBuffer> txn = env.txnRead()) {
413414
try (CursorIterable<ByteBuffer> ci = db.iterate(txn, KeyRange.all())) {
414415
final Iterator<KeyVal<ByteBuffer>> c = ci.iterator();
415416

416417
env.close();
417-
c.forEachRemaining(keyVal -> {});
418+
c.forEachRemaining(keyVal -> {
419+
});
418420
}
419421
}
420-
}
421422
}
422423

423424
// @Test
@@ -468,24 +469,23 @@ public void forEachRemainingWithClosedEnvTest() {
468469
// }
469470

470471
private void verify(final KeyRange<ByteBuffer> range, final int... expected) {
471-
// Verify using all comparator types
472-
for (final Dbi<ByteBuffer> db : dbs) {
473-
verify(range, db, expected);
474-
}
472+
final Dbi<ByteBuffer> db = getDb();
473+
verify(range, db, expected);
475474
}
476475

477476
private void verify(
478477
final Dbi<ByteBuffer> dbi, final KeyRange<ByteBuffer> range, final int... expected) {
479478
verify(range, dbi, expected);
480479
}
481480

482-
private void verify(
483-
final KeyRange<ByteBuffer> range, final Dbi<ByteBuffer> dbi, final int... expected) {
481+
private void verify(final KeyRange<ByteBuffer> range,
482+
final Dbi<ByteBuffer> dbi,
483+
final int... expected) {
484484

485485
final List<Integer> results = new ArrayList<>();
486486

487487
try (Txn<ByteBuffer> txn = env.txnRead();
488-
CursorIterable<ByteBuffer> c = dbi.iterate(txn, range)) {
488+
CursorIterable<ByteBuffer> c = dbi.iterate(txn, range)) {
489489
for (final KeyVal<ByteBuffer> kv : c) {
490490
final int key = kv.key().getInt();
491491
final int val = kv.val().getInt();
@@ -499,4 +499,29 @@ private void verify(
499499
assertThat(results.get(idx), is(expected[idx]));
500500
}
501501
}
502+
503+
private Dbi<ByteBuffer> getDb() {
504+
final Dbi<ByteBuffer> dbi = dbiFactory.factory.apply(env);
505+
populateDatabase(dbi);
506+
return dbi;
507+
}
508+
509+
510+
// --------------------------------------------------------------------------------
511+
512+
513+
private static class DbiFactory {
514+
private final String name;
515+
private final Function<Env<ByteBuffer>, Dbi<ByteBuffer>> factory;
516+
517+
private DbiFactory(String name, Function<Env<ByteBuffer>, Dbi<ByteBuffer>> factory) {
518+
this.name = name;
519+
this.factory = factory;
520+
}
521+
522+
@Override
523+
public String toString() {
524+
return name;
525+
}
526+
}
502527
}

src/test/java/org/lmdbjava/TestUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ final class TestUtils {
3333
public static final String DB_1 = "test-db-1";
3434
public static final String DB_2 = "test-db-2";
3535
public static final String DB_3 = "test-db-3";
36+
public static final String DB_4 = "test-db-2";
3637

3738
public static final int POSIX_MODE = 0664;
3839

0 commit comments

Comments
 (0)