Skip to content

Commit e53956d

Browse files
committed
Add requireNonNull checks to Dbi
1 parent 8b60f29 commit e53956d

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

src/main/java/org/lmdbjava/Dbi.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,14 @@ public final class Dbi<T> {
7878
final boolean nativeCb,
7979
final BufferProxy<T> proxy,
8080
final DbiFlagSet dbiFlagSet) {
81+
8182
if (SHOULD_CHECK) {
8283
if (nativeCb && comparator == null) {
8384
throw new IllegalArgumentException("Is nativeCb is true, you must supply a comparator");
8485
}
86+
requireNonNull(env);
8587
requireNonNull(txn);
88+
requireNonNull(proxy);
8689
requireNonNull(dbiFlagSet);
8790
txn.checkReady();
8891
}

src/test/java/org/lmdbjava/DbiTest.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,78 @@ void close() {
120120
.isInstanceOf(ConstantDerivedException.class);
121121
}
122122

123+
@Test
124+
void constructorArgsNullEnv() {
125+
assertThatThrownBy(
126+
() -> {
127+
try (Txn<ByteBuffer> txn = env.txnWrite()) {
128+
new Dbi<>(
129+
null,
130+
txn,
131+
"foo".getBytes(Env.DEFAULT_NAME_CHARSET),
132+
PROXY_OPTIMAL,
133+
DbiFlagSet.EMPTY);
134+
}
135+
})
136+
.isInstanceOf(NullPointerException.class);
137+
}
138+
139+
@Test
140+
void constructorArgsNullTxn() {
141+
assertThatThrownBy(
142+
() -> {
143+
new Dbi<>(
144+
env,
145+
null,
146+
"foo".getBytes(Env.DEFAULT_NAME_CHARSET),
147+
PROXY_OPTIMAL,
148+
DbiFlagSet.EMPTY);
149+
})
150+
.isInstanceOf(NullPointerException.class);
151+
}
152+
153+
@Test
154+
void constructorArgsNullProxy() {
155+
assertThatThrownBy(
156+
() -> {
157+
try (Txn<ByteBuffer> txn = env.txnWrite()) {
158+
new Dbi<>(
159+
env, txn, "foo".getBytes(Env.DEFAULT_NAME_CHARSET), null, DbiFlagSet.EMPTY);
160+
}
161+
})
162+
.isInstanceOf(NullPointerException.class);
163+
}
164+
165+
@Test
166+
void constructorArgsNullFlags() {
167+
assertThatThrownBy(
168+
() -> {
169+
try (Txn<ByteBuffer> txn = env.txnWrite()) {
170+
new Dbi<>(env, txn, "foo".getBytes(Env.DEFAULT_NAME_CHARSET), PROXY_OPTIMAL, null);
171+
}
172+
})
173+
.isInstanceOf(NullPointerException.class);
174+
}
175+
176+
@Test
177+
void constructorArgsNullNativeCallbackComparator() {
178+
assertThatThrownBy(
179+
() -> {
180+
try (Txn<ByteBuffer> txn = env.txnWrite()) {
181+
new Dbi<>(
182+
env,
183+
txn,
184+
"foo".getBytes(Env.DEFAULT_NAME_CHARSET),
185+
null,
186+
true,
187+
PROXY_OPTIMAL,
188+
DbiFlagSet.EMPTY);
189+
}
190+
})
191+
.isInstanceOf(IllegalArgumentException.class)
192+
.hasMessageContaining("Is nativeCb is true, you must supply a comparator");
193+
}
194+
123195
@Test
124196
void customComparator() {
125197
final Comparator<ByteBuffer> reverseOrder =

0 commit comments

Comments
 (0)