Skip to content

Commit 996d9a6

Browse files
committed
Config.getEntityListenerの戻り値に対してnullチェックを追加しました
1 parent b51734b commit 996d9a6

File tree

2 files changed

+101
-1
lines changed

2 files changed

+101
-1
lines changed

src/main/java/org/seasar/doma/internal/RuntimeConfig.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.seasar.doma.jdbc.CommandImplementors;
2626
import org.seasar.doma.jdbc.Commenter;
2727
import org.seasar.doma.jdbc.Config;
28+
import org.seasar.doma.jdbc.ConfigException;
2829
import org.seasar.doma.jdbc.JdbcLogger;
2930
import org.seasar.doma.jdbc.MapKeyNaming;
3031
import org.seasar.doma.jdbc.QueryImplementors;
@@ -150,7 +151,13 @@ public int getBatchSize() {
150151
@Override
151152
public <ENTITY, LISTENER extends EntityListener<ENTITY>> LISTENER getEntityListener(
152153
Class<LISTENER> listenerClass, Supplier<LISTENER> listenerSupplier) {
153-
return config.getEntityListener(listenerClass, listenerSupplier);
154+
LISTENER listener = config.getEntityListener(listenerClass,
155+
listenerSupplier);
156+
if (listener == null) {
157+
throw new ConfigException(config.getClass().getName(),
158+
"getEntityListener");
159+
}
160+
return listener;
154161
}
155162

156163
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 2004-2010 the Seasar Foundation and the Others.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13+
* either express or implied. See the License for the specific language
14+
* governing permissions and limitations under the License.
15+
*/
16+
package org.seasar.doma.internal;
17+
18+
import java.util.function.Supplier;
19+
20+
import javax.sql.DataSource;
21+
22+
import junit.framework.TestCase;
23+
24+
import org.seasar.doma.jdbc.Config;
25+
import org.seasar.doma.jdbc.ConfigException;
26+
import org.seasar.doma.jdbc.SimpleDataSource;
27+
import org.seasar.doma.jdbc.dialect.Dialect;
28+
import org.seasar.doma.jdbc.dialect.StandardDialect;
29+
import org.seasar.doma.jdbc.entity.EntityListener;
30+
31+
/**
32+
* @author backpaper0
33+
*
34+
*/
35+
public class RuntimeConfigTest extends TestCase {
36+
37+
public void testGetEntityListener() throws Exception {
38+
Config originalConfig = new MockConfig() {
39+
40+
@Override
41+
public <ENTITY, LISTENER extends EntityListener<ENTITY>> LISTENER getEntityListener(
42+
Class<LISTENER> listenerClass,
43+
Supplier<LISTENER> listenerSupplier) {
44+
return listenerSupplier.get();
45+
}
46+
};
47+
48+
RuntimeConfig runtimeConfig = new RuntimeConfig(originalConfig);
49+
50+
MockEntityListener entityListener = runtimeConfig.getEntityListener(
51+
MockEntityListener.class, MockEntityListener::new);
52+
assertNotNull(entityListener);
53+
}
54+
55+
public void testGetEntityListenerNullCheck() throws Exception {
56+
Config originalConfig = new MockConfig() {
57+
58+
@Override
59+
public <ENTITY, LISTENER extends EntityListener<ENTITY>> LISTENER getEntityListener(
60+
Class<LISTENER> listenerClass,
61+
Supplier<LISTENER> listenerSupplier) {
62+
return null;
63+
}
64+
};
65+
66+
RuntimeConfig runtimeConfig = new RuntimeConfig(originalConfig);
67+
68+
try {
69+
runtimeConfig.getEntityListener(MockEntityListener.class,
70+
MockEntityListener::new);
71+
fail();
72+
} catch (ConfigException e) {
73+
assertEquals(originalConfig.getClass().getName(), e.getClassName());
74+
assertEquals("getEntityListener", e.getMethodName());
75+
}
76+
}
77+
78+
private interface MockConfig extends Config {
79+
80+
@Override
81+
default Dialect getDialect() {
82+
return new StandardDialect();
83+
}
84+
85+
@Override
86+
default DataSource getDataSource() {
87+
return new SimpleDataSource();
88+
}
89+
}
90+
91+
private static class MockEntityListener implements EntityListener<Object> {
92+
}
93+
}

0 commit comments

Comments
 (0)