public void testIllegal()
throws DatabaseException {
DatabaseEntry key = new DatabaseEntry(new byte[1]);
SequenceConfig config = new SequenceConfig();
config.setAllowCreate(true);
/* Duplicates not allowed. */
Database db = openDb("dups", true);
Transaction txn = txnBegin();
try {
db.openSequence(txn, key, config);
fail();
} catch (IllegalArgumentException expected) {
String msg = expected.getMessage();
assertTrue(msg, msg.indexOf("duplicates") >= 0);
}
txnCommit(txn);
db.close();
db = openDb("foo");
txn = txnBegin();
/* Range min must be less than max. */
config.setRange(0, 0);
try {
db.openSequence(txn, key, config);
fail();
} catch (IllegalArgumentException expected) {
String msg = expected.getMessage();
assertTrue(msg, msg.indexOf("less than the maximum") >= 0);
}
/* Initial value must be within range. */
config.setRange(-10, 10);
config.setInitialValue(-11);
try {
db.openSequence(txn, key, config);
fail();
} catch (IllegalArgumentException expected) {
String msg = expected.getMessage();
assertTrue(msg, msg.indexOf("out of range") >= 0);
}
config.setInitialValue(11);
try {
db.openSequence(txn, key, config);
fail();
} catch (IllegalArgumentException expected) {
String msg = expected.getMessage();
assertTrue(msg, msg.indexOf("out of range") >= 0);
}
/* Cache size must be within range. */
config.setRange(-10, 10);
config.setCacheSize(21);
config.setInitialValue(0);
try {
db.openSequence(txn, key, config);
fail();
} catch (IllegalArgumentException expected) {
String msg = expected.getMessage();
assertTrue(msg, msg.indexOf("cache size is larger") >= 0);
}
/* Create with legal range values. */
config.setRange(1, 2);
config.setInitialValue(1);
config.setCacheSize(0);
Sequence seq = db.openSequence(txn, key, config);
/* Key must not exist if ExclusiveCreate=true. */
config.setExclusiveCreate(true);
try {
db.openSequence(txn, key, config);
fail();
} catch (DatabaseException expected) {
String msg = expected.getMessage();
assertTrue(msg, msg.indexOf("already exists") >= 0);
}
config.setExclusiveCreate(false);
seq.close();
/* Key must exist if AllowCreate=false. */
db.removeSequence(txn, key);
config.setAllowCreate(false);
try {
db.openSequence(txn, key, config);
fail();
} catch (DatabaseException expected) {
String msg = expected.getMessage();
assertTrue(msg, msg.indexOf("does not exist") >= 0);
}
/* Check wrapping not allowed. */
db.removeSequence(txn, key);
config.setAllowCreate(true);
config.setRange(-5, 5);
config.setInitialValue(-5);
seq = db.openSequence(txn, key, config);
for (long i = config.getRangeMin(); i <= config.getRangeMax(); i++) {
assertEquals(i, seq.get(txn, 1));
}
try {
seq.get(txn, 1);
fail();
} catch (DatabaseException expected) {
String msg = expected.getMessage();
assertTrue(msg, msg.indexOf("overflow") >= 0);
}
/* Check wrapping not allowed, decrement. */
db.removeSequence(txn, key);
config.setAllowCreate(true);
config.setAllowCreate(true);
config.setRange(-5, 5);
config.setInitialValue(5);
config.setDecrement(true);
seq = db.openSequence(txn, key, config);
for (long i = config.getRangeMax(); i >= config.getRangeMin(); i--) {
assertEquals(i, seq.get(txn, 1));
}
try {
seq.get(txn, 1);
fail();